Skip to content

Commit 657f80e

Browse files
Updates
1 parent 13b0993 commit 657f80e

File tree

15 files changed

+223
-1
lines changed

15 files changed

+223
-1
lines changed
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import { Delta, JsonDiffOptions } from '../models/jsondiffer.model';
2+
/**
3+
* This method returns a delta object containing all the information needed to understand what happened during
4+
* the transition from the original object to the modified one.
5+
*
6+
* @param oldStruct Original structure to be investigated
7+
* @param newStruct Modified structure to be investigated
8+
* @param options Options for changing result behavior
9+
* @returns returns a JSON diference delta
10+
*
11+
*
12+
* @example
13+
* const oldStruct = { 1: null }
14+
* const newStruct = { 1: "coffee" }
15+
*
16+
* const result = getDiff(oldStruct, newStruct)
17+
*
18+
* console.log(result)
19+
* // Output: {"edited": [["1", null, "coffee"]], added: [], removed: []}
20+
*/
21+
export declare const getDiff: (oldStruct: Record<string, any> | string, newStruct: Record<string, any> | string, options?: JsonDiffOptions) => Delta;
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import { EditedPath, StructPaths } from '../models/jsondiffer.model';
2+
/**
3+
* This method returns all paths whose leaf value has changed
4+
*
5+
* @param oldStructPaths Original paths to be investigated
6+
* @param newStructPaths Modified paths to be investigated
7+
* @returns returns an object with all edited paths
8+
*
9+
*
10+
* @example
11+
* const oldStruct = { 1: null }
12+
* const newStruct = { 1: "coffee" }
13+
*
14+
* const result = getEditedPaths(oldStruct, newStruct)
15+
*
16+
* // Output: [1, null ,"coffee"]
17+
* console.log(result)
18+
*/
19+
export declare const getEditedPaths: (oldStructPaths: StructPaths, newStructPaths: StructPaths) => Array<EditedPath>;
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import { PathsDiff, StructPaths } from '../models/jsondiffer.model';
2+
/**
3+
* This method returns all paths whose leaf value has changed
4+
*
5+
* @param oldStructPaths Original paths to be investigated
6+
* @param newStructPaths Modified paths to be investigated
7+
* @returns returns an object with all edited paths
8+
*
9+
*
10+
* @example
11+
* const oldStruct = { 1: null, 2: "tea" }
12+
* const newStruct = { 1: "coffee" }
13+
*
14+
* const result = getPathsDiff(oldStruct, newStruct)
15+
*
16+
* console.log(result)
17+
* // Output: ["2": "tea"]
18+
*/
19+
export declare const getPathsDiff: (oldStructPaths: StructPaths, newStructPaths: StructPaths) => Array<PathsDiff>;
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import { StructPaths } from '../models/jsondiffer.model';
2+
/**
3+
* This method returns all possible paths to leaf nodes and objects other than null
4+
*
5+
* @param struct A structure to be processed
6+
* @param isLodashLike Boolean to change the path generation behavior
7+
* @returns returns a list of all paths
8+
*
9+
*
10+
* @example
11+
* const struct = { 1: { 2: null } }
12+
*
13+
* const result1 = getStructPaths(struct)
14+
* const result2 = getStructPaths(struct, { isLodashLike: true }})
15+
*
16+
*
17+
* The values "@{}" and "@[]" are used internally to represent objects and arrays respectively.
18+
* But they have a particular meaning, where the key is parent, that is, it must have children.
19+
* ("@{}" !== "{}" && "@[]" !== "[]") => true
20+
*
21+
* console.log(result)
22+
* // Output: {"1": "@{}","1/2": null}
23+
*
24+
* console.log(result)
25+
* // Output: {"1": "@{}","1.2": null}
26+
*/
27+
export declare const getStructPaths: (struct: any, isLodashLike?: boolean, paths?: {
28+
[key: string]: any;
29+
} | undefined, currentPath?: string) => StructPaths;
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
export * from './get-diff';
2+
export * from './get-paths-diff';
3+
export * from './get-struct-paths';
4+
export * from './get-edited-paths';
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import { Delta } from '../models';
2+
/**
3+
* This method returns the sanitized delta.
4+
* Some paths returned by getStructPaths() may not make much sense and be redundant to the end user.
5+
*
6+
* @param delta Delta to be sanitized
7+
* @returns Sanitized Delta
8+
*/
9+
declare const sanitizeDelta: (delta: Delta) => Delta;
10+
export default sanitizeDelta;

json-difference/dist/index.d.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
export * from './core';
2+
export * from './models';

json-difference/dist/index.js

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

json-difference/dist/index.mjs

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
const O = (e, n) => {
2+
const o = [];
3+
for (const f in e)
4+
if (n.hasOwnProperty(f)) {
5+
if (typeof e[f] == "object" && typeof n[f] == "object" && JSON.stringify(e[f]) === JSON.stringify(n[f]) || e[f] === n[f])
6+
continue;
7+
if (e[f] === "@{}" || e[f] === "@[]") {
8+
const i = n[f] === "@{}" ? {} : n[f] === "@[]" ? [] : n[f];
9+
e[f] === "@{}" ? JSON.stringify(n[f]) !== "{}" && o.push([f, {}, i]) : JSON.stringify(n[f]) !== "[]" && o.push([f, [], i]);
10+
} else
11+
o.push([f, e[f], n[f]]);
12+
}
13+
return o;
14+
}, g = (e, n) => {
15+
const o = [];
16+
let f = 0;
17+
for (const i in e)
18+
if (!(i in n)) {
19+
const r = e[i] === "@{}" ? {} : e[i] === "@[]" ? [] : e[i];
20+
o[f] = [i, r], f++;
21+
}
22+
return o;
23+
}, c = (e, n, o, f) => {
24+
const i = f ? e ? "[" : "." : "/", r = f ? e ? "]" : "" : e ? "[]" : "";
25+
return n === "__start__" ? `${f && e ? "[" : ""}${o}${r}` : `${n}${i}${o}${r}`;
26+
}, p = (e, n = !1, o, f = "__start__") => {
27+
o === void 0 && (o = Array.isArray(e) ? { __root__: "@[]" } : { __root__: "@{}" });
28+
for (const i of Object.keys(e)) {
29+
const r = c(Array.isArray(e), f, i, n);
30+
typeof e[i] == "object" && e[i] !== null ? (Object.keys(e[i]).length === 0 ? o[r] = e[i] : o[r] = Array.isArray(e[i]) ? "@[]" : "@{}", p(e[i], n, o, r)) : o[r] = e[i];
31+
}
32+
return o;
33+
}, N = (e) => (e.edited = e.edited.filter((n) => !(typeof n[1] == "object" && n[2] === "@{}")).map((n) => n[2] === "@{}" ? [n[0], n[1], {}] : n[2] === "@[]" ? [n[0], n[1], []] : n), e), $ = {
34+
isLodashLike: !1
35+
}, j = (e, n, o) => {
36+
const { isLodashLike: f } = o ?? $, i = {
37+
added: [],
38+
removed: [],
39+
edited: []
40+
}, r = typeof e == "string" ? JSON.parse(e) : e, _ = typeof n == "string" ? JSON.parse(n) : n, s = p(r, f), y = p(_, f);
41+
return i.removed = g(s, y), i.added = g(y, s), i.edited = O(s, y), N(i);
42+
};
43+
export {
44+
j as getDiff,
45+
O as getEditedPaths,
46+
g as getPathsDiff,
47+
p as getStructPaths
48+
};

json-difference/dist/index.umd.js

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)