|
| 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 | +}; |
0 commit comments