immutable 4.3.2 → 5.0.0-beta.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (82) hide show
  1. package/README.md +11 -7
  2. package/dist/es/Collection.js +74 -0
  3. package/dist/es/CollectionImpl.js +781 -0
  4. package/dist/es/Hash.js +259 -0
  5. package/dist/es/Immutable.js +73 -0
  6. package/dist/es/Iterator.js +105 -0
  7. package/dist/es/List.js +691 -0
  8. package/dist/es/Map.js +832 -0
  9. package/dist/es/Math.js +44 -0
  10. package/dist/es/Operations.js +952 -0
  11. package/dist/es/OrderedMap.js +194 -0
  12. package/dist/es/OrderedSet.js +90 -0
  13. package/dist/es/PairSorting.js +29 -0
  14. package/dist/es/Range.js +167 -0
  15. package/dist/es/Record.js +290 -0
  16. package/dist/es/Repeat.js +129 -0
  17. package/dist/es/Seq.js +396 -0
  18. package/dist/es/Set.js +277 -0
  19. package/dist/es/Stack.js +259 -0
  20. package/dist/es/TrieUtils.js +115 -0
  21. package/dist/es/fromJS.js +73 -0
  22. package/dist/es/functional/get.js +37 -0
  23. package/dist/es/functional/getIn.js +40 -0
  24. package/dist/es/functional/has.js +34 -0
  25. package/dist/es/functional/hasIn.js +31 -0
  26. package/dist/es/functional/merge.js +136 -0
  27. package/dist/es/functional/remove.js +55 -0
  28. package/dist/es/functional/removeIn.js +31 -0
  29. package/dist/es/functional/set.js +51 -0
  30. package/dist/es/functional/setIn.js +31 -0
  31. package/dist/es/functional/update.js +30 -0
  32. package/dist/es/functional/updateIn.js +93 -0
  33. package/dist/es/is.js +107 -0
  34. package/dist/es/methods/asImmutable.js +28 -0
  35. package/dist/es/methods/asMutable.js +30 -0
  36. package/dist/es/methods/deleteIn.js +30 -0
  37. package/dist/es/methods/getIn.js +30 -0
  38. package/dist/es/methods/hasIn.js +30 -0
  39. package/dist/es/methods/merge.js +78 -0
  40. package/dist/es/methods/mergeDeep.js +40 -0
  41. package/dist/es/methods/mergeDeepIn.js +36 -0
  42. package/dist/es/methods/mergeIn.js +35 -0
  43. package/dist/es/methods/setIn.js +30 -0
  44. package/dist/es/methods/toObject.js +35 -0
  45. package/dist/es/methods/update.js +32 -0
  46. package/dist/es/methods/updateIn.js +30 -0
  47. package/dist/es/methods/wasAltered.js +28 -0
  48. package/dist/es/methods/withMutations.js +30 -0
  49. package/dist/es/package.json.js +26 -0
  50. package/dist/es/predicates/isAssociative.js +31 -0
  51. package/dist/es/predicates/isCollection.js +30 -0
  52. package/dist/es/predicates/isImmutable.js +31 -0
  53. package/dist/es/predicates/isIndexed.js +30 -0
  54. package/dist/es/predicates/isKeyed.js +30 -0
  55. package/dist/es/predicates/isList.js +30 -0
  56. package/dist/es/predicates/isMap.js +30 -0
  57. package/dist/es/predicates/isOrdered.js +30 -0
  58. package/dist/es/predicates/isOrderedMap.js +31 -0
  59. package/dist/es/predicates/isOrderedSet.js +31 -0
  60. package/dist/es/predicates/isRecord.js +30 -0
  61. package/dist/es/predicates/isSeq.js +30 -0
  62. package/dist/es/predicates/isSet.js +30 -0
  63. package/dist/es/predicates/isStack.js +30 -0
  64. package/dist/es/predicates/isValueObject.js +32 -0
  65. package/dist/es/toJS.js +53 -0
  66. package/dist/es/utils/arrCopy.js +34 -0
  67. package/dist/es/utils/assertNotInfinite.js +33 -0
  68. package/dist/es/utils/coerceKeyPath.js +39 -0
  69. package/dist/es/utils/deepEqual.js +98 -0
  70. package/dist/es/utils/hasOwnProperty.js +26 -0
  71. package/dist/es/utils/invariant.js +28 -0
  72. package/dist/es/utils/isArrayLike.js +43 -0
  73. package/dist/es/utils/isDataStructure.js +38 -0
  74. package/dist/es/utils/isPlainObj.js +51 -0
  75. package/dist/es/utils/mixin.js +34 -0
  76. package/dist/es/utils/quoteString.js +32 -0
  77. package/dist/es/utils/shallowCopy.js +40 -0
  78. package/dist/immutable.d.ts +136 -21
  79. package/dist/immutable.js +1 -59
  80. package/dist/immutable.min.js +32 -32
  81. package/package.json +5 -3
  82. package/dist/immutable.es.js +0 -5965
@@ -0,0 +1,691 @@
1
+ /**
2
+ * MIT License
3
+ *
4
+ * Copyright (c) 2014-present, Lee Byron and other contributors.
5
+ *
6
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
7
+ * of this software and associated documentation files (the "Software"), to deal
8
+ * in the Software without restriction, including without limitation the rights
9
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10
+ * copies of the Software, and to permit persons to whom the Software is
11
+ * furnished to do so, subject to the following conditions:
12
+ *
13
+ * The above copyright notice and this permission notice shall be included in all
14
+ * copies or substantial portions of the Software.
15
+ *
16
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22
+ * SOFTWARE.
23
+ */
24
+ import { wrapIndex, MASK, SHIFT, wholeSlice, DELETE, SIZE, SetRef, OwnerID, MakeRef, resolveEnd, resolveBegin } from './TrieUtils.js';
25
+ import { isList, IS_LIST_SYMBOL } from './predicates/isList.js';
26
+ import { IndexedCollection } from './Collection.js';
27
+ import { hasIterator, Iterator, iteratorDone, iteratorValue } from './Iterator.js';
28
+ import { setIn } from './methods/setIn.js';
29
+ import { deleteIn } from './methods/deleteIn.js';
30
+ import { update } from './methods/update.js';
31
+ import { updateIn } from './methods/updateIn.js';
32
+ import { mergeIn } from './methods/mergeIn.js';
33
+ import { mergeDeepIn } from './methods/mergeDeepIn.js';
34
+ import { withMutations } from './methods/withMutations.js';
35
+ import { asMutable } from './methods/asMutable.js';
36
+ import { asImmutable } from './methods/asImmutable.js';
37
+ import { wasAltered } from './methods/wasAltered.js';
38
+ import assertNotInfinite from './utils/assertNotInfinite.js';
39
+
40
+ var List = /*@__PURE__*/(function (IndexedCollection) {
41
+ function List(value) {
42
+ var empty = emptyList();
43
+ if (value === undefined || value === null) {
44
+ return empty;
45
+ }
46
+ if (isList(value)) {
47
+ return value;
48
+ }
49
+ var iter = IndexedCollection(value);
50
+ var size = iter.size;
51
+ if (size === 0) {
52
+ return empty;
53
+ }
54
+ assertNotInfinite(size);
55
+ if (size > 0 && size < SIZE) {
56
+ return makeList(0, size, SHIFT, null, new VNode(iter.toArray()));
57
+ }
58
+ return empty.withMutations(function (list) {
59
+ list.setSize(size);
60
+ iter.forEach(function (v, i) { return list.set(i, v); });
61
+ });
62
+ }
63
+
64
+ if ( IndexedCollection ) List.__proto__ = IndexedCollection;
65
+ List.prototype = Object.create( IndexedCollection && IndexedCollection.prototype );
66
+ List.prototype.constructor = List;
67
+
68
+ List.of = function of (/*...values*/) {
69
+ return this(arguments);
70
+ };
71
+
72
+ List.prototype.toString = function toString () {
73
+ return this.__toString('List [', ']');
74
+ };
75
+
76
+ // @pragma Access
77
+
78
+ List.prototype.get = function get (index, notSetValue) {
79
+ index = wrapIndex(this, index);
80
+ if (index >= 0 && index < this.size) {
81
+ index += this._origin;
82
+ var node = listNodeFor(this, index);
83
+ return node && node.array[index & MASK];
84
+ }
85
+ return notSetValue;
86
+ };
87
+
88
+ // @pragma Modification
89
+
90
+ List.prototype.set = function set (index, value) {
91
+ return updateList(this, index, value);
92
+ };
93
+
94
+ List.prototype.remove = function remove (index) {
95
+ return !this.has(index)
96
+ ? this
97
+ : index === 0
98
+ ? this.shift()
99
+ : index === this.size - 1
100
+ ? this.pop()
101
+ : this.splice(index, 1);
102
+ };
103
+
104
+ List.prototype.insert = function insert (index, value) {
105
+ return this.splice(index, 0, value);
106
+ };
107
+
108
+ List.prototype.clear = function clear () {
109
+ if (this.size === 0) {
110
+ return this;
111
+ }
112
+ if (this.__ownerID) {
113
+ this.size = this._origin = this._capacity = 0;
114
+ this._level = SHIFT;
115
+ this._root = this._tail = this.__hash = undefined;
116
+ this.__altered = true;
117
+ return this;
118
+ }
119
+ return emptyList();
120
+ };
121
+
122
+ List.prototype.push = function push (/*...values*/) {
123
+ var values = arguments;
124
+ var oldSize = this.size;
125
+ return this.withMutations(function (list) {
126
+ setListBounds(list, 0, oldSize + values.length);
127
+ for (var ii = 0; ii < values.length; ii++) {
128
+ list.set(oldSize + ii, values[ii]);
129
+ }
130
+ });
131
+ };
132
+
133
+ List.prototype.pop = function pop () {
134
+ return setListBounds(this, 0, -1);
135
+ };
136
+
137
+ List.prototype.unshift = function unshift (/*...values*/) {
138
+ var values = arguments;
139
+ return this.withMutations(function (list) {
140
+ setListBounds(list, -values.length);
141
+ for (var ii = 0; ii < values.length; ii++) {
142
+ list.set(ii, values[ii]);
143
+ }
144
+ });
145
+ };
146
+
147
+ List.prototype.shift = function shift () {
148
+ return setListBounds(this, 1);
149
+ };
150
+
151
+ // @pragma Composition
152
+
153
+ List.prototype.concat = function concat (/*...collections*/) {
154
+ var arguments$1 = arguments;
155
+
156
+ var seqs = [];
157
+ for (var i = 0; i < arguments.length; i++) {
158
+ var argument = arguments$1[i];
159
+ var seq = IndexedCollection(
160
+ typeof argument !== 'string' && hasIterator(argument)
161
+ ? argument
162
+ : [argument]
163
+ );
164
+ if (seq.size !== 0) {
165
+ seqs.push(seq);
166
+ }
167
+ }
168
+ if (seqs.length === 0) {
169
+ return this;
170
+ }
171
+ if (this.size === 0 && !this.__ownerID && seqs.length === 1) {
172
+ return this.constructor(seqs[0]);
173
+ }
174
+ return this.withMutations(function (list) {
175
+ seqs.forEach(function (seq) { return seq.forEach(function (value) { return list.push(value); }); });
176
+ });
177
+ };
178
+
179
+ List.prototype.setSize = function setSize (size) {
180
+ return setListBounds(this, 0, size);
181
+ };
182
+
183
+ List.prototype.map = function map (mapper, context) {
184
+ var this$1$1 = this;
185
+
186
+ return this.withMutations(function (list) {
187
+ for (var i = 0; i < this$1$1.size; i++) {
188
+ list.set(i, mapper.call(context, list.get(i), i, this$1$1));
189
+ }
190
+ });
191
+ };
192
+
193
+ // @pragma Iteration
194
+
195
+ List.prototype.slice = function slice (begin, end) {
196
+ var size = this.size;
197
+ if (wholeSlice(begin, end, size)) {
198
+ return this;
199
+ }
200
+ return setListBounds(
201
+ this,
202
+ resolveBegin(begin, size),
203
+ resolveEnd(end, size)
204
+ );
205
+ };
206
+
207
+ List.prototype.__iterator = function __iterator (type, reverse) {
208
+ var index = reverse ? this.size : 0;
209
+ var values = iterateList(this, reverse);
210
+ return new Iterator(function () {
211
+ var value = values();
212
+ return value === DONE
213
+ ? iteratorDone()
214
+ : iteratorValue(type, reverse ? --index : index++, value);
215
+ });
216
+ };
217
+
218
+ List.prototype.__iterate = function __iterate (fn, reverse) {
219
+ var index = reverse ? this.size : 0;
220
+ var values = iterateList(this, reverse);
221
+ var value;
222
+ while ((value = values()) !== DONE) {
223
+ if (fn(value, reverse ? --index : index++, this) === false) {
224
+ break;
225
+ }
226
+ }
227
+ return index;
228
+ };
229
+
230
+ List.prototype.__ensureOwner = function __ensureOwner (ownerID) {
231
+ if (ownerID === this.__ownerID) {
232
+ return this;
233
+ }
234
+ if (!ownerID) {
235
+ if (this.size === 0) {
236
+ return emptyList();
237
+ }
238
+ this.__ownerID = ownerID;
239
+ this.__altered = false;
240
+ return this;
241
+ }
242
+ return makeList(
243
+ this._origin,
244
+ this._capacity,
245
+ this._level,
246
+ this._root,
247
+ this._tail,
248
+ ownerID,
249
+ this.__hash
250
+ );
251
+ };
252
+
253
+ return List;
254
+ }(IndexedCollection));
255
+
256
+ List.isList = isList;
257
+
258
+ var ListPrototype = List.prototype;
259
+ ListPrototype[IS_LIST_SYMBOL] = true;
260
+ ListPrototype[DELETE] = ListPrototype.remove;
261
+ ListPrototype.merge = ListPrototype.concat;
262
+ ListPrototype.setIn = setIn;
263
+ ListPrototype.deleteIn = ListPrototype.removeIn = deleteIn;
264
+ ListPrototype.update = update;
265
+ ListPrototype.updateIn = updateIn;
266
+ ListPrototype.mergeIn = mergeIn;
267
+ ListPrototype.mergeDeepIn = mergeDeepIn;
268
+ ListPrototype.withMutations = withMutations;
269
+ ListPrototype.wasAltered = wasAltered;
270
+ ListPrototype.asImmutable = asImmutable;
271
+ ListPrototype['@@transducer/init'] = ListPrototype.asMutable = asMutable;
272
+ ListPrototype['@@transducer/step'] = function (result, arr) {
273
+ return result.push(arr);
274
+ };
275
+ ListPrototype['@@transducer/result'] = function (obj) {
276
+ return obj.asImmutable();
277
+ };
278
+
279
+ var VNode = function VNode(array, ownerID) {
280
+ this.array = array;
281
+ this.ownerID = ownerID;
282
+ };
283
+
284
+ // TODO: seems like these methods are very similar
285
+
286
+ VNode.prototype.removeBefore = function removeBefore (ownerID, level, index) {
287
+ if (index === level ? 1 << level : this.array.length === 0) {
288
+ return this;
289
+ }
290
+ var originIndex = (index >>> level) & MASK;
291
+ if (originIndex >= this.array.length) {
292
+ return new VNode([], ownerID);
293
+ }
294
+ var removingFirst = originIndex === 0;
295
+ var newChild;
296
+ if (level > 0) {
297
+ var oldChild = this.array[originIndex];
298
+ newChild =
299
+ oldChild && oldChild.removeBefore(ownerID, level - SHIFT, index);
300
+ if (newChild === oldChild && removingFirst) {
301
+ return this;
302
+ }
303
+ }
304
+ if (removingFirst && !newChild) {
305
+ return this;
306
+ }
307
+ var editable = editableVNode(this, ownerID);
308
+ if (!removingFirst) {
309
+ for (var ii = 0; ii < originIndex; ii++) {
310
+ editable.array[ii] = undefined;
311
+ }
312
+ }
313
+ if (newChild) {
314
+ editable.array[originIndex] = newChild;
315
+ }
316
+ return editable;
317
+ };
318
+
319
+ VNode.prototype.removeAfter = function removeAfter (ownerID, level, index) {
320
+ if (index === (level ? 1 << level : 0) || this.array.length === 0) {
321
+ return this;
322
+ }
323
+ var sizeIndex = ((index - 1) >>> level) & MASK;
324
+ if (sizeIndex >= this.array.length) {
325
+ return this;
326
+ }
327
+
328
+ var newChild;
329
+ if (level > 0) {
330
+ var oldChild = this.array[sizeIndex];
331
+ newChild =
332
+ oldChild && oldChild.removeAfter(ownerID, level - SHIFT, index);
333
+ if (newChild === oldChild && sizeIndex === this.array.length - 1) {
334
+ return this;
335
+ }
336
+ }
337
+
338
+ var editable = editableVNode(this, ownerID);
339
+ editable.array.splice(sizeIndex + 1);
340
+ if (newChild) {
341
+ editable.array[sizeIndex] = newChild;
342
+ }
343
+ return editable;
344
+ };
345
+
346
+ var DONE = {};
347
+
348
+ function iterateList(list, reverse) {
349
+ var left = list._origin;
350
+ var right = list._capacity;
351
+ var tailPos = getTailOffset(right);
352
+ var tail = list._tail;
353
+
354
+ return iterateNodeOrLeaf(list._root, list._level, 0);
355
+
356
+ function iterateNodeOrLeaf(node, level, offset) {
357
+ return level === 0
358
+ ? iterateLeaf(node, offset)
359
+ : iterateNode(node, level, offset);
360
+ }
361
+
362
+ function iterateLeaf(node, offset) {
363
+ var array = offset === tailPos ? tail && tail.array : node && node.array;
364
+ var from = offset > left ? 0 : left - offset;
365
+ var to = right - offset;
366
+ if (to > SIZE) {
367
+ to = SIZE;
368
+ }
369
+ return function () {
370
+ if (from === to) {
371
+ return DONE;
372
+ }
373
+ var idx = reverse ? --to : from++;
374
+ return array && array[idx];
375
+ };
376
+ }
377
+
378
+ function iterateNode(node, level, offset) {
379
+ var values;
380
+ var array = node && node.array;
381
+ var from = offset > left ? 0 : (left - offset) >> level;
382
+ var to = ((right - offset) >> level) + 1;
383
+ if (to > SIZE) {
384
+ to = SIZE;
385
+ }
386
+ return function () {
387
+ while (true) {
388
+ if (values) {
389
+ var value = values();
390
+ if (value !== DONE) {
391
+ return value;
392
+ }
393
+ values = null;
394
+ }
395
+ if (from === to) {
396
+ return DONE;
397
+ }
398
+ var idx = reverse ? --to : from++;
399
+ values = iterateNodeOrLeaf(
400
+ array && array[idx],
401
+ level - SHIFT,
402
+ offset + (idx << level)
403
+ );
404
+ }
405
+ };
406
+ }
407
+ }
408
+
409
+ function makeList(origin, capacity, level, root, tail, ownerID, hash) {
410
+ var list = Object.create(ListPrototype);
411
+ list.size = capacity - origin;
412
+ list._origin = origin;
413
+ list._capacity = capacity;
414
+ list._level = level;
415
+ list._root = root;
416
+ list._tail = tail;
417
+ list.__ownerID = ownerID;
418
+ list.__hash = hash;
419
+ list.__altered = false;
420
+ return list;
421
+ }
422
+
423
+ var EMPTY_LIST;
424
+ function emptyList() {
425
+ return EMPTY_LIST || (EMPTY_LIST = makeList(0, 0, SHIFT));
426
+ }
427
+
428
+ function updateList(list, index, value) {
429
+ index = wrapIndex(list, index);
430
+
431
+ if (index !== index) {
432
+ return list;
433
+ }
434
+
435
+ if (index >= list.size || index < 0) {
436
+ return list.withMutations(function (list) {
437
+ index < 0
438
+ ? setListBounds(list, index).set(0, value)
439
+ : setListBounds(list, 0, index + 1).set(index, value);
440
+ });
441
+ }
442
+
443
+ index += list._origin;
444
+
445
+ var newTail = list._tail;
446
+ var newRoot = list._root;
447
+ var didAlter = MakeRef();
448
+ if (index >= getTailOffset(list._capacity)) {
449
+ newTail = updateVNode(newTail, list.__ownerID, 0, index, value, didAlter);
450
+ } else {
451
+ newRoot = updateVNode(
452
+ newRoot,
453
+ list.__ownerID,
454
+ list._level,
455
+ index,
456
+ value,
457
+ didAlter
458
+ );
459
+ }
460
+
461
+ if (!didAlter.value) {
462
+ return list;
463
+ }
464
+
465
+ if (list.__ownerID) {
466
+ list._root = newRoot;
467
+ list._tail = newTail;
468
+ list.__hash = undefined;
469
+ list.__altered = true;
470
+ return list;
471
+ }
472
+ return makeList(list._origin, list._capacity, list._level, newRoot, newTail);
473
+ }
474
+
475
+ function updateVNode(node, ownerID, level, index, value, didAlter) {
476
+ var idx = (index >>> level) & MASK;
477
+ var nodeHas = node && idx < node.array.length;
478
+ if (!nodeHas && value === undefined) {
479
+ return node;
480
+ }
481
+
482
+ var newNode;
483
+
484
+ if (level > 0) {
485
+ var lowerNode = node && node.array[idx];
486
+ var newLowerNode = updateVNode(
487
+ lowerNode,
488
+ ownerID,
489
+ level - SHIFT,
490
+ index,
491
+ value,
492
+ didAlter
493
+ );
494
+ if (newLowerNode === lowerNode) {
495
+ return node;
496
+ }
497
+ newNode = editableVNode(node, ownerID);
498
+ newNode.array[idx] = newLowerNode;
499
+ return newNode;
500
+ }
501
+
502
+ if (nodeHas && node.array[idx] === value) {
503
+ return node;
504
+ }
505
+
506
+ if (didAlter) {
507
+ SetRef(didAlter);
508
+ }
509
+
510
+ newNode = editableVNode(node, ownerID);
511
+ if (value === undefined && idx === newNode.array.length - 1) {
512
+ newNode.array.pop();
513
+ } else {
514
+ newNode.array[idx] = value;
515
+ }
516
+ return newNode;
517
+ }
518
+
519
+ function editableVNode(node, ownerID) {
520
+ if (ownerID && node && ownerID === node.ownerID) {
521
+ return node;
522
+ }
523
+ return new VNode(node ? node.array.slice() : [], ownerID);
524
+ }
525
+
526
+ function listNodeFor(list, rawIndex) {
527
+ if (rawIndex >= getTailOffset(list._capacity)) {
528
+ return list._tail;
529
+ }
530
+ if (rawIndex < 1 << (list._level + SHIFT)) {
531
+ var node = list._root;
532
+ var level = list._level;
533
+ while (node && level > 0) {
534
+ node = node.array[(rawIndex >>> level) & MASK];
535
+ level -= SHIFT;
536
+ }
537
+ return node;
538
+ }
539
+ }
540
+
541
+ function setListBounds(list, begin, end) {
542
+ // Sanitize begin & end using this shorthand for ToInt32(argument)
543
+ // http://www.ecma-international.org/ecma-262/6.0/#sec-toint32
544
+ if (begin !== undefined) {
545
+ begin |= 0;
546
+ }
547
+ if (end !== undefined) {
548
+ end |= 0;
549
+ }
550
+ var owner = list.__ownerID || new OwnerID();
551
+ var oldOrigin = list._origin;
552
+ var oldCapacity = list._capacity;
553
+ var newOrigin = oldOrigin + begin;
554
+ var newCapacity =
555
+ end === undefined
556
+ ? oldCapacity
557
+ : end < 0
558
+ ? oldCapacity + end
559
+ : oldOrigin + end;
560
+ if (newOrigin === oldOrigin && newCapacity === oldCapacity) {
561
+ return list;
562
+ }
563
+
564
+ // If it's going to end after it starts, it's empty.
565
+ if (newOrigin >= newCapacity) {
566
+ return list.clear();
567
+ }
568
+
569
+ var newLevel = list._level;
570
+ var newRoot = list._root;
571
+
572
+ // New origin might need creating a higher root.
573
+ var offsetShift = 0;
574
+ while (newOrigin + offsetShift < 0) {
575
+ newRoot = new VNode(
576
+ newRoot && newRoot.array.length ? [undefined, newRoot] : [],
577
+ owner
578
+ );
579
+ newLevel += SHIFT;
580
+ offsetShift += 1 << newLevel;
581
+ }
582
+ if (offsetShift) {
583
+ newOrigin += offsetShift;
584
+ oldOrigin += offsetShift;
585
+ newCapacity += offsetShift;
586
+ oldCapacity += offsetShift;
587
+ }
588
+
589
+ var oldTailOffset = getTailOffset(oldCapacity);
590
+ var newTailOffset = getTailOffset(newCapacity);
591
+
592
+ // New size might need creating a higher root.
593
+ while (newTailOffset >= 1 << (newLevel + SHIFT)) {
594
+ newRoot = new VNode(
595
+ newRoot && newRoot.array.length ? [newRoot] : [],
596
+ owner
597
+ );
598
+ newLevel += SHIFT;
599
+ }
600
+
601
+ // Locate or create the new tail.
602
+ var oldTail = list._tail;
603
+ var newTail =
604
+ newTailOffset < oldTailOffset
605
+ ? listNodeFor(list, newCapacity - 1)
606
+ : newTailOffset > oldTailOffset
607
+ ? new VNode([], owner)
608
+ : oldTail;
609
+
610
+ // Merge Tail into tree.
611
+ if (
612
+ oldTail &&
613
+ newTailOffset > oldTailOffset &&
614
+ newOrigin < oldCapacity &&
615
+ oldTail.array.length
616
+ ) {
617
+ newRoot = editableVNode(newRoot, owner);
618
+ var node = newRoot;
619
+ for (var level = newLevel; level > SHIFT; level -= SHIFT) {
620
+ var idx = (oldTailOffset >>> level) & MASK;
621
+ node = node.array[idx] = editableVNode(node.array[idx], owner);
622
+ }
623
+ node.array[(oldTailOffset >>> SHIFT) & MASK] = oldTail;
624
+ }
625
+
626
+ // If the size has been reduced, there's a chance the tail needs to be trimmed.
627
+ if (newCapacity < oldCapacity) {
628
+ newTail = newTail && newTail.removeAfter(owner, 0, newCapacity);
629
+ }
630
+
631
+ // If the new origin is within the tail, then we do not need a root.
632
+ if (newOrigin >= newTailOffset) {
633
+ newOrigin -= newTailOffset;
634
+ newCapacity -= newTailOffset;
635
+ newLevel = SHIFT;
636
+ newRoot = null;
637
+ newTail = newTail && newTail.removeBefore(owner, 0, newOrigin);
638
+
639
+ // Otherwise, if the root has been trimmed, garbage collect.
640
+ } else if (newOrigin > oldOrigin || newTailOffset < oldTailOffset) {
641
+ offsetShift = 0;
642
+
643
+ // Identify the new top root node of the subtree of the old root.
644
+ while (newRoot) {
645
+ var beginIndex = (newOrigin >>> newLevel) & MASK;
646
+ if ((beginIndex !== newTailOffset >>> newLevel) & MASK) {
647
+ break;
648
+ }
649
+ if (beginIndex) {
650
+ offsetShift += (1 << newLevel) * beginIndex;
651
+ }
652
+ newLevel -= SHIFT;
653
+ newRoot = newRoot.array[beginIndex];
654
+ }
655
+
656
+ // Trim the new sides of the new root.
657
+ if (newRoot && newOrigin > oldOrigin) {
658
+ newRoot = newRoot.removeBefore(owner, newLevel, newOrigin - offsetShift);
659
+ }
660
+ if (newRoot && newTailOffset < oldTailOffset) {
661
+ newRoot = newRoot.removeAfter(
662
+ owner,
663
+ newLevel,
664
+ newTailOffset - offsetShift
665
+ );
666
+ }
667
+ if (offsetShift) {
668
+ newOrigin -= offsetShift;
669
+ newCapacity -= offsetShift;
670
+ }
671
+ }
672
+
673
+ if (list.__ownerID) {
674
+ list.size = newCapacity - newOrigin;
675
+ list._origin = newOrigin;
676
+ list._capacity = newCapacity;
677
+ list._level = newLevel;
678
+ list._root = newRoot;
679
+ list._tail = newTail;
680
+ list.__hash = undefined;
681
+ list.__altered = true;
682
+ return list;
683
+ }
684
+ return makeList(newOrigin, newCapacity, newLevel, newRoot, newTail);
685
+ }
686
+
687
+ function getTailOffset(size) {
688
+ return size < SIZE ? 0 : ((size - 1) >>> SHIFT) << SHIFT;
689
+ }
690
+
691
+ export { List, emptyList };