json-difference
Advanced tools
Comparing version 1.6.1 to 1.8.0
@@ -1,1 +0,1 @@ | ||
(function(t,d){typeof exports=="object"&&typeof module<"u"?d(exports):typeof define=="function"&&define.amd?define(["exports"],d):(t=typeof globalThis<"u"?globalThis:t||self,d(t["json-difference"]={}))})(this,function(t){"use strict";const d=(f,i)=>{const n=[];for(const e in f)if(i.hasOwnProperty(e)){if(typeof f[e]=="object"&&typeof i[e]=="object"&&JSON.stringify(f[e])===JSON.stringify(i[e]))continue;f[e]!==i[e]&&n.push([e,f[e],i[e]])}return n},y=(f,i)=>{const n=[];let e=0;for(const o in f)o in i||(n[e]=[o,f[o]],e++);return n},s=(f,i={},n="")=>{for(const e of Object.keys(f)){const o=n!==""?`${n}/${e}`:e;typeof f[e]=="object"?(Object.keys(f[e]).length===0&&(i[o]=f[e]),s(f[e],i,o)):i[o]=f[e]}return i},c=(f,i)=>{const n={added:[],removed:[],edited:[]},e=s(f),o=s(i);return n.removed=y(e,o),n.added=y(o,e),n.edited=d(e,o),n};t.getDiff=c,t.getEditedPaths=d,t.getPathsDiff=y,t.getStructPaths=s,Object.defineProperties(t,{__esModule:{value:!0},[Symbol.toStringTag]:{value:"Module"}})}); | ||
(function(d,s){typeof exports=="object"&&typeof module<"u"?s(exports):typeof define=="function"&&define.amd?define(["exports"],s):(d=typeof globalThis<"u"?globalThis:d||self,s(d["json-difference"]={}))})(this,function(d){"use strict";const s=(f,i)=>{const n=[];for(const e in f)if(i.hasOwnProperty(e)){if(typeof f[e]=="object"&&typeof i[e]=="object"&&JSON.stringify(f[e])===JSON.stringify(i[e]))continue;f[e]!==i[e]&&n.push([e,f[e],i[e]])}return n},g=(f,i)=>{const n=[];let e=0;for(const t in f)t in i||(n[e]=[t,f[t]],e++);return n},y=(f,i,n,e)=>{const t=e?f?"[":".":"/",o=e?f?"]":"":f?"[]":"";return i!==""?`${i}${t}${n}${o}`:`${e&&f?"[":""}${n}${o}`},c=(f,i=!1,n={},e="")=>{for(const t of Object.keys(f)){const o=y(Array.isArray(f),e,t,i);typeof f[t]=="object"?(Object.keys(f[t]).length===0&&(n[o]=f[t]),c(f[t],i,n,o)):n[o]=f[t]}return n},p=(f,i,n=!1)=>{const e={added:[],removed:[],edited:[]},t=c(f,n),o=c(i,n);return e.removed=g(t,o),e.added=g(o,t),e.edited=s(t,o),e};d.getDiff=p,d.getEditedPaths=s,d.getPathsDiff=g,d.getStructPaths=c,Object.defineProperties(d,{__esModule:{value:!0},[Symbol.toStringTag]:{value:"Module"}})}); |
import { Delta } from '../models/jsondiffer.model'; | ||
export declare const getDiff: (oldStruct: Record<string, any>, newStruct: Record<string, any>) => Delta; | ||
export declare const getDiff: (oldStruct: Record<string, any>, newStruct: Record<string, any>, isLodashLike?: boolean) => Delta; |
@@ -8,3 +8,4 @@ "use strict"; | ||
var get_struct_paths_1 = require("./get-struct-paths"); | ||
var getDiff = function (oldStruct, newStruct) { | ||
var getDiff = function (oldStruct, newStruct, isLodashLike) { | ||
if (isLodashLike === void 0) { isLodashLike = false; } | ||
var delta = { | ||
@@ -15,4 +16,4 @@ added: [], | ||
}; | ||
var oldStructPaths = (0, get_struct_paths_1.getStructPaths)(oldStruct); | ||
var newStructPaths = (0, get_struct_paths_1.getStructPaths)(newStruct); | ||
var oldStructPaths = (0, get_struct_paths_1.getStructPaths)(oldStruct, isLodashLike); | ||
var newStructPaths = (0, get_struct_paths_1.getStructPaths)(newStruct, isLodashLike); | ||
// A-B | ||
@@ -19,0 +20,0 @@ delta.removed = (0, get_paths_diff_1.getPathsDiff)(oldStructPaths, newStructPaths); |
@@ -10,4 +10,7 @@ "use strict"; | ||
var expectedResult = { edited: [['1/3/4', 6, 5]], added: [], removed: [['1/2', 7]] }; | ||
var expectedLodashResult = { edited: [['1.3.4', 6, 5]], added: [], removed: [['1.2', 7]] }; | ||
var result = (0, _1.getDiff)(struct1, struct2); | ||
var lodashResult = (0, _1.getDiff)(struct1, struct2, true); | ||
expect(result).toEqual(expectedResult); | ||
expect(lodashResult).toEqual(expectedLodashResult); | ||
}); | ||
@@ -20,3 +23,3 @@ test('Should return the difference between two structures containing Array property', function () { | ||
['a', 1, 11], | ||
['b/1/c2', 2, 22] | ||
['b/1[]/c2', 2, 22] | ||
], | ||
@@ -26,4 +29,14 @@ added: [], | ||
}; | ||
var expectedLodashResult = { | ||
edited: [ | ||
['a', 1, 11], | ||
['b[1].c2', 2, 22] | ||
], | ||
added: [], | ||
removed: [] | ||
}; | ||
var result = (0, _1.getDiff)(struct1, struct2); | ||
var lodashResult = (0, _1.getDiff)(struct1, struct2, true); | ||
expect(result).toEqual(expectedResult); | ||
expect(lodashResult).toEqual(expectedLodashResult); | ||
}); | ||
@@ -36,3 +49,3 @@ test('Should return the difference between two structures containing deep Array nodes', function () { | ||
['a', 1, 11], | ||
['b/0/c1/0/c3/c5/2/c6', 3, 4] | ||
['b/0[]/c1/0[]/c3/c5/2[]/c6', 3, 4] | ||
], | ||
@@ -42,4 +55,14 @@ added: [], | ||
}; | ||
var expectedLodashResult = { | ||
edited: [ | ||
['a', 1, 11], | ||
['b[0].c1[0].c3.c5[2].c6', 3, 4] | ||
], | ||
added: [], | ||
removed: [] | ||
}; | ||
var result = (0, _1.getDiff)(struct1, struct2); | ||
var lodashResult = (0, _1.getDiff)(struct1, struct2, true); | ||
expect(result).toEqual(expectedResult); | ||
expect(lodashResult).toEqual(expectedLodashResult); | ||
}); | ||
@@ -57,4 +80,14 @@ test('Should return the difference between two structures containing different node types', function () { | ||
}; | ||
var expectedLodashResult = { | ||
edited: [ | ||
['a', 1, '1'], | ||
['c', 3, true] | ||
], | ||
added: [['b', 2]], | ||
removed: [['b.c1', 2]] | ||
}; | ||
var result = (0, _1.getDiff)(struct1, struct2); | ||
var lodashResult = (0, _1.getDiff)(struct1, struct2, true); | ||
expect(result).toEqual(expectedResult); | ||
expect(expectedLodashResult).toEqual(lodashResult); | ||
}); | ||
@@ -70,3 +103,5 @@ test('Should return no diff when the structs has nested equal structures', function () { | ||
var result = (0, _1.getDiff)(oldStruct, newStruct); | ||
var lodashResult = (0, _1.getDiff)(oldStruct, newStruct, true); | ||
expect(result).toEqual(expectedResult); | ||
expect(lodashResult).toEqual(expectedResult); | ||
}); | ||
@@ -87,4 +122,24 @@ test('Should compute the difference between structures with different object values', function () { | ||
var result = (0, _1.getDiff)(oldStruct, newStruct); | ||
var lodashResult = (0, _1.getDiff)(oldStruct, newStruct, true); | ||
expect(result).toEqual(expectedResult); | ||
expect(lodashResult).toEqual(expectedResult); | ||
}); | ||
test('Should return the difference between two structures containing array and object with same key value', function () { | ||
var struct1 = { '0': [{ '0': 1 }] }; | ||
var struct2 = { '0': { '0': [1] } }; | ||
var expectedResult = { | ||
edited: [], | ||
added: [['0/0/0[]', 1]], | ||
removed: [['0/0[]/0', 1]] | ||
}; | ||
var expectedLodashResult = { | ||
edited: [], | ||
added: [['0.0[0]', 1]], | ||
removed: [['0[0].0', 1]] | ||
}; | ||
var result = (0, _1.getDiff)(struct1, struct2); | ||
var lodashResult = (0, _1.getDiff)(struct1, struct2, true); | ||
expect(result).toEqual(expectedResult); | ||
expect(lodashResult).toEqual(expectedLodashResult); | ||
}); | ||
}); |
import { StructPaths } from '../models/jsondiffer.model'; | ||
export declare const getStructPaths: (struct: any, paths?: { | ||
export declare const getStructPaths: (struct: any, isLodashLike?: boolean, paths?: { | ||
[key: string]: any; | ||
}, currentPath?: string) => StructPaths; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.getStructPaths = void 0; | ||
var getStructPaths = function (struct, paths, currentPath) { | ||
var generatePath = function (isArray, currentPath, newPath, lodashLike) { | ||
var prefix = lodashLike ? (isArray ? '[' : '.') : '/'; | ||
var suffix = lodashLike ? (isArray ? ']' : '') : isArray ? '[]' : ''; | ||
var path = currentPath !== '' ? "" + currentPath + prefix + newPath + suffix : "" + (lodashLike && isArray ? '[' : '') + newPath + suffix; | ||
return path; | ||
}; | ||
var getStructPaths = function (struct, isLodashLike, paths, currentPath) { | ||
if (isLodashLike === void 0) { isLodashLike = false; } | ||
if (paths === void 0) { paths = {}; } | ||
@@ -9,3 +16,3 @@ if (currentPath === void 0) { currentPath = ''; } | ||
var key = _a[_i]; | ||
var path = currentPath !== '' ? currentPath + "/" + key : key; | ||
var path = generatePath(Array.isArray(struct), currentPath, key, isLodashLike); | ||
if (typeof struct[key] === 'object') { | ||
@@ -15,3 +22,3 @@ if (Object.keys(struct[key]).length === 0) { | ||
} | ||
(0, exports.getStructPaths)(struct[key], paths, path); | ||
(0, exports.getStructPaths)(struct[key], isLodashLike, paths, path); | ||
} | ||
@@ -18,0 +25,0 @@ else { |
@@ -13,5 +13,22 @@ "use strict"; | ||
var oldStruct = { a: 1, b: [{ c1: [{ c3: { c5: [1, 2, { c6: 3 }] } }, { c4: 6 }] }, { c2: 2 }] }; | ||
var expectedResult = { a: 1, 'b/0/c1/0/c3/c5/0': 1, 'b/0/c1/0/c3/c5/1': 2, 'b/0/c1/0/c3/c5/2/c6': 3, 'b/0/c1/1/c4': 6, 'b/1/c2': 2 }; | ||
var expectedResult = { | ||
a: 1, | ||
'b/0[]/c1/0[]/c3/c5/0[]': 1, | ||
'b/0[]/c1/0[]/c3/c5/1[]': 2, | ||
'b/0[]/c1/0[]/c3/c5/2[]/c6': 3, | ||
'b/0[]/c1/1[]/c4': 6, | ||
'b/1[]/c2': 2 | ||
}; | ||
var expectedLodashLikeResult = { | ||
a: 1, | ||
'b[0].c1[0].c3.c5[0]': 1, | ||
'b[0].c1[0].c3.c5[1]': 2, | ||
'b[0].c1[0].c3.c5[2].c6': 3, | ||
'b[0].c1[1].c4': 6, | ||
'b[1].c2': 2 | ||
}; | ||
var result = (0, _1.getStructPaths)(oldStruct); | ||
var lodashLikeResult = (0, _1.getStructPaths)(oldStruct, true); | ||
expect(result).toEqual(expectedResult); | ||
expect(lodashLikeResult).toEqual(expectedLodashLikeResult); | ||
}); | ||
@@ -21,5 +38,40 @@ test('Should return paths when the values are objects', function () { | ||
var expectedResult = { a: [], b: {} }; | ||
var expectedLodashLikeResult = { a: [], b: {} }; | ||
var result = (0, _1.getStructPaths)(oldStruct); | ||
var lodashLikeResult = (0, _1.getStructPaths)(oldStruct, true); | ||
expect(result).toEqual(expectedResult); | ||
expect(lodashLikeResult).toEqual(expectedLodashLikeResult); | ||
}); | ||
test('Should return different paths when containing object and array with same key value at root', function () { | ||
var oldStruct = { '0': 0 }; | ||
var newStruct = [0]; | ||
var expectedOldStructPaths = { '0': 0 }; | ||
var expectedOldStructLodashLikePaths = { '0': 0 }; | ||
var expectedNewStructPaths = { '0[]': 0 }; | ||
var expectedNewStructLodashLikePaths = { '[0]': 0 }; | ||
var oldStructPaths = (0, _1.getStructPaths)(oldStruct); | ||
var newStructPaths = (0, _1.getStructPaths)(newStruct); | ||
var oldStructLodashLikePaths = (0, _1.getStructPaths)(oldStruct, true); | ||
var newStructLodashLikePaths = (0, _1.getStructPaths)(newStruct, true); | ||
expect(oldStructPaths).toEqual(expectedOldStructPaths); | ||
expect(newStructPaths).toEqual(expectedNewStructPaths); | ||
expect(oldStructLodashLikePaths).toEqual(expectedOldStructLodashLikePaths); | ||
expect(newStructLodashLikePaths).toEqual(expectedNewStructLodashLikePaths); | ||
}); | ||
test('Should return different paths when containing object and array with same key value', function () { | ||
var oldStruct = { '0': [{ '0': 1 }] }; | ||
var newStruct = { '0': { '0': [1] } }; | ||
var expectedOldStructPaths = { '0/0[]/0': 1 }; | ||
var expectedOldStructLodashLikePaths = { '0[0].0': 1 }; | ||
var expectedNewStructPaths = { '0/0/0[]': 1 }; | ||
var expectedNewStructLodashLikePaths = { '0.0[0]': 1 }; | ||
var oldStructPaths = (0, _1.getStructPaths)(oldStruct); | ||
var newStructPaths = (0, _1.getStructPaths)(newStruct); | ||
var oldStructLodashLikePaths = (0, _1.getStructPaths)(oldStruct, true); | ||
var newStructLodashLikePaths = (0, _1.getStructPaths)(newStruct, true); | ||
expect(oldStructPaths).toEqual(expectedOldStructPaths); | ||
expect(newStructPaths).toEqual(expectedNewStructPaths); | ||
expect(oldStructLodashLikePaths).toEqual(expectedOldStructLodashLikePaths); | ||
expect(newStructLodashLikePaths).toEqual(expectedNewStructLodashLikePaths); | ||
}); | ||
}); |
{ | ||
"name": "json-difference", | ||
"version": "1.6.1", | ||
"version": "1.8.0", | ||
"description": "json diff lib", | ||
@@ -5,0 +5,0 @@ "license": "MIT", |
@@ -53,7 +53,10 @@ # JsonDifference | ||
const coffee = { color: { color1: 'black', color2: 'brown' }, special: true } | ||
const oil = { color: { color1: 'red', color2: 'blue' }, special2: false } | ||
const oil = { color: { color1: 'red', color2: 'blue' }, special2: false, especial3: [{}] } | ||
// Get JsonDiff delta | ||
let diff = getDiff(coffee, oil) | ||
let diff2 = getDiff(coffee, oil, true) | ||
console.log(diff) | ||
console.log(diff2) | ||
``` | ||
@@ -65,3 +68,6 @@ | ||
{ | ||
"added": [["special2", false]], | ||
"added": [ | ||
["special2", false], | ||
["especial3/0[]", {}] | ||
], | ||
"removed": [["special", true]], | ||
@@ -74,1 +80,15 @@ "edited": [ | ||
``` | ||
```json | ||
{ | ||
"added": [ | ||
["special2", false], | ||
["especial3[0]", {}] | ||
], | ||
"removed": [["special", true]], | ||
"edited": [ | ||
["color.color1", "black", "red"], | ||
["color.color2", "brown", "blue"] | ||
] | ||
} | ||
``` |
Sorry, the diff of this file is not supported yet
25513
453
92