object-merge-advanced
Version:
Deeply merge JSON-like data structures
11 lines (9 loc) • 7.16 kB
JavaScript
/**
* @name object-merge-advanced
* @fileoverview Deeply merge JSON-like data structures
* @version 14.1.0
* @author Roy Revelt
* @license MIT
* {@link https://codsen.com/os/object-merge-advanced/}
*/
import{includes as x,isDate as b}from"lodash-es";import{nonEmpty as f}from"util-nonempty";import T from"rfdc";import{isPlainObject as d,hasOwnProp as K,existy as k,isStr as V,isNum as I,isBool as R,compareFn as J}from"codsen-utils";var A="14.1.0";import{isMatch as D}from"matcher";var M={arrayVsArrayAllMustBeFound:"any",caseSensitive:!0};function $(e,s,y){if(!e.length||!s.length)return!1;let t={...M,...y},g=typeof e=="string"?[e]:Array.from(e);return typeof s=="string"?g.some(r=>D(r,s,{caseSensitive:t.caseSensitive})):t.arrayVsArrayAllMustBeFound==="any"?s.some(r=>g.some(n=>D(n,r,{caseSensitive:t.caseSensitive}))):s.every(r=>g.some(n=>D(n,r,{caseSensitive:t.caseSensitive})))}var l=T(),H=A,m=Array.isArray;function S(e){return!!e&&e.some(s=>typeof s=="string")}function w(e,s){return Object.keys(e).length===0||Object.keys(s).length===0||Object.keys(e).every(y=>Object.keys(s).includes(y))||Object.keys(s).every(y=>Object.keys(e).includes(y))}function h(e){return e===null?"null":b(e)?"date":d(e)?"object":m(e)?"array":typeof e}var C={cb:null,mergeObjectsOnlyWhenKeysetMatches:!0,ignoreKeys:[],hardMergeKeys:[],hardArrayConcatKeys:[],mergeArraysContainingStringsToBeEmpty:!1,oneToManyArrayObjectMerge:!1,hardMergeEverything:!1,hardArrayConcat:!1,ignoreEverything:!1,concatInsteadOfMerging:!0,dedupeStringsInArrayValues:!1,mergeBoolsUsingOrNotAnd:!0,useNullAsExplicitFalse:!1};function E(e,s,y,t){let g;if(t.useNullAsExplicitFalse&&(s===null||y===null))return typeof t.cb=="function"?t.cb(s,y,null,{path:e.path,key:e.key,type:e.type}):null;let r=m(s)||d(s)?l(s):s,n=m(y)||d(y)?l(y):y,u;t.ignoreEverything?u=r:t.hardMergeEverything&&(u=n);let i=t.hardMergeEverything||t.ignoreEverything;if(m(r))if(f(r))if(m(n)&&f(n)){if(t.mergeArraysContainingStringsToBeEmpty&&(S(r)||S(n))){let c=i?u:[];return typeof t.cb=="function"?t.cb(l(s),l(y),c,{path:g,key:e.key,type:e.type}):c}if(t.hardArrayConcat){let c=i?u:r.concat(n);return typeof t.cb=="function"?t.cb(l(s),l(y),c,{path:g,key:e.key,type:e.type}):c}let a=[];for(let c=0,p=Math.max(r.length,n.length);c<p;c++)g=e.path?.length?`${e.path}.${c}`:`${c}`,d(r[c])&&d(n[c])&&(t.mergeObjectsOnlyWhenKeysetMatches&&w(r[c],n[c])||!t.mergeObjectsOnlyWhenKeysetMatches)?a.push(E({path:g,key:e.key,type:[h(r),h(n)]},r[c],n[c],t)):t.oneToManyArrayObjectMerge&&(r.length===1||n.length===1)?a.push(r.length===1?E({path:g,key:e.key,type:[h(r),h(n)]},r[0],n[c],t):E({path:g,key:e.key,type:[h(r),h(n)]},r[c],n[0],t)):t.concatInsteadOfMerging?(c<r.length&&a.push(r[c]),c<n.length&&a.push(n[c])):(c<r.length&&a.push(r[c]),c<n.length&&!x(r,n[c])&&a.push(n[c]));t.dedupeStringsInArrayValues&&a.every(c=>V(c))&&(a=[...new Set(a)].sort(J)),r=l(a)}else{let a=i?u:r;return typeof t.cb=="function"?t.cb(l(s),l(y),a,{path:g,key:e.key,type:e.type}):a}else{if(f(n)){let c=i?u:n;return typeof t.cb=="function"?t.cb(l(s),l(y),c,{path:g,key:e.key,type:e.type}):c}let a=i?u:r;return typeof t.cb=="function"?t.cb(l(s),l(y),a,{path:g,key:e.key,type:e.type}):a}else if(d(r)){if(f(r)){if(m(n)){if(f(n)){let o=i?u:n;return typeof t.cb=="function"?t.cb(l(s),l(y),o,{path:g,key:e.key,type:e.type}):o}let p=i?u:r;return typeof t.cb=="function"?t.cb(l(s),l(y),p,{path:g,key:e.key,type:e.type}):p}if(d(n)){Object.keys(n).forEach(o=>{g=e.path?.length?`${e.path}.${o}`:`${o}`,K(r,o)?$(o,t.ignoreKeys)?r[o]=E({path:g,key:o,type:[h(r),h(n)]},r[o],n[o],{...t,ignoreEverything:!0}):$(o,t.hardMergeKeys)?r[o]=E({path:g,key:o,type:[h(r),h(n)]},r[o],n[o],{...t,hardMergeEverything:!0}):$(o,t.hardArrayConcatKeys)?r[o]=E({path:g,key:o,type:[h(r),h(n)]},r[o],n[o],{...t,hardArrayConcat:!0}):r[o]=E({path:g,key:o,type:[h(r[o]),h(n[o])]},r[o],n[o],t):r[o]=n[o]});let p=i?u:r;return typeof t.cb=="function"?t.cb(l(s),l(y),p,{path:e.path,key:e.key,type:e.type}):r}let c=i?u:r;return typeof t.cb=="function"?t.cb(l(s),l(y),c,{path:e.path,key:e.key,type:e.type}):c}if(m(n)||d(n)||f(n)){let c=i?u:n;return typeof t.cb=="function"?t.cb(l(s),l(y),c,{path:e.path,key:e.key,type:e.type}):c}let a=i?u:r;return typeof t.cb=="function"?t.cb(l(s),l(y),a,{path:e.path,key:e.key,type:e.type}):a}else if(b(r)){if(Number.isFinite(+r)){if(b(n)){if(Number.isFinite(+n)){let o=i?u:r>n?r:n;return typeof t.cb=="function"?t.cb(l(s),l(y),o,{path:e.path,key:e.key,type:e.type}):o}let p=i?u:r;return typeof t.cb=="function"?t.cb(l(s),l(y),p,{path:e.path,key:e.key,type:e.type}):p}let c=i?u:n||r;return typeof t.cb=="function"?t.cb(l(s),l(y),c,{path:e.path,key:e.key,type:e.type}):c}if(b(n)){let c=i?u:n;return typeof t.cb=="function"?t.cb(l(s),l(y),c,{path:e.path,key:e.key,type:e.type}):c}let a=i?u:n;return typeof t.cb=="function"?t.cb(l(s),l(y),a,{path:e.path,key:e.key,type:e.type}):a}else if(V(r)){if(f(r)){if((m(n)||d(n)||V(n))&&f(n)){let p=i?u:n;return typeof t.cb=="function"?t.cb(l(s),l(y),p,{path:e.path,key:e.key,type:e.type}):p}let c=i?u:r;return typeof t.cb=="function"?t.cb(l(s),l(y),c,{path:e.path,key:e.key,type:e.type}):c}if(k(n)&&!R(n)){let c=i?u:n;return typeof t.cb=="function"?t.cb(l(s),l(y),c,{path:e.path,key:e.key,type:e.type}):c}let a=i?u:r;return typeof t.cb=="function"?t.cb(l(s),l(y),a,{path:e.path,key:e.key,type:e.type}):a}else if(I(r)){if(f(n)){let c=i?u:n;return typeof t.cb=="function"?t.cb(l(s),l(y),c,{path:e.path,key:e.key,type:e.type}):c}let a=i?u:r;return typeof t.cb=="function"?t.cb(l(s),l(y),a,{path:e.path,key:e.key,type:e.type}):a}else if(R(r)){if(R(n)){if(t.mergeBoolsUsingOrNotAnd){let p=i?u:r||n;return typeof t.cb=="function"?t.cb(l(s),l(y),p,{path:e.path,key:e.key,type:e.type}):p}let c=i?u:r&&n;return typeof t.cb=="function"?t.cb(l(s),l(y),c,{path:e.path,key:e.key,type:e.type}):c}if(k(n)){let c=i?u:n;return typeof t.cb=="function"?t.cb(l(s),l(y),c,{path:e.path,key:e.key,type:e.type}):c}let a=i?u:r;return typeof t.cb=="function"?t.cb(l(s),l(y),a,{path:e.path,key:e.key,type:e.type}):a}else if(r===null){if(k(n)){let c=i?u:n;return typeof t.cb=="function"?t.cb(l(s),l(y),c,{path:e.path,key:e.key,type:e.type}):c}let a=i?u:r;return typeof t.cb=="function"?t.cb(l(s),l(y),a,{path:e.path,key:e.key,type:e.type}):a}else{let a=i?u:n;return typeof t.cb=="function"?t.cb(l(s),l(y),a,{path:e.path,key:e.key,type:e.type}):a}let v=i?u:r;return typeof t.cb=="function"?t.cb(l(s),l(y),v,{path:e.path,key:e.key,type:e.type}):v}function z(e,s,y){if(!arguments.length)throw new TypeError("object-merge-advanced/mergeAdvanced(): [THROW_ID_01] Both inputs are missing");if(k(y)&&!d(y))throw new TypeError(`object-merge-advanced/mergeAdvanced(): [THROW_ID_02] The optional options object should be a plain object, currently it's ${JSON.stringify(y,null,4)} (type ${typeof y})`);let t={...C,...y};return typeof t.ignoreKeys=="string"&&(t.ignoreKeys=[t.ignoreKeys]),typeof t.hardMergeKeys=="string"&&(t.hardMergeKeys=[t.hardMergeKeys]),t?.hardMergeKeys?.includes("*")&&(t.hardMergeEverything=!0),t?.ignoreKeys?.includes("*")&&(t.ignoreEverything=!0),E({key:null,path:"",type:[h(e),h(s)]},e,s,t)}export{C as defaults,z as mergeAdvanced,H as version};