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