Added merge object
All checks were successful
Build / Build NPM Project (push) Successful in 21s
Build / Tag Version (push) Successful in 5s

This commit is contained in:
Zakary Timson 2024-06-18 19:31:08 -04:00
parent 3f85c9cf7d
commit ac5b0b8b41
2 changed files with 22 additions and 1 deletions

View File

@ -1,6 +1,6 @@
{
"name": "@ztimson/utils",
"version": "0.11.3",
"version": "0.12.0",
"description": "Utility library",
"author": "Zak Timson",
"license": "MIT",

View File

@ -35,6 +35,27 @@ export function deepCopy<T>(value: T): T {
return JSON.parse(JSON.stringify(value));
}
/**
* Merge any number of objects into the target
*
* @param target Destination of all properties
* @param sources Objects that will copied into target
* @return {any} The des
*/
function deepMerge<T>(target: any, ...sources: any[]): T {
for(const source of sources) {
for(const key in source) {
if(source[key] && typeof source[key] == 'object' && !Array.isArray(source[key])) {
if(!target[key]) target[key] = {};
deepMerge(target[key], source[key]);
} else {
target[key] = source[key];
}
}
}
return target;
}
/**
* Get/set a property of an object using dot notation
*