mirror of
https://github.com/cpvalente/ontime.git
synced 2026-08-12 19:03:47 +00:00
refactor: create utility for nested templates
This commit is contained in:
committed by
Carlos Valente
parent
7baa6a4ab9
commit
aa929fd0dc
@@ -0,0 +1,18 @@
|
||||
/**
|
||||
* Extracts a value from a nested object using a dot-separated path
|
||||
*/
|
||||
export function getPropertyFromPath<T extends object>(path: string, obj: T): any | undefined {
|
||||
const keys = path.split('.');
|
||||
let result: any = obj;
|
||||
|
||||
// iterate through variable parts, and look for the property in the given object
|
||||
for (const key of keys) {
|
||||
if (result && typeof result === 'object' && key in result) {
|
||||
result = result[key];
|
||||
} else {
|
||||
return undefined;
|
||||
}
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
Reference in New Issue
Block a user