forked from eugeneware/changeset
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.ts
152 lines (131 loc) · 4.22 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
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
import * as _ from 'underscore';
export const deepCopy = <T>(target: T): T => {
if (target === null) {
return target;
}
if (target instanceof Date) {
return new Date(target.getTime()) as any;
}
if (target instanceof Array) {
const cp = [] as any[];
(target as any[]).forEach((v) => {
cp.push(v);
});
return cp.map((n: any) => deepCopy<any>(n)) as any;
}
if (typeof target === 'object' && !_.isEmpty(target)) {
const cp = {...(target as { [key: string]: any })} as { [key: string]: any };
Object.keys(cp).forEach(k => {
cp[k] = deepCopy<any>(cp[k]);
});
return cp as T;
}
return target;
};
export enum ChangeType {
PUT = 'put',
DEL = 'del'
}
export interface ChangeSet {
type: ChangeType
key: string[];
value?: any;
}
const diff = (old: any, new_: any): ChangeSet[] => {
const changes = compare(old, new_, [], []);
return changes;
}
function delCheck(op: ChangeSet) {
if (op.type === ChangeType.PUT && op.value === undefined) {
op.type = ChangeType.DEL;
delete op.value;
}
return op;
}
const compare = (old: any, new_: any, path: string[], cache: string[]): ChangeSet[] => {
const changes: ChangeSet[] = [];
if (old !== null && new_ !== null && typeof old === 'object' && typeof new_ === 'object' && !_.contains(cache, old)) {
cache.push(old);
const oldKeys = Object.keys(old);
const newKeys = Object.keys(new_);
const sameKeys = _.intersection(oldKeys, newKeys);
sameKeys.forEach(k => {
const childChanges = compare(old[k], new_[k], path.concat(k), cache);
changes.push(...childChanges);
});
const deletions = _.difference(oldKeys, newKeys).reverse().map(k => {
return {
type: ChangeType.DEL,
key: path.concat(k),
}
});
changes.push(...deletions);
const additions = _.difference(newKeys, oldKeys).map(k => {
return delCheck({
type: ChangeType.PUT,
key: path.concat(k),
value: new_[k]
});
});
changes.push(...additions);
} else if (old !== new_) {
changes.push(delCheck({type: ChangeType.PUT, key: path, value: new_}));
}
return changes;
}
const apply = (changes: ChangeSet[], target: any, modify: boolean = false) => {
let appliedObj: any, keys: string[];
if(modify) {
appliedObj = target;
} else {
appliedObj = deepCopy(target);
}
changes.forEach((change) => {
let ptr: any;
switch (change.type) {
case ChangeType.PUT:
ptr = appliedObj;
keys = change.key;
if(keys.length) {
keys.forEach((key, index, records) => {
if(!(key in ptr)) {
ptr[key] = {};
}
if( index < records.length - 1) {
ptr = ptr[key];
} else {
ptr[key] = change.value;
}
});
} else {
appliedObj = change.value;
}
break;
case ChangeType.DEL:
ptr = appliedObj;
keys = change.key;
if(keys.length) {
keys.forEach((key, index, records) => {
if(!(key in ptr)) {
ptr[key] = {};
}
if(index < records.length - 1) {
ptr = ptr[key];
} else {
if(Array.isArray(ptr)) {
ptr.splice(parseInt(key, 10), 1);
} else {
delete ptr[key];
}
}
});
} else {
appliedObj = null;
}
break;
}
});
return appliedObj;
}
diff.apply = apply;
export default diff;