UNPKG

@bluelovers/deep-diff

Version:

Javascript utility for calculating deep difference, capturing changes, and applying changes across objects; for nodejs and the browser.

3 lines (2 loc) 6.62 kB
"use strict";var e;Object.defineProperty(exports,"__esModule",{value:!0}),exports.EnumKinds=void 0,(e=exports.EnumKinds||(exports.EnumKinds={})).DiffEdit="E",e.DiffNew="N",e.DiffDeleted="D",e.DiffArray="A";const t=["N","E","A","D"];class Diff{constructor(e,t){this.kind=e,null!=t&&t.length&&(this.path=t)}}class DiffEdit extends Diff{constructor(e,t,r){super("E",e),this.lhs=t,this.rhs=r}}class DiffNew extends Diff{constructor(e,t){super("N",e),this.rhs=t}}class DiffDeleted extends Diff{constructor(e,t){super("D",e),this.lhs=t}}class DiffArray extends Diff{constructor(e,t,r){super("A",e),this.index=t,this.item=r}}function arrayRemove(e,t){return e.splice(t,1)}function realTypeOf(e){const t=typeof e;return"object"!==t?t:e===Math?"math":null===e?"null":Array.isArray(e)?"array":"[object Date]"===Object.prototype.toString.call(e)?"date":"function"==typeof e.toString&&/^\/.*\//.test(e.toString())?"regexp":"object"}function hashThisString(e){let t=0;if(0===e.length)return t;for(let r=0;r<e.length;r++)t=(t<<5)-t+e.charCodeAt(r),t&=t;return t}function getOrderIndependentHash(e){let t=0;const r=realTypeOf(e);if("array"===r){e.forEach((function(e){t+=getOrderIndependentHash(e)}));const n=normalizeHashDesc(r,{hash:t});return t+hashThisString(n)}if("object"===r){for(const n in e)if(e.hasOwnProperty(n)){const i=normalizeHashDesc(r,{key:n,hash:getOrderIndependentHash(e[n])});t+=hashThisString(i)}return t}const n=normalizeHashDesc(r,{value:e});return t+hashThisString(n)}function normalizeHashDesc(e,t){switch(e){case"array":return`[ type: ${e} , hash: ${t.hash}]`;case"object":return`[ type: ${e}, key: ${t.key} , hash: ${t.hash}]`;default:return`[ type: ${e} , value: ${t.value}]`}}function _deepDiff(e,t,r,n,i,f,a,s){r=r||[],a=a||[];const o=(i=i||[]).slice(0);if(null!=f){if(n){if("function"==typeof n&&n(o,f))return;if("object"==typeof n){if(n.prefilter&&n.prefilter(o,f))return;if(n.normalize){const r=n.normalize(o,f,e,t);r&&(e=r[0],t=r[1])}}}o.push(f)}"regexp"===realTypeOf(e)&&"regexp"===realTypeOf(t)&&(e=e.toString(),t=t.toString());const h=typeof e,p=typeof t;let l,c,d,u;const D="undefined"!==h||a&&a.length>0&&a[a.length-1].lhs&&Object.getOwnPropertyDescriptor(a[a.length-1].lhs,f),g="undefined"!==p||a&&a.length>0&&a[a.length-1].rhs&&Object.getOwnPropertyDescriptor(a[a.length-1].rhs,f);if(!D&&g)r.push(new DiffNew(o,t));else if(!g&&D)r.push(new DiffDeleted(o,e));else if(realTypeOf(e)!==realTypeOf(t))r.push(new DiffEdit(o,e,t));else if("date"===realTypeOf(e)&&e-t!=0)r.push(new DiffEdit(o,e,t));else if("object"===h&&null!==e&&null!==t){for(l=a.length-1;l>-1;--l)if(a[l].lhs===e){u=!0;break}if(u)e!==t&&r.push(new DiffEdit(o,e,t));else{if(a.push({lhs:e,rhs:t}),Array.isArray(e)){for(s&&(e.sort((function(e,t){return getOrderIndependentHash(e)-getOrderIndependentHash(t)})),t.sort((function(e,t){return getOrderIndependentHash(e)-getOrderIndependentHash(t)}))),l=t.length-1,c=e.length-1;l>c;)r.push(new DiffArray(o,l,new DiffNew(void 0,t[l--])));for(;c>l;)r.push(new DiffArray(o,c,new DiffDeleted(void 0,e[c--])));for(;l>=0;--l)_deepDiff(e[l],t[l],r,n,o,l,a,s)}else{const i=Object.keys(e).concat(Object.getOwnPropertySymbols(e)),f=Object.keys(t).concat(Object.getOwnPropertySymbols(t));for(l=0;l<i.length;++l)d=i[l],u=f.indexOf(d),u>=0?(_deepDiff(e[d],t[d],r,n,o,d,a,s),f[u]=null):_deepDiff(e[d],void 0,r,n,o,d,a,s);for(l=0;l<f.length;++l)d=f[l],d&&_deepDiff(void 0,t[d],r,n,o,d,a,s)}a.length=a.length-1}}else e!==t&&("number"===h&&isNaN(e)&&isNaN(t)||r.push(new DiffEdit(o,e,t)))}function observableDiff(e,t,r,n,i){const f=[];if(_deepDiff(e,t,f,n,null,null,null,i),r)for(let e=0;e<f.length;++e)r(f[e]);return f}function deepDiff(e,t,r,n){const i=observableDiff(e,t,n?function(e){e&&n.push(e)}:void 0,r);return n||(i.length?i:void 0)}function _applyArrayChange(e,t,r){var n;if(null!==(n=r.path)&&void 0!==n&&n.length){let[n,i]=function _traversalObject(e,t){const r=(t=t.slice()).pop();let n=e;return t.reduce(((e,t)=>n=e[t]),n),[n,r]}(e[t],r.path);switch(r.kind){case"A":_applyArrayChange(n[i],r.index,r.item);break;case"D":delete n[i];break;case"E":case"N":n[i]=r.rhs}}else switch(r.kind){case"A":_applyArrayChange(e[t],r.index,r.item);break;case"D":e=arrayRemove(e,t);break;case"E":case"N":e[t]=r.rhs}return e}function isIDiffNode(e){return null!=e&&e&&t.includes(e.kind)}function applyChange(e,t,r){if(void 0===r&&isIDiffNode(t)&&(r=t),e&&r&&r.kind){let t=e,n=-1,i=r.path?r.path.length-1:0;for(;++n<i;)void 0===t[r.path[n]]&&(t[r.path[n]]=void 0!==r.path[n+1]&&"number"==typeof r.path[n+1]?[]:{}),t=t[r.path[n]];switch(r.kind){case"A":r.path&&void 0===t[r.path[n]]&&(t[r.path[n]]=[]),_applyArrayChange(r.path?t[r.path[n]]:t,r.index,r.item);break;case"D":delete t[r.path[n]];break;case"E":case"N":t[r.path[n]]=r.rhs}}}function revertArrayChange(e,t,r){if(r.path&&r.path.length){let n,i=e[t],f=r.path.length-1;for(n=0;n<f;n++)i=i[r.path[n]];switch(r.kind){case"A":revertArrayChange(i[r.path[n]],r.index,r.item);break;case"D":case"E":i[r.path[n]]=r.lhs;break;case"N":delete i[r.path[n]]}}else switch(r.kind){case"A":revertArrayChange(e[t],r.index,r.item);break;case"D":case"E":e[t]=r.lhs;break;case"N":e=arrayRemove(e,t)}return e}function revertChange(e,t,r){if(e&&t&&null!=r&&r.kind){let t,n,i=e;for(n=r.path.length-1,t=0;t<n;t++)void 0===i[r.path[t]]&&(i[r.path[t]]={}),i=i[r.path[t]];switch(r.kind){case"A":revertArrayChange(i[r.path[t]],r.index,r.item);break;case"D":case"E":i[r.path[t]]=r.lhs;break;case"N":delete i[r.path[t]]}}}function _applyDiffChangeCore(e,t,r){return t.forEach((t=>{r(e,!0,t)})),!0}exports.Diff=Diff,exports.DiffArray=DiffArray,exports.DiffDeleted=DiffDeleted,exports.DiffEdit=DiffEdit,exports.DiffNew=DiffNew,exports.applyChange=applyChange,exports.applyDiff=function applyDiff(e,t,r){return e&&t&&observableDiff(e,t,(function(n){r&&!r(e,t,n)||applyChange(e,t,n)})),e},exports.applyDiffChange=function applyDiffChange(e,t){if(_applyDiffChangeCore(e,t,applyChange))return e},exports.deepDiff=deepDiff,exports.default=deepDiff,exports.diff=deepDiff,exports.getOrderIndependentHash=getOrderIndependentHash,exports.isIDiffNode=isIDiffNode,exports.observableDiff=observableDiff,exports.orderIndependentDiff=function orderIndependentDiff(e,t,r,n){const i=observableDiff(e,t,n?function(e){e&&n.push(e)}:void 0,r,!0);return n||(i.length?i:void 0)},exports.orderIndependentObservableDiff=function orderIndependentObservableDiff(e,t,r,n,i,f,a){return _deepDiff(e,t,r,n,i,f,a,!0)},exports.revertChange=revertChange,exports.revertDiffChange=function revertDiffChange(e,t){if(_applyDiffChangeCore(e,t,revertChange))return e}; //# sourceMappingURL=index.cjs.production.min.cjs.map