forked from kolodny/immutability-helper
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
276 lines (225 loc) · 6.99 KB
/
index.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
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
var invariant = require('invariant');
var hasOwnProperty = Object.prototype.hasOwnProperty;
var splice = Array.prototype.splice;
var assign = Object.assign || /* istanbul ignore next */ function assign(target, source) {
getAllKeys(source).forEach(function(key) {
if (hasOwnProperty.call(source, key)) {
target[key] = source[key];
}
});
return target;
};
var getAllKeys = typeof Object.getOwnPropertySymbols === 'function' ?
function(obj) { return Object.keys(obj).concat(Object.getOwnPropertySymbols(obj)) } :
/* istanbul ignore next */ function(obj) { return Object.keys(obj) };
/* istanbul ignore next */
function copy(object) {
if (object instanceof Array) {
return object.slice();
} else if (object && typeof object === 'object') {
return assign(new object.constructor(), object);
} else {
return object;
}
}
function newContext() {
var commands = assign({}, defaultCommands);
update.extend = function(directive, fn) {
commands[directive] = fn;
};
return update;
function update(object, spec, originalSpec, originalObject) {
if (!(Array.isArray(object) && Array.isArray(spec))) {
invariant(
!Array.isArray(spec),
'update(): You provided an invalid spec to update(). The spec may ' +
'not contain an array except as the value of $set, $push, $unshift, ' +
'$splice or any custom command allowing an array value.'
);
}
invariant(
typeof spec === 'object' && spec !== null,
'update(): You provided an invalid spec to update(). The spec and ' +
'every included key path must be plain objects containing one of the ' +
'following commands: %s.',
Object.keys(commands).join(', ')
);
var specKeys = getAllKeys(spec);
getAllKeys(spec).forEach(function(key) {
if (hasOwnProperty.call(commands, key)) {
commands[key](spec[key], object, spec, originalSpec || spec, originalObject || object);
} else {
update(object[key], spec[key], originalSpec || spec, originalObject || object);
}
})
return object;
}
}
var defaultCommands = {
$push: function(value, nextObject, spec) {
invariantPushAndUnshift(nextObject, spec, '$push');
nextObject.push[
value.length ? 'apply' : 'call'
](nextObject, value)
},
$unshift: function(value, object, spec) {
invariantPushAndUnshift(object, spec, '$unshift');
object.unshift.apply(object, value);
},
$splice: function(value, object, spec) {
invariantSplices(object, spec);
value.forEach(function(args) {
invariantSplice(args);
splice.apply(object, args);
});
},
$set: function(value, object, spec, originalSpec, originalObject) {
invariantSet(spec, originalSpec);
var stack = searchForSubnode("", originalObject, object);
if (! stack) {
throw "$set path not found"
}
stack = stack.split('/');
stack.splice(0, 1);
var pointer = originalObject; // points to the current nested object
for (var i = 0, len = stack.length; i < len; i++) {
var path = stack[i];
if (pointer.hasOwnProperty(path)) {
if (i === len - 1) { // terminating condition
pointer[path] = value;
} else {
pointer = pointer[path];
}
} else {
throw "$set path not found"
}
}
},
$unset: function(value, object, spec, originalSpec, originalObject) {
invariant(
Array.isArray(value),
'update(): expected spec of $unset to be an array; got %s. ' +
'Did you forget to wrap the key(s) in an array?',
value
);
value.forEach(function(key) {
if (Object.hasOwnProperty.call(object, key)) {
delete object[key];
}
});
},
$merge: function(value, object, spec) {
invariantMerge(object, value);
getAllKeys(value).forEach(function(key) {
if (value[key] !== object[key]) {
object[key] = value[key];
}
});
},
$apply: function(fn, object, spec, originalSpec, originalObject) {
invariantApply(fn, spec, originalSpec);
var stack = searchForSubnode("", originalObject, object);
if (! stack) {
throw "$set path not found"
}
stack = stack.split('/');
stack.splice(0, 1);
var pointer = originalObject; // points to the current nested object
for (var i = 0, len = stack.length; i < len; i++) {
var path = stack[i];
if (pointer.hasOwnProperty(path)) {
if (i === len - 1) { // terminating condition
pointer[path] = fn(pointer[path]);
} else {
pointer = pointer[path];
}
} else {
if (! object) {
throw "$set path not found"
}
}
}
},
};
module.exports = newContext();
module.exports.newContext = newContext;
// invariants
function invariantPushAndUnshift(value, spec, command) {
invariant(
Array.isArray(value),
'update(): expected target of %s to be an array; got %s.',
command,
value
);
var specValue = spec[command];
invariant(
Array.isArray(specValue),
'update(): expected spec of %s to be an array; got %s. ' +
'Did you forget to wrap your parameter in an array?',
command,
specValue
);
}
function invariantSplices(value, spec) {
invariant(
Array.isArray(value),
'Expected $splice target to be an array; got %s',
value
);
invariantSplice(spec['$splice']);
}
function invariantSplice(value) {
invariant(
Array.isArray(value),
'update(): expected spec of $splice to be an array of arrays; got %s. ' +
'Did you forget to wrap your parameters in an array?',
value
);
}
function invariantApply(fn, spec, originalSpec) {
invariant(
typeof fn === 'function',
'update(): expected spec of $apply to be a function; got %s.',
fn
);
invariant(
(spec !== originalSpec && !originalSpec['$apply']),
'$apply should be nested in an accessor'
);
}
function invariantSet(spec, originalSpec) {
invariant(
Object.keys(spec).length === 1,
'Cannot have more than one key in an object with $set'
);
invariant(
(spec !== originalSpec && !originalSpec['$set']),
'$set should be nested in an accessor'
);
}
function invariantMerge(target, specValue) {
invariant(
specValue && typeof specValue === 'object',
'update(): $merge expects a spec of type \'object\'; got %s',
specValue
);
invariant(
target && typeof target === 'object',
'update(): $merge expects a target of type \'object\'; got %s',
target
);
}
// https://stackoverflow.com/questions/8790607/javascript-json-get-path-to-given-subnode
function searchForSubnode(path, obj, target) {
for (var k in obj) {
if (obj.hasOwnProperty(k))
if (obj[k] === target)
return path + '/' + k;
else if (typeof obj[k] === "object") {
var result = searchForSubnode(path + '/' + k, obj[k], target);
if (result)
return result;
}
}
return false;
}