-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.ts
More file actions
316 lines (268 loc) · 6.87 KB
/
index.ts
File metadata and controls
316 lines (268 loc) · 6.87 KB
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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
import { isDate } from 'node:util/types'
import { type Difference, NOT_COMPARABLE, VALUE_UPDATED } from './types.js'
import {
getDifferenceType,
getTypeOfUnknown,
isArray,
isFunction,
isObject,
isSymbol,
isValue
} from './utilities.js'
function _getFullPropertyName(
parentName: string,
propertyName: string
): string {
return parentName ? `${parentName}.${propertyName}` : propertyName
}
function _getArrayDifference(
parentName: string,
propertyName: string,
a1: unknown[],
a2: unknown[]
): Difference[] {
const diff: Difference[] = []
function comparer(otherArray: unknown[]) {
return function (current: unknown) {
return (
otherArray.filter(
(other) => JSON.stringify(other) === JSON.stringify(current)
).length === 0
)
}
}
const onlyInA1 = a1.filter(comparer(a2))
const onlyInA2 = a2.filter(comparer(a1))
for (const [index, element] of onlyInA1.entries()) {
diff.push(
..._getObjectDifference(
_getFullPropertyName(parentName, propertyName),
`[${index}]`,
element,
undefined
)
)
}
for (const [index, element] of onlyInA2.entries()) {
diff.push(
..._getObjectDifference(
_getFullPropertyName(parentName, propertyName),
`[${index}]`,
undefined,
element
)
)
}
return diff
}
// eslint-disable-next-line @typescript-eslint/max-params
function _getArrayDifferenceByKey(
parentName: string,
propertyName: string,
a1: unknown[],
a2: unknown[],
keyProperty: string
): Difference[] {
const diff: Difference[] = []
function comparer(otherArray: unknown[]) {
return function (current: unknown) {
return (
otherArray.filter(
(other) => other[keyProperty] === current[keyProperty]
).length === 0
)
}
}
const onlyInA1 = a1.filter(comparer(a2))
const onlyInA2 = a2.filter(comparer(a1))
for (const element of onlyInA1) {
diff.push(
..._getObjectDifference(
_getFullPropertyName(parentName, propertyName),
`[${keyProperty}=${element[keyProperty]}]`,
element,
undefined
)
)
}
for (const element of onlyInA2) {
diff.push(
..._getObjectDifference(
_getFullPropertyName(parentName, propertyName),
`[${keyProperty}=${element[keyProperty]}]`,
undefined,
element
)
)
}
for (const element of a1) {
const a2Item = a2.find((x) => x[keyProperty] === element[keyProperty])
if (a2Item) {
diff.push(
..._getObjectDifference(
_getFullPropertyName(parentName, propertyName),
`[${keyProperty}=${element[keyProperty]}]`,
element,
a2Item
)
)
}
}
return diff
}
// eslint-disable-next-line @typescript-eslint/max-params
function _getObjectDifference(
parentName: string,
propertyName: string,
valueFrom: unknown,
valueTo: unknown,
config: { ArrayKeyProperty?: string } = {}
): Difference[] {
const diff: Difference[] = []
if (propertyName === '__ob__' || propertyName === '__proto__') {
return diff
}
/*
* Type check
*/
const fromType = getTypeOfUnknown(valueFrom)
const toType = getTypeOfUnknown(valueTo)
if (fromType !== toType) {
const differenceType = getDifferenceType(valueFrom, valueTo)
diff.push({
property: _getFullPropertyName(parentName, propertyName),
type: differenceType,
from: valueFrom,
to: valueTo
})
return diff
}
/*
* Same type check
*/
if (isValue(valueFrom) && isValue(valueTo)) {
if (valueFrom === valueTo) {
return diff
}
const differenceType = getDifferenceType(valueFrom, valueTo)
diff.push({
property: _getFullPropertyName(parentName, propertyName),
type: differenceType,
from: valueFrom,
to: valueTo
})
return diff
}
if (isDate(valueFrom) && isDate(valueTo)) {
if (valueFrom.getTime() === valueTo.getTime()) {
return diff
}
diff.push({
property: _getFullPropertyName(parentName, propertyName),
type: VALUE_UPDATED,
from: valueFrom,
to: valueTo
})
return diff
}
if (isFunction(valueFrom) || isFunction(valueTo)) {
diff.push({
property: _getFullPropertyName(parentName, propertyName),
type: NOT_COMPARABLE,
from: valueFrom,
to: valueTo
})
return diff
}
if (isSymbol(valueFrom) || isSymbol(valueTo)) {
diff.push({
property: _getFullPropertyName(parentName, propertyName),
type: VALUE_UPDATED,
from: valueFrom,
to: valueTo
})
return diff
}
if (isArray(valueFrom) && isArray(valueTo)) {
if (config.ArrayKeyProperty) {
// check whether Array element has the key
let missKeyProperty = false
for (const element of valueFrom) {
if (element[config.ArrayKeyProperty] === undefined) {
missKeyProperty = true
}
}
for (const element of valueTo) {
if (element[config.ArrayKeyProperty] === undefined) {
missKeyProperty = true
}
}
if (missKeyProperty) {
diff.push(
..._getArrayDifference(parentName, propertyName, valueFrom, valueTo)
)
} else {
diff.push(
..._getArrayDifferenceByKey(
parentName,
propertyName,
valueFrom,
valueTo,
config.ArrayKeyProperty
)
)
}
} else {
diff.push(
..._getArrayDifference(parentName, propertyName, valueFrom, valueTo)
)
}
return diff
}
if (isObject(valueFrom) && isObject(valueTo)) {
for (const propertyKey of Object.getOwnPropertyNames(valueFrom)) {
diff.push(
..._getObjectDifference(
_getFullPropertyName(parentName, propertyName),
propertyKey,
valueFrom[propertyKey],
valueTo[propertyKey]
)
)
}
for (const propertyKey of Object.getOwnPropertyNames(valueTo)) {
if (valueFrom[propertyKey] !== undefined) {
continue
}
diff.push(
..._getObjectDifference(
_getFullPropertyName(parentName, propertyName),
propertyKey,
undefined,
valueTo[propertyKey]
)
)
}
}
return diff
}
/**
* Get deep difference between two objects. An empty array will be returned for two same objects comparing.
* @param valueFrom value comparing on the left
* @param valueTo value comparing on the right
* @returns List of differences. An empty array will be returned for two same objects comparing.
*/
export default function getObjectDifference(
valueFrom: unknown,
valueTo: unknown
): Difference[] {
const differences = _getObjectDifference('', '', valueFrom, valueTo)
return differences
}
export {
type Difference,
NOT_COMPARABLE,
VALUE_CREATED,
VALUE_DELETED,
VALUE_UPDATED
} from './types.js'