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.
- package/README.md +11 -7
- 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 +136 -21
- package/dist/immutable.js +1 -59
- package/dist/immutable.min.js +32 -32
- package/package.json +5 -3
- package/dist/immutable.es.js +0 -5965
package/dist/es/Seq.js
ADDED
|
@@ -0,0 +1,396 @@
|
|
|
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 } from './TrieUtils.js';
|
|
25
|
+
import { Collection } from './Collection.js';
|
|
26
|
+
import { isSeq, IS_SEQ_SYMBOL } from './predicates/isSeq.js';
|
|
27
|
+
import { isImmutable } from './predicates/isImmutable.js';
|
|
28
|
+
import { isCollection } from './predicates/isCollection.js';
|
|
29
|
+
import { isKeyed } from './predicates/isKeyed.js';
|
|
30
|
+
import { isAssociative } from './predicates/isAssociative.js';
|
|
31
|
+
import { isRecord } from './predicates/isRecord.js';
|
|
32
|
+
import { IS_ORDERED_SYMBOL } from './predicates/isOrdered.js';
|
|
33
|
+
import { Iterator, iteratorDone, iteratorValue, hasIterator, isEntriesIterable, isKeysIterable, getIterator, isIterator } from './Iterator.js';
|
|
34
|
+
import hasOwnProperty from './utils/hasOwnProperty.js';
|
|
35
|
+
import isArrayLike from './utils/isArrayLike.js';
|
|
36
|
+
|
|
37
|
+
var Seq = /*@__PURE__*/(function (Collection) {
|
|
38
|
+
function Seq(value) {
|
|
39
|
+
return value === undefined || value === null
|
|
40
|
+
? emptySequence()
|
|
41
|
+
: isImmutable(value)
|
|
42
|
+
? value.toSeq()
|
|
43
|
+
: seqFromValue(value);
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
if ( Collection ) Seq.__proto__ = Collection;
|
|
47
|
+
Seq.prototype = Object.create( Collection && Collection.prototype );
|
|
48
|
+
Seq.prototype.constructor = Seq;
|
|
49
|
+
|
|
50
|
+
Seq.prototype.toSeq = function toSeq () {
|
|
51
|
+
return this;
|
|
52
|
+
};
|
|
53
|
+
|
|
54
|
+
Seq.prototype.toString = function toString () {
|
|
55
|
+
return this.__toString('Seq {', '}');
|
|
56
|
+
};
|
|
57
|
+
|
|
58
|
+
Seq.prototype.cacheResult = function cacheResult () {
|
|
59
|
+
if (!this._cache && this.__iterateUncached) {
|
|
60
|
+
this._cache = this.entrySeq().toArray();
|
|
61
|
+
this.size = this._cache.length;
|
|
62
|
+
}
|
|
63
|
+
return this;
|
|
64
|
+
};
|
|
65
|
+
|
|
66
|
+
// abstract __iterateUncached(fn, reverse)
|
|
67
|
+
|
|
68
|
+
Seq.prototype.__iterate = function __iterate (fn, reverse) {
|
|
69
|
+
var cache = this._cache;
|
|
70
|
+
if (cache) {
|
|
71
|
+
var size = cache.length;
|
|
72
|
+
var i = 0;
|
|
73
|
+
while (i !== size) {
|
|
74
|
+
var entry = cache[reverse ? size - ++i : i++];
|
|
75
|
+
if (fn(entry[1], entry[0], this) === false) {
|
|
76
|
+
break;
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
return i;
|
|
80
|
+
}
|
|
81
|
+
return this.__iterateUncached(fn, reverse);
|
|
82
|
+
};
|
|
83
|
+
|
|
84
|
+
// abstract __iteratorUncached(type, reverse)
|
|
85
|
+
|
|
86
|
+
Seq.prototype.__iterator = function __iterator (type, reverse) {
|
|
87
|
+
var cache = this._cache;
|
|
88
|
+
if (cache) {
|
|
89
|
+
var size = cache.length;
|
|
90
|
+
var i = 0;
|
|
91
|
+
return new Iterator(function () {
|
|
92
|
+
if (i === size) {
|
|
93
|
+
return iteratorDone();
|
|
94
|
+
}
|
|
95
|
+
var entry = cache[reverse ? size - ++i : i++];
|
|
96
|
+
return iteratorValue(type, entry[0], entry[1]);
|
|
97
|
+
});
|
|
98
|
+
}
|
|
99
|
+
return this.__iteratorUncached(type, reverse);
|
|
100
|
+
};
|
|
101
|
+
|
|
102
|
+
return Seq;
|
|
103
|
+
}(Collection));
|
|
104
|
+
|
|
105
|
+
var KeyedSeq = /*@__PURE__*/(function (Seq) {
|
|
106
|
+
function KeyedSeq(value) {
|
|
107
|
+
return value === undefined || value === null
|
|
108
|
+
? emptySequence().toKeyedSeq()
|
|
109
|
+
: isCollection(value)
|
|
110
|
+
? isKeyed(value)
|
|
111
|
+
? value.toSeq()
|
|
112
|
+
: value.fromEntrySeq()
|
|
113
|
+
: isRecord(value)
|
|
114
|
+
? value.toSeq()
|
|
115
|
+
: keyedSeqFromValue(value);
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
if ( Seq ) KeyedSeq.__proto__ = Seq;
|
|
119
|
+
KeyedSeq.prototype = Object.create( Seq && Seq.prototype );
|
|
120
|
+
KeyedSeq.prototype.constructor = KeyedSeq;
|
|
121
|
+
|
|
122
|
+
KeyedSeq.prototype.toKeyedSeq = function toKeyedSeq () {
|
|
123
|
+
return this;
|
|
124
|
+
};
|
|
125
|
+
|
|
126
|
+
return KeyedSeq;
|
|
127
|
+
}(Seq));
|
|
128
|
+
|
|
129
|
+
var IndexedSeq = /*@__PURE__*/(function (Seq) {
|
|
130
|
+
function IndexedSeq(value) {
|
|
131
|
+
return value === undefined || value === null
|
|
132
|
+
? emptySequence()
|
|
133
|
+
: isCollection(value)
|
|
134
|
+
? isKeyed(value)
|
|
135
|
+
? value.entrySeq()
|
|
136
|
+
: value.toIndexedSeq()
|
|
137
|
+
: isRecord(value)
|
|
138
|
+
? value.toSeq().entrySeq()
|
|
139
|
+
: indexedSeqFromValue(value);
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
if ( Seq ) IndexedSeq.__proto__ = Seq;
|
|
143
|
+
IndexedSeq.prototype = Object.create( Seq && Seq.prototype );
|
|
144
|
+
IndexedSeq.prototype.constructor = IndexedSeq;
|
|
145
|
+
|
|
146
|
+
IndexedSeq.of = function of (/*...values*/) {
|
|
147
|
+
return IndexedSeq(arguments);
|
|
148
|
+
};
|
|
149
|
+
|
|
150
|
+
IndexedSeq.prototype.toIndexedSeq = function toIndexedSeq () {
|
|
151
|
+
return this;
|
|
152
|
+
};
|
|
153
|
+
|
|
154
|
+
IndexedSeq.prototype.toString = function toString () {
|
|
155
|
+
return this.__toString('Seq [', ']');
|
|
156
|
+
};
|
|
157
|
+
|
|
158
|
+
return IndexedSeq;
|
|
159
|
+
}(Seq));
|
|
160
|
+
|
|
161
|
+
var SetSeq = /*@__PURE__*/(function (Seq) {
|
|
162
|
+
function SetSeq(value) {
|
|
163
|
+
return (
|
|
164
|
+
isCollection(value) && !isAssociative(value) ? value : IndexedSeq(value)
|
|
165
|
+
).toSetSeq();
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
if ( Seq ) SetSeq.__proto__ = Seq;
|
|
169
|
+
SetSeq.prototype = Object.create( Seq && Seq.prototype );
|
|
170
|
+
SetSeq.prototype.constructor = SetSeq;
|
|
171
|
+
|
|
172
|
+
SetSeq.of = function of (/*...values*/) {
|
|
173
|
+
return SetSeq(arguments);
|
|
174
|
+
};
|
|
175
|
+
|
|
176
|
+
SetSeq.prototype.toSetSeq = function toSetSeq () {
|
|
177
|
+
return this;
|
|
178
|
+
};
|
|
179
|
+
|
|
180
|
+
return SetSeq;
|
|
181
|
+
}(Seq));
|
|
182
|
+
|
|
183
|
+
Seq.isSeq = isSeq;
|
|
184
|
+
Seq.Keyed = KeyedSeq;
|
|
185
|
+
Seq.Set = SetSeq;
|
|
186
|
+
Seq.Indexed = IndexedSeq;
|
|
187
|
+
|
|
188
|
+
Seq.prototype[IS_SEQ_SYMBOL] = true;
|
|
189
|
+
|
|
190
|
+
// #pragma Root Sequences
|
|
191
|
+
|
|
192
|
+
var ArraySeq = /*@__PURE__*/(function (IndexedSeq) {
|
|
193
|
+
function ArraySeq(array) {
|
|
194
|
+
this._array = array;
|
|
195
|
+
this.size = array.length;
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
if ( IndexedSeq ) ArraySeq.__proto__ = IndexedSeq;
|
|
199
|
+
ArraySeq.prototype = Object.create( IndexedSeq && IndexedSeq.prototype );
|
|
200
|
+
ArraySeq.prototype.constructor = ArraySeq;
|
|
201
|
+
|
|
202
|
+
ArraySeq.prototype.get = function get (index, notSetValue) {
|
|
203
|
+
return this.has(index) ? this._array[wrapIndex(this, index)] : notSetValue;
|
|
204
|
+
};
|
|
205
|
+
|
|
206
|
+
ArraySeq.prototype.__iterate = function __iterate (fn, reverse) {
|
|
207
|
+
var array = this._array;
|
|
208
|
+
var size = array.length;
|
|
209
|
+
var i = 0;
|
|
210
|
+
while (i !== size) {
|
|
211
|
+
var ii = reverse ? size - ++i : i++;
|
|
212
|
+
if (fn(array[ii], ii, this) === false) {
|
|
213
|
+
break;
|
|
214
|
+
}
|
|
215
|
+
}
|
|
216
|
+
return i;
|
|
217
|
+
};
|
|
218
|
+
|
|
219
|
+
ArraySeq.prototype.__iterator = function __iterator (type, reverse) {
|
|
220
|
+
var array = this._array;
|
|
221
|
+
var size = array.length;
|
|
222
|
+
var i = 0;
|
|
223
|
+
return new Iterator(function () {
|
|
224
|
+
if (i === size) {
|
|
225
|
+
return iteratorDone();
|
|
226
|
+
}
|
|
227
|
+
var ii = reverse ? size - ++i : i++;
|
|
228
|
+
return iteratorValue(type, ii, array[ii]);
|
|
229
|
+
});
|
|
230
|
+
};
|
|
231
|
+
|
|
232
|
+
return ArraySeq;
|
|
233
|
+
}(IndexedSeq));
|
|
234
|
+
|
|
235
|
+
var ObjectSeq = /*@__PURE__*/(function (KeyedSeq) {
|
|
236
|
+
function ObjectSeq(object) {
|
|
237
|
+
var keys = Object.keys(object).concat(
|
|
238
|
+
Object.getOwnPropertySymbols ? Object.getOwnPropertySymbols(object) : []
|
|
239
|
+
);
|
|
240
|
+
this._object = object;
|
|
241
|
+
this._keys = keys;
|
|
242
|
+
this.size = keys.length;
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
if ( KeyedSeq ) ObjectSeq.__proto__ = KeyedSeq;
|
|
246
|
+
ObjectSeq.prototype = Object.create( KeyedSeq && KeyedSeq.prototype );
|
|
247
|
+
ObjectSeq.prototype.constructor = ObjectSeq;
|
|
248
|
+
|
|
249
|
+
ObjectSeq.prototype.get = function get (key, notSetValue) {
|
|
250
|
+
if (notSetValue !== undefined && !this.has(key)) {
|
|
251
|
+
return notSetValue;
|
|
252
|
+
}
|
|
253
|
+
return this._object[key];
|
|
254
|
+
};
|
|
255
|
+
|
|
256
|
+
ObjectSeq.prototype.has = function has (key) {
|
|
257
|
+
return hasOwnProperty.call(this._object, key);
|
|
258
|
+
};
|
|
259
|
+
|
|
260
|
+
ObjectSeq.prototype.__iterate = function __iterate (fn, reverse) {
|
|
261
|
+
var object = this._object;
|
|
262
|
+
var keys = this._keys;
|
|
263
|
+
var size = keys.length;
|
|
264
|
+
var i = 0;
|
|
265
|
+
while (i !== size) {
|
|
266
|
+
var key = keys[reverse ? size - ++i : i++];
|
|
267
|
+
if (fn(object[key], key, this) === false) {
|
|
268
|
+
break;
|
|
269
|
+
}
|
|
270
|
+
}
|
|
271
|
+
return i;
|
|
272
|
+
};
|
|
273
|
+
|
|
274
|
+
ObjectSeq.prototype.__iterator = function __iterator (type, reverse) {
|
|
275
|
+
var object = this._object;
|
|
276
|
+
var keys = this._keys;
|
|
277
|
+
var size = keys.length;
|
|
278
|
+
var i = 0;
|
|
279
|
+
return new Iterator(function () {
|
|
280
|
+
if (i === size) {
|
|
281
|
+
return iteratorDone();
|
|
282
|
+
}
|
|
283
|
+
var key = keys[reverse ? size - ++i : i++];
|
|
284
|
+
return iteratorValue(type, key, object[key]);
|
|
285
|
+
});
|
|
286
|
+
};
|
|
287
|
+
|
|
288
|
+
return ObjectSeq;
|
|
289
|
+
}(KeyedSeq));
|
|
290
|
+
ObjectSeq.prototype[IS_ORDERED_SYMBOL] = true;
|
|
291
|
+
|
|
292
|
+
var CollectionSeq = /*@__PURE__*/(function (IndexedSeq) {
|
|
293
|
+
function CollectionSeq(collection) {
|
|
294
|
+
this._collection = collection;
|
|
295
|
+
this.size = collection.length || collection.size;
|
|
296
|
+
}
|
|
297
|
+
|
|
298
|
+
if ( IndexedSeq ) CollectionSeq.__proto__ = IndexedSeq;
|
|
299
|
+
CollectionSeq.prototype = Object.create( IndexedSeq && IndexedSeq.prototype );
|
|
300
|
+
CollectionSeq.prototype.constructor = CollectionSeq;
|
|
301
|
+
|
|
302
|
+
CollectionSeq.prototype.__iterateUncached = function __iterateUncached (fn, reverse) {
|
|
303
|
+
if (reverse) {
|
|
304
|
+
return this.cacheResult().__iterate(fn, reverse);
|
|
305
|
+
}
|
|
306
|
+
var collection = this._collection;
|
|
307
|
+
var iterator = getIterator(collection);
|
|
308
|
+
var iterations = 0;
|
|
309
|
+
if (isIterator(iterator)) {
|
|
310
|
+
var step;
|
|
311
|
+
while (!(step = iterator.next()).done) {
|
|
312
|
+
if (fn(step.value, iterations++, this) === false) {
|
|
313
|
+
break;
|
|
314
|
+
}
|
|
315
|
+
}
|
|
316
|
+
}
|
|
317
|
+
return iterations;
|
|
318
|
+
};
|
|
319
|
+
|
|
320
|
+
CollectionSeq.prototype.__iteratorUncached = function __iteratorUncached (type, reverse) {
|
|
321
|
+
if (reverse) {
|
|
322
|
+
return this.cacheResult().__iterator(type, reverse);
|
|
323
|
+
}
|
|
324
|
+
var collection = this._collection;
|
|
325
|
+
var iterator = getIterator(collection);
|
|
326
|
+
if (!isIterator(iterator)) {
|
|
327
|
+
return new Iterator(iteratorDone);
|
|
328
|
+
}
|
|
329
|
+
var iterations = 0;
|
|
330
|
+
return new Iterator(function () {
|
|
331
|
+
var step = iterator.next();
|
|
332
|
+
return step.done ? step : iteratorValue(type, iterations++, step.value);
|
|
333
|
+
});
|
|
334
|
+
};
|
|
335
|
+
|
|
336
|
+
return CollectionSeq;
|
|
337
|
+
}(IndexedSeq));
|
|
338
|
+
|
|
339
|
+
// # pragma Helper functions
|
|
340
|
+
|
|
341
|
+
var EMPTY_SEQ;
|
|
342
|
+
|
|
343
|
+
function emptySequence() {
|
|
344
|
+
return EMPTY_SEQ || (EMPTY_SEQ = new ArraySeq([]));
|
|
345
|
+
}
|
|
346
|
+
|
|
347
|
+
function keyedSeqFromValue(value) {
|
|
348
|
+
var seq = maybeIndexedSeqFromValue(value);
|
|
349
|
+
if (seq) {
|
|
350
|
+
return seq.fromEntrySeq();
|
|
351
|
+
}
|
|
352
|
+
if (typeof value === 'object') {
|
|
353
|
+
return new ObjectSeq(value);
|
|
354
|
+
}
|
|
355
|
+
throw new TypeError(
|
|
356
|
+
'Expected Array or collection object of [k, v] entries, or keyed object: ' +
|
|
357
|
+
value
|
|
358
|
+
);
|
|
359
|
+
}
|
|
360
|
+
|
|
361
|
+
function indexedSeqFromValue(value) {
|
|
362
|
+
var seq = maybeIndexedSeqFromValue(value);
|
|
363
|
+
if (seq) {
|
|
364
|
+
return seq;
|
|
365
|
+
}
|
|
366
|
+
throw new TypeError(
|
|
367
|
+
'Expected Array or collection object of values: ' + value
|
|
368
|
+
);
|
|
369
|
+
}
|
|
370
|
+
|
|
371
|
+
function seqFromValue(value) {
|
|
372
|
+
var seq = maybeIndexedSeqFromValue(value);
|
|
373
|
+
if (seq) {
|
|
374
|
+
return isEntriesIterable(value)
|
|
375
|
+
? seq.fromEntrySeq()
|
|
376
|
+
: isKeysIterable(value)
|
|
377
|
+
? seq.toSetSeq()
|
|
378
|
+
: seq;
|
|
379
|
+
}
|
|
380
|
+
if (typeof value === 'object') {
|
|
381
|
+
return new ObjectSeq(value);
|
|
382
|
+
}
|
|
383
|
+
throw new TypeError(
|
|
384
|
+
'Expected Array or collection object of values, or keyed object: ' + value
|
|
385
|
+
);
|
|
386
|
+
}
|
|
387
|
+
|
|
388
|
+
function maybeIndexedSeqFromValue(value) {
|
|
389
|
+
return isArrayLike(value)
|
|
390
|
+
? new ArraySeq(value)
|
|
391
|
+
: hasIterator(value)
|
|
392
|
+
? new CollectionSeq(value)
|
|
393
|
+
: undefined;
|
|
394
|
+
}
|
|
395
|
+
|
|
396
|
+
export { ArraySeq, IndexedSeq, KeyedSeq, Seq, SetSeq, indexedSeqFromValue, keyedSeqFromValue };
|
package/dist/es/Set.js
ADDED
|
@@ -0,0 +1,277 @@
|
|
|
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, Collection, SetCollection } from './Collection.js';
|
|
25
|
+
import { isOrdered } from './predicates/isOrdered.js';
|
|
26
|
+
import { isSet, IS_SET_SYMBOL } from './predicates/isSet.js';
|
|
27
|
+
import { emptyMap } from './Map.js';
|
|
28
|
+
import { DELETE } from './TrieUtils.js';
|
|
29
|
+
import { sortFactory } from './Operations.js';
|
|
30
|
+
import assertNotInfinite from './utils/assertNotInfinite.js';
|
|
31
|
+
import { asImmutable } from './methods/asImmutable.js';
|
|
32
|
+
import { asMutable } from './methods/asMutable.js';
|
|
33
|
+
import { withMutations } from './methods/withMutations.js';
|
|
34
|
+
import { OrderedSet } from './OrderedSet.js';
|
|
35
|
+
|
|
36
|
+
var Set = /*@__PURE__*/(function (SetCollection) {
|
|
37
|
+
function Set(value) {
|
|
38
|
+
return value === undefined || value === null
|
|
39
|
+
? emptySet()
|
|
40
|
+
: isSet(value) && !isOrdered(value)
|
|
41
|
+
? value
|
|
42
|
+
: emptySet().withMutations(function (set) {
|
|
43
|
+
var iter = SetCollection(value);
|
|
44
|
+
assertNotInfinite(iter.size);
|
|
45
|
+
iter.forEach(function (v) { return set.add(v); });
|
|
46
|
+
});
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
if ( SetCollection ) Set.__proto__ = SetCollection;
|
|
50
|
+
Set.prototype = Object.create( SetCollection && SetCollection.prototype );
|
|
51
|
+
Set.prototype.constructor = Set;
|
|
52
|
+
|
|
53
|
+
Set.of = function of (/*...values*/) {
|
|
54
|
+
return this(arguments);
|
|
55
|
+
};
|
|
56
|
+
|
|
57
|
+
Set.fromKeys = function fromKeys (value) {
|
|
58
|
+
return this(KeyedCollection(value).keySeq());
|
|
59
|
+
};
|
|
60
|
+
|
|
61
|
+
Set.intersect = function intersect (sets) {
|
|
62
|
+
sets = Collection(sets).toArray();
|
|
63
|
+
return sets.length
|
|
64
|
+
? SetPrototype.intersect.apply(Set(sets.pop()), sets)
|
|
65
|
+
: emptySet();
|
|
66
|
+
};
|
|
67
|
+
|
|
68
|
+
Set.union = function union (sets) {
|
|
69
|
+
sets = Collection(sets).toArray();
|
|
70
|
+
return sets.length
|
|
71
|
+
? SetPrototype.union.apply(Set(sets.pop()), sets)
|
|
72
|
+
: emptySet();
|
|
73
|
+
};
|
|
74
|
+
|
|
75
|
+
Set.prototype.toString = function toString () {
|
|
76
|
+
return this.__toString('Set {', '}');
|
|
77
|
+
};
|
|
78
|
+
|
|
79
|
+
// @pragma Access
|
|
80
|
+
|
|
81
|
+
Set.prototype.has = function has (value) {
|
|
82
|
+
return this._map.has(value);
|
|
83
|
+
};
|
|
84
|
+
|
|
85
|
+
// @pragma Modification
|
|
86
|
+
|
|
87
|
+
Set.prototype.add = function add (value) {
|
|
88
|
+
return updateSet(this, this._map.set(value, value));
|
|
89
|
+
};
|
|
90
|
+
|
|
91
|
+
Set.prototype.remove = function remove (value) {
|
|
92
|
+
return updateSet(this, this._map.remove(value));
|
|
93
|
+
};
|
|
94
|
+
|
|
95
|
+
Set.prototype.clear = function clear () {
|
|
96
|
+
return updateSet(this, this._map.clear());
|
|
97
|
+
};
|
|
98
|
+
|
|
99
|
+
// @pragma Composition
|
|
100
|
+
|
|
101
|
+
Set.prototype.map = function map (mapper, context) {
|
|
102
|
+
var this$1$1 = this;
|
|
103
|
+
|
|
104
|
+
// keep track if the set is altered by the map function
|
|
105
|
+
var didChanges = false;
|
|
106
|
+
|
|
107
|
+
var newMap = updateSet(
|
|
108
|
+
this,
|
|
109
|
+
this._map.mapEntries(function (ref) {
|
|
110
|
+
var v = ref[1];
|
|
111
|
+
|
|
112
|
+
var mapped = mapper.call(context, v, v, this$1$1);
|
|
113
|
+
|
|
114
|
+
if (mapped !== v) {
|
|
115
|
+
didChanges = true;
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
return [mapped, mapped];
|
|
119
|
+
}, context)
|
|
120
|
+
);
|
|
121
|
+
|
|
122
|
+
return didChanges ? newMap : this;
|
|
123
|
+
};
|
|
124
|
+
|
|
125
|
+
Set.prototype.union = function union () {
|
|
126
|
+
var iters = [], len = arguments.length;
|
|
127
|
+
while ( len-- ) iters[ len ] = arguments[ len ];
|
|
128
|
+
|
|
129
|
+
iters = iters.filter(function (x) { return x.size !== 0; });
|
|
130
|
+
if (iters.length === 0) {
|
|
131
|
+
return this;
|
|
132
|
+
}
|
|
133
|
+
if (this.size === 0 && !this.__ownerID && iters.length === 1) {
|
|
134
|
+
return this.constructor(iters[0]);
|
|
135
|
+
}
|
|
136
|
+
return this.withMutations(function (set) {
|
|
137
|
+
for (var ii = 0; ii < iters.length; ii++) {
|
|
138
|
+
if (typeof iters[ii] === 'string') {
|
|
139
|
+
set.add(iters[ii]);
|
|
140
|
+
} else {
|
|
141
|
+
SetCollection(iters[ii]).forEach(function (value) { return set.add(value); });
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
});
|
|
145
|
+
};
|
|
146
|
+
|
|
147
|
+
Set.prototype.intersect = function intersect () {
|
|
148
|
+
var iters = [], len = arguments.length;
|
|
149
|
+
while ( len-- ) iters[ len ] = arguments[ len ];
|
|
150
|
+
|
|
151
|
+
if (iters.length === 0) {
|
|
152
|
+
return this;
|
|
153
|
+
}
|
|
154
|
+
iters = iters.map(function (iter) { return SetCollection(iter); });
|
|
155
|
+
var toRemove = [];
|
|
156
|
+
this.forEach(function (value) {
|
|
157
|
+
if (!iters.every(function (iter) { return iter.includes(value); })) {
|
|
158
|
+
toRemove.push(value);
|
|
159
|
+
}
|
|
160
|
+
});
|
|
161
|
+
return this.withMutations(function (set) {
|
|
162
|
+
toRemove.forEach(function (value) {
|
|
163
|
+
set.remove(value);
|
|
164
|
+
});
|
|
165
|
+
});
|
|
166
|
+
};
|
|
167
|
+
|
|
168
|
+
Set.prototype.subtract = function subtract () {
|
|
169
|
+
var iters = [], len = arguments.length;
|
|
170
|
+
while ( len-- ) iters[ len ] = arguments[ len ];
|
|
171
|
+
|
|
172
|
+
if (iters.length === 0) {
|
|
173
|
+
return this;
|
|
174
|
+
}
|
|
175
|
+
iters = iters.map(function (iter) { return SetCollection(iter); });
|
|
176
|
+
var toRemove = [];
|
|
177
|
+
this.forEach(function (value) {
|
|
178
|
+
if (iters.some(function (iter) { return iter.includes(value); })) {
|
|
179
|
+
toRemove.push(value);
|
|
180
|
+
}
|
|
181
|
+
});
|
|
182
|
+
return this.withMutations(function (set) {
|
|
183
|
+
toRemove.forEach(function (value) {
|
|
184
|
+
set.remove(value);
|
|
185
|
+
});
|
|
186
|
+
});
|
|
187
|
+
};
|
|
188
|
+
|
|
189
|
+
Set.prototype.sort = function sort (comparator) {
|
|
190
|
+
// Late binding
|
|
191
|
+
return OrderedSet(sortFactory(this, comparator));
|
|
192
|
+
};
|
|
193
|
+
|
|
194
|
+
Set.prototype.sortBy = function sortBy (mapper, comparator) {
|
|
195
|
+
// Late binding
|
|
196
|
+
return OrderedSet(sortFactory(this, comparator, mapper));
|
|
197
|
+
};
|
|
198
|
+
|
|
199
|
+
Set.prototype.wasAltered = function wasAltered () {
|
|
200
|
+
return this._map.wasAltered();
|
|
201
|
+
};
|
|
202
|
+
|
|
203
|
+
Set.prototype.__iterate = function __iterate (fn, reverse) {
|
|
204
|
+
var this$1$1 = this;
|
|
205
|
+
|
|
206
|
+
return this._map.__iterate(function (k) { return fn(k, k, this$1$1); }, reverse);
|
|
207
|
+
};
|
|
208
|
+
|
|
209
|
+
Set.prototype.__iterator = function __iterator (type, reverse) {
|
|
210
|
+
return this._map.__iterator(type, reverse);
|
|
211
|
+
};
|
|
212
|
+
|
|
213
|
+
Set.prototype.__ensureOwner = function __ensureOwner (ownerID) {
|
|
214
|
+
if (ownerID === this.__ownerID) {
|
|
215
|
+
return this;
|
|
216
|
+
}
|
|
217
|
+
var newMap = this._map.__ensureOwner(ownerID);
|
|
218
|
+
if (!ownerID) {
|
|
219
|
+
if (this.size === 0) {
|
|
220
|
+
return this.__empty();
|
|
221
|
+
}
|
|
222
|
+
this.__ownerID = ownerID;
|
|
223
|
+
this._map = newMap;
|
|
224
|
+
return this;
|
|
225
|
+
}
|
|
226
|
+
return this.__make(newMap, ownerID);
|
|
227
|
+
};
|
|
228
|
+
|
|
229
|
+
return Set;
|
|
230
|
+
}(SetCollection));
|
|
231
|
+
|
|
232
|
+
Set.isSet = isSet;
|
|
233
|
+
|
|
234
|
+
var SetPrototype = Set.prototype;
|
|
235
|
+
SetPrototype[IS_SET_SYMBOL] = true;
|
|
236
|
+
SetPrototype[DELETE] = SetPrototype.remove;
|
|
237
|
+
SetPrototype.merge = SetPrototype.concat = SetPrototype.union;
|
|
238
|
+
SetPrototype.withMutations = withMutations;
|
|
239
|
+
SetPrototype.asImmutable = asImmutable;
|
|
240
|
+
SetPrototype['@@transducer/init'] = SetPrototype.asMutable = asMutable;
|
|
241
|
+
SetPrototype['@@transducer/step'] = function (result, arr) {
|
|
242
|
+
return result.add(arr);
|
|
243
|
+
};
|
|
244
|
+
SetPrototype['@@transducer/result'] = function (obj) {
|
|
245
|
+
return obj.asImmutable();
|
|
246
|
+
};
|
|
247
|
+
|
|
248
|
+
SetPrototype.__empty = emptySet;
|
|
249
|
+
SetPrototype.__make = makeSet;
|
|
250
|
+
|
|
251
|
+
function updateSet(set, newMap) {
|
|
252
|
+
if (set.__ownerID) {
|
|
253
|
+
set.size = newMap.size;
|
|
254
|
+
set._map = newMap;
|
|
255
|
+
return set;
|
|
256
|
+
}
|
|
257
|
+
return newMap === set._map
|
|
258
|
+
? set
|
|
259
|
+
: newMap.size === 0
|
|
260
|
+
? set.__empty()
|
|
261
|
+
: set.__make(newMap);
|
|
262
|
+
}
|
|
263
|
+
|
|
264
|
+
function makeSet(map, ownerID) {
|
|
265
|
+
var set = Object.create(SetPrototype);
|
|
266
|
+
set.size = map ? map.size : 0;
|
|
267
|
+
set._map = map;
|
|
268
|
+
set.__ownerID = ownerID;
|
|
269
|
+
return set;
|
|
270
|
+
}
|
|
271
|
+
|
|
272
|
+
var EMPTY_SET;
|
|
273
|
+
function emptySet() {
|
|
274
|
+
return EMPTY_SET || (EMPTY_SET = makeSet(emptyMap()));
|
|
275
|
+
}
|
|
276
|
+
|
|
277
|
+
export { Set };
|