Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

json-diff-kit

Package Overview
Dependencies
Maintainers
1
Versions
29
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

json-diff-kit - npm Package Compare versions

Comparing version 0.0.3 to 1.0.0

dist/viewer.css

6

dist/index.js

@@ -1,3 +0,3 @@

(()=>{var I=Object.create;var b=Object.defineProperty;var T=Object.getOwnPropertyDescriptor;var W=Object.getOwnPropertyNames;var J=Object.getPrototypeOf,R=Object.prototype.hasOwnProperty;var P=e=>b(e,"__esModule",{value:!0});var E=(e=>typeof require!="undefined"?require:typeof Proxy!="undefined"?new Proxy(e,{get:(t,i)=>(typeof require!="undefined"?require:t)[i]}):e)(function(e){if(typeof require!="undefined")return require.apply(this,arguments);throw new Error('Dynamic require of "'+e+'" is not supported')});var z=(e,t,i,s)=>{if(t&&typeof t=="object"||typeof t=="function")for(let n of W(t))!R.call(e,n)&&(i||n!=="default")&&b(e,n,{get:()=>t[n],enumerable:!(s=T(t,n))||s.enumerable});return e},_=(e,t)=>z(P(b(e!=null?I(J(e)):{},"default",!t&&e&&e.__esModule?{get:()=>e.default,enumerable:!0}:{value:e,enumerable:!0})),e);var $=(e,t=new Map)=>{if(typeof e!="object"||e===null)return!1;if(t.has(e))return!0;if(t.set(e,!0),Array.isArray(e)){for(let i=0;i<e.length;i++)if($(e[i],t))return!0;return!1}for(let i in e)if($(e[i],t))return!0;return!1},j=$;var g=(e,t,i,s=1/0)=>{if(!e||typeof e!="object")return JSON.stringify(e,t,i);let n=s<1?'"..."':Array.isArray(e)?`[${e.map(u=>g(u,t,i,s-1)).join(",")}]`:`{${Object.keys(e).map(u=>`"${u}": ${g(e[u],t,i,s-1)}`).join(", ")}}`;return JSON.stringify(JSON.parse(n),t,i)},c=g;var B=(e,t=1/0)=>Array.isArray(e)||typeof e=="object"?c(e,null,null,t):typeof e=="string"?`"${e}"`:String(e),y=B;var G=e=>Array.isArray(e)?"array":e===null?"null":typeof e,h=G;var C=(e,t,i=1,s,n)=>{if(i>s.maxDepth)return[[{level:i,type:"equal",text:"..."}],[{level:i,type:"equal",text:"..."}]];let u=[],f=[];if(e===null&&t===null||e===void 0&&t===void 0)return[u,f];if(e==null)return u.push({level:i,type:"equal",text:""}),f.push({level:i,type:"add",text:y(t)}),[u,f];if(t==null)return u.push({level:i,type:"remove",text:y(e)}),f.push({level:i,type:"equal",text:""}),[u,f];let a=Object.keys(e),o=Object.keys(t);for(a.sort(),o.sort();a.length||o.length;){let r=a[0]??null,p=o[0]??null;if(r===p)if(h(e[r])!==h(t[p]))s.showModifications?(u.push({level:i,type:"modify",text:`"${r}": ${y(e[r])}`}),f.push({level:i,type:"modify",text:`"${p}": ${y(t[p])}`})):(u.push({level:i,type:"remove",text:`"${r}": ${y(e[r])}`}),u.push({level:i,type:"equal",text:""}),f.push({level:i,type:"equal",text:""}),f.push({level:i,type:"add",text:`"${p}": ${y(t[p])}`}));else if(Array.isArray(e[r])){let x=[...e[r]],l=[...t[p]],[m,q]=n(x,l,r,p,i,s,[],[]);u.push(...m),f.push(...q)}else if(typeof e[r]=="object"){let x=C(e[r],t[p],i+1,s,n);u.push({level:i,type:"equal",text:`"${r}": {`}),u.push(...x[0]),u.push({level:i,type:"equal",text:"}"}),f.push({level:i,type:"equal",text:`"${p}": {`}),f.push(...x[1]),f.push({level:i,type:"equal",text:"}"})}else e[r]!==t[p]?s.showModifications?(u.push({level:i,type:"modify",text:`"${r}": ${y(e[r])}`}),f.push({level:i,type:"modify",text:`"${p}": ${y(t[p])}`})):(u.push({level:i,type:"remove",text:`"${r}": ${y(e[r])}`}),u.push({level:i,type:"equal",text:""}),f.push({level:i,type:"equal",text:""}),f.push({level:i,type:"add",text:`"${p}": ${y(t[p])}`})):(u.push({level:i,type:"equal",text:`"${r}": ${y(e[r])}`}),f.push({level:i,type:"equal",text:`"${p}": ${y(t[p])}`}));else r&&p?r<p?(u.push({level:i,type:"remove",text:`"${r}": ${y(e[r])}`}),f.push({level:i,type:"equal",text:""})):(u.push({level:i,type:"equal",text:""}),f.push({level:i,type:"add",text:`"${p}": ${y(t[p])}`})):r?(u.push({level:i,type:"remove",text:`"${r}": ${y(e[r])}`}),f.push({level:i,type:"equal",text:""})):p&&(u.push({level:i,type:"equal",text:""}),f.push({level:i,type:"add",text:`"${p}": ${y(t[p])}`}));r?p?r===p?(a.shift(),o.shift()):r<p?a.shift():o.shift():a.shift():o.shift()}if(u.length!==f.length)throw new Error("Diff error: length not match for left & right, please report a bug with your data.");return[u,f]},D=C;var H=(e,t,i,s,n,u)=>{let f=Array(e.length+1).fill(0).map(()=>Array(t.length+1).fill(0)),a=Array(e.length+1).fill(0).map(()=>Array(t.length+1).fill(0));for(let l=1;l<=e.length;l++)a[l][0]="up";for(let l=1;l<=t.length;l++)a[0][l]="left";for(let l=1;l<=e.length;l++)for(let m=1;m<=t.length;m++){let q=h(e[l-1]),F=h(t[m-1]);q===F&&(q==="array"||q==="object")||e[l-1]===t[m-1]?(f[l][m]=f[l-1][m-1]+1,a[l][m]="diag"):f[l-1][m]>=f[l][m-1]?(f[l][m]=f[l-1][m],a[l][m]="up"):(f[l][m]=f[l][m-1],a[l][m]="left")}let o=e.length,r=t.length,p=[],x=[];for(;o>0||r>0;)if(a[o][r]==="diag"){let l=h(e[o-1]);if(l==="array"){let[m,q]=A(e[o-1],t[r-1],i,s,n+2,u);p.unshift(...m),x.unshift(...q)}else if(l==="object"){let[m,q]=D(e[o-1],t[r-1],n+2,u,A);p.unshift({level:n+1,type:"equal",text:"}"}),x.unshift({level:n+1,type:"equal",text:"}"}),p.unshift(...m),x.unshift(...q),p.unshift({level:n+1,type:"equal",text:"{"}),x.unshift({level:n+1,type:"equal",text:"{"})}else p.unshift({level:n+1,type:"equal",text:y(e[o-1])}),x.unshift({level:n+1,type:"equal",text:y(t[r-1])});o--,r--}else a[o][r]==="up"?u.showModifications&&o>1&&a[o-1][r]==="left"?(p.unshift({level:n+1,type:"modify",text:y(e[o-1])}),x.unshift({level:n+1,type:"modify",text:y(t[r-1])}),o--,r--):(p.unshift({level:n+1,type:"remove",text:y(e[o-1])}),x.unshift({level:n+1,type:"equal",text:""}),o--):(p.unshift({level:n+1,type:"equal",text:""}),x.unshift({level:n+1,type:"add",text:y(t[r-1])}),r--);return[p,x]},A=(e,t,i,s,n,u,f=[],a=[])=>{if(i&&s?(f.push({level:n,type:"equal",text:`"${i}": [`}),a.push({level:n,type:"equal",text:`"${s}": [`})):(f.push({level:n,type:"equal",text:"["}),a.push({level:n,type:"equal",text:"["})),n>=u.maxDepth)f.push({level:n+1,type:"equal",text:"..."}),a.push({level:n+1,type:"equal",text:"..."});else{let[o,r]=H(e,t,i,s,n,u);f.push(...o),a.push(...r)}return f.push({level:n,type:"equal",text:"]"}),a.push({level:n,type:"equal",text:"]"}),[f,a]},S=A;var N=(e,t,i,s,n,u,f=[],a=[])=>{if(e=[...e],t=[...t],i&&s?(f.push({level:n,type:"equal",text:`"${i}": [`}),a.push({level:n,type:"equal",text:`"${s}": [`})):(f.push({level:n,type:"equal",text:"["}),a.push({level:n,type:"equal",text:"["})),n>=u.maxDepth)f.push({level:n+1,type:"equal",text:"..."}),a.push({level:n+1,type:"equal",text:"..."});else for(;e.length||t.length;){let o=e[0]??null,r=t[0]??null;if(o&&r){if(h(o)!==h(r))f.push({level:n+1,type:"remove",text:y(o)}),f.push({level:n,type:"equal",text:""}),a.push({level:n,type:"equal",text:""}),a.push({level:n+1,type:"add",text:y(r)});else if(h(o)==="object"){f.push({level:n+1,type:"equal",text:"{"}),a.push({level:n+1,type:"equal",text:"{"});let[p,x]=D(o,r,n+2,u,N);f.push(...p),a.push(...x),f.push({level:n+1,type:"equal",text:"}"}),a.push({level:n+1,type:"equal",text:"}"})}else if(h(o)==="array"){let[p,x]=N(o,r,"","",n+2,u,[],[]);f.push(...p),a.push(...x)}else o===r?(f.push({level:n+1,type:"equal",text:y(o)}),a.push({level:n+1,type:"equal",text:y(r)})):u.showModifications?(f.push({level:n+1,type:"modify",text:y(o)}),a.push({level:n+1,type:"modify",text:y(r)})):(f.push({level:n+1,type:"remove",text:y(o)}),f.push({level:n+1,type:"equal",text:""}),a.push({level:n+1,type:"equal",text:""}),a.push({level:n+1,type:"add",text:y(r)}));e.shift(),t.shift()}else o?(f.push({level:n+1,type:"remove",text:y(o)}),a.push({level:n+1,type:"equal",text:""}),e.shift()):r&&(f.push({level:n+1,type:"equal",text:""}),a.push({level:n+1,type:"add",text:y(r)}),t.shift())}return f.push({level:n,type:"equal",text:"]"}),a.push({level:n,type:"equal",text:"]"}),[f,a]},k=N;var w=e=>{if(!e||typeof e!="object")return e;if(Array.isArray(e)){let i=[...e];return i.sort((s,n)=>typeof s=="number"&&typeof n=="number"?s-n:String(s).localeCompare(String(n))),i.map(s=>w(s))}let t={...e};for(let i in t)t[i]=w(t[i]);return t},O=w;var M=class{options;arrayDiffFunc;constructor({detectCircular:t=!0,maxDepth:i=1/0,showModifications:s=!0,arrayDiffMethod:n="normal"}={}){this.options={detectCircular:t,maxDepth:i,showModifications:s,arrayDiffMethod:n},this.arrayDiffFunc=n==="lcs"||n==="unorder-lcs"?S:k}detectCircular(t){if(this.options.detectCircular&&j(t))throw new Error(`Circular reference detected in object (with keys ${Object.keys(t).map(i=>`"${i}"`).join(", ")})`)}sortResultLines(t,i){for(let s=0;s<t.length;s++){let n=!1;for(let u=1;u<t.length;u++)if(t[u].type==="remove"&&t[u-1].type==="equal"&&i[u].type==="equal"&&i[u-1].type==="add"){let f=t[u-1];t[u-1]=t[u],t[u]=f;let a=i[u-1];i[u-1]=i[u],i[u]=a,n=!0}if(!n)break}}calculateLineNumbers(t){let i=0;for(let s of t)!s.text||(s.lineNumber=++i)}calculateCommas(t){let i=Array(t.length).fill(0);for(let s=t.length-1;s>0;s--)t[s].text?i[s-1]=s:i[s-1]=i[s];for(let s=0;s<t.length;s++)!t[s].text.endsWith("{")&&!t[s].text.endsWith("[")&&t[s].text&&i[s]&&t[s].level<=t[i[s]].level&&(t[s].comma=!0)}diff(t,i){this.detectCircular(t),this.detectCircular(i),(this.options.arrayDiffMethod==="unorder-normal"||this.options.arrayDiffMethod==="unorder-lcs")&&(t=O(t),i=O(i));let s=[],n=[],u=h(t),f=h(i);if(u!==f){s=c(t,null,1,this.options.maxDepth).split(`
`).map(r=>({level:r.match(/^\s+/)?.[0]?.length||0,type:"remove",text:r.replace(/^\s+/,"").replace(/,$/g,""),comma:r.endsWith(",")})),n=c(i,null,1,this.options.maxDepth).split(`
`).map(r=>({level:r.match(/^\s+/)?.[0]?.length||0,type:"add",text:r.replace(/^\s+/,"").replace(/,$/g,""),comma:r.endsWith(",")}));let a=s.length,o=n.length;s.push(...Array(o).fill({level:0,type:"equal",text:""})),n.unshift(...Array(a).fill({level:0,type:"equal",text:""}))}else u==="object"?([s,n]=D(t,i,1,this.options,this.arrayDiffFunc),s.unshift({level:0,type:"equal",text:"{"}),s.push({level:0,type:"equal",text:"}"}),n.unshift({level:0,type:"equal",text:"{"}),n.push({level:0,type:"equal",text:"}"})):u==="array"?[s,n]=this.arrayDiffFunc(t,i,"","",0,this.options):t!==i?this.options.showModifications?(s=[{level:0,type:"modify",text:c(t,null,null,this.options.maxDepth)}],n=[{level:0,type:"modify",text:c(i,null,null,this.options.maxDepth)}]):(s=[{level:0,type:"remove",text:c(t,null,null,this.options.maxDepth)},{level:0,type:"equal",text:""}],n=[{level:0,type:"equal",text:""},{level:0,type:"add",text:c(i,null,null,this.options.maxDepth)}]):(s=[{level:0,type:"equal",text:c(t,null,null,this.options.maxDepth)}],n=[{level:0,type:"equal",text:c(i,null,null,this.options.maxDepth)}]);return this.sortResultLines(s,n),this.calculateLineNumbers(s),this.calculateLineNumbers(n),this.calculateCommas(s),this.calculateCommas(n),[s,n]}},K=M;var d=_(E("react")),V=e=>{let[t,i]=e.diff,s=`${String(t.length).length/2}em`,n=e.indent??2,u=n==="tab"?" ":" ",f=n==="tab"?1:n,a=o=>{let r=t[o],p=i[o];return d.createElement("tr",{key:o},e.lineNumbers&&d.createElement("td",{className:`line-${r.type} line-number`,style:{width:s}},r.lineNumber),d.createElement("td",{className:`line-${r.type}`},d.createElement("pre",null,r.text&&u.repeat(r.level*f),r.text,r.comma&&",")),e.lineNumbers&&d.createElement("td",{className:`line-${p.type} line-number`,style:{width:s}},p.lineNumber),d.createElement("td",{className:`line-${p.type}`},d.createElement("pre",null,p.text&&u.repeat(p.level*f),p.text,p.comma&&",")))};return d.createElement("table",{className:`json-diff-viewer ${e.className||""}`,style:e.style},d.createElement("tbody",null,t.map((o,r)=>a(r))))};V.displayName="Viewer";var Q=V;})();
(()=>{var I=Object.create;var b=Object.defineProperty;var R=Object.getOwnPropertyDescriptor;var T=Object.getOwnPropertyNames;var W=Object.getPrototypeOf,J=Object.prototype.hasOwnProperty;var P=e=>b(e,"__esModule",{value:!0});var E=(e=>typeof require!="undefined"?require:typeof Proxy!="undefined"?new Proxy(e,{get:(t,i)=>(typeof require!="undefined"?require:t)[i]}):e)(function(e){if(typeof require!="undefined")return require.apply(this,arguments);throw new Error('Dynamic require of "'+e+'" is not supported')});var z=(e,t,i,r)=>{if(t&&typeof t=="object"||typeof t=="function")for(let n of T(t))!J.call(e,n)&&(i||n!=="default")&&b(e,n,{get:()=>t[n],enumerable:!(r=R(t,n))||r.enumerable});return e},_=(e,t)=>z(P(b(e!=null?I(W(e)):{},"default",!t&&e&&e.__esModule?{get:()=>e.default,enumerable:!0}:{value:e,enumerable:!0})),e);var $=(e,t=new Map)=>{if(typeof e!="object"||e===null)return!1;if(t.has(e))return!0;if(t.set(e,!0),Array.isArray(e)){for(let i=0;i<e.length;i++)if($(e[i],t))return!0;return!1}for(let i in e)if($(e[i],t))return!0;return!1},j=$;var g=(e,t,i,r=1/0)=>{if(!e||typeof e!="object")return JSON.stringify(e,t,i);let n=r<1?'"..."':Array.isArray(e)?`[${e.map(u=>g(u,t,i,r-1)).join(",")}]`:`{${Object.keys(e).map(u=>`"${u}": ${g(e[u],t,i,r-1)}`).join(", ")}}`;return JSON.stringify(JSON.parse(n),t,i)},c=g;var B=(e,t=1/0)=>Array.isArray(e)||typeof e=="object"?c(e,null,null,t):typeof e=="string"?`"${e}"`:String(e),y=B;var G=e=>Array.isArray(e)?"array":e===null?"null":typeof e,h=G;var C=(e,t,i=1,r,n)=>{if(i>r.maxDepth)return[[{level:i,type:"equal",text:"..."}],[{level:i,type:"equal",text:"..."}]];let u=[],s=[];if(e===null&&t===null||e===void 0&&t===void 0)return[u,s];if(e==null)return u.push({level:i,type:"equal",text:""}),s.push({level:i,type:"add",text:y(t)}),[u,s];if(t==null)return u.push({level:i,type:"remove",text:y(e)}),s.push({level:i,type:"equal",text:""}),[u,s];let o=Object.keys(e),a=Object.keys(t);for(o.sort(),a.sort();o.length||a.length;){let f=o[0]??null,p=a[0]??null;if(f===p)if(h(e[f])!==h(t[p]))r.showModifications?(u.push({level:i,type:"modify",text:`"${f}": ${y(e[f])}`}),s.push({level:i,type:"modify",text:`"${p}": ${y(t[p])}`})):(u.push({level:i,type:"remove",text:`"${f}": ${y(e[f])}`}),u.push({level:i,type:"equal",text:""}),s.push({level:i,type:"equal",text:""}),s.push({level:i,type:"add",text:`"${p}": ${y(t[p])}`}));else if(Array.isArray(e[f])){let x=[...e[f]],l=[...t[p]],[m,q]=n(x,l,f,p,i,r,[],[]);u.push(...m),s.push(...q)}else if(typeof e[f]=="object"){let x=C(e[f],t[p],i+1,r,n);u.push({level:i,type:"equal",text:`"${f}": {`}),u.push(...x[0]),u.push({level:i,type:"equal",text:"}"}),s.push({level:i,type:"equal",text:`"${p}": {`}),s.push(...x[1]),s.push({level:i,type:"equal",text:"}"})}else e[f]!==t[p]?r.showModifications?(u.push({level:i,type:"modify",text:`"${f}": ${y(e[f])}`}),s.push({level:i,type:"modify",text:`"${p}": ${y(t[p])}`})):(u.push({level:i,type:"remove",text:`"${f}": ${y(e[f])}`}),u.push({level:i,type:"equal",text:""}),s.push({level:i,type:"equal",text:""}),s.push({level:i,type:"add",text:`"${p}": ${y(t[p])}`})):(u.push({level:i,type:"equal",text:`"${f}": ${y(e[f])}`}),s.push({level:i,type:"equal",text:`"${p}": ${y(t[p])}`}));else f&&p?f<p?(u.push({level:i,type:"remove",text:`"${f}": ${y(e[f])}`}),s.push({level:i,type:"equal",text:""})):(u.push({level:i,type:"equal",text:""}),s.push({level:i,type:"add",text:`"${p}": ${y(t[p])}`})):f?(u.push({level:i,type:"remove",text:`"${f}": ${y(e[f])}`}),s.push({level:i,type:"equal",text:""})):p&&(u.push({level:i,type:"equal",text:""}),s.push({level:i,type:"add",text:`"${p}": ${y(t[p])}`}));f?p?f===p?(o.shift(),a.shift()):f<p?o.shift():a.shift():o.shift():a.shift()}if(u.length!==s.length)throw new Error("Diff error: length not match for left & right, please report a bug with your data.");return[u,s]},D=C;var H=(e,t,i,r,n,u)=>{let s=Array(e.length+1).fill(0).map(()=>Array(t.length+1).fill(0)),o=Array(e.length+1).fill(0).map(()=>Array(t.length+1).fill(0));for(let l=1;l<=e.length;l++)o[l][0]="up";for(let l=1;l<=t.length;l++)o[0][l]="left";for(let l=1;l<=e.length;l++)for(let m=1;m<=t.length;m++){let q=h(e[l-1]),V=h(t[m-1]);q===V&&(q==="array"||q==="object")||e[l-1]===t[m-1]?(s[l][m]=s[l-1][m-1]+1,o[l][m]="diag"):s[l-1][m]>=s[l][m-1]?(s[l][m]=s[l-1][m],o[l][m]="up"):(s[l][m]=s[l][m-1],o[l][m]="left")}let a=e.length,f=t.length,p=[],x=[];for(;a>0||f>0;)if(o[a][f]==="diag"){let l=h(e[a-1]);if(l==="array"){let[m,q]=A(e[a-1],t[f-1],i,r,n+2,u);p.unshift(...m),x.unshift(...q)}else if(l==="object"){let[m,q]=D(e[a-1],t[f-1],n+2,u,A);p.unshift({level:n+1,type:"equal",text:"}"}),x.unshift({level:n+1,type:"equal",text:"}"}),p.unshift(...m),x.unshift(...q),p.unshift({level:n+1,type:"equal",text:"{"}),x.unshift({level:n+1,type:"equal",text:"{"})}else p.unshift({level:n+1,type:"equal",text:y(e[a-1])}),x.unshift({level:n+1,type:"equal",text:y(t[f-1])});a--,f--}else o[a][f]==="up"?u.showModifications&&a>1&&o[a-1][f]==="left"?(p.unshift({level:n+1,type:"modify",text:y(e[a-1])}),x.unshift({level:n+1,type:"modify",text:y(t[f-1])}),a--,f--):(p.unshift({level:n+1,type:"remove",text:y(e[a-1])}),x.unshift({level:n+1,type:"equal",text:""}),a--):(p.unshift({level:n+1,type:"equal",text:""}),x.unshift({level:n+1,type:"add",text:y(t[f-1])}),f--);return[p,x]},A=(e,t,i,r,n,u,s=[],o=[])=>{if(i&&r?(s.push({level:n,type:"equal",text:`"${i}": [`}),o.push({level:n,type:"equal",text:`"${r}": [`})):(s.push({level:n,type:"equal",text:"["}),o.push({level:n,type:"equal",text:"["})),n>=u.maxDepth)s.push({level:n+1,type:"equal",text:"..."}),o.push({level:n+1,type:"equal",text:"..."});else{let[a,f]=H(e,t,i,r,n,u);s.push(...a),o.push(...f)}return s.push({level:n,type:"equal",text:"]"}),o.push({level:n,type:"equal",text:"]"}),[s,o]},S=A;var N=(e,t,i,r,n,u,s=[],o=[])=>{if(e=[...e],t=[...t],i&&r?(s.push({level:n,type:"equal",text:`"${i}": [`}),o.push({level:n,type:"equal",text:`"${r}": [`})):(s.push({level:n,type:"equal",text:"["}),o.push({level:n,type:"equal",text:"["})),n>=u.maxDepth)s.push({level:n+1,type:"equal",text:"..."}),o.push({level:n+1,type:"equal",text:"..."});else for(;e.length||t.length;){let a=e[0]??null,f=t[0]??null;if(a&&f){if(h(a)!==h(f))s.push({level:n+1,type:"remove",text:y(a)}),s.push({level:n,type:"equal",text:""}),o.push({level:n,type:"equal",text:""}),o.push({level:n+1,type:"add",text:y(f)});else if(h(a)==="object"){s.push({level:n+1,type:"equal",text:"{"}),o.push({level:n+1,type:"equal",text:"{"});let[p,x]=D(a,f,n+2,u,N);s.push(...p),o.push(...x),s.push({level:n+1,type:"equal",text:"}"}),o.push({level:n+1,type:"equal",text:"}"})}else if(h(a)==="array"){let[p,x]=N(a,f,"","",n+2,u,[],[]);s.push(...p),o.push(...x)}else a===f?(s.push({level:n+1,type:"equal",text:y(a)}),o.push({level:n+1,type:"equal",text:y(f)})):u.showModifications?(s.push({level:n+1,type:"modify",text:y(a)}),o.push({level:n+1,type:"modify",text:y(f)})):(s.push({level:n+1,type:"remove",text:y(a)}),s.push({level:n+1,type:"equal",text:""}),o.push({level:n+1,type:"equal",text:""}),o.push({level:n+1,type:"add",text:y(f)}));e.shift(),t.shift()}else a?(s.push({level:n+1,type:"remove",text:y(a)}),o.push({level:n+1,type:"equal",text:""}),e.shift()):f&&(s.push({level:n+1,type:"equal",text:""}),o.push({level:n+1,type:"add",text:y(f)}),t.shift())}return s.push({level:n,type:"equal",text:"]"}),o.push({level:n,type:"equal",text:"]"}),[s,o]},k=N;var w=e=>{if(!e||typeof e!="object")return e;if(Array.isArray(e)){let i=[...e];return i.sort((r,n)=>typeof r=="number"&&typeof n=="number"?r-n:String(r).localeCompare(String(n))),i.map(r=>w(r))}let t={...e};for(let i in t)t[i]=w(t[i]);return t},O=w;var F=class{options;arrayDiffFunc;constructor({detectCircular:t=!0,maxDepth:i=1/0,showModifications:r=!0,arrayDiffMethod:n="normal"}={}){this.options={detectCircular:t,maxDepth:i,showModifications:r,arrayDiffMethod:n},this.arrayDiffFunc=n==="lcs"||n==="unorder-lcs"?S:k}detectCircular(t){if(this.options.detectCircular&&j(t))throw new Error(`Circular reference detected in object (with keys ${Object.keys(t).map(i=>`"${i}"`).join(", ")})`)}sortResultLines(t,i){for(let r=0;r<t.length;r++){let n=!1;for(let u=1;u<t.length;u++)if(t[u].type==="remove"&&t[u-1].type==="equal"&&i[u].type==="equal"&&i[u-1].type==="add"){let s=t[u-1];t[u-1]=t[u],t[u]=s;let o=i[u-1];i[u-1]=i[u],i[u]=o,n=!0}if(!n)break}}calculateLineNumbers(t){let i=0;for(let r of t)!r.text||(r.lineNumber=++i)}calculateCommas(t){let i=Array(t.length).fill(0);for(let r=t.length-1;r>0;r--)t[r].text?i[r-1]=r:i[r-1]=i[r];for(let r=0;r<t.length;r++)!t[r].text.endsWith("{")&&!t[r].text.endsWith("[")&&t[r].text&&i[r]&&t[r].level<=t[i[r]].level&&(t[r].comma=!0)}diff(t,i){this.detectCircular(t),this.detectCircular(i),(this.options.arrayDiffMethod==="unorder-normal"||this.options.arrayDiffMethod==="unorder-lcs")&&(t=O(t),i=O(i));let r=[],n=[],u=h(t),s=h(i);if(u!==s){r=c(t,null,1,this.options.maxDepth).split(`
`).map(f=>({level:f.match(/^\s+/)?.[0]?.length||0,type:"remove",text:f.replace(/^\s+/,"").replace(/,$/g,""),comma:f.endsWith(",")})),n=c(i,null,1,this.options.maxDepth).split(`
`).map(f=>({level:f.match(/^\s+/)?.[0]?.length||0,type:"add",text:f.replace(/^\s+/,"").replace(/,$/g,""),comma:f.endsWith(",")}));let o=r.length,a=n.length;r.push(...Array(a).fill({level:0,type:"equal",text:""})),n.unshift(...Array(o).fill({level:0,type:"equal",text:""}))}else u==="object"?([r,n]=D(t,i,1,this.options,this.arrayDiffFunc),r.unshift({level:0,type:"equal",text:"{"}),r.push({level:0,type:"equal",text:"}"}),n.unshift({level:0,type:"equal",text:"{"}),n.push({level:0,type:"equal",text:"}"})):u==="array"?[r,n]=this.arrayDiffFunc(t,i,"","",0,this.options):t!==i?this.options.showModifications?(r=[{level:0,type:"modify",text:c(t,null,null,this.options.maxDepth)}],n=[{level:0,type:"modify",text:c(i,null,null,this.options.maxDepth)}]):(r=[{level:0,type:"remove",text:c(t,null,null,this.options.maxDepth)},{level:0,type:"equal",text:""}],n=[{level:0,type:"equal",text:""},{level:0,type:"add",text:c(i,null,null,this.options.maxDepth)}]):(r=[{level:0,type:"equal",text:c(t,null,null,this.options.maxDepth)}],n=[{level:0,type:"equal",text:c(i,null,null,this.options.maxDepth)}]);return this.sortResultLines(r,n),this.calculateLineNumbers(r),this.calculateLineNumbers(n),this.calculateCommas(r),this.calculateCommas(n),[r,n]}},K=F;var d=_(E("react")),M=e=>{let[t,i]=e.diff,r=`${String(t.length).length/2}em`,n=e.indent??2,u=n==="tab"?" ":" ",s=n==="tab"?1:n,o=a=>{let f=t[a],p=i[a];return d.createElement("tr",{key:a},e.lineNumbers&&d.createElement("td",{className:`line-${f.type} line-number`,style:{width:r}},f.lineNumber),d.createElement("td",{className:`line-${f.type}`},d.createElement("pre",null,f.text&&u.repeat(f.level*s),f.text,f.comma&&",")),e.lineNumbers&&d.createElement("td",{className:`line-${p.type} line-number`,style:{width:r}},p.lineNumber),d.createElement("td",{className:`line-${p.type}`},d.createElement("pre",null,p.text&&u.repeat(p.level*s),p.text,p.comma&&",")))};return d.createElement("table",{className:`json-diff-viewer ${e.className||""}`,style:e.style},d.createElement("tbody",null,t.map((a,f)=>o(f))))};M.displayName="Viewer";var Q=M;})();
{
"name": "json-diff-kit",
"version": "0.0.3",
"version": "1.0.0",
"description": "A better JSON differ & viewer.",

@@ -50,3 +50,3 @@ "typings": "typings",

},
"readme": "# JSON Diff Kit\n\n[![NPM version][npm-image]][npm-url]\n[![Downloads][download-badge]][npm-url]\n\n> A better JSON differ & viewer.\n\n## Install\n\nYou can install `json-diff-kit` via various package managers.\n\n```sh\n# using npm\nnpm i json-diff-kit --save\n\n# using yarn\nyarn add json-diff-kit\n\n# using pnpm\npnpm add json-diff-kit\n```\n\n## Quick Start\n\nTo generate the diff data:\n\n```ts\nimport { Differ } from 'json-diff-kit';\n\nconst before = {\n a: 1,\n b: 2,\n d: [1, 5, 4],\n e: ['1', 2, { f: 3, g: null, h: [5], i: [] }, 9],\n};\nconst after = {\n b: 2,\n c: 3,\n d: [1, 3, 4, 6],\n e: ['1', 2, 3, { f: 4, g: false, i: [7, 8] }, 10],\n};\n\n// all configs are optional\nconst differ = new Differ({\n detectCircular: true,\n maxDepth: Infinity,\n showModifications: true,\n arrayDiffMethod: 'lcs',\n});\n\nconst diff = differ.diff(before, after);\nconsole.log(diff);\n```\n\nYou can use your own component to visualize the `diff` data, or use the built-in viewer:\n\n```tsx\nimport { Viewer } from 'json-diff-kit';\nimport type { DiffResult } from 'json-diff-kit';\n\ninterface PageProps {\n diff: [DiffResult[], DiffResult[]];\n}\n\nconst Page: React.FC<PageProps> = props => {\n return (\n <Viewer\n diff={props.diff}\n indent={4}\n lineNumbers={true}\n />\n );\n};\n```\n\nThe result is here:\n\n![The result (using LCS array diff method).](./preview.png)\n\n## More Complex Usages\n\nPlease check the [GitHub pages](https://rexskz.github.io/json-diff-kit/). The code is at [here](./demo/index.tsx).\n\n## License\n\nMIT\n\n[npm-url]: https://npmjs.org/package/json-diff-kit\n[npm-image]: https://img.shields.io/npm/v/json-diff-kit.svg?style=flat-square\n\n[download-badge]: http://img.shields.io/npm/dm/json-diff-kit.svg?style=flat-square\n"
"readme": "# JSON Diff Kit\n\n[![NPM version][npm-image]][npm-url]\n[![Downloads][download-badge]][npm-url]\n\nA better JSON differ & viewer.\n\n> Notice: considering most of the browsers now support ES6, this library has only the ES6 version. If you need the compatibility with older browsers, please configure the compiler in your project (e.g. add this library to the `include` field in `babel.config.js`).\n\n## Install\n\nYou can install `json-diff-kit` via various package managers.\n\n```sh\n# using npm\nnpm i json-diff-kit --save\n\n# using yarn\nyarn add json-diff-kit\n\n# using pnpm\npnpm add json-diff-kit\n```\n\n## Quick Start\n\nTo generate the diff data:\n\n```ts\nimport { Differ } from 'json-diff-kit';\n\nconst before = {\n a: 1,\n b: 2,\n d: [1, 5, 4],\n e: ['1', 2, { f: 3, g: null, h: [5], i: [] }, 9],\n};\nconst after = {\n b: 2,\n c: 3,\n d: [1, 3, 4, 6],\n e: ['1', 2, 3, { f: 4, g: false, i: [7, 8] }, 10],\n};\n\n// all configs are optional\nconst differ = new Differ({\n detectCircular: true, // default `true`\n maxDepth: Infinity, // default `Infinity`\n showModifications: true, // default `true`\n arrayDiffMethod: 'lcs', // default `\"normal\"`\n});\n\nconst diff = differ.diff(before, after);\nconsole.log(diff);\n```\n\nYou can use your own component to visualize the `diff` data, or use the built-in viewer:\n\n```tsx\nimport { Viewer } from 'json-diff-kit';\nimport type { DiffResult } from 'json-diff-kit';\n\nimport 'json-diff-kit/dist/viewer.css';\n\ninterface PageProps {\n diff: [DiffResult[], DiffResult[]];\n}\n\nconst Page: React.FC<PageProps> = props => {\n return (\n <Viewer\n diff={props.diff} // required\n indent={4} // default `2`\n lineNumbers={true} // default `false`\n />\n );\n};\n```\n\nThe result is here:\n\n![The result (using LCS array diff method).](./preview.png)\n\n## More Complex Usages\n\nPlease check the [demo pages](https://json-diff-kit.js.org/).\n\n## License\n\nMIT\n\n[npm-url]: https://npmjs.org/package/json-diff-kit\n[npm-image]: https://img.shields.io/npm/v/json-diff-kit.svg?style=flat-square\n\n[download-badge]: https://img.shields.io/npm/dm/json-diff-kit.svg?style=flat-square\n"
}

@@ -6,4 +6,6 @@ # JSON Diff Kit

> A better JSON differ & viewer.
A better JSON differ & viewer.
> Notice: considering most of the browsers now support ES6, this library has only the ES6 version. If you need the compatibility with older browsers, please configure the compiler in your project (e.g. add this library to the `include` field in `babel.config.js`).
## Install

@@ -46,6 +48,6 @@

const differ = new Differ({
detectCircular: true,
maxDepth: Infinity,
showModifications: true,
arrayDiffMethod: 'lcs',
detectCircular: true, // default `true`
maxDepth: Infinity, // default `Infinity`
showModifications: true, // default `true`
arrayDiffMethod: 'lcs', // default `"normal"`
});

@@ -63,2 +65,4 @@

import 'json-diff-kit/dist/viewer.css';
interface PageProps {

@@ -71,5 +75,5 @@ diff: [DiffResult[], DiffResult[]];

<Viewer
diff={props.diff}
indent={4}
lineNumbers={true}
diff={props.diff} // required
indent={4} // default `2`
lineNumbers={true} // default `false`
/>

@@ -86,3 +90,3 @@ );

Please check the [GitHub pages](https://rexskz.github.io/json-diff-kit/). The code is at [here](./demo/index.tsx).
Please check the [demo pages](https://json-diff-kit.js.org/).

@@ -96,2 +100,2 @@ ## License

[download-badge]: http://img.shields.io/npm/dm/json-diff-kit.svg?style=flat-square
[download-badge]: https://img.shields.io/npm/dm/json-diff-kit.svg?style=flat-square

@@ -0,0 +0,0 @@ export interface DifferOptions {

import Differ from './differ';
import Viewer from './viewer';
export type { ArrayDiffFunc, DifferOptions, DiffResult, } from './differ';
export { Differ, Viewer };
declare const detectCircular: (value: any, map?: Map<any, boolean>) => boolean;
export default detectCircular;
import type { DifferOptions, DiffResult } from '../differ';
declare const diffArrayLCS: (arrLeft: any[], arrRight: any[], keyLeft: string, keyRight: string, level: number, options: DifferOptions, linesLeft?: DiffResult[], linesRight?: DiffResult[]) => [DiffResult[], DiffResult[]];
export default diffArrayLCS;
import type { DiffResult, DifferOptions } from '../differ';
declare const diffArrayNormal: (arrLeft: any[], arrRight: any[], keyLeft: string, keyRight: string, level: number, options: DifferOptions, linesLeft?: DiffResult[], linesRight?: DiffResult[]) => [DiffResult[], DiffResult[]];
export default diffArrayNormal;
import type { DifferOptions, DiffResult, ArrayDiffFunc } from '../differ';
declare const diffObject: (lhs: Record<string, any>, rhs: Record<string, any>, level: number, options: DifferOptions, arrayDiffFunc: ArrayDiffFunc) => [DiffResult[], DiffResult[]];
export default diffObject;
declare const formatValue: (value: any, depth?: number) => string;
export default formatValue;
declare const getType: (value: any) => "string" | "number" | "bigint" | "boolean" | "symbol" | "undefined" | "object" | "function" | "array" | "null";
export default getType;
declare const sortInnerArrays: (source: any) => any;
export default sortInnerArrays;
declare const stringify: (obj: any, replacer?: (this: any, key: string, value: any) => any, space?: string | number, depth?: number) => string;
export default stringify;

@@ -0,0 +0,0 @@ import * as React from 'react';

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc