recursive-diff
Version:
Find diff between any two variables where variables be any valid JavaScript data type like string, numeric, array or object
1 lines • 3.75 kB
JavaScript
!function(e,t){"object"==typeof exports&&"object"==typeof module?module.exports=t():"function"==typeof define&&define.amd?define([],t):"object"==typeof exports?exports.recursiveDiff=t():e.recursiveDiff=t()}("undefined"!=typeof self?self:this,(()=>{return e={182:e=>{const t={NUMBER:"NUMBER",BOOLEAN:"BOOLEAN",STRING:"STRING",NULL:"NULL",UNDEFINED:"UNDEFINED",DATE:"DATE",ARRAY:"ARRAY",MAP:"MAP",SET:"SET",ITERABLE_OBJECT:"ITERABLE_OBJECT",DEFAULT:"OBJECT"};e.exports={types:t,iterableTypes:[t.ITERABLE_OBJECT,t.MAP,t.ARRAY,t.SET],errors:{EMPTY_DIFF:"No diff object is provided, Nothing to apply",INVALID_DIFF_FORMAT:"Invalid diff format",INVALID_DIFF_OP:"Unsupported operation provided into diff object"}}},688:(e,t,n)=>{const{types:r,iterableTypes:o,errors:i}=n(182),s=n(555),a={[r.NUMBER]:s.isNumber,[r.BOOLEAN]:s.isBoolean,[r.STRING]:s.isString,[r.DATE]:s.isDate,[r.UNDEFINED]:s.isUndefined,[r.NULL]:s.isNull,[r.ARRAY]:s.isArray,[r.MAP]:s.isMap,[r.SET]:s.isSet,[r.ITERABLE_OBJECT]:s.isIterableObject},l={[r.DATE]:s.areDatesEqual};function f(e){const t=Object.keys(a);let n=r.DEFAULT;for(let r=0;r<t.length;r+=1)if(a[t[r]](e)){n=t[r];break}return n}function c(e,t,n,r,o){const i={op:n,path:r};return"add"!==n&&"update"!==n||(i.val=t),o&&"add"!==n&&(i.oldVal=e),i}function p(e,t,n,i,a){const u=f(e),E=f(t),d=i||[],A=a||[];if(function(e,t){return e===t&&o.indexOf(e)>=0}(u,E)){const o=function(e,t,n){if(n===r.ARRAY){const n=e.length>t.length?new Array(e.length):new Array(t.length);return n.fill(0),new Set(n.map(((e,t)=>t)))}return new Set(Object.keys(e).concat(Object.keys(t)))}(e,t,u).values();let{value:i,done:s}=o.next();for(;!s;){Object.prototype.hasOwnProperty.call(e,i)?Object.prototype.hasOwnProperty.call(t,i)?p(e[i],t[i],n,d.concat(i),A):A.push(c(e[i],t[i],"delete",d.concat(i),n)):A.push(c(e[i],t[i],"add",d.concat(i),n));const r=o.next();i=r.value,s=r.done}}else{const o=function(e,t,n,o){let i;return n===r.UNDEFINED&&o!==r.UNDEFINED?i="add":n!==r.UNDEFINED&&o===r.UNDEFINED?i="delete":function(e,t,n,r){return n===r&&(l[n]?l[n](e,t):e===t)}(e,t,n,o)?s.noop():i="update",i}(e,t,u,E);null!=o&&A.push(c(e,t,o,i,n))}return A}const u={add:s.setValueByPath,update:s.setValueByPath,delete:s.deleteValueByPath};e.exports={getDiff:(e,t,n=!1)=>p(e,t,n),applyDiff:(e,t,n)=>function(e,t,n){if(!(t instanceof Array))throw new Error(i.INVALID_DIFF_FORMAT);let r=e;return t.forEach((e=>{const{op:t,val:o,path:s}=e;if(!u[t])throw new Error(i.INVALID_DIFF_OP);r=u[t](r,s,o,n)})),r}(e,t,n)}},555:e=>{const t=e=>t=>t instanceof e,n=t(Date),r=t(Array),o=t(Map),i=t(Set),s=e=>"[object Object]"===Object.prototype.toString.call(e);e.exports={isNumber:e=>"number"==typeof e,isBoolean:e=>"boolean"==typeof e,isString:e=>"string"==typeof e,isDate:n,isUndefined:e=>void 0===e,isNull:e=>null===e,isArray:r,isMap:o,isSet:i,isIterableObject:s,noop:()=>{},areDatesEqual:(e,t)=>e.getTime()===t.getTime(),setValueByPath:function(e,t=[],n,o){if(!r(t))throw new Error(`Diff path: "${t}" is not valid`);const{length:i}=t;if(0===i)return n;let s=e;for(let r=0;r<i;r+=1){const a=t[r];if(!s)throw new Error(`Invalid path: "${t}" for object: ${JSON.stringify(e,null,2)}`);if(null==a)throw new Error(`Invalid path: "${t}" for object: ${JSON.stringify(e,null,2)}`);r!==i-1?(s=s[a],o&&o(s)):s[a]=n}return e},deleteValueByPath:function(e,t){const n=t||[];if(0===n.length)return;let r=e;const{length:o}=n;for(let i=0;i<o;i+=1)if(i!==o-1){if(!r[n[i]])throw new Error(`Invalid path: "${t}" for object: ${JSON.stringify(e,null,2)}`);r=r[n[i]]}else if(s(r))delete r[n[i]];else{const e=parseInt(n[i],10);for(;r.length>e;)r.pop()}return e}}}},t={},function n(r){var o=t[r];if(void 0!==o)return o.exports;var i=t[r]={exports:{}};return e[r](i,i.exports,n),i.exports}(688);var e,t}));