diff options
author | sijanec <sijanecantonluka@gmail.com> | 2020-08-30 20:14:01 +0200 |
---|---|---|
committer | sijanec <sijanecantonluka@gmail.com> | 2020-08-30 20:14:01 +0200 |
commit | d9969379798e23b2a62f8bf6eab3967d0497b1a8 (patch) | |
tree | a8132c6cf24ee3df1f3d8c768b60bf9378a4bbae /dist/js/lib/mergedeep.js | |
parent | spremenil server message (diff) | |
download | beziapp-d9969379798e23b2a62f8bf6eab3967d0497b1a8.tar beziapp-d9969379798e23b2a62f8bf6eab3967d0497b1a8.tar.gz beziapp-d9969379798e23b2a62f8bf6eab3967d0497b1a8.tar.bz2 beziapp-d9969379798e23b2a62f8bf6eab3967d0497b1a8.tar.lz beziapp-d9969379798e23b2a62f8bf6eab3967d0497b1a8.tar.xz beziapp-d9969379798e23b2a62f8bf6eab3967d0497b1a8.tar.zst beziapp-d9969379798e23b2a62f8bf6eab3967d0497b1a8.zip |
Diffstat (limited to 'dist/js/lib/mergedeep.js')
-rwxr-xr-x | dist/js/lib/mergedeep.js | 33 |
1 files changed, 30 insertions, 3 deletions
diff --git a/dist/js/lib/mergedeep.js b/dist/js/lib/mergedeep.js index fa2403d..a56aa1d 100755 --- a/dist/js/lib/mergedeep.js +++ b/dist/js/lib/mergedeep.js @@ -1,4 +1,31 @@ +// https://stackoverflow.com/questions/27936772/how-to-deep-merge-instead-of-shallow-merge +/** + * Simple object check. + * @param item + * @returns {boolean} + */ +function isObject(item) { + return (item && typeof item === 'object' && !Array.isArray(item)); +} -function isObject(item){return(item&&typeof item==='object'&&!Array.isArray(item));} -function mergeDeep(target,...sources){if(!sources.length)return target;const source=sources.shift();if(isObject(target)&&isObject(source)){for(const key in source){if(isObject(source[key])){if(!target[key])Object.assign(target,{[key]:{}});mergeDeep(target[key],source[key]);}else{Object.assign(target,{[key]:source[key]});}}} -return mergeDeep(target,...sources);}
\ No newline at end of file +/** + * Deep merge two objects. + * @param target + * @param ...sources + */ +function mergeDeep(target, ...sources) { + if (!sources.length) return target; + const source = sources.shift(); + + if (isObject(target) && isObject(source)) { + for (const key in source) { + if (isObject(source[key])) { + if (!target[key]) Object.assign(target, { [key]: {} }); + mergeDeep(target[key], source[key]); + } else { + Object.assign(target, { [key]: source[key] }); + } + } + } + return mergeDeep(target, ...sources); +} |