Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const removeKey = (key, {[key]: _, ...rest}) => rest;
- const getNewObj = toAdd => Array.isArray(toAdd) ? getObj(toAdd) : toAdd
- const DE_DUPE = (e, i, arr) => arr.indexOf(e) === i
- const removeEmptyObj = (o) => {
- let ignores = [null, undefined, ""],
- isNonEmpty = d => !ignores.includes(d) && (typeof(d) !== "object" || Object.keys(d).length)
- return JSON.parse(JSON.stringify(o), function(k, v) {
- if (isNonEmpty(v))
- return v;
- });
- }
- const getDeepMerge = (ogObj, opts={})=> {
- const { add, uniq=true, replace=false, removeEmpty=false } = opts
- const isObject = obj => obj && typeof obj === 'object';
- return [ogObj, add].reduce((prev, obj) => {
- let _add = {...prev}
- Object.keys(obj).forEach(key => {
- const pVal = _add[key];
- const oVal = obj[key];
- if (Array.isArray(pVal) && Array.isArray(oVal)) {
- _add[key] = pVal.concat(...oVal).filter(uniq ? DE_DUPE : null)
- if(replace){
- _add[key] = oVal }
- _add = removeEmpty ? removeKey(key, _add) : _add
- }
- else if (isObject(pVal) && isObject(oVal)) {
- _add[key] = getDeepMerge(pVal, {...opts, add:oVal})
- if(replace){
- _add[key] = oVal
- }
- }
- else {
- _add[key] = oVal
- }
- });
- return removeEmptyObj(_add)
- }, {});
- }
- ```
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement