Skip to content

Commit 61187e5

Browse files
committed
feat: add wip code
1 parent cb1d39a commit 61187e5

6 files changed

Lines changed: 76 additions & 30 deletions

File tree

dist.browser/json-difference.mjs

Lines changed: 43 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,55 @@
1-
const y = (f, i) => {
2-
const n = [];
3-
for (const e in f)
4-
if (i.hasOwnProperty(e)) {
5-
if (typeof f[e] == "object" && typeof i[e] == "object" && JSON.stringify(f[e]) === JSON.stringify(i[e]))
1+
const y = (n, f) => {
2+
const t = [];
3+
for (const e in n)
4+
if (f.hasOwnProperty(e)) {
5+
if (typeof n[e] == "object" && typeof f[e] == "object" && JSON.stringify(n[e]) === JSON.stringify(f[e]))
66
continue;
7-
f[e] !== i[e] && n.push([e, f[e], i[e]]);
7+
n[e] !== f[e] && t.push([e, n[e], f[e]]);
88
}
9-
return n;
10-
}, d = (f, i) => {
11-
const n = [];
9+
return t;
10+
}, s = (n, f) => {
11+
const t = [];
1212
let e = 0;
13-
for (const o in f)
14-
o in i || (n[e] = [o, f[o]], e++);
15-
return n;
16-
}, g = (f, i, n, e) => {
17-
const o = e ? f ? "[" : "." : "/", t = e ? f ? "]" : "" : f ? "[]" : "";
18-
return i !== "" ? `${i}${o}${n}${t}` : `${e && f ? "[" : ""}${n}${t}`;
19-
}, c = (f, i = !1, n = {}, e = "") => {
20-
for (const o of Object.keys(f)) {
21-
const t = g(Array.isArray(f), e, o, i);
22-
typeof f[o] == "object" ? (Object.keys(f[o]).length === 0 && (n[t] = f[o]), c(f[o], i, n, t)) : n[t] = f[o];
13+
for (const o in n)
14+
o in f || (t[e] = [o, n[o]], e++);
15+
return t;
16+
}, d = (n, f, t, e) => {
17+
const o = e ? n ? "[" : "." : "/", c = e ? n ? "]" : "" : n ? "[]" : "";
18+
return f !== "" ? `${f}${o}${t}${c}` : `${e && n ? "[" : ""}${t}${c}`;
19+
}, r = (n, f = !1, t = {}, e = "") => {
20+
for (const o of Object.keys(n)) {
21+
const c = d(Array.isArray(n), e, o, f);
22+
typeof n[o] == "object" ? (Object.keys(n[o]).length === 0 && (t[c] = n[o]), r(n[o], f, t, c)) : t[c] = n[o];
2323
}
24-
return n;
25-
}, s = (f, i, n = !1) => {
24+
return t;
25+
}, p = (n, f, t = !1) => {
2626
const e = {
2727
added: [],
2828
removed: [],
2929
edited: []
30-
}, o = c(f, n), t = c(i, n);
31-
return e.removed = d(o, t), e.added = d(t, o), e.edited = y(o, t), e;
30+
}, o = r(n, t), c = r(f, t);
31+
return e.removed = s(o, c), e.added = s(c, o), e.edited = y(o, c), e;
32+
}, g = (n, [f, t]) => {
33+
const e = f.split("/");
34+
let o = n;
35+
const c = e.length - 1;
36+
for (let l = 0; l < c; ++l) {
37+
const i = e[l];
38+
i in o || (console.log({ key: i }), i.includes("[]") && i.replace("[]", "")), o = o[i];
39+
}
40+
return o[e[c]] = t, n;
41+
}, a = (n, f) => {
42+
let t = n;
43+
return f.removed.forEach((e) => {
44+
const o = e[0].replaceAll("\\/.*?\\[]", (c) => (console.log("entrei"), `[${c.replaceAll("/", "").replaceAll("[]", "")}]`));
45+
console.log({ el: e, cafe: o });
46+
}), t;
3247
};
3348
export {
34-
s as getDiff,
49+
g as addDynamicProperty,
50+
a as applyDeltaDiff,
51+
p as getDiff,
3552
y as getEditedPaths,
36-
d as getPathsDiff,
37-
c as getStructPaths
53+
s as getPathsDiff,
54+
r as getStructPaths
3855
};

dist.browser/json-difference.umd.js

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

package.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
"homepage": "https://github.com/lukascivil/jsondiffer#readme",
3636
"devDependencies": {
3737
"@types/jest": "^28.1.6",
38+
"@types/lodash.unset": "^4.5.7",
3839
"@typescript-eslint/eslint-plugin": "^5.0.0",
3940
"eslint": "^8.0.1",
4041
"eslint-config-standard-with-typescript": "^22.0.0",
@@ -49,5 +50,8 @@
4950
"typescript": "4.4.3",
5051
"vite": "^3.0.7",
5152
"vite-tsconfig-paths": "^3.5.0"
53+
},
54+
"dependencies": {
55+
"lodash.unset": "^4.5.2"
5256
}
5357
}

src/core/apply-delta-diff.ts

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,22 @@
11
// Models
22
import { Delta } from '../models/jsondiffer.model'
33
import { addDynamicProperty } from './add-dynamic-property'
4+
import { unset } from 'lodash'
45

56
export const applyDeltaDiff = (struct: any, delta: Delta): any => {
67
let struct1 = struct
78

89
// Remove added properties
910
delta.removed.forEach((el) => {
10-
console.log({ el })
11-
// struct1 = filterObject(struct, el[0])
11+
const cafe = el[0].replaceAll('\\/.*?\\[]', (matched) => {
12+
console.log('entrei')
13+
const value = matched.replaceAll('/', '').replaceAll('[]', '')
14+
15+
return `[${value}]`
16+
})
17+
18+
console.log({ el, cafe })
19+
// struct1 = unset(struct, el[0])
1220
})
1321

1422
// Add removed properties

tsconfig.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
"declaration": true,
77
"outDir": "./dist",
88
"strict": true,
9-
"lib": ["es6", "dom"]
9+
"lib": ["es6", "dom", "es2021"]
1010
},
1111
"include": ["src"],
1212
"exclude": ["node_modules", "**/__tests__/*"]

yarn.lock

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -899,6 +899,18 @@
899899
resolved "https://registry.yarnpkg.com/@types/json5/-/json5-0.0.29.tgz#ee28707ae94e11d2b827bcbe5270bcea7f3e71ee"
900900
integrity sha512-dRLjCWHYg4oaA77cxO64oO+7JwCwnIzkZPdrrC71jQmQtlhM556pwKo5bUzqvZndkVbeFLIIi+9TC40JNF5hNQ==
901901

902+
"@types/lodash.unset@^4.5.7":
903+
version "4.5.7"
904+
resolved "https://registry.yarnpkg.com/@types/lodash.unset/-/lodash.unset-4.5.7.tgz#54b98d60c4cf3a55120c63530ea1b872e6321f57"
905+
integrity sha512-/i371dATnLQ4tazwcX/n+rGk3M6RnMbA3lJKrKFjELicPExmZ1LcKtGfHBECuPS2TTl3yDuaFmWtmfACVuBBAQ==
906+
dependencies:
907+
"@types/lodash" "*"
908+
909+
"@types/lodash@*":
910+
version "4.14.184"
911+
resolved "https://registry.yarnpkg.com/@types/lodash/-/lodash-4.14.184.tgz#23f96cd2a21a28e106dc24d825d4aa966de7a9fe"
912+
integrity sha512-RoZphVtHbxPZizt4IcILciSWiC6dcn+eZ8oX9IWEYfDMcocdd42f7NPI6fQj+6zI8y4E0L7gu2pcZKLGTRaV9Q==
913+
902914
"@types/node@*":
903915
version "16.11.11"
904916
resolved "https://registry.yarnpkg.com/@types/node/-/node-16.11.11.tgz#6ea7342dfb379ea1210835bada87b3c512120234"
@@ -2935,6 +2947,11 @@ lodash.merge@^4.6.2:
29352947
resolved "https://registry.yarnpkg.com/lodash.merge/-/lodash.merge-4.6.2.tgz#558aa53b43b661e1925a0afdfa36a9a1085fe57a"
29362948
integrity sha512-0KpjqXRVvrYyCsX1swR/XTK0va6VQkQM6MNo7PqW77ByjAhoARA8EfrP1N4+KlKj8YS0ZUCtRT/YUuhyYDujIQ==
29372949

2950+
lodash.unset@^4.5.2:
2951+
version "4.5.2"
2952+
resolved "https://registry.yarnpkg.com/lodash.unset/-/lodash.unset-4.5.2.tgz#370d1d3e85b72a7e1b0cdf2d272121306f23e4ed"
2953+
integrity sha512-bwKX88k2JhCV9D1vtE8+naDKlLiGrSmf8zi/Y9ivFHwbmRfA8RxS/aVJ+sIht2XOwqoNr4xUPUkGZpc1sHFEKg==
2954+
29382955
lru-cache@^6.0.0:
29392956
version "6.0.0"
29402957
resolved "https://registry.yarnpkg.com/lru-cache/-/lru-cache-6.0.0.tgz#6d6fe6570ebd96aaf90fcad1dafa3b2566db3a94"

0 commit comments

Comments
 (0)