-
Notifications
You must be signed in to change notification settings - Fork 186
/
index.ts
365 lines (333 loc) · 11.6 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
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
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
declare let process: any;
function stringifiable(obj: any) {
// Safely stringify Object.create(null)
/* istanbul ignore next */
return typeof obj === 'object' && !('toString' in obj) ?
Object.prototype.toString.call(obj).slice(8, -1) :
obj;
}
const isProduction = typeof process === 'object' && process.env.NODE_ENV === 'production';
export function invariant(condition: boolean, message: () => string) {
if (!condition) {
/* istanbul ignore next */
if (isProduction) {
throw new Error('Invariant failed');
}
throw new Error(message());
}
}
const hasOwnProperty = Object.prototype.hasOwnProperty;
const splice = Array.prototype.splice;
const toString = Object.prototype.toString;
function type<T>(obj: T) {
return (toString.call(obj) as string).slice(8, -1);
}
const assign = Object.assign || /* istanbul ignore next */ (<T, S>(target: T, source: S) => {
getAllKeys(source).forEach(key => {
if (hasOwnProperty.call(source, key)) {
target[key] = source[key];
}
});
return target as T & S;
});
const getAllKeys = typeof Object.getOwnPropertySymbols === 'function'
? obj => Object.keys(obj).concat(Object.getOwnPropertySymbols(obj) as any)
/* istanbul ignore next */
: obj => Object.keys(obj);
function copy<T, U, K, V, X>(
object: T extends ReadonlyArray<U>
? ReadonlyArray<U>
: T extends Map<K, V>
? Map<K, V>
: T extends Set<X>
? Set<X>
: T extends object
? T
: any,
) {
return Array.isArray(object)
? assign(object.constructor(object.length), object)
: (type(object) === 'Map')
? new Map(object as Map<K, V>)
: (type(object) === 'Set')
? new Set(object as Set<X>)
: (object && typeof object === 'object')
? assign(Object.create(Object.getPrototypeOf(object)), object) as T
/* istanbul ignore next */
: object as T;
}
export class Context {
private commands = assign({}, defaultCommands);
constructor() {
this.update = this.update.bind(this);
// Deprecated: update.extend, update.isEquals and update.newContext
(this.update as any).extend = this.extend = this.extend.bind(this);
(this.update as any).isEquals = (x, y) => x === y;
(this.update as any).newContext = () => new Context().update;
}
get isEquals() {
return (this.update as any).isEquals;
}
set isEquals(value: (x, y) => boolean) {
(this.update as any).isEquals = value;
}
public extend<T>(directive: string, fn: (param: any, old: T) => T) {
this.commands[directive] = fn;
}
public update<T, C extends CustomCommands<object> = never>(
object: T,
$spec: Spec<T, C>,
): T {
const spec = (typeof $spec === 'function') ? { $apply: $spec } : $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: ${Object.keys(this.commands).join(', ')}.`,
);
let nextObject = object;
getAllKeys(spec).forEach(key => {
if (hasOwnProperty.call(this.commands, key)) {
const objectWasNextObject = object === nextObject;
nextObject = this.commands[key](spec[key], nextObject, spec, object);
if (objectWasNextObject && this.isEquals(nextObject, object)) {
nextObject = object;
}
} else {
const nextValueForKey =
type(object) === 'Map'
? this.update((object as any as Map<any, any>).get(key), spec[key])
: this.update(object[key], spec[key]);
const nextObjectValue =
type(nextObject) === 'Map'
? (nextObject as any as Map<any, any>).get(key)
: nextObject[key];
if (!this.isEquals(nextValueForKey, nextObjectValue)
|| typeof nextValueForKey === 'undefined'
&& !hasOwnProperty.call(object, key)
) {
if (nextObject === object) {
nextObject = copy(object as any);
}
if (type(nextObject) === 'Map') {
(nextObject as any as Map<any, any>).set(key, nextValueForKey);
} else {
nextObject[key] = nextValueForKey;
}
}
}
});
return nextObject;
}
}
const defaultCommands = {
$push(value: any, nextObject: any, spec: any) {
invariantPushAndUnshift(nextObject, spec, '$push');
return value.length ? nextObject.concat(value) : nextObject;
},
$unshift(value: any, nextObject: any, spec: any) {
invariantPushAndUnshift(nextObject, spec, '$unshift');
return value.length ? value.concat(nextObject) : nextObject;
},
$splice(value: any, nextObject: any, spec: any, originalObject: any) {
invariantSplices(nextObject, spec);
value.forEach((args: any) => {
invariantSplice(args);
if (nextObject === originalObject && args.length) {
nextObject = copy(originalObject);
}
splice.apply(nextObject, args);
});
return nextObject;
},
$set(value: any, _nextObject: any, spec: any) {
invariantSet(spec);
return value;
},
$toggle(targets: any, nextObject: any) {
invariantSpecArray(targets, '$toggle');
const nextObjectCopy = targets.length ? copy(nextObject) : nextObject;
targets.forEach((target: any) => {
nextObjectCopy[target] = !nextObject[target];
});
return nextObjectCopy;
},
$unset(value: any, nextObject: any, _spec: any, originalObject: any) {
invariantSpecArray(value, '$unset');
value.forEach((key: any) => {
if (Object.hasOwnProperty.call(nextObject, key)) {
if (nextObject === originalObject) {
nextObject = copy(originalObject);
}
delete nextObject[key];
}
});
return nextObject;
},
$add(values: any, nextObject: any, _spec: any, originalObject: any) {
invariantMapOrSet(nextObject, '$add');
invariantSpecArray(values, '$add');
if (type(nextObject) === 'Map') {
values.forEach(([key, value]) => {
if (nextObject === originalObject && nextObject.get(key) !== value) {
nextObject = copy(originalObject);
}
nextObject.set(key, value);
});
} else {
values.forEach((value: any) => {
if (nextObject === originalObject && !nextObject.has(value)) {
nextObject = copy(originalObject);
}
nextObject.add(value);
});
}
return nextObject;
},
$remove(value: any, nextObject: any, _spec: any, originalObject: any) {
invariantMapOrSet(nextObject, '$remove');
invariantSpecArray(value, '$remove');
value.forEach((key: any) => {
if (nextObject === originalObject && nextObject.has(key)) {
nextObject = copy(originalObject);
}
nextObject.delete(key);
});
return nextObject;
},
$merge(value: any, nextObject: any, _spec: any, originalObject: any) {
invariantMerge(nextObject, value);
getAllKeys(value).forEach((key: any) => {
if (value[key] !== nextObject[key]) {
if (nextObject === originalObject) {
nextObject = copy(originalObject);
}
nextObject[key] = value[key];
}
});
return nextObject;
},
$apply(value: any, original: any) {
invariantApply(value);
return value(original);
},
};
const defaultContext = new Context();
export const isEquals = (defaultContext.update as any).isEquals;
export const extend = defaultContext.extend;
export default defaultContext.update;
// @ts-ignore
exports.default.default = module.exports = assign(exports.default, exports);
// invariants
function invariantPushAndUnshift(value: any, spec: any, command: any) {
invariant(
Array.isArray(value),
() => `update(): expected target of ${stringifiable(command)} to be an array; got ${stringifiable(value)}.`,
);
invariantSpecArray(spec[command], command);
}
function invariantSpecArray(spec: any, command: any) {
invariant(
Array.isArray(spec),
() => `update(): expected spec of ${stringifiable(command)} to be an array; got ${stringifiable(spec)}. ` +
`Did you forget to wrap your parameter in an array?`,
);
}
function invariantSplices(value: any, spec: any) {
invariant(
Array.isArray(value),
() => `Expected $splice target to be an array; got ${stringifiable(value)}`,
);
invariantSplice(spec.$splice);
}
function invariantSplice(value: any) {
invariant(
Array.isArray(value),
() => `update(): expected spec of $splice to be an array of arrays; got ${stringifiable(value)}. ` +
`Did you forget to wrap your parameters in an array?`,
);
}
function invariantApply(fn: any) {
invariant(
typeof fn === 'function',
() => `update(): expected spec of $apply to be a function; got ${stringifiable(fn)}.`,
);
}
function invariantSet(spec: any) {
invariant(
Object.keys(spec).length === 1,
() => `Cannot have more than one key in an object with $set`,
);
}
function invariantMerge(target: any, specValue: any) {
invariant(
specValue && typeof specValue === 'object',
() => `update(): $merge expects a spec of type 'object'; got ${stringifiable(specValue)}`,
);
invariant(
target && typeof target === 'object',
() => `update(): $merge expects a target of type 'object'; got ${stringifiable(target)}`,
);
}
function invariantMapOrSet(target: any, command: any) {
const typeOfTarget = type(target);
invariant(
typeOfTarget === 'Map' || typeOfTarget === 'Set',
() => `update(): ${stringifiable(command)} expects a target of type Set or Map; got ${stringifiable(typeOfTarget)}`,
);
}
// Usage with custom commands is as follows:
//
// interface MyCommands {
// $foo: string;
// }
//
// update<Foo, CustomCommands<MyCommands>>(..., { $foo: "bar" });
//
// It is suggested that if you use custom commands frequently, you wrap and re-export a
// properly-typed version of `update`:
//
// function myUpdate<T>(object: T, spec: Spec<T, CustomCommands<MyCommands>>) {
// return update(object, spec);
// }
//
// See https://github.com/kolodny/immutability-helper/pull/108 for explanation of why this
// type exists.
export type CustomCommands<T> = T & { __noInferenceCustomCommandsBrand: any };
export type Spec<T, C extends CustomCommands<object> = never> =
| (
T extends (Array<infer U> | ReadonlyArray<infer U>) ? ArraySpec<U, C> :
T extends (Map<infer K, infer V> | ReadonlyMap<infer K, infer V>) ? MapSpec<K, V, C> :
T extends (Set<infer X> | ReadonlySet<infer X>) ? SetSpec<X> :
T extends object ? ObjectSpec<T, C> :
never
)
| { $set: T }
| { $apply: (v: T) => T }
| ((v: T) => T)
| (C extends CustomCommands<infer O> ? O : never);
type ArraySpec<T, C extends CustomCommands<object>> =
| { $push: ReadonlyArray<T> }
| { $unshift: ReadonlyArray<T> }
| { $splice: ReadonlyArray<[number, number?] | [number, number, ...T[]]> }
| { [index: string]: Spec<T, C> }; // Note that this does not type check properly if index: number.
type MapSpec<K, V, C extends CustomCommands<object>> =
| { $add: ReadonlyArray<[K, V]> }
| { $remove: ReadonlyArray<K> }
| { [key: string]: Spec<V, C> };
type SetSpec<T> =
| { $add: ReadonlyArray<T> }
| { $remove: ReadonlyArray<T> };
type ObjectSpec<T, C extends CustomCommands<object>> =
| { $toggle: ReadonlyArray<keyof T> }
| { $unset: ReadonlyArray<keyof T> }
| { $merge: Partial<T> }
| { [K in keyof T]?: Spec<T[K], C> };