-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathJSONUtil.js
More file actions
34 lines (32 loc) · 830 Bytes
/
JSONUtil.js
File metadata and controls
34 lines (32 loc) · 830 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
import { diffChars } from "https://taisukef.github.io/jsdiff-es/src/diff/character.js";
class JSONUtil {
static diff(d1, d2) { // as string
const res = {};
let cnt = 0;
const names = [];
for (const name in d1) {
names.push(name);
const v1 = d1[name];
const v2 = d2[name] || "";
const diff = diffChars(v1.toString(), v2.toString());
//console.log(diff);
if (diff.length > 1 || diff[0].added || diff[0].removed) {
res[name] = diff;
cnt++;
}
}
for (const name in d2) {
if (names.indexOf(name) >= 0) {
continue;
}
const v = d2[name];
res[name] = [{ added: true, count: v.length, removed: false, value: v }];
cnt++;
}
if (cnt == 0) {
return null;
}
return res;
};
}
export { JSONUtil };