[go: nahoru, domu]

Skip to content

Commit

Permalink
feat: add Tools.set method (#2399)
Browse files Browse the repository at this point in the history
  • Loading branch information
siam-ese committed Jun 3, 2024
1 parent 1e5c40c commit ea8f50c
Showing 1 changed file with 23 additions and 0 deletions.
23 changes: 23 additions & 0 deletions packages/core/src/shared/tools.ts
Original file line number Diff line number Diff line change
Expand Up @@ -671,4 +671,27 @@ export class Tools {

return !containsInvalidChars && isValidLength;
}

/**
* As lodash set, via a path string to set value to deep property
* set(obj, 'xx.yy', val)
* @param data
* @param propertyPath
* @param value
*/
static set(data: Record<string, any>, propertyPath: string, value: any) {
const paths = propertyPath.split('.');
const key = paths.pop();

paths.forEach((prop) => {
if (!data[prop]) {
data[prop] = {};
}
data = data[prop];
});

if (key) {
data[key] = value;
}
}
}

0 comments on commit ea8f50c

Please sign in to comment.