immutable 5.0.0-beta.3 → 5.0.0-beta.4
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.
- package/README.md +3 -3
- package/dist/es/Collection.js +74 -0
- package/dist/es/CollectionImpl.js +781 -0
- package/dist/es/Hash.js +259 -0
- package/dist/es/Immutable.js +73 -0
- package/dist/es/Iterator.js +105 -0
- package/dist/es/List.js +691 -0
- package/dist/es/Map.js +832 -0
- package/dist/es/Math.js +44 -0
- package/dist/es/Operations.js +952 -0
- package/dist/es/OrderedMap.js +194 -0
- package/dist/es/OrderedSet.js +90 -0
- package/dist/es/PairSorting.js +29 -0
- package/dist/es/Range.js +167 -0
- package/dist/es/Record.js +290 -0
- package/dist/es/Repeat.js +129 -0
- package/dist/es/Seq.js +396 -0
- package/dist/es/Set.js +277 -0
- package/dist/es/Stack.js +259 -0
- package/dist/es/TrieUtils.js +115 -0
- package/dist/es/fromJS.js +73 -0
- package/dist/es/functional/get.js +37 -0
- package/dist/es/functional/getIn.js +40 -0
- package/dist/es/functional/has.js +34 -0
- package/dist/es/functional/hasIn.js +31 -0
- package/dist/es/functional/merge.js +136 -0
- package/dist/es/functional/remove.js +55 -0
- package/dist/es/functional/removeIn.js +31 -0
- package/dist/es/functional/set.js +51 -0
- package/dist/es/functional/setIn.js +31 -0
- package/dist/es/functional/update.js +30 -0
- package/dist/es/functional/updateIn.js +93 -0
- package/dist/es/is.js +107 -0
- package/dist/es/methods/asImmutable.js +28 -0
- package/dist/es/methods/asMutable.js +30 -0
- package/dist/es/methods/deleteIn.js +30 -0
- package/dist/es/methods/getIn.js +30 -0
- package/dist/es/methods/hasIn.js +30 -0
- package/dist/es/methods/merge.js +78 -0
- package/dist/es/methods/mergeDeep.js +40 -0
- package/dist/es/methods/mergeDeepIn.js +36 -0
- package/dist/es/methods/mergeIn.js +35 -0
- package/dist/es/methods/setIn.js +30 -0
- package/dist/es/methods/toObject.js +35 -0
- package/dist/es/methods/update.js +32 -0
- package/dist/es/methods/updateIn.js +30 -0
- package/dist/es/methods/wasAltered.js +28 -0
- package/dist/es/methods/withMutations.js +30 -0
- package/dist/es/package.json.js +26 -0
- package/dist/es/predicates/isAssociative.js +31 -0
- package/dist/es/predicates/isCollection.js +30 -0
- package/dist/es/predicates/isImmutable.js +31 -0
- package/dist/es/predicates/isIndexed.js +30 -0
- package/dist/es/predicates/isKeyed.js +30 -0
- package/dist/es/predicates/isList.js +30 -0
- package/dist/es/predicates/isMap.js +30 -0
- package/dist/es/predicates/isOrdered.js +30 -0
- package/dist/es/predicates/isOrderedMap.js +31 -0
- package/dist/es/predicates/isOrderedSet.js +31 -0
- package/dist/es/predicates/isRecord.js +30 -0
- package/dist/es/predicates/isSeq.js +30 -0
- package/dist/es/predicates/isSet.js +30 -0
- package/dist/es/predicates/isStack.js +30 -0
- package/dist/es/predicates/isValueObject.js +32 -0
- package/dist/es/toJS.js +53 -0
- package/dist/es/utils/arrCopy.js +34 -0
- package/dist/es/utils/assertNotInfinite.js +33 -0
- package/dist/es/utils/coerceKeyPath.js +39 -0
- package/dist/es/utils/deepEqual.js +98 -0
- package/dist/es/utils/hasOwnProperty.js +26 -0
- package/dist/es/utils/invariant.js +28 -0
- package/dist/es/utils/isArrayLike.js +43 -0
- package/dist/es/utils/isDataStructure.js +38 -0
- package/dist/es/utils/isPlainObj.js +51 -0
- package/dist/es/utils/mixin.js +34 -0
- package/dist/es/utils/quoteString.js +32 -0
- package/dist/es/utils/shallowCopy.js +40 -0
- package/dist/immutable.d.ts +131 -14
- package/dist/immutable.js +1 -59
- package/dist/immutable.min.js +32 -32
- package/package.json +2 -3
- package/dist/immutable.es.js +0 -5965
|
@@ -0,0 +1,194 @@
|
|
|
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 { KeyedCollection } from './Collection.js';
|
|
25
|
+
import { IS_ORDERED_SYMBOL } from './predicates/isOrdered.js';
|
|
26
|
+
import { isOrderedMap } from './predicates/isOrderedMap.js';
|
|
27
|
+
import { emptyMap, Map } from './Map.js';
|
|
28
|
+
import { emptyList } from './List.js';
|
|
29
|
+
import { DELETE, NOT_SET, SIZE } from './TrieUtils.js';
|
|
30
|
+
import assertNotInfinite from './utils/assertNotInfinite.js';
|
|
31
|
+
|
|
32
|
+
var OrderedMap = /*@__PURE__*/(function (Map) {
|
|
33
|
+
function OrderedMap(value) {
|
|
34
|
+
return value === undefined || value === null
|
|
35
|
+
? emptyOrderedMap()
|
|
36
|
+
: isOrderedMap(value)
|
|
37
|
+
? value
|
|
38
|
+
: emptyOrderedMap().withMutations(function (map) {
|
|
39
|
+
var iter = KeyedCollection(value);
|
|
40
|
+
assertNotInfinite(iter.size);
|
|
41
|
+
iter.forEach(function (v, k) { return map.set(k, v); });
|
|
42
|
+
});
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
if ( Map ) OrderedMap.__proto__ = Map;
|
|
46
|
+
OrderedMap.prototype = Object.create( Map && Map.prototype );
|
|
47
|
+
OrderedMap.prototype.constructor = OrderedMap;
|
|
48
|
+
|
|
49
|
+
OrderedMap.of = function of (/*...values*/) {
|
|
50
|
+
return this(arguments);
|
|
51
|
+
};
|
|
52
|
+
|
|
53
|
+
OrderedMap.prototype.toString = function toString () {
|
|
54
|
+
return this.__toString('OrderedMap {', '}');
|
|
55
|
+
};
|
|
56
|
+
|
|
57
|
+
// @pragma Access
|
|
58
|
+
|
|
59
|
+
OrderedMap.prototype.get = function get (k, notSetValue) {
|
|
60
|
+
var index = this._map.get(k);
|
|
61
|
+
return index !== undefined ? this._list.get(index)[1] : notSetValue;
|
|
62
|
+
};
|
|
63
|
+
|
|
64
|
+
// @pragma Modification
|
|
65
|
+
|
|
66
|
+
OrderedMap.prototype.clear = function clear () {
|
|
67
|
+
if (this.size === 0) {
|
|
68
|
+
return this;
|
|
69
|
+
}
|
|
70
|
+
if (this.__ownerID) {
|
|
71
|
+
this.size = 0;
|
|
72
|
+
this._map.clear();
|
|
73
|
+
this._list.clear();
|
|
74
|
+
this.__altered = true;
|
|
75
|
+
return this;
|
|
76
|
+
}
|
|
77
|
+
return emptyOrderedMap();
|
|
78
|
+
};
|
|
79
|
+
|
|
80
|
+
OrderedMap.prototype.set = function set (k, v) {
|
|
81
|
+
return updateOrderedMap(this, k, v);
|
|
82
|
+
};
|
|
83
|
+
|
|
84
|
+
OrderedMap.prototype.remove = function remove (k) {
|
|
85
|
+
return updateOrderedMap(this, k, NOT_SET);
|
|
86
|
+
};
|
|
87
|
+
|
|
88
|
+
OrderedMap.prototype.__iterate = function __iterate (fn, reverse) {
|
|
89
|
+
var this$1$1 = this;
|
|
90
|
+
|
|
91
|
+
return this._list.__iterate(
|
|
92
|
+
function (entry) { return entry && fn(entry[1], entry[0], this$1$1); },
|
|
93
|
+
reverse
|
|
94
|
+
);
|
|
95
|
+
};
|
|
96
|
+
|
|
97
|
+
OrderedMap.prototype.__iterator = function __iterator (type, reverse) {
|
|
98
|
+
return this._list.fromEntrySeq().__iterator(type, reverse);
|
|
99
|
+
};
|
|
100
|
+
|
|
101
|
+
OrderedMap.prototype.__ensureOwner = function __ensureOwner (ownerID) {
|
|
102
|
+
if (ownerID === this.__ownerID) {
|
|
103
|
+
return this;
|
|
104
|
+
}
|
|
105
|
+
var newMap = this._map.__ensureOwner(ownerID);
|
|
106
|
+
var newList = this._list.__ensureOwner(ownerID);
|
|
107
|
+
if (!ownerID) {
|
|
108
|
+
if (this.size === 0) {
|
|
109
|
+
return emptyOrderedMap();
|
|
110
|
+
}
|
|
111
|
+
this.__ownerID = ownerID;
|
|
112
|
+
this.__altered = false;
|
|
113
|
+
this._map = newMap;
|
|
114
|
+
this._list = newList;
|
|
115
|
+
return this;
|
|
116
|
+
}
|
|
117
|
+
return makeOrderedMap(newMap, newList, ownerID, this.__hash);
|
|
118
|
+
};
|
|
119
|
+
|
|
120
|
+
return OrderedMap;
|
|
121
|
+
}(Map));
|
|
122
|
+
|
|
123
|
+
OrderedMap.isOrderedMap = isOrderedMap;
|
|
124
|
+
|
|
125
|
+
OrderedMap.prototype[IS_ORDERED_SYMBOL] = true;
|
|
126
|
+
OrderedMap.prototype[DELETE] = OrderedMap.prototype.remove;
|
|
127
|
+
|
|
128
|
+
function makeOrderedMap(map, list, ownerID, hash) {
|
|
129
|
+
var omap = Object.create(OrderedMap.prototype);
|
|
130
|
+
omap.size = map ? map.size : 0;
|
|
131
|
+
omap._map = map;
|
|
132
|
+
omap._list = list;
|
|
133
|
+
omap.__ownerID = ownerID;
|
|
134
|
+
omap.__hash = hash;
|
|
135
|
+
omap.__altered = false;
|
|
136
|
+
return omap;
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
var EMPTY_ORDERED_MAP;
|
|
140
|
+
function emptyOrderedMap() {
|
|
141
|
+
return (
|
|
142
|
+
EMPTY_ORDERED_MAP ||
|
|
143
|
+
(EMPTY_ORDERED_MAP = makeOrderedMap(emptyMap(), emptyList()))
|
|
144
|
+
);
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
function updateOrderedMap(omap, k, v) {
|
|
148
|
+
var map = omap._map;
|
|
149
|
+
var list = omap._list;
|
|
150
|
+
var i = map.get(k);
|
|
151
|
+
var has = i !== undefined;
|
|
152
|
+
var newMap;
|
|
153
|
+
var newList;
|
|
154
|
+
if (v === NOT_SET) {
|
|
155
|
+
// removed
|
|
156
|
+
if (!has) {
|
|
157
|
+
return omap;
|
|
158
|
+
}
|
|
159
|
+
if (list.size >= SIZE && list.size >= map.size * 2) {
|
|
160
|
+
newList = list.filter(function (entry, idx) { return entry !== undefined && i !== idx; });
|
|
161
|
+
newMap = newList
|
|
162
|
+
.toKeyedSeq()
|
|
163
|
+
.map(function (entry) { return entry[0]; })
|
|
164
|
+
.flip()
|
|
165
|
+
.toMap();
|
|
166
|
+
if (omap.__ownerID) {
|
|
167
|
+
newMap.__ownerID = newList.__ownerID = omap.__ownerID;
|
|
168
|
+
}
|
|
169
|
+
} else {
|
|
170
|
+
newMap = map.remove(k);
|
|
171
|
+
newList = i === list.size - 1 ? list.pop() : list.set(i, undefined);
|
|
172
|
+
}
|
|
173
|
+
} else if (has) {
|
|
174
|
+
if (v === list.get(i)[1]) {
|
|
175
|
+
return omap;
|
|
176
|
+
}
|
|
177
|
+
newMap = map;
|
|
178
|
+
newList = list.set(i, [k, v]);
|
|
179
|
+
} else {
|
|
180
|
+
newMap = map.set(k, list.size);
|
|
181
|
+
newList = list.set(list.size, [k, v]);
|
|
182
|
+
}
|
|
183
|
+
if (omap.__ownerID) {
|
|
184
|
+
omap.size = newMap.size;
|
|
185
|
+
omap._map = newMap;
|
|
186
|
+
omap._list = newList;
|
|
187
|
+
omap.__hash = undefined;
|
|
188
|
+
omap.__altered = true;
|
|
189
|
+
return omap;
|
|
190
|
+
}
|
|
191
|
+
return makeOrderedMap(newMap, newList);
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
export { OrderedMap, emptyOrderedMap };
|
|
@@ -0,0 +1,90 @@
|
|
|
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 { SetCollection, KeyedCollection } from './Collection.js';
|
|
25
|
+
import { IS_ORDERED_SYMBOL } from './predicates/isOrdered.js';
|
|
26
|
+
import { isOrderedSet } from './predicates/isOrderedSet.js';
|
|
27
|
+
import { IndexedCollectionPrototype } from './CollectionImpl.js';
|
|
28
|
+
import { Set } from './Set.js';
|
|
29
|
+
import { emptyOrderedMap } from './OrderedMap.js';
|
|
30
|
+
import assertNotInfinite from './utils/assertNotInfinite.js';
|
|
31
|
+
|
|
32
|
+
var OrderedSet = /*@__PURE__*/(function (Set) {
|
|
33
|
+
function OrderedSet(value) {
|
|
34
|
+
return value === undefined || value === null
|
|
35
|
+
? emptyOrderedSet()
|
|
36
|
+
: isOrderedSet(value)
|
|
37
|
+
? value
|
|
38
|
+
: emptyOrderedSet().withMutations(function (set) {
|
|
39
|
+
var iter = SetCollection(value);
|
|
40
|
+
assertNotInfinite(iter.size);
|
|
41
|
+
iter.forEach(function (v) { return set.add(v); });
|
|
42
|
+
});
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
if ( Set ) OrderedSet.__proto__ = Set;
|
|
46
|
+
OrderedSet.prototype = Object.create( Set && Set.prototype );
|
|
47
|
+
OrderedSet.prototype.constructor = OrderedSet;
|
|
48
|
+
|
|
49
|
+
OrderedSet.of = function of (/*...values*/) {
|
|
50
|
+
return this(arguments);
|
|
51
|
+
};
|
|
52
|
+
|
|
53
|
+
OrderedSet.fromKeys = function fromKeys (value) {
|
|
54
|
+
return this(KeyedCollection(value).keySeq());
|
|
55
|
+
};
|
|
56
|
+
|
|
57
|
+
OrderedSet.prototype.toString = function toString () {
|
|
58
|
+
return this.__toString('OrderedSet {', '}');
|
|
59
|
+
};
|
|
60
|
+
|
|
61
|
+
return OrderedSet;
|
|
62
|
+
}(Set));
|
|
63
|
+
|
|
64
|
+
OrderedSet.isOrderedSet = isOrderedSet;
|
|
65
|
+
|
|
66
|
+
var OrderedSetPrototype = OrderedSet.prototype;
|
|
67
|
+
OrderedSetPrototype[IS_ORDERED_SYMBOL] = true;
|
|
68
|
+
OrderedSetPrototype.zip = IndexedCollectionPrototype.zip;
|
|
69
|
+
OrderedSetPrototype.zipWith = IndexedCollectionPrototype.zipWith;
|
|
70
|
+
OrderedSetPrototype.zipAll = IndexedCollectionPrototype.zipAll;
|
|
71
|
+
|
|
72
|
+
OrderedSetPrototype.__empty = emptyOrderedSet;
|
|
73
|
+
OrderedSetPrototype.__make = makeOrderedSet;
|
|
74
|
+
|
|
75
|
+
function makeOrderedSet(map, ownerID) {
|
|
76
|
+
var set = Object.create(OrderedSetPrototype);
|
|
77
|
+
set.size = map ? map.size : 0;
|
|
78
|
+
set._map = map;
|
|
79
|
+
set.__ownerID = ownerID;
|
|
80
|
+
return set;
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
var EMPTY_ORDERED_SET;
|
|
84
|
+
function emptyOrderedSet() {
|
|
85
|
+
return (
|
|
86
|
+
EMPTY_ORDERED_SET || (EMPTY_ORDERED_SET = makeOrderedSet(emptyOrderedMap()))
|
|
87
|
+
);
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
export { OrderedSet };
|
|
@@ -0,0 +1,29 @@
|
|
|
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
|
+
var PairSorting = {
|
|
25
|
+
LeftThenRight: -1,
|
|
26
|
+
RightThenLeft: +1,
|
|
27
|
+
};
|
|
28
|
+
|
|
29
|
+
export { PairSorting };
|
package/dist/es/Range.js
ADDED
|
@@ -0,0 +1,167 @@
|
|
|
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, wholeSlice, resolveBegin, resolveEnd } from './TrieUtils.js';
|
|
25
|
+
import { IndexedSeq } from './Seq.js';
|
|
26
|
+
import { Iterator, iteratorDone, iteratorValue } from './Iterator.js';
|
|
27
|
+
import invariant from './utils/invariant.js';
|
|
28
|
+
import deepEqual from './utils/deepEqual.js';
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* Returns a lazy seq of nums from start (inclusive) to end
|
|
32
|
+
* (exclusive), by step, where start defaults to 0, step to 1, and end to
|
|
33
|
+
* infinity. When start is equal to end, returns empty list.
|
|
34
|
+
*/
|
|
35
|
+
var Range = /*@__PURE__*/(function (IndexedSeq) {
|
|
36
|
+
function Range(start, end, step) {
|
|
37
|
+
if (!(this instanceof Range)) {
|
|
38
|
+
return new Range(start, end, step);
|
|
39
|
+
}
|
|
40
|
+
invariant(step !== 0, 'Cannot step a Range by 0');
|
|
41
|
+
start = start || 0;
|
|
42
|
+
if (end === undefined) {
|
|
43
|
+
end = Infinity;
|
|
44
|
+
}
|
|
45
|
+
step = step === undefined ? 1 : Math.abs(step);
|
|
46
|
+
if (end < start) {
|
|
47
|
+
step = -step;
|
|
48
|
+
}
|
|
49
|
+
this._start = start;
|
|
50
|
+
this._end = end;
|
|
51
|
+
this._step = step;
|
|
52
|
+
this.size = Math.max(0, Math.ceil((end - start) / step - 1) + 1);
|
|
53
|
+
if (this.size === 0) {
|
|
54
|
+
if (EMPTY_RANGE) {
|
|
55
|
+
return EMPTY_RANGE;
|
|
56
|
+
}
|
|
57
|
+
EMPTY_RANGE = this;
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
if ( IndexedSeq ) Range.__proto__ = IndexedSeq;
|
|
62
|
+
Range.prototype = Object.create( IndexedSeq && IndexedSeq.prototype );
|
|
63
|
+
Range.prototype.constructor = Range;
|
|
64
|
+
|
|
65
|
+
Range.prototype.toString = function toString () {
|
|
66
|
+
if (this.size === 0) {
|
|
67
|
+
return 'Range []';
|
|
68
|
+
}
|
|
69
|
+
return (
|
|
70
|
+
'Range [ ' +
|
|
71
|
+
this._start +
|
|
72
|
+
'...' +
|
|
73
|
+
this._end +
|
|
74
|
+
(this._step !== 1 ? ' by ' + this._step : '') +
|
|
75
|
+
' ]'
|
|
76
|
+
);
|
|
77
|
+
};
|
|
78
|
+
|
|
79
|
+
Range.prototype.get = function get (index, notSetValue) {
|
|
80
|
+
return this.has(index)
|
|
81
|
+
? this._start + wrapIndex(this, index) * this._step
|
|
82
|
+
: notSetValue;
|
|
83
|
+
};
|
|
84
|
+
|
|
85
|
+
Range.prototype.includes = function includes (searchValue) {
|
|
86
|
+
var possibleIndex = (searchValue - this._start) / this._step;
|
|
87
|
+
return (
|
|
88
|
+
possibleIndex >= 0 &&
|
|
89
|
+
possibleIndex < this.size &&
|
|
90
|
+
possibleIndex === Math.floor(possibleIndex)
|
|
91
|
+
);
|
|
92
|
+
};
|
|
93
|
+
|
|
94
|
+
Range.prototype.slice = function slice (begin, end) {
|
|
95
|
+
if (wholeSlice(begin, end, this.size)) {
|
|
96
|
+
return this;
|
|
97
|
+
}
|
|
98
|
+
begin = resolveBegin(begin, this.size);
|
|
99
|
+
end = resolveEnd(end, this.size);
|
|
100
|
+
if (end <= begin) {
|
|
101
|
+
return new Range(0, 0);
|
|
102
|
+
}
|
|
103
|
+
return new Range(
|
|
104
|
+
this.get(begin, this._end),
|
|
105
|
+
this.get(end, this._end),
|
|
106
|
+
this._step
|
|
107
|
+
);
|
|
108
|
+
};
|
|
109
|
+
|
|
110
|
+
Range.prototype.indexOf = function indexOf (searchValue) {
|
|
111
|
+
var offsetValue = searchValue - this._start;
|
|
112
|
+
if (offsetValue % this._step === 0) {
|
|
113
|
+
var index = offsetValue / this._step;
|
|
114
|
+
if (index >= 0 && index < this.size) {
|
|
115
|
+
return index;
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
return -1;
|
|
119
|
+
};
|
|
120
|
+
|
|
121
|
+
Range.prototype.lastIndexOf = function lastIndexOf (searchValue) {
|
|
122
|
+
return this.indexOf(searchValue);
|
|
123
|
+
};
|
|
124
|
+
|
|
125
|
+
Range.prototype.__iterate = function __iterate (fn, reverse) {
|
|
126
|
+
var size = this.size;
|
|
127
|
+
var step = this._step;
|
|
128
|
+
var value = reverse ? this._start + (size - 1) * step : this._start;
|
|
129
|
+
var i = 0;
|
|
130
|
+
while (i !== size) {
|
|
131
|
+
if (fn(value, reverse ? size - ++i : i++, this) === false) {
|
|
132
|
+
break;
|
|
133
|
+
}
|
|
134
|
+
value += reverse ? -step : step;
|
|
135
|
+
}
|
|
136
|
+
return i;
|
|
137
|
+
};
|
|
138
|
+
|
|
139
|
+
Range.prototype.__iterator = function __iterator (type, reverse) {
|
|
140
|
+
var size = this.size;
|
|
141
|
+
var step = this._step;
|
|
142
|
+
var value = reverse ? this._start + (size - 1) * step : this._start;
|
|
143
|
+
var i = 0;
|
|
144
|
+
return new Iterator(function () {
|
|
145
|
+
if (i === size) {
|
|
146
|
+
return iteratorDone();
|
|
147
|
+
}
|
|
148
|
+
var v = value;
|
|
149
|
+
value += reverse ? -step : step;
|
|
150
|
+
return iteratorValue(type, reverse ? size - ++i : i++, v);
|
|
151
|
+
});
|
|
152
|
+
};
|
|
153
|
+
|
|
154
|
+
Range.prototype.equals = function equals (other) {
|
|
155
|
+
return other instanceof Range
|
|
156
|
+
? this._start === other._start &&
|
|
157
|
+
this._end === other._end &&
|
|
158
|
+
this._step === other._step
|
|
159
|
+
: deepEqual(this, other);
|
|
160
|
+
};
|
|
161
|
+
|
|
162
|
+
return Range;
|
|
163
|
+
}(IndexedSeq));
|
|
164
|
+
|
|
165
|
+
var EMPTY_RANGE;
|
|
166
|
+
|
|
167
|
+
export { Range };
|