fast-json-diff
Advanced tools
Comparing version 0.0.3 to 0.0.4
@@ -14,16 +14,17 @@ function B(e, t) { | ||
} | ||
function y(e) { | ||
function m(e) { | ||
return Object.prototype.toString.call(e).slice(8, -1); | ||
} | ||
const T = /* @__PURE__ */ new Set(["Number", "String", "Boolean", "Undefined", "Null"]); | ||
function d(e) { | ||
function g(e) { | ||
return T.has(e); | ||
} | ||
function a(e) { | ||
return e.at(-1) ?? "$root"; | ||
function l(e) { | ||
const t = e.at(-1); | ||
return t || "$root"; | ||
} | ||
function m(e, t, n, r, u) { | ||
const i = y(e), s = y(t); | ||
if (i !== s && !(d(i) && d(s))) { | ||
const c = a(n); | ||
function A(e, t, n, r, u) { | ||
const i = m(e), s = m(t); | ||
if (i !== s && !(g(i) && g(s))) { | ||
const c = l(n); | ||
return [ | ||
@@ -36,3 +37,3 @@ { type: "remove", key: c, value: e }, | ||
case "Date": | ||
return g(e.getTime(), t.getTime(), n).map((f) => ({ | ||
return h(e.getTime(), t.getTime(), n).map((f) => ({ | ||
...f, | ||
@@ -43,37 +44,38 @@ value: new Date(f.value), | ||
case "Object": | ||
const c = v(e, t, n, r, u); | ||
return c.length ? n.length ? [{ type: "update", key: a(n), changes: c }] : c : []; | ||
const c = x(e, t, n, r, u); | ||
return c.length ? n.length ? [{ type: "update", key: l(n), changes: c }] : c : []; | ||
case "Array": | ||
return E(e, t, n, r, u); | ||
return I(e, t, n, r, u); | ||
case "Function": | ||
return []; | ||
default: | ||
return g(e, t, n); | ||
return h(e, t, n); | ||
} | ||
} | ||
function v(e, t, n, r, u, i = !1) { | ||
function x(e, t, n, r, u, i = !1) { | ||
i == null && (i = !1); | ||
const s = [], { added: c, intersection: f, removed: l } = D(t, e); | ||
const s = [], { added: c, intersection: f, removed: a } = D(t, e); | ||
for (const o of f) { | ||
const w = n.concat([o]), V = i ? u : u.concat([o]), N = m(e[o], t[o], w, r, V); | ||
s.push(...N); | ||
const y = n.concat([o]), p = i ? u : u.concat([o]), d = A(e[o], t[o], y, r, p); | ||
s.push(...d); | ||
} | ||
for (const o of c) | ||
s.push({ type: "add", key: o, value: t[o] }); | ||
for (const o of l) | ||
for (const o of a) | ||
s.push({ type: "remove", key: o, value: e[o] }); | ||
return s; | ||
} | ||
function E(e, t, n, r, u) { | ||
const i = I(r, u), s = i ?? "$index", c = p(e, s), f = p(t, s), l = v(c, f, n, r, u, !0); | ||
return l.length === 0 ? [] : [ | ||
{ | ||
type: "update", | ||
key: a(n), | ||
embeddedKey: typeof s == "function" && s.length === 2 ? s(t[0], !0) : s, | ||
changes: l | ||
} | ||
]; | ||
function I(e, t, n, r, u) { | ||
const i = E(r, u), s = i ?? "$index", c = v(e, s), f = v(t, s), a = x(c, f, n, r, u, !0); | ||
if (a.length === 0) | ||
return []; | ||
const o = typeof s == "function" && s.length === 2 ? s(t[0], !0) : s, y = a.map((d) => ({ ...d, embeddedKey: "$index" })); | ||
return [{ | ||
type: "update", | ||
key: l(n), | ||
embeddedKey: o, | ||
changes: y | ||
}]; | ||
} | ||
function I(e, t) { | ||
function E(e, t) { | ||
if (e != null) { | ||
@@ -88,3 +90,3 @@ const n = t.join("."), r = e[n]; | ||
} | ||
function p(e, t) { | ||
function v(e, t) { | ||
let n = {}; | ||
@@ -98,6 +100,6 @@ if (t !== "$index") | ||
} | ||
function g(e, t, n) { | ||
return e === t ? [] : [{ type: "update", key: a(n), value: t, oldValue: e }]; | ||
function h(e, t, n) { | ||
return e === t ? [] : [{ type: "update", key: l(n), value: t, oldValue: e }]; | ||
} | ||
function h(e, t, n) { | ||
function K(e, t, n) { | ||
if (Array.isArray(e)) { | ||
@@ -127,27 +129,27 @@ if (n === "$index") { | ||
} | ||
function A(e, t, n) { | ||
function S(e, t, n) { | ||
return e[t] = n; | ||
} | ||
function x(e, t, n) { | ||
function $(e, t, n) { | ||
return Array.isArray(e) ? e.push(n) : e ? e[t] = n : null; | ||
} | ||
function K(e, t, n) { | ||
function k(e, t, n) { | ||
const { type: r, key: u, value: i } = t; | ||
switch (r) { | ||
case "add": | ||
return x(e, u, i); | ||
return $(e, u, i); | ||
case "update": | ||
return A(e, u, i); | ||
return S(e, u, i); | ||
case "remove": | ||
return h(e, u, n); | ||
return K(e, u, n); | ||
} | ||
} | ||
function S(e, t) { | ||
function C(e, t) { | ||
const n = []; | ||
for (const r of t.changes) | ||
if (r.value != null || r.type === "remove") | ||
n.push(K(e, r, t.embeddedKey)); | ||
n.push(k(e, r, t.embeddedKey)); | ||
else { | ||
let u; | ||
t.embeddedKey === "$index" ? u = e[r.key] : u = e.find((i) => i[t.embeddedKey].toString() === r.key.toString()), n.push(C(u, r.changes)); | ||
t.embeddedKey === "$index" ? u = e[r.key] : u = e.find((i) => i[t.embeddedKey].toString() === r.key.toString()), n.push(V(u, r.changes)); | ||
} | ||
@@ -157,13 +159,13 @@ return n; | ||
function F(e, t) { | ||
return Array.isArray(e) ? S(e, t) : C(e, t.changes); | ||
return Array.isArray(e) ? C(e, t) : V(e, t.changes); | ||
} | ||
function k(e, t, n = "$index") { | ||
function w(e, t, n = "$index") { | ||
const { type: r, key: u, value: i, oldValue: s } = t; | ||
switch (r) { | ||
case "add": | ||
return h(e, u, n); | ||
return K(e, u, n); | ||
case "update": | ||
return A(e, u, s); | ||
return S(e, u, s); | ||
case "remove": | ||
return x(e, u, i); | ||
return $(e, u, i); | ||
} | ||
@@ -175,6 +177,6 @@ } | ||
if (r.value != null || r.type === "remove") | ||
n.push(k(e, r, t.embeddedKey)); | ||
n.push(w(e, r, t.embeddedKey)); | ||
else { | ||
let u; | ||
t.embeddedKey === "$index" ? u = e[+r.key] : u = e.find((i) => i[t.embeddedKey].toString() === r.key), n.push($(u, r.changes)); | ||
t.embeddedKey === "$index" ? u = e[+r.key] : u = e.find((i) => i[t.embeddedKey].toString() === r.key), n.push(N(u, r.changes)); | ||
} | ||
@@ -184,18 +186,18 @@ return n; | ||
function U(e, t) { | ||
return Array.isArray(e) ? R(e, t) : $(e, t.changes); | ||
return Array.isArray(e) ? R(e, t) : N(e, t.changes); | ||
} | ||
function q(e, t, n) { | ||
return m(e, t, [], n, []); | ||
return A(e, t, [], n, []); | ||
} | ||
function C(e, t) { | ||
function V(e, t) { | ||
if (t) | ||
for (const n of t) | ||
"value" in n || n.type === "remove" ? K(e, n, n.embeddedKey) : Array.isArray(e) ? S(e, n) : F(e[n.key], n); | ||
"value" in n || n.type === "remove" ? k(e, n, n.embeddedKey) : Array.isArray(e) ? C(e, n) : F(e[n.key], n); | ||
return e; | ||
} | ||
function $(e, t) { | ||
function N(e, t) { | ||
if (t) | ||
for (let n = t.length - 1; n >= 0; n--) { | ||
const r = t[n]; | ||
r.changes ? U(e[r.key], r) : k(e, r); | ||
r.changes ? U(e[r.key], r) : w(e, r); | ||
} | ||
@@ -205,5 +207,5 @@ return e; | ||
export { | ||
C as applyChangeset, | ||
V as applyChangeset, | ||
q as diff, | ||
$ as revertChangeset | ||
N as revertChangeset | ||
}; |
{ | ||
"name": "fast-json-diff", | ||
"version": "0.0.3", | ||
"version": "0.0.4", | ||
"type": "module", | ||
@@ -18,3 +18,9 @@ "types": "./dist/index.d.ts", | ||
}, | ||
"scripts": { | ||
"build": "vite build", | ||
"test": "vitest --watch false", | ||
"prepublishOnly": "npm run build" | ||
}, | ||
"devDependencies": { | ||
"ajv": "^8.12.0", | ||
"@typescript-eslint/eslint-plugin": "5.57.1", | ||
@@ -30,8 +36,3 @@ "@typescript-eslint/parser": "5.57.1", | ||
"vitest": "0.29.8" | ||
}, | ||
"dependencies": {}, | ||
"scripts": { | ||
"build": "vite build", | ||
"test": "vitest" | ||
} | ||
} | ||
} |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
0
15073
11
8
279