-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.ts
78 lines (73 loc) · 2.39 KB
/
index.ts
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
export interface Diff<T = any> {
type: 'ADD' | 'REMOVE' | 'CHANGE';
path: (string | number)[];
old?: T;
new?: T;
}
const needDig = (obj: any) => {
if (obj !== null && typeof obj === 'object') {
const cname = Object.getPrototypeOf(obj).constructor.name;
if (cname === 'Object' || cname === 'Array') return cname;
}
};
const diff = <T = any, TObj = Record<string, T> | T[]>(oldObj: TObj, newObj: TObj) => {
const result: Diff[] = [];
const _diff = (oldObj: TObj, newObj: TObj, isArray: boolean) => {
if (isArray) {
const oldL = (oldObj as unknown as any[]).length,
newL = (newObj as unknown as any[]).length;
for (let i = oldL; i < newL; i++) {
result.push({
type: 'ADD',
path: [i],
new: newObj[i]
});
}
} else {
for (const key in newObj) {
if (!(key in (oldObj as any))) {
result.push({
type: 'ADD',
path: [key],
new: newObj[key]
});
}
}
}
for (let key in oldObj) {
isArray && ((key as string | number) = +key);
const oldV = oldObj[key];
const path = [key];
if (!(key in (newObj as any))) {
result.push({
type: 'REMOVE',
path,
old: oldV
});
continue;
}
const newV = newObj[key];
const t = needDig(oldV);
if (!t || t !== needDig(newV)) {
if (oldV !== newV) {
result.push({
type: 'CHANGE',
path,
old: oldV,
new: newV
});
}
continue;
}
let i = result.length;
_diff(oldV as unknown as TObj, newV as unknown as TObj, t === 'Array');
const j = result.length;
for (; i < j; i++) {
result[i].path.unshift(key);
}
}
};
_diff(oldObj == null ? ({} as TObj) : oldObj, newObj == null ? ({} as TObj) : newObj, Array.isArray(oldObj));
return result;
};
export default diff;