Skip to content

Commit

Permalink
fix: root path was not collected (#47)
Browse files Browse the repository at this point in the history
* fix: root path was not collected

* feat: update build files
  • Loading branch information
lukascivil authored Jul 25, 2023
1 parent 2246be1 commit cd0d1e1
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 34 deletions.
51 changes: 26 additions & 25 deletions dist.browser/json-difference.mjs
Original file line number Diff line number Diff line change
@@ -1,47 +1,48 @@
const p = (f, i) => {
const O = (f, n) => {
const o = [];
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]) || f[e] === i[e])
if (n.hasOwnProperty(e)) {
if (typeof f[e] == "object" && typeof n[e] == "object" && JSON.stringify(f[e]) === JSON.stringify(n[e]) || f[e] === n[e])
continue;
if (f[e] === "@{}" || f[e] === "@[]") {
const n = i[e] === "@{}" ? {} : i[e] === "@[]" ? [] : i[e];
f[e] === "@{}" ? JSON.stringify(i[e]) !== "{}" && o.push([e, {}, n]) : JSON.stringify(i[e]) !== "[]" && o.push([e, [], n]);
const i = n[e] === "@{}" ? {} : n[e] === "@[]" ? [] : n[e];
f[e] === "@{}" ? JSON.stringify(n[e]) !== "{}" && o.push([e, {}, i]) : JSON.stringify(n[e]) !== "[]" && o.push([e, [], i]);
} else
o.push([e, f[e], i[e]]);
o.push([e, f[e], n[e]]);
}
return o;
}, d = (f, i) => {
}, g = (f, n) => {
const o = [];
let e = 0;
for (const n in f)
if (!(n in i)) {
const y = f[n] === "@{}" ? {} : f[n] === "@[]" ? [] : f[n];
o[e] = [n, y], e++;
for (const i in f)
if (!(i in n)) {
const y = f[i] === "@{}" ? {} : f[i] === "@[]" ? [] : f[i];
o[e] = [i, y], e++;
}
return o;
}, O = (f, i, o, e) => {
const n = e ? f ? "[" : "." : "/", y = e ? f ? "]" : "" : f ? "[]" : "";
return i !== "" ? `${i}${n}${o}${y}` : `${e && f ? "[" : ""}${o}${y}`;
}, g = (f, i = !1, o = {}, e = "") => {
for (const n of Object.keys(f)) {
const y = O(Array.isArray(f), e, n, i);
typeof f[n] == "object" && f[n] !== null ? (Object.keys(f[n]).length === 0 ? o[y] = f[n] : o[y] = Array.isArray(f[n]) ? "@[]" : "@{}", g(f[n], i, o, y)) : o[y] = f[n];
}, p = (f, n, o, e) => {
const i = e ? f ? "[" : "." : "/", y = e ? f ? "]" : "" : f ? "[]" : "";
return n !== "" ? `${n}${i}${o}${y}` : `${e && f ? "[" : ""}${o}${y}`;
}, d = (f, n = !1, o, e = "") => {
o === void 0 && (o = Array.isArray(f) ? { "": "@[]" } : { "": "@{}" });
for (const i of Object.keys(f)) {
const y = p(Array.isArray(f), e, i, n);
typeof f[i] == "object" && f[i] !== null ? (Object.keys(f[i]).length === 0 ? o[y] = f[i] : o[y] = Array.isArray(f[i]) ? "@[]" : "@{}", d(f[i], n, o, y)) : o[y] = f[i];
}
return o;
}, $ = {
isLodashLike: !1
}, b = (f, i, o) => {
const { isLodashLike: e } = o ?? $, n = {
}, b = (f, n, o) => {
const { isLodashLike: e } = o ?? $, i = {
added: [],
removed: [],
edited: []
}, y = g(f, e), s = g(i, e);
return n.removed = d(y, s), n.added = d(s, y), n.edited = p(y, s), n;
}, y = d(f, e), s = d(n, e);
return i.removed = g(y, s), i.added = g(s, y), i.edited = O(y, s), i;
};
export {
b as getDiff,
p as getEditedPaths,
d as getPathsDiff,
g as getStructPaths
O as getEditedPaths,
g as getPathsDiff,
d as getStructPaths
};
2 changes: 1 addition & 1 deletion dist.browser/json-difference.umd.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 13 additions & 0 deletions src/core/get-diff.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,19 @@ describe('GetDiff function', () => {
expect(lodashResult).toEqual(expectedLodashResult)
})

test('Should return the difference between [] and {}', () => {
const struct1 = [] as any
const struct2 = {}
const expectedResult: Delta = { edited: [['', [], {}]], added: [], removed: [] }
const expectedLodashResult: Delta = { edited: [['', [], {}]], added: [], removed: [] }

const result = getDiff(struct1, struct2)
const lodashResult = getDiff(struct1, struct2, { isLodashLike: true })

expect(result).toEqual(expectedResult)
expect(lodashResult).toEqual(expectedLodashResult)
})

test('Should return the difference between two structures containing Array property', () => {
const struct1 = { a: 1, b: [{ c1: 1 }, { c2: 2 }] }
const struct2 = { a: 11, b: [{ c1: 1 }, { c2: 22 }] }
Expand Down
38 changes: 31 additions & 7 deletions src/core/get-struct-paths.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,25 @@ import { getStructPaths } from '.'
describe('GetStructPaths function', () => {
test('Should return all paths from a basic structure', () => {
const oldStruct = { 1: { 2: 7, 3: { 4: 6 } } }
const expectedResult = { '1': '@{}', '1/2': 7, '1/3': '@{}', '1/3/4': 6 }
const expectedResult = { '': '@{}', '1': '@{}', '1/2': 7, '1/3': '@{}', '1/3/4': 6 }

const result = getStructPaths(oldStruct)

expect(result).toEqual(expectedResult)
})

test('Should return path for root []', () => {
const oldStruct = [] as any
const expectedResult = { '': '@[]' }

const result = getStructPaths(oldStruct)

expect(result).toEqual(expectedResult)
})

test('Should return path for root {}', () => {
const oldStruct = {} as any
const expectedResult = { '': '@{}' }

const result = getStructPaths(oldStruct)

Expand All @@ -13,6 +31,7 @@ describe('GetStructPaths function', () => {
test('Should return all paths containing Array property', () => {
const oldStruct = { a: 1, b: [{ c1: [{ c3: { c5: [1, 2, { c6: 3 }] } }, { c4: 6 }] }, { c2: 2 }] }
const expectedResult = {
'': '@{}',
a: 1,
b: '@[]',
'b/0[]': '@{}',
Expand All @@ -30,6 +49,7 @@ describe('GetStructPaths function', () => {
'b/1[]/c2': 2
}
const expectedLodashLikeResult = {
'': '@{}',
a: 1,
b: '@[]',
'b[0]': '@{}',
Expand All @@ -56,8 +76,8 @@ describe('GetStructPaths function', () => {

test('Should return paths when the values are objects', () => {
const oldStruct = { a: [], b: {} }
const expectedResult = { a: [], b: {} }
const expectedLodashLikeResult = { a: [], b: {} }
const expectedResult = { '': '@{}', a: [], b: {} }
const expectedLodashLikeResult = { '': '@{}', a: [], b: {} }

const result = getStructPaths(oldStruct)
const lodashLikeResult = getStructPaths(oldStruct, true)
Expand All @@ -69,10 +89,10 @@ describe('GetStructPaths function', () => {
test('Should return different paths when containing object and array with same key value at root', () => {
const oldStruct = { '0': 0 }
const newStruct = [0]
const expectedOldStructPaths = { '0': 0 }
const expectedOldStructLodashLikePaths = { '0': 0 }
const expectedNewStructPaths = { '0[]': 0 }
const expectedNewStructLodashLikePaths = { '[0]': 0 }
const expectedOldStructPaths = { '': '@{}', '0': 0 }
const expectedOldStructLodashLikePaths = { '': '@{}', '0': 0 }
const expectedNewStructPaths = { '': '@[]', '0[]': 0 }
const expectedNewStructLodashLikePaths = { '': '@[]', '[0]': 0 }

const oldStructPaths = getStructPaths(oldStruct)
const newStructPaths = getStructPaths(newStruct)
Expand All @@ -89,21 +109,25 @@ describe('GetStructPaths function', () => {
const oldStruct = { '0': [{ '0': 1 }] }
const newStruct = { '0': { '0': [1] } }
const expectedOldStructPaths = {
'': '@{}',
'0': '@[]',
'0/0[]': '@{}',
'0/0[]/0': 1
}
const expectedOldStructLodashLikePaths = {
'': '@{}',
'0': '@[]',
'0[0]': '@{}',
'0[0].0': 1
}
const expectedNewStructPaths = {
'': '@{}',
'0': '@{}',
'0/0': '@[]',
'0/0/0[]': 1
}
const expectedNewStructLodashLikePaths = {
'': '@{}',
'0': '@{}',
'0.0': '@[]',
'0.0[0]': 1
Expand Down
6 changes: 5 additions & 1 deletion src/core/get-struct-paths.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,11 @@ const generatePath = (isArray: boolean, currentPath: string, newPath: string, lo
* console.log(result)
* // Output: {"1": "@{}","1.2": null}
*/
export const getStructPaths = (struct: any, isLodashLike = false, paths: { [key: string]: any } = {}, currentPath = ''): StructPaths => {
export const getStructPaths = (struct: any, isLodashLike = false, paths?: { [key: string]: any }, currentPath = ''): StructPaths => {
if (paths === undefined) {
paths = Array.isArray(struct) ? { '': '@[]' } : { '': '@{}' }
}

for (const key of Object.keys(struct)) {
const path = generatePath(Array.isArray(struct), currentPath, key, isLodashLike)

Expand Down

0 comments on commit cd0d1e1

Please sign in to comment.