-
Notifications
You must be signed in to change notification settings - Fork 1
/
diff.js
185 lines (159 loc) · 5.11 KB
/
diff.js
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
// enum DifferenceType IDENTICAL,LEFT,RIGHT,DIFF_PRIM,DIFF_OBJ;
ID = "IDENTICAL"
LF = "LEFT"
RT = "RIGHT"
DP = "DIFF_PRIM"
DO = "DIFF_OBJ"
// enum ObjectType OBJECT, ARRAY, STRING, NUMBER, BOOLEAN, NULL, FUNCTION, UNDEFINED, BIGINT
// getDiffs = null // a,b -> [[difftype, depthkey], ...]
// getDiffsFromDiffObj = null // diffobj -> [[difftype, depthkey], ...]
// getDiffObj = null // a,b -> diffobj
// getWithin = null // a,[k1,...,kn] -> a[k1][...][kn]
// diffToText = null // [difftype, left, (right)] -> "Changed from L to R"
function getDiffObj(a,b) {
// Primitives case (either is primitive)
if (isPrimitive(a) || isPrimitive(b))
return (a===b)?[ID,a]:[DP,a,b]
// Arrays case
let arr = (isArray(a)?2:0) + (isArray(b)?1:0)
if (arr === 3)
return getDiffArr(a,b)
else if (arr !== 0)
return [DP,a,b]
let ak = keys(a)
let bk = keys(b)
let ks = uniq([ak,bk].flat()).sort()
let ret = {}
for (const k of ks) {
if ((k in a) && (k in b))
ret[k] = getDiffObj(a[k],b[k])
else if (k in a)
ret[k] = [LF,a[k]]
else
ret[k] = [RT,b[k]]
}
return ret
}
function getDiffArr(a,b) {
let ret = []
let lmin = Math.min(a.length, b.length)
for (i=0; i<lmin; i++)
ret.push(getDiffObj(a[i],b[i]))
if (a.length>b.length)
for (i=lmin; i<a.length; i++)
ret.push([LF,a[i]])
if (b.length>a.length)
for (i=lmin; i<b.length; i++)
ret.push([RT,b[i]])
return ret
}
function getDiffs(a,b) {
diffo = getDiffObj(a,b)
return getDiffsFromDiffObj(diffo)
}
function getDiffsFromDiffObj(diffo, prefix=[]) {
let ret = []
if ((typeof diffo[0]) === 'string') {
if (diffo[0] !== ID)
ret.push([diffo[0],prefix])
// console.log("new obj: ",ret)
return ret
}
let ks = keys(diffo)
for (const k of ks) {
let p = Array.from(prefix)
p.push(k)
let rs = getDiffsFromDiffObj(diffo[k],p)
// console.log("rs is ",rs)
for (const r of rs) {
// console.log("adding: ", r)
if (r.length>0)
ret.push(r)
// console.log("arr now: ", ret)
}
}
return ret
}
function keys(o) {
if (("keys" in o) && (typeof o.keys === "function"))
return o.keys()
return Object.keys(o).sort()
}
function isPrimitive(o) {
return o === null || o === undefined ||
(typeof o) === "string" || (typeof o) === "number" ||
(typeof o) === "bigint" || (typeof o) === "boolean" ||
(typeof o) === "function";
}
function isArray(o) {
return (o instanceof Array)
}
// From https://stackoverflow.com/a/14438954
function uniq(arr) {
return [...new Set(arr)]
}
function getWithin(obj, indicies) {
for (const key of indicies)
obj = obj[key]
return obj
}
function diffToText(diffp) {
if (diffp[0] === ID) return "Property Unchanged: "+JSON.stringify(diffp[1])
if (diffp[0] === LF) return "Property Removed: "+JSON.stringify(diffp[1])
if (diffp[0] === RT) return "Property Added: "+JSON.stringify(diffp[1])
if (diffp[0] === DP) return "Property Changed: "+JSON.stringify(diffp[1])+" -> "+JSON.stringify(diffp[2])
if (diffp[0] === DO) return "Property Changed: "+JSON.stringify(diffp[1])+" -> "+JSON.stringify(diffp[2])
}
function sortKeys(obj) {
if (Array.isArray(obj)) {
return obj.map(sortKeys);
} else if (typeof obj === 'object' && obj !== null) {
// separate "id" key if exists
const {id, ...rest} = obj;
// sorted keys, with "id" always first if it exists
const keys = Object.keys(rest).sort();
if (id !== undefined) {
keys.unshift("id");
}
return keys.reduce((acc, key) => {
acc[key] = sortKeys(obj[key]);
return acc;
}, {});
}
return obj;
}
passing = true
obj1 = {a:"hello",b:"hoot?",c:"text!",e:"diffcode"}
obj2 = {a:"hello", d:"text!",e:"morecode"}
arr1 = [0,1, "2",3n,7]
arr2 = [0,1n,"2",3n,4,5,6]
function tests() {
assertEquals = function(expected,actual) {
if (expected!==actual) {
console.error("Assertion failed. Expected: "+JSON.stringify(expected)+" got "+JSON.stringify(actual)+" instead.")
passing = false
}
}
// Test getWithin
assertEquals("hi",getWithin({a:{b:"hi"}},["a","b"]))
assertEquals(undefined,getWithin({a:{b:"hi"}},["a","c"]))
// Primative comparisons
assertEquals(ID, getDiffObj("obj1","obj1")[0])
assertEquals(DP, getDiffObj("",false)[0])
assertEquals(DP, getDiffObj("",{})[0])
assertEquals(DP, getDiffObj({},false)[0])
assertEquals(DP, getDiffObj(0,0n)[0])
// Object comparisons
assertEquals(ID, getDiffObj(obj1,obj2).a[0])
assertEquals(LF, getDiffObj(obj1,obj2).b[0])
assertEquals(RT, getDiffObj(obj1,obj2).d[0])
assertEquals(DP, getDiffObj(obj1,obj2).e[0])
assertEquals(DP, getWithin(getDiffs(obj1,obj2),[3,0]))
// Sort keys
let left = {"b":"yes","a":123,"c":{"3":[5,3,1,8],"1":null},"id":"someID"};
let right = {"id":"someID","a":123,"b":"yes","c":{"1":null,"3":[5,3,1,8]}};
let str = JSON.stringify(left);
assertEquals(JSON.stringify(right),JSON.stringify(sortKeys(left)));
assertEquals(str,JSON.stringify(left));
}
tests() ; if (passing) console.log("[Object Diff] Unit Tests Passed!"); else console.error("[Object Diff] Unit Tests Failed!")