fast-json-diff
Advanced tools
Comparing version 0.0.5 to 0.0.6
@@ -1,10 +0,10 @@ | ||
function B(e, t) { | ||
const n = {}; | ||
function B(e, n) { | ||
const t = {}; | ||
for (const r of e) | ||
n[r[t]] = r; | ||
return n; | ||
t[r[n]] = r; | ||
return t; | ||
} | ||
function D(e, t) { | ||
const n = new Set(Object.keys(e)), r = new Set(Object.keys(t)), u = [], i = []; | ||
for (const c of n) | ||
function D(e, n) { | ||
const t = new Set(Object.keys(e)), r = new Set(Object.keys(n)), u = [], i = []; | ||
for (const c of t) | ||
r.has(c) ? (i.push(c), r.delete(c)) : u.push(c); | ||
@@ -14,20 +14,20 @@ const s = Array.from(r); | ||
} | ||
function m(e) { | ||
function g(e) { | ||
return Object.prototype.toString.call(e).slice(8, -1); | ||
} | ||
const T = /* @__PURE__ */ new Set(["Number", "String", "Boolean", "Undefined", "Null"]); | ||
function g(e) { | ||
return T.has(e); | ||
const I = /* @__PURE__ */ new Set(["Number", "String", "Boolean", "Undefined", "Null"]); | ||
function v(e) { | ||
return I.has(e); | ||
} | ||
function l(e) { | ||
const t = e.at(-1); | ||
return t || "$root"; | ||
const n = e.at(-1); | ||
return n || "$root"; | ||
} | ||
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); | ||
function x(e, n, t, r, u) { | ||
const i = g(e), s = g(n); | ||
if (i !== s && !(v(i) && v(s))) { | ||
const c = l(t); | ||
return [ | ||
{ type: "remove", key: c, value: e }, | ||
{ type: "add", key: c, value: t } | ||
{ type: "add", key: c, value: n } | ||
]; | ||
@@ -37,3 +37,3 @@ } | ||
case "Date": | ||
return h(e.getTime(), t.getTime(), n).map((f) => ({ | ||
return A(e.getTime(), n.getTime(), t).map((f) => ({ | ||
...f, | ||
@@ -44,21 +44,21 @@ value: new Date(f.value), | ||
case "Object": | ||
const c = x(e, t, n, r, u); | ||
return c.length ? n.length ? [{ type: "update", key: l(n), changes: c }] : c : []; | ||
const c = K(e, n, t, r, u); | ||
return c.length ? t.length ? [{ type: "update", key: l(t), changes: c }] : c : []; | ||
case "Array": | ||
return I(e, t, n, r, u); | ||
return T(e, n, t, r, u); | ||
case "Function": | ||
return []; | ||
default: | ||
return h(e, t, n); | ||
return A(e, n, t); | ||
} | ||
} | ||
function x(e, t, n, r, u, i = !1) { | ||
function K(e, n, t, r, u, i = !1) { | ||
i == null && (i = !1); | ||
const s = [], { added: c, intersection: f, removed: a } = D(t, e); | ||
const s = [], { added: c, intersection: f, removed: a } = D(n, e); | ||
for (const o of f) { | ||
const y = n.concat([o]), p = i ? u : u.concat([o]), d = A(e[o], t[o], y, r, p); | ||
s.push(...d); | ||
const d = t.concat([o]), m = i ? u : u.concat([o]), y = x(e[o], n[o], d, r, m); | ||
s.push(...y); | ||
} | ||
for (const o of c) | ||
s.push({ type: "add", key: o, value: t[o] }); | ||
s.push({ type: "add", key: o, value: n[o] }); | ||
for (const o of a) | ||
@@ -68,57 +68,57 @@ s.push({ type: "remove", key: o, value: e[o] }); | ||
} | ||
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); | ||
function T(e, n, t, r, u) { | ||
const i = E(r, u), s = i ?? "$index", c = h(e, s), f = h(n, s), a = K(c, f, t, 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" })); | ||
const o = typeof s == "function" && s.length === 2 ? s(n[0], !0) : s, d = a.map((y) => ({ ...y, embeddedKey: "$index" })); | ||
return [{ | ||
type: "update", | ||
key: l(n), | ||
key: l(t), | ||
embeddedKey: o, | ||
changes: y | ||
changes: d | ||
}]; | ||
} | ||
function E(e, t) { | ||
function E(e, n) { | ||
if (e != null) { | ||
const n = t.join("."), r = e[n]; | ||
const t = n.join("."), r = e[t]; | ||
if (r != null) | ||
return r; | ||
for (const u in e) | ||
if (n.match(new RegExp(u))) | ||
if (t.match(new RegExp(u))) | ||
return e[u]; | ||
} | ||
} | ||
function v(e, t) { | ||
let n = {}; | ||
if (t !== "$index") | ||
n = B(e, t); | ||
function h(e, n) { | ||
let t = {}; | ||
if (n !== "$index") | ||
t = B(e, n); | ||
else | ||
for (let r = 0; r < e.length; r++) | ||
n[r] = e[r]; | ||
return n; | ||
t[r] = e[r]; | ||
return t; | ||
} | ||
function h(e, t, n) { | ||
return e === t ? [] : [{ type: "update", key: l(n), value: t, oldValue: e }]; | ||
function A(e, n, t) { | ||
return e === n ? [] : [{ type: "update", key: l(t), value: n, oldValue: e }]; | ||
} | ||
function K(e, t, n) { | ||
function $(e, n, t) { | ||
if (Array.isArray(e)) { | ||
if (n === "$index") { | ||
e.splice(t); | ||
if (t === "$index") { | ||
e.splice(n); | ||
return; | ||
} | ||
const r = L(e, n, t); | ||
const r = L(e, t, n); | ||
if (r === -1) { | ||
console.warn(`Element “${n}” -> “${t}” could not be found in the array`); | ||
console.warn(`Element “${t}” -> “${n}” could not be found in the array`); | ||
return; | ||
} | ||
return e.splice(r ?? t, 1); | ||
return e.splice(r ?? n, 1); | ||
} else { | ||
delete e[t]; | ||
delete e[n]; | ||
return; | ||
} | ||
} | ||
function L(e, t, n) { | ||
function L(e, n, t) { | ||
for (let r = 0; r < e.length; r++) { | ||
const u = e[r]; | ||
if (u && u[t] && u[t].toString() === n.toString()) | ||
if (u && u[n] && u[n].toString() === t.toString()) | ||
return r; | ||
@@ -128,71 +128,77 @@ } | ||
} | ||
function S(e, t, n) { | ||
return e[t] = n; | ||
function k(e, n, t) { | ||
return e[n] = t; | ||
} | ||
function $(e, t, n) { | ||
return Array.isArray(e) ? e.push(n) : e ? e[t] = n : null; | ||
function S(e, n, t) { | ||
return Array.isArray(e) ? e.push(t) : e ? e[n] = t : null; | ||
} | ||
function k(e, t, n) { | ||
const { type: r, key: u, value: i } = t; | ||
function C(e, n, t) { | ||
const { type: r, key: u, value: i } = n; | ||
switch (r) { | ||
case "add": | ||
return $(e, u, i); | ||
return S(e, u, i); | ||
case "update": | ||
return S(e, u, i); | ||
return k(e, u, i); | ||
case "remove": | ||
return K(e, u, n); | ||
return $(e, u, t); | ||
} | ||
} | ||
function C(e, t) { | ||
const n = []; | ||
for (const r of t.changes) | ||
function N(e, n) { | ||
const t = []; | ||
for (const r of n.changes) | ||
if (r.value != null || r.type === "remove") | ||
n.push(k(e, r, t.embeddedKey)); | ||
t.push(C(e, r, n.embeddedKey)); | ||
else { | ||
let u; | ||
t.embeddedKey === "$index" ? u = e[r.key] : u = e.find((i) => i[t.embeddedKey].toString() === r.key.toString()), n.push(V(u, r.changes)); | ||
n.embeddedKey === "$index" ? u = e[r.key] : u = e.find((i) => i[n.embeddedKey].toString() === r.key.toString()), t.push(p(u, r.changes)); | ||
} | ||
return n; | ||
return t; | ||
} | ||
function F(e, t) { | ||
return Array.isArray(e) ? C(e, t) : V(e, t.changes); | ||
function F(e, n) { | ||
return Array.isArray(e) ? N(e, n) : p(e, n.changes); | ||
} | ||
function w(e, t, n = "$index") { | ||
const { type: r, key: u, value: i, oldValue: s } = t; | ||
function w(e, n, t = "$index") { | ||
const { type: r, key: u, value: i, oldValue: s } = n; | ||
switch (r) { | ||
case "add": | ||
return K(e, u, n); | ||
return $(e, u, t); | ||
case "update": | ||
return S(e, u, s); | ||
return k(e, u, s); | ||
case "remove": | ||
return $(e, u, i); | ||
return S(e, u, i); | ||
} | ||
} | ||
function R(e, t) { | ||
const n = []; | ||
for (const r of t.changes) | ||
function R(e, n) { | ||
const t = []; | ||
for (const r of n.changes) | ||
if (r.value != null || r.type === "remove") | ||
n.push(w(e, r, t.embeddedKey)); | ||
t.push(w(e, r, n.embeddedKey)); | ||
else { | ||
let u; | ||
t.embeddedKey === "$index" ? u = e[+r.key] : u = e.find((i) => i[t.embeddedKey].toString() === r.key), n.push(N(u, r.changes)); | ||
n.embeddedKey === "$index" ? u = e[+r.key] : u = e.find((i) => i[n.embeddedKey].toString() === r.key), t.push(V(u, r.changes)); | ||
} | ||
return n; | ||
return t; | ||
} | ||
function U(e, t) { | ||
return Array.isArray(e) ? R(e, t) : N(e, t.changes); | ||
function U(e, n) { | ||
return Array.isArray(e) ? R(e, n) : V(e, n.changes); | ||
} | ||
function q(e, t, n) { | ||
return A(e, t, [], n, []); | ||
function q(e, n, t) { | ||
return x(e, n, [], 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) ? C(e, n) : F(e[n.key], n); | ||
function p(e, n) { | ||
if (n) | ||
for (const t of n) | ||
if ("value" in t || t.type === "remove") | ||
C(e, t, t.embeddedKey); | ||
else if (Array.isArray(e)) { | ||
const r = parseInt(t.embeddedKey === "$index" ? t.key : ""); | ||
isNaN(r) || !t.changes ? N(e, t) : p(e[r], t.changes); | ||
} else | ||
F(e[t.key], t); | ||
return e; | ||
} | ||
function N(e, t) { | ||
if (t) | ||
for (let n = t.length - 1; n >= 0; n--) { | ||
const r = t[n]; | ||
function V(e, n) { | ||
if (n) | ||
for (let t = n.length - 1; t >= 0; t--) { | ||
const r = n[t]; | ||
r.changes ? U(e[r.key], r) : w(e, r); | ||
@@ -203,5 +209,5 @@ } | ||
export { | ||
V as applyChangeset, | ||
p as applyChangeset, | ||
q as diff, | ||
N as revertChangeset | ||
V as revertChangeset | ||
}; |
{ | ||
"name": "fast-json-diff", | ||
"version": "0.0.5", | ||
"version": "0.0.6", | ||
"type": "module", | ||
@@ -5,0 +5,0 @@ "types": "./dist/index.d.ts", |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
15456
286