-
Notifications
You must be signed in to change notification settings - Fork 2
/
update.js
182 lines (134 loc) · 4.66 KB
/
update.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
/*
React-compatible data-structure update utility
(C) 2016-2017 Doug Hoyte
2-clause BSD license
*/
function shallowCopy(x) {
return Object.assign(new x.constructor(), x);
}
export default function update(view, upd) {
if (typeof(upd) !== 'object') throw(new Error("update is not an object"));
// Process commands:
if (upd.hasOwnProperty('$set')) {
return upd['$set'];
}
if (upd.hasOwnProperty('$push')) {
if (view === undefined || view === null) view = [];
if (!Array.isArray(view)) throw(new Error("view is not an array in push"));
if (!Array.isArray(upd['$push'])) throw(new Error("update is not an array in push"));
if (upd['$push'].length === 0) return view;
let new_view = shallowCopy(view);
for (let e of upd['$push']) {
new_view.push(e);
}
return new_view;
}
if (upd.hasOwnProperty('$unshift')) {
if (view === undefined || view === null) view = [];
if (!Array.isArray(view)) throw(new Error("view is not an array in unshift"));
if (!Array.isArray(upd['$unshift'])) throw(new Error("update is not an array in unshift"));
if (upd['$unshift'].length === 0) return view;
let new_view = shallowCopy(view);
for (let e of upd['$unshift'].reverse()) {
new_view.unshift(e);
}
return new_view;
}
if (upd.hasOwnProperty('$splice')) {
if (view === undefined || view === null) view = [];
if (!Array.isArray(view)) throw(new Error("view is not an array in splice"));
if (!Array.isArray(upd['$splice'])) throw(new Error("update is not an array in splice"));
let new_view = shallowCopy(view);
for (let s of upd['$splice']) {
if (!Array.isArray(s)) throw(new Error("update element is not an array"));
new_view.splice.apply(new_view, s);
}
return new_view;
}
if (upd.hasOwnProperty('$apply')) {
if (typeof(upd['$apply']) !== 'function') throw(new Error("update is not a function in apply"));
let new_view;
if (Array.isArray(view) || (typeof(view) === 'object' && view !== null)) {
new_view = shallowCopy(view);
} else if (view !== Object(view)) {
new_view = view;
}
return upd['$apply'](new_view);
}
// Commands that can be combined with same-level recursion
if (view === undefined || view === null) view = {};
let new_view = shallowCopy(view);
let changed = false;
if (upd.hasOwnProperty('$merge')) {
if (typeof(view) !== 'object') throw(new Error("view is not an object in merge"));
if (typeof(upd) !== 'object') throw(new Error("update is not an object in merge"));
for (let k of Object.keys(upd['$merge'])) {
if (!(k in view) || upd['$merge'][k] !== view[k]) {
changed = true;
break;
}
}
if (changed) Object.assign(new_view, upd['$merge']);
}
if (upd.hasOwnProperty('$unset')) {
if (typeof(view) !== 'object') throw(new Error("view is not an object in unset"));
if (typeof(upd['$unset']) === 'object') {
for (let k of upd['$unset']) {
if (k in new_view) {
delete new_view[k];
changed = true;
}
}
} else {
if (upd['$unset'] in new_view) {
delete new_view[upd['$unset']];
changed = true;
}
}
}
// Recurse to handle nested commands in upd:
if (Array.isArray(view)) {
for (let key in upd) {
const int = parseInt(key);
if (key != int) throw(new Error("non-numeric key in array update")); // deliberate != instead of !==
new_view[int] = update(new_view[int], upd[key]);
if (new_view[int] !== view[int]) {
changed = true;
}
}
return changed ? new_view : view;
} else if (typeof(view) === 'object') {
for (let key in upd) {
let upd_key = key;
if (!(key[0] === '$' && key[1] !== '$')) {
if (key.startsWith("$$")) key = key.substr(1);
new_view[key] = update(new_view[key], upd[upd_key]);
if (new_view[key] !== view[key] || (new_view[key] === undefined && !view.hasOwnProperty(key))) {
changed = true;
}
}
}
return changed ? new_view : view;
}
throw(new Error("view not an array or object"));
}
function escapePathKey(k) {
if (k.startsWith('$')) k = '$' + k;
return k;
}
function compilePath(path, leaf) {
if (Array.isArray(path)) {
if (path.length > 0) {
return { [escapePathKey(path[0])]: compilePath(path.slice(1), leaf) };
}
} else {
let m = path.match(/^[^.]+/);
if (m) {
return { [escapePathKey(m[0])]: compilePath(path.substr(m[0].length+1), leaf) };
}
}
return leaf;
}
export function updatePath(view, op, path, params) {
return update(view, compilePath(path, { ['$' + op]: params }));
}