Added merge object
This commit is contained in:
parent
3f85c9cf7d
commit
ac5b0b8b41
@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "@ztimson/utils",
|
||||
"version": "0.11.3",
|
||||
"version": "0.12.0",
|
||||
"description": "Utility library",
|
||||
"author": "Zak Timson",
|
||||
"license": "MIT",
|
||||
|
@ -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
|
||||
*
|
||||
|
Loading…
Reference in New Issue
Block a user