refactor: create utility for nested templates

This commit is contained in:
Carlos Valente
2025-01-12 09:57:29 +01:00
committed by Carlos Valente
parent 7baa6a4ab9
commit aa929fd0dc
5 changed files with 38 additions and 1 deletions
+18
View File
@@ -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;
}