forked from kolodny/immutability-helper
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
239 lines (219 loc) · 7.06 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
import invariant from './invariant.js'
const {splice} = Array.prototype
const {hasOwnProperty, toString} = Object.prototype
const type = obj => toString.call(obj).slice(8, -1)
const getAllKeys = typeof Object.getOwnPropertySymbols === 'function' ?
obj => Object.keys(obj).concat(Object.getOwnPropertySymbols(obj)) :
/* istanbul ignore next */ obj => Object.keys(obj)
/* istanbul ignore next */
function copy(object) {
if (Array.isArray(object)) {
return Object.assign(object.constructor(object.length), object)
} else if (type(object) === 'Map') {
return new Map(object)
} else if (type(object) === 'Set') {
return new Set(object)
} else if (object && typeof object === 'object') {
const prototype = object.constructor && object.constructor.prototype
return Object.assign(Object.create(prototype || null), object)
} else {
return object
}
}
function newContext() {
const commands = Object.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(', ')
)
let nextObject = object
let index, key
getAllKeys(spec).forEach(function(key) {
if (hasOwnProperty.call(commands, key)) {
const objectWasNextObject = object === nextObject
nextObject = commands[key](spec[key], nextObject, spec, object)
if (objectWasNextObject && update.isEquals(nextObject, object)) {
nextObject = object
}
} else {
const nextValueForKey = update(object[key], spec[key])
if (!update.isEquals(nextValueForKey, nextObject[key]) || typeof nextValueForKey === 'undefined' && !hasOwnProperty.call(object, key)) {
if (nextObject === object) {
nextObject = copy(object)
}
nextObject[key] = nextValueForKey
}
}
})
return nextObject
}
}
const defaultCommands = {
$push: function(value, nextObject, spec) {
invariantPushAndUnshift(nextObject, spec, '$push')
return value.length ? nextObject.concat(value) : nextObject
},
$unshift: function(value, nextObject, spec) {
invariantPushAndUnshift(nextObject, spec, '$unshift')
return value.length ? value.concat(nextObject) : nextObject
},
$splice: function(value, nextObject, spec, originalObject) {
invariantSplices(nextObject, spec)
value.forEach(function(args) {
invariantSplice(args)
if (nextObject === originalObject && args.length) nextObject = copy(originalObject)
splice.apply(nextObject, args)
})
return nextObject
},
$set: function(value, nextObject, spec) {
invariantSet(spec)
return value
},
$toggle: function(targets, nextObject) {
invariantSpecArray(targets, '$toggle')
const nextObjectCopy = targets.length ? copy(nextObject) : nextObject
targets.forEach(function(target) {
nextObjectCopy[target] = !nextObject[target]
})
return nextObjectCopy
},
$unset: function(value, nextObject, spec, originalObject) {
invariantSpecArray(value, '$unset')
value.forEach(function(key) {
if (Object.hasOwnProperty.call(nextObject, key)) {
if (nextObject === originalObject) nextObject = copy(originalObject)
delete nextObject[key]
}
})
return nextObject
},
$add: function(value, nextObject, spec, originalObject) {
invariantMapOrSet(nextObject, '$add')
invariantSpecArray(value, '$add')
if (type(nextObject) === 'Map') {
value.forEach(function(pair) {
const [key, value] = pair
if (nextObject === originalObject && nextObject.get(key) !== value) nextObject = copy(originalObject)
nextObject.set(key, value)
})
} else {
value.forEach(function(value) {
if (nextObject === originalObject && !nextObject.has(value)) nextObject = copy(originalObject)
nextObject.add(value)
})
}
return nextObject
},
$remove: function(value, nextObject, spec, originalObject) {
invariantMapOrSet(nextObject, '$remove')
invariantSpecArray(value, '$remove')
value.forEach(function(key) {
if (nextObject === originalObject && nextObject.has(key)) nextObject = copy(originalObject)
nextObject.delete(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)
}
}
// invariants
function invariantPushAndUnshift(value, spec, command) {
invariant(
Array.isArray(value),
'update(): expected target of %s to be an array got %s.',
command,
value
)
invariantSpecArray(spec[command], command)
}
function invariantSpecArray(spec, command) {
invariant(
Array.isArray(spec),
'update(): expected spec of %s to be an array got %s. ' +
'Did you forget to wrap your parameter in an array?',
command,
spec
)
}
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
)
}
function invariantMapOrSet(target, command) {
const typeOfTarget = type(target)
invariant(
typeOfTarget === 'Map' || typeOfTarget === 'Set',
'update(): %s expects a target of type Set or Map got %s',
command,
typeOfTarget
)
}
export default newContext()
export {newContext}