Skip to content

Commit

Permalink
feat: change delta interface
Browse files Browse the repository at this point in the history
  • Loading branch information
lukascivil committed Aug 16, 2022
1 parent c7e3b5c commit 99f829c
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 45 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ Output:

```json
{
"new": {
"added": {
"special2": false
},
"removed": {
Expand Down
42 changes: 21 additions & 21 deletions dist.browser/json-difference.mjs
Original file line number Diff line number Diff line change
@@ -1,40 +1,40 @@
const d = (n, o) => {
const t = (o, n) => {
const i = [];
let e = {};
for (const f in n)
if (o.hasOwnProperty(f)) {
if (typeof n[f] == "object" && typeof o[f] == "object" && JSON.stringify(n[f]) === JSON.stringify(o[f]))
for (const f in o)
if (n.hasOwnProperty(f)) {
if (typeof o[f] == "object" && typeof n[f] == "object" && JSON.stringify(o[f]) === JSON.stringify(n[f]))
continue;
n[f] !== o[f] && (e = {
o[f] !== n[f] && (e = {
[f]: {
oldValue: n[f],
newValue: o[f]
oldValue: o[f],
newValue: n[f]
}
}, i.push(e));
}
return i;
}, t = (n, o) => {
}, y = (o, n) => {
const i = {};
for (const e in n)
e in o || (i[e] = n[e]);
for (const e in o)
e in n || (i[e] = o[e]);
return i;
}, y = (n, o = {}, i = "") => {
for (const e of Object.keys(n)) {
}, d = (o, n = {}, i = "") => {
for (const e of Object.keys(o)) {
const f = i !== "" ? `${i}/${e}` : e;
typeof n[e] == "object" ? (Object.keys(n[e]).length === 0 && (o[f] = n[e]), y(n[e], o, f)) : o[f] = n[e];
typeof o[e] == "object" ? (Object.keys(o[e]).length === 0 && (n[f] = o[e]), d(o[e], n, f)) : n[f] = o[e];
}
return o;
}, s = (n, o) => {
return n;
}, s = (o, n) => {
const i = {
new: {},
added: {},
removed: {},
edited: []
}, e = y(n), f = y(o);
return i.removed = t(e, f), i.new = t(f, e), i.edited = d(e, f), i;
}, e = d(o), f = d(n);
return i.removed = y(e, f), i.added = y(f, e), i.edited = t(e, f), i;
};
export {
s as getDiff,
d as getEditedPaths,
t as getPathsDiff,
y as getStructPaths
t as getEditedPaths,
y 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.

26 changes: 15 additions & 11 deletions src/core/get-diff.spec.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
// Packages
import { getDiff } from '.';

// Models
import { Delta } from '../models/jsondiffer.model';

describe('GetDiff function', () => {
test('Should return the difference between two basic structures', () => {
const struct1 = { 1: { 2: 7, 3: { 4: 6 } } };
const struct2 = { 1: { 3: { 4: 5 } } };
const expectedResult = { edited: [{ '1/3/4': { newValue: 5, oldValue: 6 } }], new: {}, removed: { '1/2': 7 } };
const expectedResult: Delta = { edited: [{ '1/3/4': { newValue: 5, oldValue: 6 } }], added: {}, removed: { '1/2': 7 } };

const result = getDiff(struct1, struct2);

Expand All @@ -14,9 +18,9 @@ describe('GetDiff function', () => {
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 }] };
const expectedResult = {
const expectedResult: Delta = {
edited: [{ a: { newValue: 11, oldValue: 1 } }, { 'b/1/c2': { newValue: 22, oldValue: 2 } }],
new: {},
added: {},
removed: {}
};

Expand All @@ -28,9 +32,9 @@ describe('GetDiff function', () => {
test('Should return the difference between two structures containing deep Array nodes', () => {
const struct1 = { a: 1, b: [{ c1: [{ c3: { c5: [1, 2, { c6: 3 }] } }, { c4: 6 }] }, { c2: 2 }] };
const struct2 = { a: 11, b: [{ c1: [{ c3: { c5: [1, 2, { c6: 4 }] } }, { c4: 6 }] }, { c2: 2 }] };
const expectedResult = {
const expectedResult: Delta = {
edited: [{ a: { newValue: 11, oldValue: 1 } }, { 'b/0/c1/0/c3/c5/2/c6': { newValue: 4, oldValue: 3 } }],
new: {},
added: {},
removed: {}
};

Expand All @@ -42,9 +46,9 @@ describe('GetDiff function', () => {
test('Should return the difference between two structures containing different node types', () => {
const struct1 = { a: 1, b: { c1: 2 }, c: 3 };
const struct2 = { a: '1', b: 2, c: true };
const expectedResult = {
const expectedResult: Delta = {
edited: [{ a: { newValue: '1', oldValue: 1 } }, { c: { newValue: true, oldValue: 3 } }],
new: { b: 2 },
added: { b: 2 },
removed: { 'b/c1': 2 }
};

Expand All @@ -56,9 +60,9 @@ describe('GetDiff function', () => {
test('Should return no diff when the structs has nested equal structures', () => {
const oldStruct = { a: [], b: {} };
const newStruct = { a: [], b: {} };
const expectedResult = {
const expectedResult: Delta = {
edited: [],
new: {},
added: {},
removed: {}
};

Expand All @@ -70,14 +74,14 @@ describe('GetDiff function', () => {
test('Should compute the difference between structures with different object values', () => {
const oldStruct = { a: [], b: {}, c: [], d: {} };
const newStruct = { a: {}, b: [], c: false, d: 1 };
const expectedResult = {
const expectedResult: Delta = {
edited: [
{ a: { newValue: {}, oldValue: [] } },
{ b: { newValue: [], oldValue: {} } },
{ c: { newValue: false, oldValue: [] } },
{ d: { newValue: 1, oldValue: {} } }
],
new: {},
added: {},
removed: {}
};

Expand Down
10 changes: 5 additions & 5 deletions src/core/get-diff.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@ import { getPathsDiff } from './get-paths-diff';
import { getStructPaths } from './get-struct-paths';

// Models
import { IDelta } from '../models/jsondiffer.model';
import { Delta } from '../models/jsondiffer.model';

export const getDiff = (oldStruct: Record<string, any>, newStruct: Record<string, any>): IDelta => {
const delta: IDelta = {
new: {},
export const getDiff = (oldStruct: Record<string, any>, newStruct: Record<string, any>): Delta => {
const delta: Delta = {
added: {},
removed: {},
edited: []
};
Expand All @@ -19,7 +19,7 @@ export const getDiff = (oldStruct: Record<string, any>, newStruct: Record<string
delta.removed = getPathsDiff(oldStructPaths, newStructPaths);

// B-A
delta.new = getPathsDiff(newStructPaths, oldStructPaths);
delta.added = getPathsDiff(newStructPaths, oldStructPaths);

// a->b
delta.edited = getEditedPaths(oldStructPaths, newStructPaths);
Expand Down
10 changes: 4 additions & 6 deletions src/models/jsondiffer.model.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
type DefaultValue = number | string | boolean;

export type EditedPath = { [key: string]: { newValue: DefaultValue; oldValue: DefaultValue } };
export type EditedPath = { [key: string]: { newValue: any; oldValue: any } };

export type StructPaths = Record<string, any>;

export type PathsDiff = Record<string, DefaultValue>;
export type PathsDiff = Record<string, any>;

export interface IDelta {
new: PathsDiff;
export interface Delta {
added: PathsDiff;
removed: PathsDiff;
edited: Array<EditedPath>;
}

0 comments on commit 99f829c

Please sign in to comment.