forked from kolodny/immutability-helper
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
208 lines (189 loc) · 5.87 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
var invariant = require('invariant');
var hasOwnProperty = Object.prototype.hasOwnProperty;
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 assign(object.constructor(object.length), object)
} else if (object && typeof object === 'object') {
var prototype = object.constructor && object.constructor.prototype
return assign(Object.create(prototype || null), object);
} else {
return object;
}
}
function newContext() {
var commands = assign({}, defaultCommands);
update.extend = function(directive, fn) {
commands[directive] = fn;
};
update.isEquals = function(a, b) { return a === b; };
return update;
function update(object, spec) {
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 nextObject = object;
var index, key;
getAllKeys(spec).forEach(function(key) {
if (hasOwnProperty.call(commands, key)) {
var objectWasNextObject = object === nextObject;
nextObject = commands[key](spec[key], nextObject, spec, object);
if (objectWasNextObject && update.isEquals(nextObject, object)) {
nextObject = object;
}
} else {
var nextValueForKey = update(object[key], spec[key]);
if (!update.isEquals(nextValueForKey, nextObject[key])) {
if (nextObject === object) {
nextObject = copy(object);
}
nextObject[key] = nextValueForKey;
}
}
})
return nextObject;
}
}
var defaultCommands = {
$push: function(value, nextObject, spec) {
invariantPushAndUnshift(nextObject, spec, '$push');
if (value.length) {
nextObject = copy(nextObject)
nextObject.push.apply(nextObject, value)
}
return nextObject;
},
$unshift: function(value, nextObject, spec) {
invariantPushAndUnshift(nextObject, spec, '$unshift');
if (value.length) {
nextObject = copy(nextObject)
nextObject.unshift.apply(nextObject, value)
}
return nextObject;
},
$splice: function(value, nextObject, spec, originalObject) {
invariantSplices(nextObject, spec);
value.forEach(function(args) {
invariantSplice(args);
if (nextObject === originalObject && args.length) nextObject = copy(originalObject);
nextObject.splice.apply(nextObject, args);
});
return nextObject;
},
$set: function(value, nextObject, spec) {
invariantSet(spec);
return value;
},
$unset: function(value, nextObject, spec, 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(nextObject, key)) {
if (nextObject === originalObject) nextObject = copy(originalObject);
delete nextObject[key];
}
});
return nextObject;
},
$merge: function(value, nextObject, spec, originalObject) {
invariantMerge(nextObject, value);
getAllKeys(value).forEach(function(key) {
if (value[key] !== nextObject[key]) {
if (nextObject === originalObject) nextObject = copy(originalObject);
nextObject[key] = value[key];
}
});
return nextObject;
},
$apply: function(value, original) {
invariantApply(value);
return value(original);
}
};
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) {
invariant(
typeof fn === 'function',
'update(): expected spec of $apply to be a function; got %s.',
fn
);
}
function invariantSet(spec) {
invariant(
Object.keys(spec).length === 1,
'Cannot have more than one key in an object with $set'
);
}
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
);
}