UNPKG

193 kBTypeScriptView Raw
1/**
2 * Immutable data encourages pure functions (data-in, data-out) and lends itself
3 * to much simpler application development and enabling techniques from
4 * functional programming such as lazy evaluation.
5 *
6 * While designed to bring these powerful functional concepts to JavaScript, it
7 * presents an Object-Oriented API familiar to Javascript engineers and closely
8 * mirroring that of Array, Map, and Set. It is easy and efficient to convert to
9 * and from plain Javascript types.
10 *
11 * ## How to read these docs
12 *
13 * In order to better explain what kinds of values the Immutable.js API expects
14 * and produces, this documentation is presented in a statically typed dialect of
15 * JavaScript (like [Flow][] or [TypeScript][]). You *don't need* to use these
16 * type checking tools in order to use Immutable.js, however becoming familiar
17 * with their syntax will help you get a deeper understanding of this API.
18 *
19 * **A few examples and how to read them.**
20 *
21 * All methods describe the kinds of data they accept and the kinds of data
22 * they return. For example a function which accepts two numbers and returns
23 * a number would look like this:
24 *
25 * ```js
26 * sum(first: number, second: number): number
27 * ```
28 *
29 * Sometimes, methods can accept different kinds of data or return different
30 * kinds of data, and this is described with a *type variable*, which is
31 * typically in all-caps. For example, a function which always returns the same
32 * kind of data it was provided would look like this:
33 *
34 * ```js
35 * identity<T>(value: T): T
36 * ```
37 *
38 * Type variables are defined with classes and referred to in methods. For
39 * example, a class that holds onto a value for you might look like this:
40 *
41 * ```js
42 * class Box<T> {
43 * constructor(value: T)
44 * getValue(): T
45 * }
46 * ```
47 *
48 * In order to manipulate Immutable data, methods that we're used to affecting
49 * a Collection instead return a new Collection of the same type. The type
50 * `this` refers to the same kind of class. For example, a List which returns
51 * new Lists when you `push` a value onto it might look like:
52 *
53 * ```js
54 * class List<T> {
55 * push(value: T): this
56 * }
57 * ```
58 *
59 * Many methods in Immutable.js accept values which implement the JavaScript
60 * [Iterable][] protocol, and might appear like `Iterable<string>` for something
61 * which represents sequence of strings. Typically in JavaScript we use plain
62 * Arrays (`[]`) when an Iterable is expected, but also all of the Immutable.js
63 * collections are iterable themselves!
64 *
65 * For example, to get a value deep within a structure of data, we might use
66 * `getIn` which expects an `Iterable` path:
67 *
68 * ```
69 * getIn(path: Iterable<string | number>): unknown
70 * ```
71 *
72 * To use this method, we could pass an array: `data.getIn([ "key", 2 ])`.
73 *
74 *
75 * Note: All examples are presented in the modern [ES2015][] version of
76 * JavaScript. Use tools like Babel to support older browsers.
77 *
78 * For example:
79 *
80 * ```js
81 * // ES2015
82 * const mappedFoo = foo.map(x => x * x);
83 * // ES5
84 * var mappedFoo = foo.map(function (x) { return x * x; });
85 * ```
86 *
87 * [ES2015]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/New_in_JavaScript/ECMAScript_6_support_in_Mozilla
88 * [TypeScript]: https://www.typescriptlang.org/
89 * [Flow]: https://flowtype.org/
90 * [Iterable]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Iteration_protocols
91 */
92
93declare namespace Immutable {
94 /** @ignore */
95 type OnlyObject<T> = Extract<T, object>;
96
97 /** @ignore */
98 type ContainObject<T> = OnlyObject<T> extends object
99 ? OnlyObject<T> extends never
100 ? false
101 : true
102 : false;
103
104 /**
105 * @ignore
106 *
107 * Used to convert deeply all immutable types to a plain TS type.
108 * Using `unknown` on object instead of recursive call as we have a circular reference issue
109 */
110 export type DeepCopy<T> = T extends Record<infer R>
111 ? // convert Record to DeepCopy plain JS object
112 {
113 [key in keyof R]: ContainObject<R[key]> extends true ? unknown : R[key];
114 }
115 : T extends MapOf<infer R>
116 ? // convert MapOf to DeepCopy plain JS object
117 {
118 [key in keyof R]: ContainObject<R[key]> extends true ? unknown : R[key];
119 }
120 : T extends Collection.Keyed<infer KeyedKey, infer V>
121 ? // convert KeyedCollection to DeepCopy plain JS object
122 {
123 [key in KeyedKey extends string | number | symbol
124 ? KeyedKey
125 : string]: V extends object ? unknown : V;
126 }
127 : // convert IndexedCollection or Immutable.Set to DeepCopy plain JS array
128 T extends Collection<infer _, infer V>
129 ? Array<DeepCopy<V>>
130 : T extends string | number // Iterable scalar types : should be kept as is
131 ? T
132 : T extends Iterable<infer V> // Iterable are converted to plain JS array
133 ? Array<DeepCopy<V>>
134 : T extends object // plain JS object are converted deeply
135 ? {
136 [ObjectKey in keyof T]: ContainObject<T[ObjectKey]> extends true
137 ? unknown
138 : T[ObjectKey];
139 }
140 : // other case : should be kept as is
141 T;
142
143 /**
144 * Describes which item in a pair should be placed first when sorting
145 *
146 * @ignore
147 */
148 export enum PairSorting {
149 LeftThenRight = -1,
150 RightThenLeft = +1,
151 }
152
153 /**
154 * Function comparing two items of the same type. It can return:
155 *
156 * * a PairSorting value, to indicate whether the left-hand item or the right-hand item should be placed before the other
157 *
158 * * the traditional numeric return value - especially -1, 0, or 1
159 *
160 * @ignore
161 */
162 export type Comparator<T> = (left: T, right: T) => PairSorting | number;
163
164 /**
165 * Lists are ordered indexed dense collections, much like a JavaScript
166 * Array.
167 *
168 * Lists are immutable and fully persistent with O(log32 N) gets and sets,
169 * and O(1) push and pop.
170 *
171 * Lists implement Deque, with efficient addition and removal from both the
172 * end (`push`, `pop`) and beginning (`unshift`, `shift`).
173 *
174 * Unlike a JavaScript Array, there is no distinction between an
175 * "unset" index and an index set to `undefined`. `List#forEach` visits all
176 * indices from 0 to size, regardless of whether they were explicitly defined.
177 */
178 namespace List {
179 /**
180 * True if the provided value is a List
181 *
182 * <!-- runkit:activate -->
183 * ```js
184 * const { List } = require('immutable');
185 * List.isList([]); // false
186 * List.isList(List()); // true
187 * ```
188 */
189 function isList(maybeList: unknown): maybeList is List<unknown>;
190
191 /**
192 * Creates a new List containing `values`.
193 *
194 * <!-- runkit:activate -->
195 * ```js
196 * const { List } = require('immutable');
197 * List.of(1, 2, 3, 4)
198 * // List [ 1, 2, 3, 4 ]
199 * ```
200 *
201 * Note: Values are not altered or converted in any way.
202 *
203 * <!-- runkit:activate -->
204 * ```js
205 * const { List } = require('immutable');
206 * List.of({x:1}, 2, [3], 4)
207 * // List [ { x: 1 }, 2, [ 3 ], 4 ]
208 * ```
209 */
210 function of<T>(...values: Array<T>): List<T>;
211 }
212
213 /**
214 * Create a new immutable List containing the values of the provided
215 * collection-like.
216 *
217 * Note: `List` is a factory function and not a class, and does not use the
218 * `new` keyword during construction.
219 *
220 * <!-- runkit:activate -->
221 * ```js
222 * const { List, Set } = require('immutable')
223 *
224 * const emptyList = List()
225 * // List []
226 *
227 * const plainArray = [ 1, 2, 3, 4 ]
228 * const listFromPlainArray = List(plainArray)
229 * // List [ 1, 2, 3, 4 ]
230 *
231 * const plainSet = Set([ 1, 2, 3, 4 ])
232 * const listFromPlainSet = List(plainSet)
233 * // List [ 1, 2, 3, 4 ]
234 *
235 * const arrayIterator = plainArray[Symbol.iterator]()
236 * const listFromCollectionArray = List(arrayIterator)
237 * // List [ 1, 2, 3, 4 ]
238 *
239 * listFromPlainArray.equals(listFromCollectionArray) // true
240 * listFromPlainSet.equals(listFromCollectionArray) // true
241 * listFromPlainSet.equals(listFromPlainArray) // true
242 * ```
243 */
244 function List<T>(collection?: Iterable<T> | ArrayLike<T>): List<T>;
245
246 interface List<T> extends Collection.Indexed<T> {
247 /**
248 * The number of items in this List.
249 */
250 readonly size: number;
251
252 // Persistent changes
253
254 /**
255 * Returns a new List which includes `value` at `index`. If `index` already
256 * exists in this List, it will be replaced.
257 *
258 * `index` may be a negative number, which indexes back from the end of the
259 * List. `v.set(-1, "value")` sets the last item in the List.
260 *
261 * If `index` larger than `size`, the returned List's `size` will be large
262 * enough to include the `index`.
263 *
264 * <!-- runkit:activate
265 * { "preamble": "const { List } = require('immutable');" }
266 * -->
267 * ```js
268 * const originalList = List([ 0 ]);
269 * // List [ 0 ]
270 * originalList.set(1, 1);
271 * // List [ 0, 1 ]
272 * originalList.set(0, 'overwritten');
273 * // List [ "overwritten" ]
274 * originalList.set(2, 2);
275 * // List [ 0, undefined, 2 ]
276 *
277 * List().set(50000, 'value').size;
278 * // 50001
279 * ```
280 *
281 * Note: `set` can be used in `withMutations`.
282 */
283 set(index: number, value: T): List<T>;
284
285 /**
286 * Returns a new List which excludes this `index` and with a size 1 less
287 * than this List. Values at indices above `index` are shifted down by 1 to
288 * fill the position.
289 *
290 * This is synonymous with `list.splice(index, 1)`.
291 *
292 * `index` may be a negative number, which indexes back from the end of the
293 * List. `v.delete(-1)` deletes the last item in the List.
294 *
295 * Note: `delete` cannot be safely used in IE8
296 *
297 * <!-- runkit:activate
298 * { "preamble": "const { List } = require('immutable');" }
299 * -->
300 * ```js
301 * List([ 0, 1, 2, 3, 4 ]).delete(0);
302 * // List [ 1, 2, 3, 4 ]
303 * ```
304 *
305 * Since `delete()` re-indexes values, it produces a complete copy, which
306 * has `O(N)` complexity.
307 *
308 * Note: `delete` *cannot* be used in `withMutations`.
309 *
310 * @alias remove
311 */
312 delete(index: number): List<T>;
313 remove(index: number): List<T>;
314
315 /**
316 * Returns a new List with `value` at `index` with a size 1 more than this
317 * List. Values at indices above `index` are shifted over by 1.
318 *
319 * This is synonymous with `list.splice(index, 0, value)`.
320 *
321 * <!-- runkit:activate
322 * { "preamble": "const { List } = require('immutable');" }
323 * -->
324 * ```js
325 * List([ 0, 1, 2, 3, 4 ]).insert(6, 5)
326 * // List [ 0, 1, 2, 3, 4, 5 ]
327 * ```
328 *
329 * Since `insert()` re-indexes values, it produces a complete copy, which
330 * has `O(N)` complexity.
331 *
332 * Note: `insert` *cannot* be used in `withMutations`.
333 */
334 insert(index: number, value: T): List<T>;
335
336 /**
337 * Returns a new List with 0 size and no values in constant time.
338 *
339 * <!-- runkit:activate
340 * { "preamble": "const { List } = require('immutable');" }
341 * -->
342 * ```js
343 * List([ 1, 2, 3, 4 ]).clear()
344 * // List []
345 * ```
346 *
347 * Note: `clear` can be used in `withMutations`.
348 */
349 clear(): List<T>;
350
351 /**
352 * Returns a new List with the provided `values` appended, starting at this
353 * List's `size`.
354 *
355 * <!-- runkit:activate
356 * { "preamble": "const { List } = require('immutable');" }
357 * -->
358 * ```js
359 * List([ 1, 2, 3, 4 ]).push(5)
360 * // List [ 1, 2, 3, 4, 5 ]
361 * ```
362 *
363 * Note: `push` can be used in `withMutations`.
364 */
365 push(...values: Array<T>): List<T>;
366
367 /**
368 * Returns a new List with a size ones less than this List, excluding
369 * the last index in this List.
370 *
371 * Note: this differs from `Array#pop` because it returns a new
372 * List rather than the removed value. Use `last()` to get the last value
373 * in this List.
374 *
375 * ```js
376 * List([ 1, 2, 3, 4 ]).pop()
377 * // List[ 1, 2, 3 ]
378 * ```
379 *
380 * Note: `pop` can be used in `withMutations`.
381 */
382 pop(): List<T>;
383
384 /**
385 * Returns a new List with the provided `values` prepended, shifting other
386 * values ahead to higher indices.
387 *
388 * <!-- runkit:activate
389 * { "preamble": "const { List } = require('immutable');" }
390 * -->
391 * ```js
392 * List([ 2, 3, 4]).unshift(1);
393 * // List [ 1, 2, 3, 4 ]
394 * ```
395 *
396 * Note: `unshift` can be used in `withMutations`.
397 */
398 unshift(...values: Array<T>): List<T>;
399
400 /**
401 * Returns a new List with a size ones less than this List, excluding
402 * the first index in this List, shifting all other values to a lower index.
403 *
404 * Note: this differs from `Array#shift` because it returns a new
405 * List rather than the removed value. Use `first()` to get the first
406 * value in this List.
407 *
408 * <!-- runkit:activate
409 * { "preamble": "const { List } = require('immutable');" }
410 * -->
411 * ```js
412 * List([ 0, 1, 2, 3, 4 ]).shift();
413 * // List [ 1, 2, 3, 4 ]
414 * ```
415 *
416 * Note: `shift` can be used in `withMutations`.
417 */
418 shift(): List<T>;
419
420 /**
421 * Returns a new List with an updated value at `index` with the return
422 * value of calling `updater` with the existing value, or `notSetValue` if
423 * `index` was not set. If called with a single argument, `updater` is
424 * called with the List itself.
425 *
426 * `index` may be a negative number, which indexes back from the end of the
427 * List. `v.update(-1)` updates the last item in the List.
428 *
429 * <!-- runkit:activate
430 * { "preamble": "const { List } = require('immutable');" }
431 * -->
432 * ```js
433 * const list = List([ 'a', 'b', 'c' ])
434 * const result = list.update(2, val => val.toUpperCase())
435 * // List [ "a", "b", "C" ]
436 * ```
437 *
438 * This can be very useful as a way to "chain" a normal function into a
439 * sequence of methods. RxJS calls this "let" and lodash calls it "thru".
440 *
441 * For example, to sum a List after mapping and filtering:
442 *
443 * <!-- runkit:activate
444 * { "preamble": "const { List } = require('immutable');" }
445 * -->
446 * ```js
447 * function sum(collection) {
448 * return collection.reduce((sum, x) => sum + x, 0)
449 * }
450 *
451 * List([ 1, 2, 3 ])
452 * .map(x => x + 1)
453 * .filter(x => x % 2 === 0)
454 * .update(sum)
455 * // 6
456 * ```
457 *
458 * Note: `update(index)` can be used in `withMutations`.
459 *
460 * @see `Map#update`
461 */
462 update(index: number, notSetValue: T, updater: (value: T) => T): this;
463 update(
464 index: number,
465 updater: (value: T | undefined) => T | undefined
466 ): this;
467 update<R>(updater: (value: this) => R): R;
468
469 /**
470 * Returns a new List with size `size`. If `size` is less than this
471 * List's size, the new List will exclude values at the higher indices.
472 * If `size` is greater than this List's size, the new List will have
473 * undefined values for the newly available indices.
474 *
475 * When building a new List and the final size is known up front, `setSize`
476 * used in conjunction with `withMutations` may result in the more
477 * performant construction.
478 */
479 setSize(size: number): List<T>;
480
481 // Deep persistent changes
482
483 /**
484 * Returns a new List having set `value` at this `keyPath`. If any keys in
485 * `keyPath` do not exist, a new immutable Map will be created at that key.
486 *
487 * Index numbers are used as keys to determine the path to follow in
488 * the List.
489 *
490 * <!-- runkit:activate -->
491 * ```js
492 * const { List } = require('immutable')
493 * const list = List([ 0, 1, 2, List([ 3, 4 ])])
494 * list.setIn([3, 0], 999);
495 * // List [ 0, 1, 2, List [ 999, 4 ] ]
496 * ```
497 *
498 * Plain JavaScript Object or Arrays may be nested within an Immutable.js
499 * Collection, and setIn() can update those values as well, treating them
500 * immutably by creating new copies of those values with the changes applied.
501 *
502 * <!-- runkit:activate -->
503 * ```js
504 * const { List } = require('immutable')
505 * const list = List([ 0, 1, 2, { plain: 'object' }])
506 * list.setIn([3, 'plain'], 'value');
507 * // List([ 0, 1, 2, { plain: 'value' }])
508 * ```
509 *
510 * Note: `setIn` can be used in `withMutations`.
511 */
512 setIn(keyPath: Iterable<unknown>, value: unknown): this;
513
514 /**
515 * Returns a new List having removed the value at this `keyPath`. If any
516 * keys in `keyPath` do not exist, no change will occur.
517 *
518 * <!-- runkit:activate -->
519 * ```js
520 * const { List } = require('immutable')
521 * const list = List([ 0, 1, 2, List([ 3, 4 ])])
522 * list.deleteIn([3, 0]);
523 * // List [ 0, 1, 2, List [ 4 ] ]
524 * ```
525 *
526 * Plain JavaScript Object or Arrays may be nested within an Immutable.js
527 * Collection, and removeIn() can update those values as well, treating them
528 * immutably by creating new copies of those values with the changes applied.
529 *
530 * <!-- runkit:activate -->
531 * ```js
532 * const { List } = require('immutable')
533 * const list = List([ 0, 1, 2, { plain: 'object' }])
534 * list.removeIn([3, 'plain']);
535 * // List([ 0, 1, 2, {}])
536 * ```
537 *
538 * Note: `deleteIn` *cannot* be safely used in `withMutations`.
539 *
540 * @alias removeIn
541 */
542 deleteIn(keyPath: Iterable<unknown>): this;
543 removeIn(keyPath: Iterable<unknown>): this;
544
545 /**
546 * Note: `updateIn` can be used in `withMutations`.
547 *
548 * @see `Map#updateIn`
549 */
550 updateIn(
551 keyPath: Iterable<unknown>,
552 notSetValue: unknown,
553 updater: (value: unknown) => unknown
554 ): this;
555 updateIn(
556 keyPath: Iterable<unknown>,
557 updater: (value: unknown) => unknown
558 ): this;
559
560 /**
561 * Note: `mergeIn` can be used in `withMutations`.
562 *
563 * @see `Map#mergeIn`
564 */
565 mergeIn(keyPath: Iterable<unknown>, ...collections: Array<unknown>): this;
566
567 /**
568 * Note: `mergeDeepIn` can be used in `withMutations`.
569 *
570 * @see `Map#mergeDeepIn`
571 */
572 mergeDeepIn(
573 keyPath: Iterable<unknown>,
574 ...collections: Array<unknown>
575 ): this;
576
577 // Transient changes
578
579 /**
580 * Note: Not all methods can be safely used on a mutable collection or within
581 * `withMutations`! Check the documentation for each method to see if it
582 * allows being used in `withMutations`.
583 *
584 * @see `Map#withMutations`
585 */
586 withMutations(mutator: (mutable: this) => unknown): this;
587
588 /**
589 * An alternative API for withMutations()
590 *
591 * Note: Not all methods can be safely used on a mutable collection or within
592 * `withMutations`! Check the documentation for each method to see if it
593 * allows being used in `withMutations`.
594 *
595 * @see `Map#asMutable`
596 */
597 asMutable(): this;
598
599 /**
600 * @see `Map#wasAltered`
601 */
602 wasAltered(): boolean;
603
604 /**
605 * @see `Map#asImmutable`
606 */
607 asImmutable(): this;
608
609 // Sequence algorithms
610
611 /**
612 * Returns a new List with other values or collections concatenated to this one.
613 *
614 * Note: `concat` can be used in `withMutations`.
615 *
616 * @alias merge
617 */
618 concat<C>(...valuesOrCollections: Array<Iterable<C> | C>): List<T | C>;
619 merge<C>(...collections: Array<Iterable<C>>): List<T | C>;
620
621 /**
622 * Returns a new List with values passed through a
623 * `mapper` function.
624 *
625 * <!-- runkit:activate
626 * { "preamble": "const { List } = require('immutable');" }
627 * -->
628 * ```js
629 * List([ 1, 2 ]).map(x => 10 * x)
630 * // List [ 10, 20 ]
631 * ```
632 */
633 map<M>(
634 mapper: (value: T, key: number, iter: this) => M,
635 context?: unknown
636 ): List<M>;
637
638 /**
639 * Flat-maps the List, returning a new List.
640 *
641 * Similar to `list.map(...).flatten(true)`.
642 */
643 flatMap<M>(
644 mapper: (value: T, key: number, iter: this) => Iterable<M>,
645 context?: unknown
646 ): List<M>;
647
648 /**
649 * Returns a new List with only the values for which the `predicate`
650 * function returns true.
651 *
652 * Note: `filter()` always returns a new instance, even if it results in
653 * not filtering out any values.
654 */
655 filter<F extends T>(
656 predicate: (value: T, index: number, iter: this) => value is F,
657 context?: unknown
658 ): List<F>;
659 filter(
660 predicate: (value: T, index: number, iter: this) => unknown,
661 context?: unknown
662 ): this;
663
664 /**
665 * Returns a new List with the values for which the `predicate`
666 * function returns false and another for which is returns true.
667 */
668 partition<F extends T, C>(
669 predicate: (this: C, value: T, index: number, iter: this) => value is F,
670 context?: C
671 ): [List<T>, List<F>];
672 partition<C>(
673 predicate: (this: C, value: T, index: number, iter: this) => unknown,
674 context?: C
675 ): [this, this];
676
677 /**
678 * Returns a List "zipped" with the provided collection.
679 *
680 * Like `zipWith`, but using the default `zipper`: creating an `Array`.
681 *
682 * <!-- runkit:activate
683 * { "preamble": "const { List } = require('immutable');" }
684 * -->
685 * ```js
686 * const a = List([ 1, 2, 3 ]);
687 * const b = List([ 4, 5, 6 ]);
688 * const c = a.zip(b); // List [ [ 1, 4 ], [ 2, 5 ], [ 3, 6 ] ]
689 * ```
690 */
691 zip<U>(other: Collection<unknown, U>): List<[T, U]>;
692 zip<U, V>(
693 other: Collection<unknown, U>,
694 other2: Collection<unknown, V>
695 ): List<[T, U, V]>;
696 zip(...collections: Array<Collection<unknown, unknown>>): List<unknown>;
697
698 /**
699 * Returns a List "zipped" with the provided collections.
700 *
701 * Unlike `zip`, `zipAll` continues zipping until the longest collection is
702 * exhausted. Missing values from shorter collections are filled with `undefined`.
703 *
704 * <!-- runkit:activate
705 * { "preamble": "const { List } = require('immutable');" }
706 * -->
707 * ```js
708 * const a = List([ 1, 2 ]);
709 * const b = List([ 3, 4, 5 ]);
710 * const c = a.zipAll(b); // List [ [ 1, 3 ], [ 2, 4 ], [ undefined, 5 ] ]
711 * ```
712 *
713 * Note: Since zipAll will return a collection as large as the largest
714 * input, some results may contain undefined values. TypeScript cannot
715 * account for these without cases (as of v2.5).
716 */
717 zipAll<U>(other: Collection<unknown, U>): List<[T, U]>;
718 zipAll<U, V>(
719 other: Collection<unknown, U>,
720 other2: Collection<unknown, V>
721 ): List<[T, U, V]>;
722 zipAll(...collections: Array<Collection<unknown, unknown>>): List<unknown>;
723
724 /**
725 * Returns a List "zipped" with the provided collections by using a
726 * custom `zipper` function.
727 *
728 * <!-- runkit:activate
729 * { "preamble": "const { List } = require('immutable');" }
730 * -->
731 * ```js
732 * const a = List([ 1, 2, 3 ]);
733 * const b = List([ 4, 5, 6 ]);
734 * const c = a.zipWith((a, b) => a + b, b);
735 * // List [ 5, 7, 9 ]
736 * ```
737 */
738 zipWith<U, Z>(
739 zipper: (value: T, otherValue: U) => Z,
740 otherCollection: Collection<unknown, U>
741 ): List<Z>;
742 zipWith<U, V, Z>(
743 zipper: (value: T, otherValue: U, thirdValue: V) => Z,
744 otherCollection: Collection<unknown, U>,
745 thirdCollection: Collection<unknown, V>
746 ): List<Z>;
747 zipWith<Z>(
748 zipper: (...values: Array<unknown>) => Z,
749 ...collections: Array<Collection<unknown, unknown>>
750 ): List<Z>;
751 }
752
753 /**
754 * Immutable Map is an unordered Collection.Keyed of (key, value) pairs with
755 * `O(log32 N)` gets and `O(log32 N)` persistent sets.
756 *
757 * Iteration order of a Map is undefined, however is stable. Multiple
758 * iterations of the same Map will iterate in the same order.
759 *
760 * Map's keys can be of any type, and use `Immutable.is` to determine key
761 * equality. This allows the use of any value (including NaN) as a key.
762 *
763 * Because `Immutable.is` returns equality based on value semantics, and
764 * Immutable collections are treated as values, any Immutable collection may
765 * be used as a key.
766 *
767 * <!-- runkit:activate -->
768 * ```js
769 * const { Map, List } = require('immutable');
770 * Map().set(List([ 1 ]), 'listofone').get(List([ 1 ]));
771 * // 'listofone'
772 * ```
773 *
774 * Any JavaScript object may be used as a key, however strict identity is used
775 * to evaluate key equality. Two similar looking objects will represent two
776 * different keys.
777 *
778 * Implemented by a hash-array mapped trie.
779 */
780 namespace Map {
781 /**
782 * True if the provided value is a Map
783 *
784 * <!-- runkit:activate -->
785 * ```js
786 * const { Map } = require('immutable')
787 * Map.isMap({}) // false
788 * Map.isMap(Map()) // true
789 * ```
790 */
791 function isMap(maybeMap: unknown): maybeMap is Map<unknown, unknown>;
792
793 /**
794 * Creates a new Map from alternating keys and values
795 *
796 * <!-- runkit:activate -->
797 * ```js
798 * const { Map } = require('immutable')
799 * Map.of(
800 * 'key', 'value',
801 * 'numerical value', 3,
802 * 0, 'numerical key'
803 * )
804 * // Map { 0: "numerical key", "key": "value", "numerical value": 3 }
805 * ```
806 *
807 * @deprecated Use Map([ [ 'k', 'v' ] ]) or Map({ k: 'v' })
808 */
809 function of(...keyValues: Array<unknown>): Map<unknown, unknown>;
810 }
811
812 /**
813 * Creates a new Immutable Map.
814 *
815 * Created with the same key value pairs as the provided Collection.Keyed or
816 * JavaScript Object or expects a Collection of [K, V] tuple entries.
817 *
818 * Note: `Map` is a factory function and not a class, and does not use the
819 * `new` keyword during construction.
820 *
821 * <!-- runkit:activate -->
822 * ```js
823 * const { Map } = require('immutable')
824 * Map({ key: "value" })
825 * Map([ [ "key", "value" ] ])
826 * ```
827 *
828 * Keep in mind, when using JS objects to construct Immutable Maps, that
829 * JavaScript Object properties are always strings, even if written in a
830 * quote-less shorthand, while Immutable Maps accept keys of any type.
831 *
832 * <!-- runkit:activate
833 * { "preamble": "const { Map } = require('immutable');" }
834 * -->
835 * ```js
836 * let obj = { 1: "one" }
837 * Object.keys(obj) // [ "1" ]
838 * assert.equal(obj["1"], obj[1]) // "one" === "one"
839 *
840 * let map = Map(obj)
841 * assert.notEqual(map.get("1"), map.get(1)) // "one" !== undefined
842 * ```
843 *
844 * Property access for JavaScript Objects first converts the key to a string,
845 * but since Immutable Map keys can be of any type the argument to `get()` is
846 * not altered.
847 */
848 function Map<K, V>(collection?: Iterable<[K, V]>): Map<K, V>;
849 function Map<R extends { [key in string | number | symbol]: unknown }>(
850 obj: R
851 ): MapOf<R>;
852 function Map<V>(obj: { [key: string]: V }): Map<string, V>;
853 function Map<K extends string | symbol, V>(obj: { [P in K]?: V }): Map<K, V>;
854
855 /**
856 * Represent a Map constructed by an object
857 *
858 * @ignore
859 */
860 interface MapOf<R extends { [key in string | number | symbol]: unknown }>
861 extends Map<keyof R, R[keyof R]> {
862 /**
863 * Returns the value associated with the provided key, or notSetValue if
864 * the Collection does not contain this key.
865 *
866 * Note: it is possible a key may be associated with an `undefined` value,
867 * so if `notSetValue` is not provided and this method returns `undefined`,
868 * that does not guarantee the key was not found.
869 */
870 get<K extends keyof R>(key: K, notSetValue?: unknown): R[K];
871 get<NSV>(key: any, notSetValue: NSV): NSV;
872
873 // https://github.com/microsoft/TypeScript/pull/39094
874 getIn<P extends ReadonlyArray<string | number | symbol>>(
875 searchKeyPath: [...P],
876 notSetValue?: unknown
877 ): RetrievePath<R, P>;
878
879 set<K extends keyof R>(key: K, value: R[K]): this;
880
881 update(updater: (value: this) => this): this;
882 update<K extends keyof R>(key: K, updater: (value: R[K]) => R[K]): this;
883 update<K extends keyof R, NSV extends R[K]>(
884 key: K,
885 notSetValue: NSV,
886 updater: (value: R[K]) => R[K]
887 ): this;
888
889 // Possible best type is MapOf<Omit<R, K>> but Omit seems to broke other function calls
890 // and generate recursion error with other methods (update, merge, etc.) until those functions are defined in MapOf
891 delete<K extends keyof R>(
892 key: K
893 ): Extract<R[K], undefined> extends never ? never : this;
894 remove<K extends keyof R>(
895 key: K
896 ): Extract<R[K], undefined> extends never ? never : this;
897
898 toJS(): { [K in keyof R]: DeepCopy<R[K]> };
899
900 toJSON(): { [K in keyof R]: R[K] };
901 }
902
903 // Loosely based off of this work.
904 // https://github.com/immutable-js/immutable-js/issues/1462#issuecomment-584123268
905
906 /** @ignore */
907 type GetMapType<S> = S extends MapOf<infer T> ? T : S;
908
909 /** @ignore */
910 type Head<T extends ReadonlyArray<any>> = T extends [
911 infer H,
912 ...Array<unknown>
913 ]
914 ? H
915 : never;
916
917 /** @ignore */
918 type Tail<T extends ReadonlyArray<any>> = T extends [unknown, ...infer I]
919 ? I
920 : Array<never>;
921
922 /** @ignore */
923 type RetrievePathReducer<
924 T,
925 C,
926 L extends ReadonlyArray<any>
927 > = C extends keyof GetMapType<T>
928 ? L extends []
929 ? GetMapType<T>[C]
930 : RetrievePathReducer<GetMapType<T>[C], Head<L>, Tail<L>>
931 : never;
932
933 /** @ignore */
934 type RetrievePath<
935 R,
936 P extends ReadonlyArray<string | number | symbol>
937 > = P extends [] ? P : RetrievePathReducer<R, Head<P>, Tail<P>>;
938
939 interface Map<K, V> extends Collection.Keyed<K, V> {
940 /**
941 * The number of entries in this Map.
942 */
943 readonly size: number;
944
945 // Persistent changes
946
947 /**
948 * Returns a new Map also containing the new key, value pair. If an equivalent
949 * key already exists in this Map, it will be replaced.
950 *
951 * <!-- runkit:activate -->
952 * ```js
953 * const { Map } = require('immutable')
954 * const originalMap = Map()
955 * const newerMap = originalMap.set('key', 'value')
956 * const newestMap = newerMap.set('key', 'newer value')
957 *
958 * originalMap
959 * // Map {}
960 * newerMap
961 * // Map { "key": "value" }
962 * newestMap
963 * // Map { "key": "newer value" }
964 * ```
965 *
966 * Note: `set` can be used in `withMutations`.
967 */
968 set(key: K, value: V): this;
969
970 /**
971 * Returns a new Map which excludes this `key`.
972 *
973 * Note: `delete` cannot be safely used in IE8, but is provided to mirror
974 * the ES6 collection API.
975 *
976 * <!-- runkit:activate -->
977 * ```js
978 * const { Map } = require('immutable')
979 * const originalMap = Map({
980 * key: 'value',
981 * otherKey: 'other value'
982 * })
983 * // Map { "key": "value", "otherKey": "other value" }
984 * originalMap.delete('otherKey')
985 * // Map { "key": "value" }
986 * ```
987 *
988 * Note: `delete` can be used in `withMutations`.
989 *
990 * @alias remove
991 */
992 delete(key: K): this;
993 remove(key: K): this;
994
995 /**
996 * Returns a new Map which excludes the provided `keys`.
997 *
998 * <!-- runkit:activate -->
999 * ```js
1000 * const { Map } = require('immutable')
1001 * const names = Map({ a: "Aaron", b: "Barry", c: "Connor" })
1002 * names.deleteAll([ 'a', 'c' ])
1003 * // Map { "b": "Barry" }
1004 * ```
1005 *
1006 * Note: `deleteAll` can be used in `withMutations`.
1007 *
1008 * @alias removeAll
1009 */
1010 deleteAll(keys: Iterable<K>): this;
1011 removeAll(keys: Iterable<K>): this;
1012
1013 /**
1014 * Returns a new Map containing no keys or values.
1015 *
1016 * <!-- runkit:activate -->
1017 * ```js
1018 * const { Map } = require('immutable')
1019 * Map({ key: 'value' }).clear()
1020 * // Map {}
1021 * ```
1022 *
1023 * Note: `clear` can be used in `withMutations`.
1024 */
1025 clear(): this;
1026
1027 /**
1028 * Returns a new Map having updated the value at this `key` with the return
1029 * value of calling `updater` with the existing value.
1030 *
1031 * Similar to: `map.set(key, updater(map.get(key)))`.
1032 *
1033 * <!-- runkit:activate -->
1034 * ```js
1035 * const { Map } = require('immutable')
1036 * const aMap = Map({ key: 'value' })
1037 * const newMap = aMap.update('key', value => value + value)
1038 * // Map { "key": "valuevalue" }
1039 * ```
1040 *
1041 * This is most commonly used to call methods on collections within a
1042 * structure of data. For example, in order to `.push()` onto a nested `List`,
1043 * `update` and `push` can be used together:
1044 *
1045 * <!-- runkit:activate
1046 * { "preamble": "const { Map, List } = require('immutable');" }
1047 * -->
1048 * ```js
1049 * const aMap = Map({ nestedList: List([ 1, 2, 3 ]) })
1050 * const newMap = aMap.update('nestedList', list => list.push(4))
1051 * // Map { "nestedList": List [ 1, 2, 3, 4 ] }
1052 * ```
1053 *
1054 * When a `notSetValue` is provided, it is provided to the `updater`
1055 * function when the value at the key does not exist in the Map.
1056 *
1057 * <!-- runkit:activate
1058 * { "preamble": "const { Map } = require('immutable');" }
1059 * -->
1060 * ```js
1061 * const aMap = Map({ key: 'value' })
1062 * const newMap = aMap.update('noKey', 'no value', value => value + value)
1063 * // Map { "key": "value", "noKey": "no valueno value" }
1064 * ```
1065 *
1066 * However, if the `updater` function returns the same value it was called
1067 * with, then no change will occur. This is still true if `notSetValue`
1068 * is provided.
1069 *
1070 * <!-- runkit:activate
1071 * { "preamble": "const { Map } = require('immutable');" }
1072 * -->
1073 * ```js
1074 * const aMap = Map({ apples: 10 })
1075 * const newMap = aMap.update('oranges', 0, val => val)
1076 * // Map { "apples": 10 }
1077 * assert.strictEqual(newMap, map);
1078 * ```
1079 *
1080 * For code using ES2015 or later, using `notSetValue` is discourged in
1081 * favor of function parameter default values. This helps to avoid any
1082 * potential confusion with identify functions as described above.
1083 *
1084 * The previous example behaves differently when written with default values:
1085 *
1086 * <!-- runkit:activate
1087 * { "preamble": "const { Map } = require('immutable');" }
1088 * -->
1089 * ```js
1090 * const aMap = Map({ apples: 10 })
1091 * const newMap = aMap.update('oranges', (val = 0) => val)
1092 * // Map { "apples": 10, "oranges": 0 }
1093 * ```
1094 *
1095 * If no key is provided, then the `updater` function return value is
1096 * returned as well.
1097 *
1098 * <!-- runkit:activate
1099 * { "preamble": "const { Map } = require('immutable');" }
1100 * -->
1101 * ```js
1102 * const aMap = Map({ key: 'value' })
1103 * const result = aMap.update(aMap => aMap.get('key'))
1104 * // "value"
1105 * ```
1106 *
1107 * This can be very useful as a way to "chain" a normal function into a
1108 * sequence of methods. RxJS calls this "let" and lodash calls it "thru".
1109 *
1110 * For example, to sum the values in a Map
1111 *
1112 * <!-- runkit:activate
1113 * { "preamble": "const { Map } = require('immutable');" }
1114 * -->
1115 * ```js
1116 * function sum(collection) {
1117 * return collection.reduce((sum, x) => sum + x, 0)
1118 * }
1119 *
1120 * Map({ x: 1, y: 2, z: 3 })
1121 * .map(x => x + 1)
1122 * .filter(x => x % 2 === 0)
1123 * .update(sum)
1124 * // 6
1125 * ```
1126 *
1127 * Note: `update(key)` can be used in `withMutations`.
1128 */
1129 update(key: K, notSetValue: V, updater: (value: V) => V): this;
1130 update(key: K, updater: (value: V | undefined) => V | undefined): this;
1131 update<R>(updater: (value: this) => R): R;
1132
1133 /**
1134 * Returns a new Map resulting from merging the provided Collections
1135 * (or JS objects) into this Map. In other words, this takes each entry of
1136 * each collection and sets it on this Map.
1137 *
1138 * Note: Values provided to `merge` are shallowly converted before being
1139 * merged. No nested values are altered.
1140 *
1141 * <!-- runkit:activate -->
1142 * ```js
1143 * const { Map } = require('immutable')
1144 * const one = Map({ a: 10, b: 20, c: 30 })
1145 * const two = Map({ b: 40, a: 50, d: 60 })
1146 * one.merge(two) // Map { "a": 50, "b": 40, "c": 30, "d": 60 }
1147 * two.merge(one) // Map { "b": 20, "a": 10, "d": 60, "c": 30 }
1148 * ```
1149 *
1150 * Note: `merge` can be used in `withMutations`.
1151 *
1152 * @alias concat
1153 */
1154 merge<KC, VC>(
1155 ...collections: Array<Iterable<[KC, VC]>>
1156 ): Map<K | KC, Exclude<V, VC> | VC>;
1157 merge<C>(
1158 ...collections: Array<{ [key: string]: C }>
1159 ): Map<K | string, Exclude<V, C> | C>;
1160
1161 concat<KC, VC>(
1162 ...collections: Array<Iterable<[KC, VC]>>
1163 ): Map<K | KC, Exclude<V, VC> | VC>;
1164 concat<C>(
1165 ...collections: Array<{ [key: string]: C }>
1166 ): Map<K | string, Exclude<V, C> | C>;
1167
1168 /**
1169 * Like `merge()`, `mergeWith()` returns a new Map resulting from merging
1170 * the provided Collections (or JS objects) into this Map, but uses the
1171 * `merger` function for dealing with conflicts.
1172 *
1173 * <!-- runkit:activate -->
1174 * ```js
1175 * const { Map } = require('immutable')
1176 * const one = Map({ a: 10, b: 20, c: 30 })
1177 * const two = Map({ b: 40, a: 50, d: 60 })
1178 * one.mergeWith((oldVal, newVal) => oldVal / newVal, two)
1179 * // { "a": 0.2, "b": 0.5, "c": 30, "d": 60 }
1180 * two.mergeWith((oldVal, newVal) => oldVal / newVal, one)
1181 * // { "b": 2, "a": 5, "d": 60, "c": 30 }
1182 * ```
1183 *
1184 * Note: `mergeWith` can be used in `withMutations`.
1185 */
1186 mergeWith<KC, VC, VCC>(
1187 merger: (oldVal: V, newVal: VC, key: K) => VCC,
1188 ...collections: Array<Iterable<[KC, VC]>>
1189 ): Map<K | KC, V | VC | VCC>;
1190 mergeWith<C, CC>(
1191 merger: (oldVal: V, newVal: C, key: string) => CC,
1192 ...collections: Array<{ [key: string]: C }>
1193 ): Map<K | string, V | C | CC>;
1194
1195 /**
1196 * Like `merge()`, but when two compatible collections are encountered with
1197 * the same key, it merges them as well, recursing deeply through the nested
1198 * data. Two collections are considered to be compatible (and thus will be
1199 * merged together) if they both fall into one of three categories: keyed
1200 * (e.g., `Map`s, `Record`s, and objects), indexed (e.g., `List`s and
1201 * arrays), or set-like (e.g., `Set`s). If they fall into separate
1202 * categories, `mergeDeep` will replace the existing collection with the
1203 * collection being merged in. This behavior can be customized by using
1204 * `mergeDeepWith()`.
1205 *
1206 * Note: Indexed and set-like collections are merged using
1207 * `concat()`/`union()` and therefore do not recurse.
1208 *
1209 * <!-- runkit:activate -->
1210 * ```js
1211 * const { Map } = require('immutable')
1212 * const one = Map({ a: Map({ x: 10, y: 10 }), b: Map({ x: 20, y: 50 }) })
1213 * const two = Map({ a: Map({ x: 2 }), b: Map({ y: 5 }), c: Map({ z: 3 }) })
1214 * one.mergeDeep(two)
1215 * // Map {
1216 * // "a": Map { "x": 2, "y": 10 },
1217 * // "b": Map { "x": 20, "y": 5 },
1218 * // "c": Map { "z": 3 }
1219 * // }
1220 * ```
1221 *
1222 * Note: `mergeDeep` can be used in `withMutations`.
1223 */
1224 mergeDeep<KC, VC>(
1225 ...collections: Array<Iterable<[KC, VC]>>
1226 ): Map<K | KC, V | VC>;
1227 mergeDeep<C>(
1228 ...collections: Array<{ [key: string]: C }>
1229 ): Map<K | string, V | C>;
1230
1231 /**
1232 * Like `mergeDeep()`, but when two non-collections or incompatible
1233 * collections are encountered at the same key, it uses the `merger`
1234 * function to determine the resulting value. Collections are considered
1235 * incompatible if they fall into separate categories between keyed,
1236 * indexed, and set-like.
1237 *
1238 * <!-- runkit:activate -->
1239 * ```js
1240 * const { Map } = require('immutable')
1241 * const one = Map({ a: Map({ x: 10, y: 10 }), b: Map({ x: 20, y: 50 }) })
1242 * const two = Map({ a: Map({ x: 2 }), b: Map({ y: 5 }), c: Map({ z: 3 }) })
1243 * one.mergeDeepWith((oldVal, newVal) => oldVal / newVal, two)
1244 * // Map {
1245 * // "a": Map { "x": 5, "y": 10 },
1246 * // "b": Map { "x": 20, "y": 10 },
1247 * // "c": Map { "z": 3 }
1248 * // }
1249 * ```
1250 *
1251 * Note: `mergeDeepWith` can be used in `withMutations`.
1252 */
1253 mergeDeepWith(
1254 merger: (oldVal: unknown, newVal: unknown, key: unknown) => unknown,
1255 ...collections: Array<Iterable<[K, V]> | { [key: string]: V }>
1256 ): this;
1257
1258 // Deep persistent changes
1259
1260 /**
1261 * Returns a new Map having set `value` at this `keyPath`. If any keys in
1262 * `keyPath` do not exist, a new immutable Map will be created at that key.
1263 *
1264 * <!-- runkit:activate -->
1265 * ```js
1266 * const { Map } = require('immutable')
1267 * const originalMap = Map({
1268 * subObject: Map({
1269 * subKey: 'subvalue',
1270 * subSubObject: Map({
1271 * subSubKey: 'subSubValue'
1272 * })
1273 * })
1274 * })
1275 *
1276 * const newMap = originalMap.setIn(['subObject', 'subKey'], 'ha ha!')
1277 * // Map {
1278 * // "subObject": Map {
1279 * // "subKey": "ha ha!",
1280 * // "subSubObject": Map { "subSubKey": "subSubValue" }
1281 * // }
1282 * // }
1283 *
1284 * const newerMap = originalMap.setIn(
1285 * ['subObject', 'subSubObject', 'subSubKey'],
1286 * 'ha ha ha!'
1287 * )
1288 * // Map {
1289 * // "subObject": Map {
1290 * // "subKey": "subvalue",
1291 * // "subSubObject": Map { "subSubKey": "ha ha ha!" }
1292 * // }
1293 * // }
1294 * ```
1295 *
1296 * Plain JavaScript Object or Arrays may be nested within an Immutable.js
1297 * Collection, and setIn() can update those values as well, treating them
1298 * immutably by creating new copies of those values with the changes applied.
1299 *
1300 * <!-- runkit:activate -->
1301 * ```js
1302 * const { Map } = require('immutable')
1303 * const originalMap = Map({
1304 * subObject: {
1305 * subKey: 'subvalue',
1306 * subSubObject: {
1307 * subSubKey: 'subSubValue'
1308 * }
1309 * }
1310 * })
1311 *
1312 * originalMap.setIn(['subObject', 'subKey'], 'ha ha!')
1313 * // Map {
1314 * // "subObject": {
1315 * // subKey: "ha ha!",
1316 * // subSubObject: { subSubKey: "subSubValue" }
1317 * // }
1318 * // }
1319 * ```
1320 *
1321 * If any key in the path exists but cannot be updated (such as a primitive
1322 * like number or a custom Object like Date), an error will be thrown.
1323 *
1324 * Note: `setIn` can be used in `withMutations`.
1325 */
1326 setIn(keyPath: Iterable<unknown>, value: unknown): this;
1327
1328 /**
1329 * Returns a new Map having removed the value at this `keyPath`. If any keys
1330 * in `keyPath` do not exist, no change will occur.
1331 *
1332 * Note: `deleteIn` can be used in `withMutations`.
1333 *
1334 * @alias removeIn
1335 */
1336 deleteIn(keyPath: Iterable<unknown>): this;
1337 removeIn(keyPath: Iterable<unknown>): this;
1338
1339 /**
1340 * Returns a new Map having applied the `updater` to the entry found at the
1341 * keyPath.
1342 *
1343 * This is most commonly used to call methods on collections nested within a
1344 * structure of data. For example, in order to `.push()` onto a nested `List`,
1345 * `updateIn` and `push` can be used together:
1346 *
1347 * <!-- runkit:activate -->
1348 * ```js
1349 * const { Map, List } = require('immutable')
1350 * const map = Map({ inMap: Map({ inList: List([ 1, 2, 3 ]) }) })
1351 * const newMap = map.updateIn(['inMap', 'inList'], list => list.push(4))
1352 * // Map { "inMap": Map { "inList": List [ 1, 2, 3, 4 ] } }
1353 * ```
1354 *
1355 * If any keys in `keyPath` do not exist, new Immutable `Map`s will
1356 * be created at those keys. If the `keyPath` does not already contain a
1357 * value, the `updater` function will be called with `notSetValue`, if
1358 * provided, otherwise `undefined`.
1359 *
1360 * <!-- runkit:activate
1361 * { "preamble": "const { Map } = require('immutable')" }
1362 * -->
1363 * ```js
1364 * const map = Map({ a: Map({ b: Map({ c: 10 }) }) })
1365 * const newMap = map.updateIn(['a', 'b', 'c'], val => val * 2)
1366 * // Map { "a": Map { "b": Map { "c": 20 } } }
1367 * ```
1368 *
1369 * If the `updater` function returns the same value it was called with, then
1370 * no change will occur. This is still true if `notSetValue` is provided.
1371 *
1372 * <!-- runkit:activate
1373 * { "preamble": "const { Map } = require('immutable')" }
1374 * -->
1375 * ```js
1376 * const map = Map({ a: Map({ b: Map({ c: 10 }) }) })
1377 * const newMap = map.updateIn(['a', 'b', 'x'], 100, val => val)
1378 * // Map { "a": Map { "b": Map { "c": 10 } } }
1379 * assert.strictEqual(newMap, aMap)
1380 * ```
1381 *
1382 * For code using ES2015 or later, using `notSetValue` is discourged in
1383 * favor of function parameter default values. This helps to avoid any
1384 * potential confusion with identify functions as described above.
1385 *
1386 * The previous example behaves differently when written with default values:
1387 *
1388 * <!-- runkit:activate
1389 * { "preamble": "const { Map } = require('immutable')" }
1390 * -->
1391 * ```js
1392 * const map = Map({ a: Map({ b: Map({ c: 10 }) }) })
1393 * const newMap = map.updateIn(['a', 'b', 'x'], (val = 100) => val)
1394 * // Map { "a": Map { "b": Map { "c": 10, "x": 100 } } }
1395 * ```
1396 *
1397 * Plain JavaScript Object or Arrays may be nested within an Immutable.js
1398 * Collection, and updateIn() can update those values as well, treating them
1399 * immutably by creating new copies of those values with the changes applied.
1400 *
1401 * <!-- runkit:activate
1402 * { "preamble": "const { Map } = require('immutable')" }
1403 * -->
1404 * ```js
1405 * const map = Map({ a: { b: { c: 10 } } })
1406 * const newMap = map.updateIn(['a', 'b', 'c'], val => val * 2)
1407 * // Map { "a": { b: { c: 20 } } }
1408 * ```
1409 *
1410 * If any key in the path exists but cannot be updated (such as a primitive
1411 * like number or a custom Object like Date), an error will be thrown.
1412 *
1413 * Note: `updateIn` can be used in `withMutations`.
1414 */
1415 updateIn(
1416 keyPath: Iterable<unknown>,
1417 notSetValue: unknown,
1418 updater: (value: unknown) => unknown
1419 ): this;
1420 updateIn(
1421 keyPath: Iterable<unknown>,
1422 updater: (value: unknown) => unknown
1423 ): this;
1424
1425 /**
1426 * A combination of `updateIn` and `merge`, returning a new Map, but
1427 * performing the merge at a point arrived at by following the keyPath.
1428 * In other words, these two lines are equivalent:
1429 *
1430 * ```js
1431 * map.updateIn(['a', 'b', 'c'], abc => abc.merge(y))
1432 * map.mergeIn(['a', 'b', 'c'], y)
1433 * ```
1434 *
1435 * Note: `mergeIn` can be used in `withMutations`.
1436 */
1437 mergeIn(keyPath: Iterable<unknown>, ...collections: Array<unknown>): this;
1438
1439 /**
1440 * A combination of `updateIn` and `mergeDeep`, returning a new Map, but
1441 * performing the deep merge at a point arrived at by following the keyPath.
1442 * In other words, these two lines are equivalent:
1443 *
1444 * ```js
1445 * map.updateIn(['a', 'b', 'c'], abc => abc.mergeDeep(y))
1446 * map.mergeDeepIn(['a', 'b', 'c'], y)
1447 * ```
1448 *
1449 * Note: `mergeDeepIn` can be used in `withMutations`.
1450 */
1451 mergeDeepIn(
1452 keyPath: Iterable<unknown>,
1453 ...collections: Array<unknown>
1454 ): this;
1455
1456 // Transient changes
1457
1458 /**
1459 * Every time you call one of the above functions, a new immutable Map is
1460 * created. If a pure function calls a number of these to produce a final
1461 * return value, then a penalty on performance and memory has been paid by
1462 * creating all of the intermediate immutable Maps.
1463 *
1464 * If you need to apply a series of mutations to produce a new immutable
1465 * Map, `withMutations()` creates a temporary mutable copy of the Map which
1466 * can apply mutations in a highly performant manner. In fact, this is
1467 * exactly how complex mutations like `merge` are done.
1468 *
1469 * As an example, this results in the creation of 2, not 4, new Maps:
1470 *
1471 * <!-- runkit:activate -->
1472 * ```js
1473 * const { Map } = require('immutable')
1474 * const map1 = Map()
1475 * const map2 = map1.withMutations(map => {
1476 * map.set('a', 1).set('b', 2).set('c', 3)
1477 * })
1478 * assert.equal(map1.size, 0)
1479 * assert.equal(map2.size, 3)
1480 * ```
1481 *
1482 * Note: Not all methods can be used on a mutable collection or within
1483 * `withMutations`! Read the documentation for each method to see if it
1484 * is safe to use in `withMutations`.
1485 */
1486 withMutations(mutator: (mutable: this) => unknown): this;
1487
1488 /**
1489 * Another way to avoid creation of intermediate Immutable maps is to create
1490 * a mutable copy of this collection. Mutable copies *always* return `this`,
1491 * and thus shouldn't be used for equality. Your function should never return
1492 * a mutable copy of a collection, only use it internally to create a new
1493 * collection.
1494 *
1495 * If possible, use `withMutations` to work with temporary mutable copies as
1496 * it provides an easier to use API and considers many common optimizations.
1497 *
1498 * Note: if the collection is already mutable, `asMutable` returns itself.
1499 *
1500 * Note: Not all methods can be used on a mutable collection or within
1501 * `withMutations`! Read the documentation for each method to see if it
1502 * is safe to use in `withMutations`.
1503 *
1504 * @see `Map#asImmutable`
1505 */
1506 asMutable(): this;
1507
1508 /**
1509 * Returns true if this is a mutable copy (see `asMutable()`) and mutative
1510 * alterations have been applied.
1511 *
1512 * @see `Map#asMutable`
1513 */
1514 wasAltered(): boolean;
1515
1516 /**
1517 * The yin to `asMutable`'s yang. Because it applies to mutable collections,
1518 * this operation is *mutable* and may return itself (though may not
1519 * return itself, i.e. if the result is an empty collection). Once
1520 * performed, the original mutable copy must no longer be mutated since it
1521 * may be the immutable result.
1522 *
1523 * If possible, use `withMutations` to work with temporary mutable copies as
1524 * it provides an easier to use API and considers many common optimizations.
1525 *
1526 * @see `Map#asMutable`
1527 */
1528 asImmutable(): this;
1529
1530 // Sequence algorithms
1531
1532 /**
1533 * Returns a new Map with values passed through a
1534 * `mapper` function.
1535 *
1536 * Map({ a: 1, b: 2 }).map(x => 10 * x)
1537 * // Map { a: 10, b: 20 }
1538 */
1539 map<M>(
1540 mapper: (value: V, key: K, iter: this) => M,
1541 context?: unknown
1542 ): Map<K, M>;
1543
1544 /**
1545 * @see Collection.Keyed.mapKeys
1546 */
1547 mapKeys<M>(
1548 mapper: (key: K, value: V, iter: this) => M,
1549 context?: unknown
1550 ): Map<M, V>;
1551
1552 /**
1553 * @see Collection.Keyed.mapEntries
1554 */
1555 mapEntries<KM, VM>(
1556 mapper: (
1557 entry: [K, V],
1558 index: number,
1559 iter: this
1560 ) => [KM, VM] | undefined,
1561 context?: unknown
1562 ): Map<KM, VM>;
1563
1564 /**
1565 * Flat-maps the Map, returning a new Map.
1566 *
1567 * Similar to `data.map(...).flatten(true)`.
1568 */
1569 flatMap<KM, VM>(
1570 mapper: (value: V, key: K, iter: this) => Iterable<[KM, VM]>,
1571 context?: unknown
1572 ): Map<KM, VM>;
1573
1574 /**
1575 * Returns a new Map with only the entries for which the `predicate`
1576 * function returns true.
1577 *
1578 * Note: `filter()` always returns a new instance, even if it results in
1579 * not filtering out any values.
1580 */
1581 filter<F extends V>(
1582 predicate: (value: V, key: K, iter: this) => value is F,
1583 context?: unknown
1584 ): Map<K, F>;
1585 filter(
1586 predicate: (value: V, key: K, iter: this) => unknown,
1587 context?: unknown
1588 ): this;
1589
1590 /**
1591 * Returns a new Map with the values for which the `predicate`
1592 * function returns false and another for which is returns true.
1593 */
1594 partition<F extends V, C>(
1595 predicate: (this: C, value: V, key: K, iter: this) => value is F,
1596 context?: C
1597 ): [Map<K, V>, Map<K, F>];
1598 partition<C>(
1599 predicate: (this: C, value: V, key: K, iter: this) => unknown,
1600 context?: C
1601 ): [this, this];
1602
1603 /**
1604 * @see Collection.Keyed.flip
1605 */
1606 flip(): Map<V, K>;
1607 }
1608
1609 /**
1610 * A type of Map that has the additional guarantee that the iteration order of
1611 * entries will be the order in which they were set().
1612 *
1613 * The iteration behavior of OrderedMap is the same as native ES6 Map and
1614 * JavaScript Object.
1615 *
1616 * Note that `OrderedMap` are more expensive than non-ordered `Map` and may
1617 * consume more memory. `OrderedMap#set` is amortized O(log32 N), but not
1618 * stable.
1619 */
1620 namespace OrderedMap {
1621 /**
1622 * True if the provided value is an OrderedMap.
1623 */
1624 function isOrderedMap(
1625 maybeOrderedMap: unknown
1626 ): maybeOrderedMap is OrderedMap<unknown, unknown>;
1627 }
1628
1629 /**
1630 * Creates a new Immutable OrderedMap.
1631 *
1632 * Created with the same key value pairs as the provided Collection.Keyed or
1633 * JavaScript Object or expects a Collection of [K, V] tuple entries.
1634 *
1635 * The iteration order of key-value pairs provided to this constructor will
1636 * be preserved in the OrderedMap.
1637 *
1638 * let newOrderedMap = OrderedMap({key: "value"})
1639 * let newOrderedMap = OrderedMap([["key", "value"]])
1640 *
1641 * Note: `OrderedMap` is a factory function and not a class, and does not use
1642 * the `new` keyword during construction.
1643 */
1644 function OrderedMap<K, V>(collection?: Iterable<[K, V]>): OrderedMap<K, V>;
1645 function OrderedMap<V>(obj: { [key: string]: V }): OrderedMap<string, V>;
1646
1647 interface OrderedMap<K, V> extends Map<K, V> {
1648 /**
1649 * The number of entries in this OrderedMap.
1650 */
1651 readonly size: number;
1652
1653 /**
1654 * Returns a new OrderedMap also containing the new key, value pair. If an
1655 * equivalent key already exists in this OrderedMap, it will be replaced
1656 * while maintaining the existing order.
1657 *
1658 * <!-- runkit:activate -->
1659 * ```js
1660 * const { OrderedMap } = require('immutable')
1661 * const originalMap = OrderedMap({a:1, b:1, c:1})
1662 * const updatedMap = originalMap.set('b', 2)
1663 *
1664 * originalMap
1665 * // OrderedMap {a: 1, b: 1, c: 1}
1666 * updatedMap
1667 * // OrderedMap {a: 1, b: 2, c: 1}
1668 * ```
1669 *
1670 * Note: `set` can be used in `withMutations`.
1671 */
1672 set(key: K, value: V): this;
1673
1674 /**
1675 * Returns a new OrderedMap resulting from merging the provided Collections
1676 * (or JS objects) into this OrderedMap. In other words, this takes each
1677 * entry of each collection and sets it on this OrderedMap.
1678 *
1679 * Note: Values provided to `merge` are shallowly converted before being
1680 * merged. No nested values are altered.
1681 *
1682 * <!-- runkit:activate -->
1683 * ```js
1684 * const { OrderedMap } = require('immutable')
1685 * const one = OrderedMap({ a: 10, b: 20, c: 30 })
1686 * const two = OrderedMap({ b: 40, a: 50, d: 60 })
1687 * one.merge(two) // OrderedMap { "a": 50, "b": 40, "c": 30, "d": 60 }
1688 * two.merge(one) // OrderedMap { "b": 20, "a": 10, "d": 60, "c": 30 }
1689 * ```
1690 *
1691 * Note: `merge` can be used in `withMutations`.
1692 *
1693 * @alias concat
1694 */
1695 merge<KC, VC>(
1696 ...collections: Array<Iterable<[KC, VC]>>
1697 ): OrderedMap<K | KC, Exclude<V, VC> | VC>;
1698 merge<C>(
1699 ...collections: Array<{ [key: string]: C }>
1700 ): OrderedMap<K | string, Exclude<V, C> | C>;
1701
1702 concat<KC, VC>(
1703 ...collections: Array<Iterable<[KC, VC]>>
1704 ): OrderedMap<K | KC, Exclude<V, VC> | VC>;
1705 concat<C>(
1706 ...collections: Array<{ [key: string]: C }>
1707 ): OrderedMap<K | string, Exclude<V, C> | C>;
1708
1709 mergeWith<KC, VC, VCC>(
1710 merger: (oldVal: V, newVal: VC, key: K) => VCC,
1711 ...collections: Array<Iterable<[KC, VC]>>
1712 ): OrderedMap<K | KC, V | VC | VCC>;
1713 mergeWith<C, CC>(
1714 merger: (oldVal: V, newVal: C, key: string) => CC,
1715 ...collections: Array<{ [key: string]: C }>
1716 ): OrderedMap<K | string, V | C | CC>;
1717
1718 mergeDeep<KC, VC>(
1719 ...collections: Array<Iterable<[KC, VC]>>
1720 ): OrderedMap<K | KC, V | VC>;
1721 mergeDeep<C>(
1722 ...collections: Array<{ [key: string]: C }>
1723 ): OrderedMap<K | string, V | C>;
1724
1725 // Sequence algorithms
1726
1727 /**
1728 * Returns a new OrderedMap with values passed through a
1729 * `mapper` function.
1730 *
1731 * OrderedMap({ a: 1, b: 2 }).map(x => 10 * x)
1732 * // OrderedMap { "a": 10, "b": 20 }
1733 *
1734 * Note: `map()` always returns a new instance, even if it produced the same
1735 * value at every step.
1736 */
1737 map<M>(
1738 mapper: (value: V, key: K, iter: this) => M,
1739 context?: unknown
1740 ): OrderedMap<K, M>;
1741
1742 /**
1743 * @see Collection.Keyed.mapKeys
1744 */
1745 mapKeys<M>(
1746 mapper: (key: K, value: V, iter: this) => M,
1747 context?: unknown
1748 ): OrderedMap<M, V>;
1749
1750 /**
1751 * @see Collection.Keyed.mapEntries
1752 */
1753 mapEntries<KM, VM>(
1754 mapper: (
1755 entry: [K, V],
1756 index: number,
1757 iter: this
1758 ) => [KM, VM] | undefined,
1759 context?: unknown
1760 ): OrderedMap<KM, VM>;
1761
1762 /**
1763 * Flat-maps the OrderedMap, returning a new OrderedMap.
1764 *
1765 * Similar to `data.map(...).flatten(true)`.
1766 */
1767 flatMap<KM, VM>(
1768 mapper: (value: V, key: K, iter: this) => Iterable<[KM, VM]>,
1769 context?: unknown
1770 ): OrderedMap<KM, VM>;
1771
1772 /**
1773 * Returns a new OrderedMap with only the entries for which the `predicate`
1774 * function returns true.
1775 *
1776 * Note: `filter()` always returns a new instance, even if it results in
1777 * not filtering out any values.
1778 */
1779 filter<F extends V>(
1780 predicate: (value: V, key: K, iter: this) => value is F,
1781 context?: unknown
1782 ): OrderedMap<K, F>;
1783 filter(
1784 predicate: (value: V, key: K, iter: this) => unknown,
1785 context?: unknown
1786 ): this;
1787
1788 /**
1789 * Returns a new OrderedMap with the values for which the `predicate`
1790 * function returns false and another for which is returns true.
1791 */
1792 partition<F extends V, C>(
1793 predicate: (this: C, value: V, key: K, iter: this) => value is F,
1794 context?: C
1795 ): [OrderedMap<K, V>, OrderedMap<K, F>];
1796 partition<C>(
1797 predicate: (this: C, value: V, key: K, iter: this) => unknown,
1798 context?: C
1799 ): [this, this];
1800
1801 /**
1802 * @see Collection.Keyed.flip
1803 */
1804 flip(): OrderedMap<V, K>;
1805 }
1806
1807 /**
1808 * A Collection of unique values with `O(log32 N)` adds and has.
1809 *
1810 * When iterating a Set, the entries will be (value, value) pairs. Iteration
1811 * order of a Set is undefined, however is stable. Multiple iterations of the
1812 * same Set will iterate in the same order.
1813 *
1814 * Set values, like Map keys, may be of any type. Equality is determined using
1815 * `Immutable.is`, enabling Sets to uniquely include other Immutable
1816 * collections, custom value types, and NaN.
1817 */
1818 namespace Set {
1819 /**
1820 * True if the provided value is a Set
1821 */
1822 function isSet(maybeSet: unknown): maybeSet is Set<unknown>;
1823
1824 /**
1825 * Creates a new Set containing `values`.
1826 */
1827 function of<T>(...values: Array<T>): Set<T>;
1828
1829 /**
1830 * `Set.fromKeys()` creates a new immutable Set containing the keys from
1831 * this Collection or JavaScript Object.
1832 */
1833 function fromKeys<T>(iter: Collection.Keyed<T, unknown>): Set<T>;
1834 // tslint:disable-next-line unified-signatures
1835 function fromKeys<T>(iter: Collection<T, unknown>): Set<T>;
1836 function fromKeys(obj: { [key: string]: unknown }): Set<string>;
1837
1838 /**
1839 * `Set.intersect()` creates a new immutable Set that is the intersection of
1840 * a collection of other sets.
1841 *
1842 * ```js
1843 * const { Set } = require('immutable')
1844 * const intersected = Set.intersect([
1845 * Set([ 'a', 'b', 'c' ])
1846 * Set([ 'c', 'a', 't' ])
1847 * ])
1848 * // Set [ "a", "c" ]
1849 * ```
1850 */
1851 function intersect<T>(sets: Iterable<Iterable<T>>): Set<T>;
1852
1853 /**
1854 * `Set.union()` creates a new immutable Set that is the union of a
1855 * collection of other sets.
1856 *
1857 * ```js
1858 * const { Set } = require('immutable')
1859 * const unioned = Set.union([
1860 * Set([ 'a', 'b', 'c' ])
1861 * Set([ 'c', 'a', 't' ])
1862 * ])
1863 * // Set [ "a", "b", "c", "t" ]
1864 * ```
1865 */
1866 function union<T>(sets: Iterable<Iterable<T>>): Set<T>;
1867 }
1868
1869 /**
1870 * Create a new immutable Set containing the values of the provided
1871 * collection-like.
1872 *
1873 * Note: `Set` is a factory function and not a class, and does not use the
1874 * `new` keyword during construction.
1875 */
1876 function Set<T>(collection?: Iterable<T> | ArrayLike<T>): Set<T>;
1877
1878 interface Set<T> extends Collection.Set<T> {
1879 /**
1880 * The number of items in this Set.
1881 */
1882 readonly size: number;
1883
1884 // Persistent changes
1885
1886 /**
1887 * Returns a new Set which also includes this value.
1888 *
1889 * Note: `add` can be used in `withMutations`.
1890 */
1891 add(value: T): this;
1892
1893 /**
1894 * Returns a new Set which excludes this value.
1895 *
1896 * Note: `delete` can be used in `withMutations`.
1897 *
1898 * Note: `delete` **cannot** be safely used in IE8, use `remove` if
1899 * supporting old browsers.
1900 *
1901 * @alias remove
1902 */
1903 delete(value: T): this;
1904 remove(value: T): this;
1905
1906 /**
1907 * Returns a new Set containing no values.
1908 *
1909 * Note: `clear` can be used in `withMutations`.
1910 */
1911 clear(): this;
1912
1913 /**
1914 * Returns a Set including any value from `collections` that does not already
1915 * exist in this Set.
1916 *
1917 * Note: `union` can be used in `withMutations`.
1918 * @alias merge
1919 * @alias concat
1920 */
1921 union<C>(...collections: Array<Iterable<C>>): Set<T | C>;
1922 merge<C>(...collections: Array<Iterable<C>>): Set<T | C>;
1923 concat<C>(...collections: Array<Iterable<C>>): Set<T | C>;
1924
1925 /**
1926 * Returns a Set which has removed any values not also contained
1927 * within `collections`.
1928 *
1929 * Note: `intersect` can be used in `withMutations`.
1930 */
1931 intersect(...collections: Array<Iterable<T>>): this;
1932
1933 /**
1934 * Returns a Set excluding any values contained within `collections`.
1935 *
1936 * <!-- runkit:activate -->
1937 * ```js
1938 * const { OrderedSet } = require('immutable')
1939 * OrderedSet([ 1, 2, 3 ]).subtract([1, 3])
1940 * // OrderedSet [2]
1941 * ```
1942 *
1943 * Note: `subtract` can be used in `withMutations`.
1944 */
1945 subtract(...collections: Array<Iterable<T>>): this;
1946
1947 // Transient changes
1948
1949 /**
1950 * Note: Not all methods can be used on a mutable collection or within
1951 * `withMutations`! Check the documentation for each method to see if it
1952 * mentions being safe to use in `withMutations`.
1953 *
1954 * @see `Map#withMutations`
1955 */
1956 withMutations(mutator: (mutable: this) => unknown): this;
1957
1958 /**
1959 * Note: Not all methods can be used on a mutable collection or within
1960 * `withMutations`! Check the documentation for each method to see if it
1961 * mentions being safe to use in `withMutations`.
1962 *
1963 * @see `Map#asMutable`
1964 */
1965 asMutable(): this;
1966
1967 /**
1968 * @see `Map#wasAltered`
1969 */
1970 wasAltered(): boolean;
1971
1972 /**
1973 * @see `Map#asImmutable`
1974 */
1975 asImmutable(): this;
1976
1977 // Sequence algorithms
1978
1979 /**
1980 * Returns a new Set with values passed through a
1981 * `mapper` function.
1982 *
1983 * Set([1,2]).map(x => 10 * x)
1984 * // Set [10,20]
1985 */
1986 map<M>(
1987 mapper: (value: T, key: T, iter: this) => M,
1988 context?: unknown
1989 ): Set<M>;
1990
1991 /**
1992 * Flat-maps the Set, returning a new Set.
1993 *
1994 * Similar to `set.map(...).flatten(true)`.
1995 */
1996 flatMap<M>(
1997 mapper: (value: T, key: T, iter: this) => Iterable<M>,
1998 context?: unknown
1999 ): Set<M>;
2000
2001 /**
2002 * Returns a new Set with only the values for which the `predicate`
2003 * function returns true.
2004 *
2005 * Note: `filter()` always returns a new instance, even if it results in
2006 * not filtering out any values.
2007 */
2008 filter<F extends T>(
2009 predicate: (value: T, key: T, iter: this) => value is F,
2010 context?: unknown
2011 ): Set<F>;
2012 filter(
2013 predicate: (value: T, key: T, iter: this) => unknown,
2014 context?: unknown
2015 ): this;
2016
2017 /**
2018 * Returns a new Set with the values for which the `predicate` function
2019 * returns false and another for which is returns true.
2020 */
2021 partition<F extends T, C>(
2022 predicate: (this: C, value: T, key: T, iter: this) => value is F,
2023 context?: C
2024 ): [Set<T>, Set<F>];
2025 partition<C>(
2026 predicate: (this: C, value: T, key: T, iter: this) => unknown,
2027 context?: C
2028 ): [this, this];
2029 }
2030
2031 /**
2032 * A type of Set that has the additional guarantee that the iteration order of
2033 * values will be the order in which they were `add`ed.
2034 *
2035 * The iteration behavior of OrderedSet is the same as native ES6 Set.
2036 *
2037 * Note that `OrderedSet` are more expensive than non-ordered `Set` and may
2038 * consume more memory. `OrderedSet#add` is amortized O(log32 N), but not
2039 * stable.
2040 */
2041 namespace OrderedSet {
2042 /**
2043 * True if the provided value is an OrderedSet.
2044 */
2045 function isOrderedSet(
2046 maybeOrderedSet: unknown
2047 ): maybeOrderedSet is OrderedSet<unknown>;
2048
2049 /**
2050 * Creates a new OrderedSet containing `values`.
2051 */
2052 function of<T>(...values: Array<T>): OrderedSet<T>;
2053
2054 /**
2055 * `OrderedSet.fromKeys()` creates a new immutable OrderedSet containing
2056 * the keys from this Collection or JavaScript Object.
2057 */
2058 function fromKeys<T>(iter: Collection.Keyed<T, unknown>): OrderedSet<T>;
2059 // tslint:disable-next-line unified-signatures
2060 function fromKeys<T>(iter: Collection<T, unknown>): OrderedSet<T>;
2061 function fromKeys(obj: { [key: string]: unknown }): OrderedSet<string>;
2062 }
2063
2064 /**
2065 * Create a new immutable OrderedSet containing the values of the provided
2066 * collection-like.
2067 *
2068 * Note: `OrderedSet` is a factory function and not a class, and does not use
2069 * the `new` keyword during construction.
2070 */
2071 function OrderedSet<T>(
2072 collection?: Iterable<T> | ArrayLike<T>
2073 ): OrderedSet<T>;
2074
2075 interface OrderedSet<T> extends Set<T> {
2076 /**
2077 * The number of items in this OrderedSet.
2078 */
2079 readonly size: number;
2080
2081 /**
2082 * Returns an OrderedSet including any value from `collections` that does
2083 * not already exist in this OrderedSet.
2084 *
2085 * Note: `union` can be used in `withMutations`.
2086 * @alias merge
2087 * @alias concat
2088 */
2089 union<C>(...collections: Array<Iterable<C>>): OrderedSet<T | C>;
2090 merge<C>(...collections: Array<Iterable<C>>): OrderedSet<T | C>;
2091 concat<C>(...collections: Array<Iterable<C>>): OrderedSet<T | C>;
2092
2093 // Sequence algorithms
2094
2095 /**
2096 * Returns a new Set with values passed through a
2097 * `mapper` function.
2098 *
2099 * OrderedSet([ 1, 2 ]).map(x => 10 * x)
2100 * // OrderedSet [10, 20]
2101 */
2102 map<M>(
2103 mapper: (value: T, key: T, iter: this) => M,
2104 context?: unknown
2105 ): OrderedSet<M>;
2106
2107 /**
2108 * Flat-maps the OrderedSet, returning a new OrderedSet.
2109 *
2110 * Similar to `set.map(...).flatten(true)`.
2111 */
2112 flatMap<M>(
2113 mapper: (value: T, key: T, iter: this) => Iterable<M>,
2114 context?: unknown
2115 ): OrderedSet<M>;
2116
2117 /**
2118 * Returns a new OrderedSet with only the values for which the `predicate`
2119 * function returns true.
2120 *
2121 * Note: `filter()` always returns a new instance, even if it results in
2122 * not filtering out any values.
2123 */
2124 filter<F extends T>(
2125 predicate: (value: T, key: T, iter: this) => value is F,
2126 context?: unknown
2127 ): OrderedSet<F>;
2128 filter(
2129 predicate: (value: T, key: T, iter: this) => unknown,
2130 context?: unknown
2131 ): this;
2132
2133 /**
2134 * Returns a new OrderedSet with the values for which the `predicate`
2135 * function returns false and another for which is returns true.
2136 */
2137 partition<F extends T, C>(
2138 predicate: (this: C, value: T, key: T, iter: this) => value is F,
2139 context?: C
2140 ): [OrderedSet<T>, OrderedSet<F>];
2141 partition<C>(
2142 predicate: (this: C, value: T, key: T, iter: this) => unknown,
2143 context?: C
2144 ): [this, this];
2145
2146 /**
2147 * Returns an OrderedSet of the same type "zipped" with the provided
2148 * collections.
2149 *
2150 * Like `zipWith`, but using the default `zipper`: creating an `Array`.
2151 *
2152 * ```js
2153 * const a = OrderedSet([ 1, 2, 3 ])
2154 * const b = OrderedSet([ 4, 5, 6 ])
2155 * const c = a.zip(b)
2156 * // OrderedSet [ [ 1, 4 ], [ 2, 5 ], [ 3, 6 ] ]
2157 * ```
2158 */
2159 zip<U>(other: Collection<unknown, U>): OrderedSet<[T, U]>;
2160 zip<U, V>(
2161 other1: Collection<unknown, U>,
2162 other2: Collection<unknown, V>
2163 ): OrderedSet<[T, U, V]>;
2164 zip(
2165 ...collections: Array<Collection<unknown, unknown>>
2166 ): OrderedSet<unknown>;
2167
2168 /**
2169 * Returns a OrderedSet of the same type "zipped" with the provided
2170 * collections.
2171 *
2172 * Unlike `zip`, `zipAll` continues zipping until the longest collection is
2173 * exhausted. Missing values from shorter collections are filled with `undefined`.
2174 *
2175 * ```js
2176 * const a = OrderedSet([ 1, 2 ]);
2177 * const b = OrderedSet([ 3, 4, 5 ]);
2178 * const c = a.zipAll(b); // OrderedSet [ [ 1, 3 ], [ 2, 4 ], [ undefined, 5 ] ]
2179 * ```
2180 *
2181 * Note: Since zipAll will return a collection as large as the largest
2182 * input, some results may contain undefined values. TypeScript cannot
2183 * account for these without cases (as of v2.5).
2184 */
2185 zipAll<U>(other: Collection<unknown, U>): OrderedSet<[T, U]>;
2186 zipAll<U, V>(
2187 other1: Collection<unknown, U>,
2188 other2: Collection<unknown, V>
2189 ): OrderedSet<[T, U, V]>;
2190 zipAll(
2191 ...collections: Array<Collection<unknown, unknown>>
2192 ): OrderedSet<unknown>;
2193
2194 /**
2195 * Returns an OrderedSet of the same type "zipped" with the provided
2196 * collections by using a custom `zipper` function.
2197 *
2198 * @see Seq.Indexed.zipWith
2199 */
2200 zipWith<U, Z>(
2201 zipper: (value: T, otherValue: U) => Z,
2202 otherCollection: Collection<unknown, U>
2203 ): OrderedSet<Z>;
2204 zipWith<U, V, Z>(
2205 zipper: (value: T, otherValue: U, thirdValue: V) => Z,
2206 otherCollection: Collection<unknown, U>,
2207 thirdCollection: Collection<unknown, V>
2208 ): OrderedSet<Z>;
2209 zipWith<Z>(
2210 zipper: (...values: Array<unknown>) => Z,
2211 ...collections: Array<Collection<unknown, unknown>>
2212 ): OrderedSet<Z>;
2213 }
2214
2215 /**
2216 * Stacks are indexed collections which support very efficient O(1) addition
2217 * and removal from the front using `unshift(v)` and `shift()`.
2218 *
2219 * For familiarity, Stack also provides `push(v)`, `pop()`, and `peek()`, but
2220 * be aware that they also operate on the front of the list, unlike List or
2221 * a JavaScript Array.
2222 *
2223 * Note: `reverse()` or any inherent reverse traversal (`reduceRight`,
2224 * `lastIndexOf`, etc.) is not efficient with a Stack.
2225 *
2226 * Stack is implemented with a Single-Linked List.
2227 */
2228 namespace Stack {
2229 /**
2230 * True if the provided value is a Stack
2231 */
2232 function isStack(maybeStack: unknown): maybeStack is Stack<unknown>;
2233
2234 /**
2235 * Creates a new Stack containing `values`.
2236 */
2237 function of<T>(...values: Array<T>): Stack<T>;
2238 }
2239
2240 /**
2241 * Create a new immutable Stack containing the values of the provided
2242 * collection-like.
2243 *
2244 * The iteration order of the provided collection is preserved in the
2245 * resulting `Stack`.
2246 *
2247 * Note: `Stack` is a factory function and not a class, and does not use the
2248 * `new` keyword during construction.
2249 */
2250 function Stack<T>(collection?: Iterable<T> | ArrayLike<T>): Stack<T>;
2251
2252 interface Stack<T> extends Collection.Indexed<T> {
2253 /**
2254 * The number of items in this Stack.
2255 */
2256 readonly size: number;
2257
2258 // Reading values
2259
2260 /**
2261 * Alias for `Stack.first()`.
2262 */
2263 peek(): T | undefined;
2264
2265 // Persistent changes
2266
2267 /**
2268 * Returns a new Stack with 0 size and no values.
2269 *
2270 * Note: `clear` can be used in `withMutations`.
2271 */
2272 clear(): Stack<T>;
2273
2274 /**
2275 * Returns a new Stack with the provided `values` prepended, shifting other
2276 * values ahead to higher indices.
2277 *
2278 * This is very efficient for Stack.
2279 *
2280 * Note: `unshift` can be used in `withMutations`.
2281 */
2282 unshift(...values: Array<T>): Stack<T>;
2283
2284 /**
2285 * Like `Stack#unshift`, but accepts a collection rather than varargs.
2286 *
2287 * Note: `unshiftAll` can be used in `withMutations`.
2288 */
2289 unshiftAll(iter: Iterable<T>): Stack<T>;
2290
2291 /**
2292 * Returns a new Stack with a size ones less than this Stack, excluding
2293 * the first item in this Stack, shifting all other values to a lower index.
2294 *
2295 * Note: this differs from `Array#shift` because it returns a new
2296 * Stack rather than the removed value. Use `first()` or `peek()` to get the
2297 * first value in this Stack.
2298 *
2299 * Note: `shift` can be used in `withMutations`.
2300 */
2301 shift(): Stack<T>;
2302
2303 /**
2304 * Alias for `Stack#unshift` and is not equivalent to `List#push`.
2305 */
2306 push(...values: Array<T>): Stack<T>;
2307
2308 /**
2309 * Alias for `Stack#unshiftAll`.
2310 */
2311 pushAll(iter: Iterable<T>): Stack<T>;
2312
2313 /**
2314 * Alias for `Stack#shift` and is not equivalent to `List#pop`.
2315 */
2316 pop(): Stack<T>;
2317
2318 // Transient changes
2319
2320 /**
2321 * Note: Not all methods can be used on a mutable collection or within
2322 * `withMutations`! Check the documentation for each method to see if it
2323 * mentions being safe to use in `withMutations`.
2324 *
2325 * @see `Map#withMutations`
2326 */
2327 withMutations(mutator: (mutable: this) => unknown): this;
2328
2329 /**
2330 * Note: Not all methods can be used on a mutable collection or within
2331 * `withMutations`! Check the documentation for each method to see if it
2332 * mentions being safe to use in `withMutations`.
2333 *
2334 * @see `Map#asMutable`
2335 */
2336 asMutable(): this;
2337
2338 /**
2339 * @see `Map#wasAltered`
2340 */
2341 wasAltered(): boolean;
2342
2343 /**
2344 * @see `Map#asImmutable`
2345 */
2346 asImmutable(): this;
2347
2348 // Sequence algorithms
2349
2350 /**
2351 * Returns a new Stack with other collections concatenated to this one.
2352 */
2353 concat<C>(...valuesOrCollections: Array<Iterable<C> | C>): Stack<T | C>;
2354
2355 /**
2356 * Returns a new Stack with values passed through a
2357 * `mapper` function.
2358 *
2359 * Stack([ 1, 2 ]).map(x => 10 * x)
2360 * // Stack [ 10, 20 ]
2361 *
2362 * Note: `map()` always returns a new instance, even if it produced the same
2363 * value at every step.
2364 */
2365 map<M>(
2366 mapper: (value: T, key: number, iter: this) => M,
2367 context?: unknown
2368 ): Stack<M>;
2369
2370 /**
2371 * Flat-maps the Stack, returning a new Stack.
2372 *
2373 * Similar to `stack.map(...).flatten(true)`.
2374 */
2375 flatMap<M>(
2376 mapper: (value: T, key: number, iter: this) => Iterable<M>,
2377 context?: unknown
2378 ): Stack<M>;
2379
2380 /**
2381 * Returns a new Set with only the values for which the `predicate`
2382 * function returns true.
2383 *
2384 * Note: `filter()` always returns a new instance, even if it results in
2385 * not filtering out any values.
2386 */
2387 filter<F extends T>(
2388 predicate: (value: T, index: number, iter: this) => value is F,
2389 context?: unknown
2390 ): Set<F>;
2391 filter(
2392 predicate: (value: T, index: number, iter: this) => unknown,
2393 context?: unknown
2394 ): this;
2395
2396 /**
2397 * Returns a Stack "zipped" with the provided collections.
2398 *
2399 * Like `zipWith`, but using the default `zipper`: creating an `Array`.
2400 *
2401 * ```js
2402 * const a = Stack([ 1, 2, 3 ]);
2403 * const b = Stack([ 4, 5, 6 ]);
2404 * const c = a.zip(b); // Stack [ [ 1, 4 ], [ 2, 5 ], [ 3, 6 ] ]
2405 * ```
2406 */
2407 zip<U>(other: Collection<unknown, U>): Stack<[T, U]>;
2408 zip<U, V>(
2409 other: Collection<unknown, U>,
2410 other2: Collection<unknown, V>
2411 ): Stack<[T, U, V]>;
2412 zip(...collections: Array<Collection<unknown, unknown>>): Stack<unknown>;
2413
2414 /**
2415 * Returns a Stack "zipped" with the provided collections.
2416 *
2417 * Unlike `zip`, `zipAll` continues zipping until the longest collection is
2418 * exhausted. Missing values from shorter collections are filled with `undefined`.
2419 *
2420 * ```js
2421 * const a = Stack([ 1, 2 ]);
2422 * const b = Stack([ 3, 4, 5 ]);
2423 * const c = a.zipAll(b); // Stack [ [ 1, 3 ], [ 2, 4 ], [ undefined, 5 ] ]
2424 * ```
2425 *
2426 * Note: Since zipAll will return a collection as large as the largest
2427 * input, some results may contain undefined values. TypeScript cannot
2428 * account for these without cases (as of v2.5).
2429 */
2430 zipAll<U>(other: Collection<unknown, U>): Stack<[T, U]>;
2431 zipAll<U, V>(
2432 other: Collection<unknown, U>,
2433 other2: Collection<unknown, V>
2434 ): Stack<[T, U, V]>;
2435 zipAll(...collections: Array<Collection<unknown, unknown>>): Stack<unknown>;
2436
2437 /**
2438 * Returns a Stack "zipped" with the provided collections by using a
2439 * custom `zipper` function.
2440 *
2441 * ```js
2442 * const a = Stack([ 1, 2, 3 ]);
2443 * const b = Stack([ 4, 5, 6 ]);
2444 * const c = a.zipWith((a, b) => a + b, b);
2445 * // Stack [ 5, 7, 9 ]
2446 * ```
2447 */
2448 zipWith<U, Z>(
2449 zipper: (value: T, otherValue: U) => Z,
2450 otherCollection: Collection<unknown, U>
2451 ): Stack<Z>;
2452 zipWith<U, V, Z>(
2453 zipper: (value: T, otherValue: U, thirdValue: V) => Z,
2454 otherCollection: Collection<unknown, U>,
2455 thirdCollection: Collection<unknown, V>
2456 ): Stack<Z>;
2457 zipWith<Z>(
2458 zipper: (...values: Array<unknown>) => Z,
2459 ...collections: Array<Collection<unknown, unknown>>
2460 ): Stack<Z>;
2461 }
2462
2463 /**
2464 * Returns a Seq.Indexed of numbers from `start` (inclusive) to `end`
2465 * (exclusive), by `step`, where `start` defaults to 0, `step` to 1, and `end` to
2466 * infinity. When `start` is equal to `end`, returns empty range.
2467 *
2468 * Note: `Range` is a factory function and not a class, and does not use the
2469 * `new` keyword during construction.
2470 *
2471 * ```js
2472 * const { Range } = require('immutable')
2473 * Range() // [ 0, 1, 2, 3, ... ]
2474 * Range(10) // [ 10, 11, 12, 13, ... ]
2475 * Range(10, 15) // [ 10, 11, 12, 13, 14 ]
2476 * Range(10, 30, 5) // [ 10, 15, 20, 25 ]
2477 * Range(30, 10, 5) // [ 30, 25, 20, 15 ]
2478 * Range(30, 30, 5) // []
2479 * ```
2480 */
2481 function Range(
2482 start: number,
2483 end: number,
2484 step?: number
2485 ): Seq.Indexed<number>;
2486
2487 /**
2488 * Returns a Seq.Indexed of `value` repeated `times` times. When `times` is
2489 * not defined, returns an infinite `Seq` of `value`.
2490 *
2491 * Note: `Repeat` is a factory function and not a class, and does not use the
2492 * `new` keyword during construction.
2493 *
2494 * ```js
2495 * const { Repeat } = require('immutable')
2496 * Repeat('foo') // [ 'foo', 'foo', 'foo', ... ]
2497 * Repeat('bar', 4) // [ 'bar', 'bar', 'bar', 'bar' ]
2498 * ```
2499 */
2500 function Repeat<T>(value: T, times?: number): Seq.Indexed<T>;
2501
2502 /**
2503 * A record is similar to a JS object, but enforces a specific set of allowed
2504 * string keys, and has default values.
2505 *
2506 * The `Record()` function produces new Record Factories, which when called
2507 * create Record instances.
2508 *
2509 * ```js
2510 * const { Record } = require('immutable')
2511 * const ABRecord = Record({ a: 1, b: 2 })
2512 * const myRecord = ABRecord({ b: 3 })
2513 * ```
2514 *
2515 * Records always have a value for the keys they define. `remove`ing a key
2516 * from a record simply resets it to the default value for that key.
2517 *
2518 * ```js
2519 * myRecord.get('a') // 1
2520 * myRecord.get('b') // 3
2521 * const myRecordWithoutB = myRecord.remove('b')
2522 * myRecordWithoutB.get('b') // 2
2523 * ```
2524 *
2525 * Values provided to the constructor not found in the Record type will
2526 * be ignored. For example, in this case, ABRecord is provided a key "x" even
2527 * though only "a" and "b" have been defined. The value for "x" will be
2528 * ignored for this record.
2529 *
2530 * ```js
2531 * const myRecord = ABRecord({ b: 3, x: 10 })
2532 * myRecord.get('x') // undefined
2533 * ```
2534 *
2535 * Because Records have a known set of string keys, property get access works
2536 * as expected, however property sets will throw an Error.
2537 *
2538 * Note: IE8 does not support property access. Only use `get()` when
2539 * supporting IE8.
2540 *
2541 * ```js
2542 * myRecord.b // 3
2543 * myRecord.b = 5 // throws Error
2544 * ```
2545 *
2546 * Record Types can be extended as well, allowing for custom methods on your
2547 * Record. This is not a common pattern in functional environments, but is in
2548 * many JS programs.
2549 *
2550 * However Record Types are more restricted than typical JavaScript classes.
2551 * They do not use a class constructor, which also means they cannot use
2552 * class properties (since those are technically part of a constructor).
2553 *
2554 * While Record Types can be syntactically created with the JavaScript `class`
2555 * form, the resulting Record function is actually a factory function, not a
2556 * class constructor. Even though Record Types are not classes, JavaScript
2557 * currently requires the use of `new` when creating new Record instances if
2558 * they are defined as a `class`.
2559 *
2560 * ```
2561 * class ABRecord extends Record({ a: 1, b: 2 }) {
2562 * getAB() {
2563 * return this.a + this.b;
2564 * }
2565 * }
2566 *
2567 * var myRecord = new ABRecord({b: 3})
2568 * myRecord.getAB() // 4
2569 * ```
2570 *
2571 *
2572 * **Flow Typing Records:**
2573 *
2574 * Immutable.js exports two Flow types designed to make it easier to use
2575 * Records with flow typed code, `RecordOf<TProps>` and `RecordFactory<TProps>`.
2576 *
2577 * When defining a new kind of Record factory function, use a flow type that
2578 * describes the values the record contains along with `RecordFactory<TProps>`.
2579 * To type instances of the Record (which the factory function returns),
2580 * use `RecordOf<TProps>`.
2581 *
2582 * Typically, new Record definitions will export both the Record factory
2583 * function as well as the Record instance type for use in other code.
2584 *
2585 * ```js
2586 * import type { RecordFactory, RecordOf } from 'immutable';
2587 *
2588 * // Use RecordFactory<TProps> for defining new Record factory functions.
2589 * type Point3DProps = { x: number, y: number, z: number };
2590 * const defaultValues: Point3DProps = { x: 0, y: 0, z: 0 };
2591 * const makePoint3D: RecordFactory<Point3DProps> = Record(defaultValues);
2592 * export makePoint3D;
2593 *
2594 * // Use RecordOf<T> for defining new instances of that Record.
2595 * export type Point3D = RecordOf<Point3DProps>;
2596 * const some3DPoint: Point3D = makePoint3D({ x: 10, y: 20, z: 30 });
2597 * ```
2598 *
2599 * **Flow Typing Record Subclasses:**
2600 *
2601 * Records can be subclassed as a means to add additional methods to Record
2602 * instances. This is generally discouraged in favor of a more functional API,
2603 * since Subclasses have some minor overhead. However the ability to create
2604 * a rich API on Record types can be quite valuable.
2605 *
2606 * When using Flow to type Subclasses, do not use `RecordFactory<TProps>`,
2607 * instead apply the props type when subclassing:
2608 *
2609 * ```js
2610 * type PersonProps = {name: string, age: number};
2611 * const defaultValues: PersonProps = {name: 'Aristotle', age: 2400};
2612 * const PersonRecord = Record(defaultValues);
2613 * class Person extends PersonRecord<PersonProps> {
2614 * getName(): string {
2615 * return this.get('name')
2616 * }
2617 *
2618 * setName(name: string): this {
2619 * return this.set('name', name);
2620 * }
2621 * }
2622 * ```
2623 *
2624 * **Choosing Records vs plain JavaScript objects**
2625 *
2626 * Records offer a persistently immutable alternative to plain JavaScript
2627 * objects, however they're not required to be used within Immutable.js
2628 * collections. In fact, the deep-access and deep-updating functions
2629 * like `getIn()` and `setIn()` work with plain JavaScript Objects as well.
2630 *
2631 * Deciding to use Records or Objects in your application should be informed
2632 * by the tradeoffs and relative benefits of each:
2633 *
2634 * - *Runtime immutability*: plain JS objects may be carefully treated as
2635 * immutable, however Record instances will *throw* if attempted to be
2636 * mutated directly. Records provide this additional guarantee, however at
2637 * some marginal runtime cost. While JS objects are mutable by nature, the
2638 * use of type-checking tools like [Flow](https://medium.com/@gcanti/immutability-with-flow-faa050a1aef4)
2639 * can help gain confidence in code written to favor immutability.
2640 *
2641 * - *Value equality*: Records use value equality when compared with `is()`
2642 * or `record.equals()`. That is, two Records with the same keys and values
2643 * are equal. Plain objects use *reference equality*. Two objects with the
2644 * same keys and values are not equal since they are different objects.
2645 * This is important to consider when using objects as keys in a `Map` or
2646 * values in a `Set`, which use equality when retrieving values.
2647 *
2648 * - *API methods*: Records have a full featured API, with methods like
2649 * `.getIn()`, and `.equals()`. These can make working with these values
2650 * easier, but comes at the cost of not allowing keys with those names.
2651 *
2652 * - *Default values*: Records provide default values for every key, which
2653 * can be useful when constructing Records with often unchanging values.
2654 * However default values can make using Flow and TypeScript more laborious.
2655 *
2656 * - *Serialization*: Records use a custom internal representation to
2657 * efficiently store and update their values. Converting to and from this
2658 * form isn't free. If converting Records to plain objects is common,
2659 * consider sticking with plain objects to begin with.
2660 */
2661 namespace Record {
2662 /**
2663 * True if `maybeRecord` is an instance of a Record.
2664 */
2665 function isRecord(maybeRecord: unknown): maybeRecord is Record<{}>;
2666
2667 /**
2668 * Records allow passing a second parameter to supply a descriptive name
2669 * that appears when converting a Record to a string or in any error
2670 * messages. A descriptive name for any record can be accessed by using this
2671 * method. If one was not provided, the string "Record" is returned.
2672 *
2673 * ```js
2674 * const { Record } = require('immutable')
2675 * const Person = Record({
2676 * name: null
2677 * }, 'Person')
2678 *
2679 * var me = Person({ name: 'My Name' })
2680 * me.toString() // "Person { "name": "My Name" }"
2681 * Record.getDescriptiveName(me) // "Person"
2682 * ```
2683 */
2684 function getDescriptiveName(record: Record<any>): string;
2685
2686 /**
2687 * A Record.Factory is created by the `Record()` function. Record instances
2688 * are created by passing it some of the accepted values for that Record
2689 * type:
2690 *
2691 * <!-- runkit:activate
2692 * { "preamble": "const { Record } = require('immutable')" }
2693 * -->
2694 * ```js
2695 * // makePerson is a Record Factory function
2696 * const makePerson = Record({ name: null, favoriteColor: 'unknown' });
2697 *
2698 * // alan is a Record instance
2699 * const alan = makePerson({ name: 'Alan' });
2700 * ```
2701 *
2702 * Note that Record Factories return `Record<TProps> & Readonly<TProps>`,
2703 * this allows use of both the Record instance API, and direct property
2704 * access on the resulting instances:
2705 *
2706 * <!-- runkit:activate
2707 * { "preamble": "const { Record } = require('immutable');const makePerson = Record({ name: null, favoriteColor: 'unknown' });const alan = makePerson({ name: 'Alan' });" }
2708 * -->
2709 * ```js
2710 * // Use the Record API
2711 * console.log('Record API: ' + alan.get('name'))
2712 *
2713 * // Or direct property access (Readonly)
2714 * console.log('property access: ' + alan.name)
2715 * ```
2716 *
2717 * **Flow Typing Records:**
2718 *
2719 * Use the `RecordFactory<TProps>` Flow type to get high quality type checking of
2720 * Records:
2721 *
2722 * ```js
2723 * import type { RecordFactory, RecordOf } from 'immutable';
2724 *
2725 * // Use RecordFactory<TProps> for defining new Record factory functions.
2726 * type PersonProps = { name: ?string, favoriteColor: string };
2727 * const makePerson: RecordFactory<PersonProps> = Record({ name: null, favoriteColor: 'unknown' });
2728 *
2729 * // Use RecordOf<T> for defining new instances of that Record.
2730 * type Person = RecordOf<PersonProps>;
2731 * const alan: Person = makePerson({ name: 'Alan' });
2732 * ```
2733 */
2734 namespace Factory {}
2735
2736 interface Factory<TProps extends object> {
2737 (values?: Partial<TProps> | Iterable<[string, unknown]>): Record<TProps> &
2738 Readonly<TProps>;
2739 new (
2740 values?: Partial<TProps> | Iterable<[string, unknown]>
2741 ): Record<TProps> & Readonly<TProps>;
2742
2743 /**
2744 * The name provided to `Record(values, name)` can be accessed with
2745 * `displayName`.
2746 */
2747 displayName: string;
2748 }
2749
2750 function Factory<TProps extends object>(
2751 values?: Partial<TProps> | Iterable<[string, unknown]>
2752 ): Record<TProps> & Readonly<TProps>;
2753 }
2754
2755 /**
2756 * Unlike other types in Immutable.js, the `Record()` function creates a new
2757 * Record Factory, which is a function that creates Record instances.
2758 *
2759 * See above for examples of using `Record()`.
2760 *
2761 * Note: `Record` is a factory function and not a class, and does not use the
2762 * `new` keyword during construction.
2763 */
2764 function Record<TProps extends object>(
2765 defaultValues: TProps,
2766 name?: string
2767 ): Record.Factory<TProps>;
2768
2769 interface Record<TProps extends object> {
2770 // Reading values
2771
2772 has(key: string): key is keyof TProps & string;
2773
2774 /**
2775 * Returns the value associated with the provided key, which may be the
2776 * default value defined when creating the Record factory function.
2777 *
2778 * If the requested key is not defined by this Record type, then
2779 * notSetValue will be returned if provided. Note that this scenario would
2780 * produce an error when using Flow or TypeScript.
2781 */
2782 get<K extends keyof TProps>(key: K, notSetValue?: unknown): TProps[K];
2783 get<T>(key: string, notSetValue: T): T;
2784
2785 // Reading deep values
2786
2787 hasIn(keyPath: Iterable<unknown>): boolean;
2788 getIn(keyPath: Iterable<unknown>): unknown;
2789
2790 // Value equality
2791
2792 equals(other: unknown): boolean;
2793 hashCode(): number;
2794
2795 // Persistent changes
2796
2797 set<K extends keyof TProps>(key: K, value: TProps[K]): this;
2798 update<K extends keyof TProps>(
2799 key: K,
2800 updater: (value: TProps[K]) => TProps[K]
2801 ): this;
2802 merge(
2803 ...collections: Array<Partial<TProps> | Iterable<[string, unknown]>>
2804 ): this;
2805 mergeDeep(
2806 ...collections: Array<Partial<TProps> | Iterable<[string, unknown]>>
2807 ): this;
2808
2809 mergeWith(
2810 merger: (oldVal: unknown, newVal: unknown, key: keyof TProps) => unknown,
2811 ...collections: Array<Partial<TProps> | Iterable<[string, unknown]>>
2812 ): this;
2813 mergeDeepWith(
2814 merger: (oldVal: unknown, newVal: unknown, key: unknown) => unknown,
2815 ...collections: Array<Partial<TProps> | Iterable<[string, unknown]>>
2816 ): this;
2817
2818 /**
2819 * Returns a new instance of this Record type with the value for the
2820 * specific key set to its default value.
2821 *
2822 * @alias remove
2823 */
2824 delete<K extends keyof TProps>(key: K): this;
2825 remove<K extends keyof TProps>(key: K): this;
2826
2827 /**
2828 * Returns a new instance of this Record type with all values set
2829 * to their default values.
2830 */
2831 clear(): this;
2832
2833 // Deep persistent changes
2834
2835 setIn(keyPath: Iterable<unknown>, value: unknown): this;
2836 updateIn(
2837 keyPath: Iterable<unknown>,
2838 updater: (value: unknown) => unknown
2839 ): this;
2840 mergeIn(keyPath: Iterable<unknown>, ...collections: Array<unknown>): this;
2841 mergeDeepIn(
2842 keyPath: Iterable<unknown>,
2843 ...collections: Array<unknown>
2844 ): this;
2845
2846 /**
2847 * @alias removeIn
2848 */
2849 deleteIn(keyPath: Iterable<unknown>): this;
2850 removeIn(keyPath: Iterable<unknown>): this;
2851
2852 // Conversion to JavaScript types
2853
2854 /**
2855 * Deeply converts this Record to equivalent native JavaScript Object.
2856 *
2857 * Note: This method may not be overridden. Objects with custom
2858 * serialization to plain JS may override toJSON() instead.
2859 */
2860 toJS(): DeepCopy<TProps>;
2861
2862 /**
2863 * Shallowly converts this Record to equivalent native JavaScript Object.
2864 */
2865 toJSON(): TProps;
2866
2867 /**
2868 * Shallowly converts this Record to equivalent JavaScript Object.
2869 */
2870 toObject(): TProps;
2871
2872 // Transient changes
2873
2874 /**
2875 * Note: Not all methods can be used on a mutable collection or within
2876 * `withMutations`! Only `set` may be used mutatively.
2877 *
2878 * @see `Map#withMutations`
2879 */
2880 withMutations(mutator: (mutable: this) => unknown): this;
2881
2882 /**
2883 * @see `Map#asMutable`
2884 */
2885 asMutable(): this;
2886
2887 /**
2888 * @see `Map#wasAltered`
2889 */
2890 wasAltered(): boolean;
2891
2892 /**
2893 * @see `Map#asImmutable`
2894 */
2895 asImmutable(): this;
2896
2897 // Sequence algorithms
2898
2899 toSeq(): Seq.Keyed<keyof TProps, TProps[keyof TProps]>;
2900
2901 [Symbol.iterator](): IterableIterator<[keyof TProps, TProps[keyof TProps]]>;
2902 }
2903
2904 /**
2905 * RecordOf<T> is used in TypeScript to define interfaces expecting an
2906 * instance of record with type T.
2907 *
2908 * This is equivalent to an instance of a record created by a Record Factory.
2909 */
2910 type RecordOf<TProps extends object> = Record<TProps> & Readonly<TProps>;
2911
2912 /**
2913 * `Seq` describes a lazy operation, allowing them to efficiently chain
2914 * use of all the higher-order collection methods (such as `map` and `filter`)
2915 * by not creating intermediate collections.
2916 *
2917 * **Seq is immutable** — Once a Seq is created, it cannot be
2918 * changed, appended to, rearranged or otherwise modified. Instead, any
2919 * mutative method called on a `Seq` will return a new `Seq`.
2920 *
2921 * **Seq is lazy** — `Seq` does as little work as necessary to respond to any
2922 * method call. Values are often created during iteration, including implicit
2923 * iteration when reducing or converting to a concrete data structure such as
2924 * a `List` or JavaScript `Array`.
2925 *
2926 * For example, the following performs no work, because the resulting
2927 * `Seq`'s values are never iterated:
2928 *
2929 * ```js
2930 * const { Seq } = require('immutable')
2931 * const oddSquares = Seq([ 1, 2, 3, 4, 5, 6, 7, 8 ])
2932 * .filter(x => x % 2 !== 0)
2933 * .map(x => x * x)
2934 * ```
2935 *
2936 * Once the `Seq` is used, it performs only the work necessary. In this
2937 * example, no intermediate arrays are ever created, filter is called three
2938 * times, and map is only called once:
2939 *
2940 * ```js
2941 * oddSquares.get(1); // 9
2942 * ```
2943 *
2944 * Any collection can be converted to a lazy Seq with `Seq()`.
2945 *
2946 * <!-- runkit:activate -->
2947 * ```js
2948 * const { Map } = require('immutable')
2949 * const map = Map({ a: 1, b: 2, c: 3 })
2950 * const lazySeq = Seq(map)
2951 * ```
2952 *
2953 * `Seq` allows for the efficient chaining of operations, allowing for the
2954 * expression of logic that can otherwise be very tedious:
2955 *
2956 * ```js
2957 * lazySeq
2958 * .flip()
2959 * .map(key => key.toUpperCase())
2960 * .flip()
2961 * // Seq { A: 1, B: 1, C: 1 }
2962 * ```
2963 *
2964 * As well as expressing logic that would otherwise seem memory or time
2965 * limited, for example `Range` is a special kind of Lazy sequence.
2966 *
2967 * <!-- runkit:activate -->
2968 * ```js
2969 * const { Range } = require('immutable')
2970 * Range(1, Infinity)
2971 * .skip(1000)
2972 * .map(n => -n)
2973 * .filter(n => n % 2 === 0)
2974 * .take(2)
2975 * .reduce((r, n) => r * n, 1)
2976 * // 1006008
2977 * ```
2978 *
2979 * Seq is often used to provide a rich collection API to JavaScript Object.
2980 *
2981 * ```js
2982 * Seq({ x: 0, y: 1, z: 2 }).map(v => v * 2).toObject();
2983 * // { x: 0, y: 2, z: 4 }
2984 * ```
2985 */
2986
2987 namespace Seq {
2988 /**
2989 * True if `maybeSeq` is a Seq, it is not backed by a concrete
2990 * structure such as Map, List, or Set.
2991 */
2992 function isSeq(
2993 maybeSeq: unknown
2994 ): maybeSeq is
2995 | Seq.Indexed<unknown>
2996 | Seq.Keyed<unknown, unknown>
2997 | Seq.Set<unknown>;
2998
2999 /**
3000 * `Seq` which represents key-value pairs.
3001 */
3002 namespace Keyed {}
3003
3004 /**
3005 * Always returns a Seq.Keyed, if input is not keyed, expects an
3006 * collection of [K, V] tuples.
3007 *
3008 * Note: `Seq.Keyed` is a conversion function and not a class, and does not
3009 * use the `new` keyword during construction.
3010 */
3011 function Keyed<K, V>(collection?: Iterable<[K, V]>): Seq.Keyed<K, V>;
3012 function Keyed<V>(obj: { [key: string]: V }): Seq.Keyed<string, V>;
3013
3014 interface Keyed<K, V> extends Seq<K, V>, Collection.Keyed<K, V> {
3015 /**
3016 * Deeply converts this Keyed Seq to equivalent native JavaScript Object.
3017 *
3018 * Converts keys to Strings.
3019 */
3020 toJS(): { [key in string | number | symbol]: DeepCopy<V> };
3021
3022 /**
3023 * Shallowly converts this Keyed Seq to equivalent native JavaScript Object.
3024 *
3025 * Converts keys to Strings.
3026 */
3027 toJSON(): { [key in string | number | symbol]: V };
3028
3029 /**
3030 * Shallowly converts this collection to an Array.
3031 */
3032 toArray(): Array<[K, V]>;
3033
3034 /**
3035 * Returns itself
3036 */
3037 toSeq(): this;
3038
3039 /**
3040 * Returns a new Seq with other collections concatenated to this one.
3041 *
3042 * All entries will be present in the resulting Seq, even if they
3043 * have the same key.
3044 */
3045 concat<KC, VC>(
3046 ...collections: Array<Iterable<[KC, VC]>>
3047 ): Seq.Keyed<K | KC, V | VC>;
3048 concat<C>(
3049 ...collections: Array<{ [key: string]: C }>
3050 ): Seq.Keyed<K | string, V | C>;
3051
3052 /**
3053 * Returns a new Seq.Keyed with values passed through a
3054 * `mapper` function.
3055 *
3056 * ```js
3057 * const { Seq } = require('immutable')
3058 * Seq.Keyed({ a: 1, b: 2 }).map(x => 10 * x)
3059 * // Seq { "a": 10, "b": 20 }
3060 * ```
3061 *
3062 * Note: `map()` always returns a new instance, even if it produced the
3063 * same value at every step.
3064 */
3065 map<M>(
3066 mapper: (value: V, key: K, iter: this) => M,
3067 context?: unknown
3068 ): Seq.Keyed<K, M>;
3069
3070 /**
3071 * @see Collection.Keyed.mapKeys
3072 */
3073 mapKeys<M>(
3074 mapper: (key: K, value: V, iter: this) => M,
3075 context?: unknown
3076 ): Seq.Keyed<M, V>;
3077
3078 /**
3079 * @see Collection.Keyed.mapEntries
3080 */
3081 mapEntries<KM, VM>(
3082 mapper: (
3083 entry: [K, V],
3084 index: number,
3085 iter: this
3086 ) => [KM, VM] | undefined,
3087 context?: unknown
3088 ): Seq.Keyed<KM, VM>;
3089
3090 /**
3091 * Flat-maps the Seq, returning a Seq of the same type.
3092 *
3093 * Similar to `seq.map(...).flatten(true)`.
3094 */
3095 flatMap<KM, VM>(
3096 mapper: (value: V, key: K, iter: this) => Iterable<[KM, VM]>,
3097 context?: unknown
3098 ): Seq.Keyed<KM, VM>;
3099
3100 /**
3101 * Returns a new Seq with only the entries for which the `predicate`
3102 * function returns true.
3103 *
3104 * Note: `filter()` always returns a new instance, even if it results in
3105 * not filtering out any values.
3106 */
3107 filter<F extends V>(
3108 predicate: (value: V, key: K, iter: this) => value is F,
3109 context?: unknown
3110 ): Seq.Keyed<K, F>;
3111 filter(
3112 predicate: (value: V, key: K, iter: this) => unknown,
3113 context?: unknown
3114 ): this;
3115
3116 /**
3117 * Returns a new keyed Seq with the values for which the `predicate`
3118 * function returns false and another for which is returns true.
3119 */
3120 partition<F extends V, C>(
3121 predicate: (this: C, value: V, key: K, iter: this) => value is F,
3122 context?: C
3123 ): [Seq.Keyed<K, V>, Seq.Keyed<K, F>];
3124 partition<C>(
3125 predicate: (this: C, value: V, key: K, iter: this) => unknown,
3126 context?: C
3127 ): [this, this];
3128
3129 /**
3130 * @see Collection.Keyed.flip
3131 */
3132 flip(): Seq.Keyed<V, K>;
3133
3134 [Symbol.iterator](): IterableIterator<[K, V]>;
3135 }
3136
3137 /**
3138 * `Seq` which represents an ordered indexed list of values.
3139 */
3140 namespace Indexed {
3141 /**
3142 * Provides an Seq.Indexed of the values provided.
3143 */
3144 function of<T>(...values: Array<T>): Seq.Indexed<T>;
3145 }
3146
3147 /**
3148 * Always returns Seq.Indexed, discarding associated keys and
3149 * supplying incrementing indices.
3150 *
3151 * Note: `Seq.Indexed` is a conversion function and not a class, and does
3152 * not use the `new` keyword during construction.
3153 */
3154 function Indexed<T>(
3155 collection?: Iterable<T> | ArrayLike<T>
3156 ): Seq.Indexed<T>;
3157
3158 interface Indexed<T> extends Seq<number, T>, Collection.Indexed<T> {
3159 /**
3160 * Deeply converts this Indexed Seq to equivalent native JavaScript Array.
3161 */
3162 toJS(): Array<DeepCopy<T>>;
3163
3164 /**
3165 * Shallowly converts this Indexed Seq to equivalent native JavaScript Array.
3166 */
3167 toJSON(): Array<T>;
3168
3169 /**
3170 * Shallowly converts this collection to an Array.
3171 */
3172 toArray(): Array<T>;
3173
3174 /**
3175 * Returns itself
3176 */
3177 toSeq(): this;
3178
3179 /**
3180 * Returns a new Seq with other collections concatenated to this one.
3181 */
3182 concat<C>(
3183 ...valuesOrCollections: Array<Iterable<C> | C>
3184 ): Seq.Indexed<T | C>;
3185
3186 /**
3187 * Returns a new Seq.Indexed with values passed through a
3188 * `mapper` function.
3189 *
3190 * ```js
3191 * const { Seq } = require('immutable')
3192 * Seq.Indexed([ 1, 2 ]).map(x => 10 * x)
3193 * // Seq [ 10, 20 ]
3194 * ```
3195 *
3196 * Note: `map()` always returns a new instance, even if it produced the
3197 * same value at every step.
3198 */
3199 map<M>(
3200 mapper: (value: T, key: number, iter: this) => M,
3201 context?: unknown
3202 ): Seq.Indexed<M>;
3203
3204 /**
3205 * Flat-maps the Seq, returning a a Seq of the same type.
3206 *
3207 * Similar to `seq.map(...).flatten(true)`.
3208 */
3209 flatMap<M>(
3210 mapper: (value: T, key: number, iter: this) => Iterable<M>,
3211 context?: unknown
3212 ): Seq.Indexed<M>;
3213
3214 /**
3215 * Returns a new Seq with only the values for which the `predicate`
3216 * function returns true.
3217 *
3218 * Note: `filter()` always returns a new instance, even if it results in
3219 * not filtering out any values.
3220 */
3221 filter<F extends T>(
3222 predicate: (value: T, index: number, iter: this) => value is F,
3223 context?: unknown
3224 ): Seq.Indexed<F>;
3225 filter(
3226 predicate: (value: T, index: number, iter: this) => unknown,
3227 context?: unknown
3228 ): this;
3229
3230 /**
3231 * Returns a new indexed Seq with the values for which the `predicate`
3232 * function returns false and another for which is returns true.
3233 */
3234 partition<F extends T, C>(
3235 predicate: (this: C, value: T, index: number, iter: this) => value is F,
3236 context?: C
3237 ): [Seq.Indexed<T>, Seq.Indexed<F>];
3238 partition<C>(
3239 predicate: (this: C, value: T, index: number, iter: this) => unknown,
3240 context?: C
3241 ): [this, this];
3242
3243 /**
3244 * Returns a Seq "zipped" with the provided collections.
3245 *
3246 * Like `zipWith`, but using the default `zipper`: creating an `Array`.
3247 *
3248 * ```js
3249 * const a = Seq([ 1, 2, 3 ]);
3250 * const b = Seq([ 4, 5, 6 ]);
3251 * const c = a.zip(b); // Seq [ [ 1, 4 ], [ 2, 5 ], [ 3, 6 ] ]
3252 * ```
3253 */
3254 zip<U>(other: Collection<unknown, U>): Seq.Indexed<[T, U]>;
3255 zip<U, V>(
3256 other: Collection<unknown, U>,
3257 other2: Collection<unknown, V>
3258 ): Seq.Indexed<[T, U, V]>;
3259 zip(
3260 ...collections: Array<Collection<unknown, unknown>>
3261 ): Seq.Indexed<unknown>;
3262
3263 /**
3264 * Returns a Seq "zipped" with the provided collections.
3265 *
3266 * Unlike `zip`, `zipAll` continues zipping until the longest collection is
3267 * exhausted. Missing values from shorter collections are filled with `undefined`.
3268 *
3269 * ```js
3270 * const a = Seq([ 1, 2 ]);
3271 * const b = Seq([ 3, 4, 5 ]);
3272 * const c = a.zipAll(b); // Seq [ [ 1, 3 ], [ 2, 4 ], [ undefined, 5 ] ]
3273 * ```
3274 */
3275 zipAll<U>(other: Collection<unknown, U>): Seq.Indexed<[T, U]>;
3276 zipAll<U, V>(
3277 other: Collection<unknown, U>,
3278 other2: Collection<unknown, V>
3279 ): Seq.Indexed<[T, U, V]>;
3280 zipAll(
3281 ...collections: Array<Collection<unknown, unknown>>
3282 ): Seq.Indexed<unknown>;
3283
3284 /**
3285 * Returns a Seq "zipped" with the provided collections by using a
3286 * custom `zipper` function.
3287 *
3288 * ```js
3289 * const a = Seq([ 1, 2, 3 ]);
3290 * const b = Seq([ 4, 5, 6 ]);
3291 * const c = a.zipWith((a, b) => a + b, b);
3292 * // Seq [ 5, 7, 9 ]
3293 * ```
3294 */
3295 zipWith<U, Z>(
3296 zipper: (value: T, otherValue: U) => Z,
3297 otherCollection: Collection<unknown, U>
3298 ): Seq.Indexed<Z>;
3299 zipWith<U, V, Z>(
3300 zipper: (value: T, otherValue: U, thirdValue: V) => Z,
3301 otherCollection: Collection<unknown, U>,
3302 thirdCollection: Collection<unknown, V>
3303 ): Seq.Indexed<Z>;
3304 zipWith<Z>(
3305 zipper: (...values: Array<unknown>) => Z,
3306 ...collections: Array<Collection<unknown, unknown>>
3307 ): Seq.Indexed<Z>;
3308
3309 [Symbol.iterator](): IterableIterator<T>;
3310 }
3311
3312 /**
3313 * `Seq` which represents a set of values.
3314 *
3315 * Because `Seq` are often lazy, `Seq.Set` does not provide the same guarantee
3316 * of value uniqueness as the concrete `Set`.
3317 */
3318 namespace Set {
3319 /**
3320 * Returns a Seq.Set of the provided values
3321 */
3322 function of<T>(...values: Array<T>): Seq.Set<T>;
3323 }
3324
3325 /**
3326 * Always returns a Seq.Set, discarding associated indices or keys.
3327 *
3328 * Note: `Seq.Set` is a conversion function and not a class, and does not
3329 * use the `new` keyword during construction.
3330 */
3331 function Set<T>(collection?: Iterable<T> | ArrayLike<T>): Seq.Set<T>;
3332
3333 interface Set<T> extends Seq<T, T>, Collection.Set<T> {
3334 /**
3335 * Deeply converts this Set Seq to equivalent native JavaScript Array.
3336 */
3337 toJS(): Array<DeepCopy<T>>;
3338
3339 /**
3340 * Shallowly converts this Set Seq to equivalent native JavaScript Array.
3341 */
3342 toJSON(): Array<T>;
3343
3344 /**
3345 * Shallowly converts this collection to an Array.
3346 */
3347 toArray(): Array<T>;
3348
3349 /**
3350 * Returns itself
3351 */
3352 toSeq(): this;
3353
3354 /**
3355 * Returns a new Seq with other collections concatenated to this one.
3356 *
3357 * All entries will be present in the resulting Seq, even if they
3358 * are duplicates.
3359 */
3360 concat<U>(...collections: Array<Iterable<U>>): Seq.Set<T | U>;
3361
3362 /**
3363 * Returns a new Seq.Set with values passed through a
3364 * `mapper` function.
3365 *
3366 * ```js
3367 * Seq.Set([ 1, 2 ]).map(x => 10 * x)
3368 * // Seq { 10, 20 }
3369 * ```
3370 *
3371 * Note: `map()` always returns a new instance, even if it produced the
3372 * same value at every step.
3373 */
3374 map<M>(
3375 mapper: (value: T, key: T, iter: this) => M,
3376 context?: unknown
3377 ): Seq.Set<M>;
3378
3379 /**
3380 * Flat-maps the Seq, returning a Seq of the same type.
3381 *
3382 * Similar to `seq.map(...).flatten(true)`.
3383 */
3384 flatMap<M>(
3385 mapper: (value: T, key: T, iter: this) => Iterable<M>,
3386 context?: unknown
3387 ): Seq.Set<M>;
3388
3389 /**
3390 * Returns a new Seq with only the values for which the `predicate`
3391 * function returns true.
3392 *
3393 * Note: `filter()` always returns a new instance, even if it results in
3394 * not filtering out any values.
3395 */
3396 filter<F extends T>(
3397 predicate: (value: T, key: T, iter: this) => value is F,
3398 context?: unknown
3399 ): Seq.Set<F>;
3400 filter(
3401 predicate: (value: T, key: T, iter: this) => unknown,
3402 context?: unknown
3403 ): this;
3404
3405 /**
3406 * Returns a new set Seq with the values for which the `predicate`
3407 * function returns false and another for which is returns true.
3408 */
3409 partition<F extends T, C>(
3410 predicate: (this: C, value: T, key: T, iter: this) => value is F,
3411 context?: C
3412 ): [Seq.Set<T>, Seq.Set<F>];
3413 partition<C>(
3414 predicate: (this: C, value: T, key: T, iter: this) => unknown,
3415 context?: C
3416 ): [this, this];
3417
3418 [Symbol.iterator](): IterableIterator<T>;
3419 }
3420 }
3421
3422 /**
3423 * Creates a Seq.
3424 *
3425 * Returns a particular kind of `Seq` based on the input.
3426 *
3427 * * If a `Seq`, that same `Seq`.
3428 * * If an `Collection`, a `Seq` of the same kind (Keyed, Indexed, or Set).
3429 * * If an Array-like, an `Seq.Indexed`.
3430 * * If an Iterable Object, an `Seq.Indexed`.
3431 * * If an Object, a `Seq.Keyed`.
3432 *
3433 * Note: An Iterator itself will be treated as an object, becoming a `Seq.Keyed`,
3434 * which is usually not what you want. You should turn your Iterator Object into
3435 * an iterable object by defining a Symbol.iterator (or @@iterator) method which
3436 * returns `this`.
3437 *
3438 * Note: `Seq` is a conversion function and not a class, and does not use the
3439 * `new` keyword during construction.
3440 */
3441 function Seq<S extends Seq<unknown, unknown>>(seq: S): S;
3442 function Seq<K, V>(collection: Collection.Keyed<K, V>): Seq.Keyed<K, V>;
3443 function Seq<T>(collection: Collection.Set<T>): Seq.Set<T>;
3444 function Seq<T>(
3445 collection: Collection.Indexed<T> | Iterable<T> | ArrayLike<T>
3446 ): Seq.Indexed<T>;
3447 function Seq<V>(obj: { [key: string]: V }): Seq.Keyed<string, V>;
3448 function Seq<K = unknown, V = unknown>(): Seq<K, V>;
3449
3450 interface Seq<K, V> extends Collection<K, V> {
3451 /**
3452 * Some Seqs can describe their size lazily. When this is the case,
3453 * size will be an integer. Otherwise it will be undefined.
3454 *
3455 * For example, Seqs returned from `map()` or `reverse()`
3456 * preserve the size of the original `Seq` while `filter()` does not.
3457 *
3458 * Note: `Range`, `Repeat` and `Seq`s made from `Array`s and `Object`s will
3459 * always have a size.
3460 */
3461 readonly size: number | undefined;
3462
3463 // Force evaluation
3464
3465 /**
3466 * Because Sequences are lazy and designed to be chained together, they do
3467 * not cache their results. For example, this map function is called a total
3468 * of 6 times, as each `join` iterates the Seq of three values.
3469 *
3470 * var squares = Seq([ 1, 2, 3 ]).map(x => x * x)
3471 * squares.join() + squares.join()
3472 *
3473 * If you know a `Seq` will be used multiple times, it may be more
3474 * efficient to first cache it in memory. Here, the map function is called
3475 * only 3 times.
3476 *
3477 * var squares = Seq([ 1, 2, 3 ]).map(x => x * x).cacheResult()
3478 * squares.join() + squares.join()
3479 *
3480 * Use this method judiciously, as it must fully evaluate a Seq which can be
3481 * a burden on memory and possibly performance.
3482 *
3483 * Note: after calling `cacheResult`, a Seq will always have a `size`.
3484 */
3485 cacheResult(): this;
3486
3487 // Sequence algorithms
3488
3489 /**
3490 * Returns a new Seq with values passed through a
3491 * `mapper` function.
3492 *
3493 * ```js
3494 * const { Seq } = require('immutable')
3495 * Seq([ 1, 2 ]).map(x => 10 * x)
3496 * // Seq [ 10, 20 ]
3497 * ```
3498 *
3499 * Note: `map()` always returns a new instance, even if it produced the same
3500 * value at every step.
3501 */
3502 map<M>(
3503 mapper: (value: V, key: K, iter: this) => M,
3504 context?: unknown
3505 ): Seq<K, M>;
3506
3507 /**
3508 * Returns a new Seq with values passed through a
3509 * `mapper` function.
3510 *
3511 * ```js
3512 * const { Seq } = require('immutable')
3513 * Seq([ 1, 2 ]).map(x => 10 * x)
3514 * // Seq [ 10, 20 ]
3515 * ```
3516 *
3517 * Note: `map()` always returns a new instance, even if it produced the same
3518 * value at every step.
3519 * Note: used only for sets.
3520 */
3521 map<M>(
3522 mapper: (value: V, key: K, iter: this) => M,
3523 context?: unknown
3524 ): Seq<M, M>;
3525
3526 /**
3527 * Flat-maps the Seq, returning a Seq of the same type.
3528 *
3529 * Similar to `seq.map(...).flatten(true)`.
3530 */
3531 flatMap<M>(
3532 mapper: (value: V, key: K, iter: this) => Iterable<M>,
3533 context?: unknown
3534 ): Seq<K, M>;
3535
3536 /**
3537 * Flat-maps the Seq, returning a Seq of the same type.
3538 *
3539 * Similar to `seq.map(...).flatten(true)`.
3540 * Note: Used only for sets.
3541 */
3542 flatMap<M>(
3543 mapper: (value: V, key: K, iter: this) => Iterable<M>,
3544 context?: unknown
3545 ): Seq<M, M>;
3546
3547 /**
3548 * Returns a new Seq with only the values for which the `predicate`
3549 * function returns true.
3550 *
3551 * Note: `filter()` always returns a new instance, even if it results in
3552 * not filtering out any values.
3553 */
3554 filter<F extends V>(
3555 predicate: (value: V, key: K, iter: this) => value is F,
3556 context?: unknown
3557 ): Seq<K, F>;
3558 filter(
3559 predicate: (value: V, key: K, iter: this) => unknown,
3560 context?: unknown
3561 ): this;
3562
3563 /**
3564 * Returns a new Seq with the values for which the `predicate` function
3565 * returns false and another for which is returns true.
3566 */
3567 partition<F extends V, C>(
3568 predicate: (this: C, value: V, key: K, iter: this) => value is F,
3569 context?: C
3570 ): [Seq<K, V>, Seq<K, F>];
3571 partition<C>(
3572 predicate: (this: C, value: V, key: K, iter: this) => unknown,
3573 context?: C
3574 ): [this, this];
3575 }
3576
3577 /**
3578 * The `Collection` is a set of (key, value) entries which can be iterated, and
3579 * is the base class for all collections in `immutable`, allowing them to
3580 * make use of all the Collection methods (such as `map` and `filter`).
3581 *
3582 * Note: A collection is always iterated in the same order, however that order
3583 * may not always be well defined, as is the case for the `Map` and `Set`.
3584 *
3585 * Collection is the abstract base class for concrete data structures. It
3586 * cannot be constructed directly.
3587 *
3588 * Implementations should extend one of the subclasses, `Collection.Keyed`,
3589 * `Collection.Indexed`, or `Collection.Set`.
3590 */
3591 namespace Collection {
3592 /**
3593 * @deprecated use `const { isKeyed } = require('immutable')`
3594 */
3595 function isKeyed(
3596 maybeKeyed: unknown
3597 ): maybeKeyed is Collection.Keyed<unknown, unknown>;
3598
3599 /**
3600 * @deprecated use `const { isIndexed } = require('immutable')`
3601 */
3602 function isIndexed(
3603 maybeIndexed: unknown
3604 ): maybeIndexed is Collection.Indexed<unknown>;
3605
3606 /**
3607 * @deprecated use `const { isAssociative } = require('immutable')`
3608 */
3609 function isAssociative(
3610 maybeAssociative: unknown
3611 ): maybeAssociative is
3612 | Collection.Keyed<unknown, unknown>
3613 | Collection.Indexed<unknown>;
3614
3615 /**
3616 * @deprecated use `const { isOrdered } = require('immutable')`
3617 */
3618 function isOrdered(maybeOrdered: unknown): boolean;
3619
3620 /**
3621 * Keyed Collections have discrete keys tied to each value.
3622 *
3623 * When iterating `Collection.Keyed`, each iteration will yield a `[K, V]`
3624 * tuple, in other words, `Collection#entries` is the default iterator for
3625 * Keyed Collections.
3626 */
3627 namespace Keyed {}
3628
3629 /**
3630 * Creates a Collection.Keyed
3631 *
3632 * Similar to `Collection()`, however it expects collection-likes of [K, V]
3633 * tuples if not constructed from a Collection.Keyed or JS Object.
3634 *
3635 * Note: `Collection.Keyed` is a conversion function and not a class, and
3636 * does not use the `new` keyword during construction.
3637 */
3638 function Keyed<K, V>(collection?: Iterable<[K, V]>): Collection.Keyed<K, V>;
3639 function Keyed<V>(obj: { [key: string]: V }): Collection.Keyed<string, V>;
3640
3641 interface Keyed<K, V> extends Collection<K, V> {
3642 /**
3643 * Deeply converts this Keyed collection to equivalent native JavaScript Object.
3644 *
3645 * Converts keys to Strings.
3646 */
3647 toJS(): { [key in string | number | symbol]: DeepCopy<V> };
3648
3649 /**
3650 * Shallowly converts this Keyed collection to equivalent native JavaScript Object.
3651 *
3652 * Converts keys to Strings.
3653 */
3654 toJSON(): { [key in string | number | symbol]: V };
3655
3656 /**
3657 * Shallowly converts this collection to an Array.
3658 */
3659 toArray(): Array<[K, V]>;
3660
3661 /**
3662 * Returns Seq.Keyed.
3663 * @override
3664 */
3665 toSeq(): Seq.Keyed<K, V>;
3666
3667 // Sequence functions
3668
3669 /**
3670 * Returns a new Collection.Keyed of the same type where the keys and values
3671 * have been flipped.
3672 *
3673 * <!-- runkit:activate -->
3674 * ```js
3675 * const { Map } = require('immutable')
3676 * Map({ a: 'z', b: 'y' }).flip()
3677 * // Map { "z": "a", "y": "b" }
3678 * ```
3679 */
3680 flip(): Collection.Keyed<V, K>;
3681
3682 /**
3683 * Returns a new Collection with other collections concatenated to this one.
3684 */
3685 concat<KC, VC>(
3686 ...collections: Array<Iterable<[KC, VC]>>
3687 ): Collection.Keyed<K | KC, V | VC>;
3688 concat<C>(
3689 ...collections: Array<{ [key: string]: C }>
3690 ): Collection.Keyed<K | string, V | C>;
3691
3692 /**
3693 * Returns a new Collection.Keyed with values passed through a
3694 * `mapper` function.
3695 *
3696 * ```js
3697 * const { Collection } = require('immutable')
3698 * Collection.Keyed({ a: 1, b: 2 }).map(x => 10 * x)
3699 * // Seq { "a": 10, "b": 20 }
3700 * ```
3701 *
3702 * Note: `map()` always returns a new instance, even if it produced the
3703 * same value at every step.
3704 */
3705 map<M>(
3706 mapper: (value: V, key: K, iter: this) => M,
3707 context?: unknown
3708 ): Collection.Keyed<K, M>;
3709
3710 /**
3711 * Returns a new Collection.Keyed of the same type with keys passed through
3712 * a `mapper` function.
3713 *
3714 * <!-- runkit:activate -->
3715 * ```js
3716 * const { Map } = require('immutable')
3717 * Map({ a: 1, b: 2 }).mapKeys(x => x.toUpperCase())
3718 * // Map { "A": 1, "B": 2 }
3719 * ```
3720 *
3721 * Note: `mapKeys()` always returns a new instance, even if it produced
3722 * the same key at every step.
3723 */
3724 mapKeys<M>(
3725 mapper: (key: K, value: V, iter: this) => M,
3726 context?: unknown
3727 ): Collection.Keyed<M, V>;
3728
3729 /**
3730 * Returns a new Collection.Keyed of the same type with entries
3731 * ([key, value] tuples) passed through a `mapper` function.
3732 *
3733 * <!-- runkit:activate -->
3734 * ```js
3735 * const { Map } = require('immutable')
3736 * Map({ a: 1, b: 2 })
3737 * .mapEntries(([ k, v ]) => [ k.toUpperCase(), v * 2 ])
3738 * // Map { "A": 2, "B": 4 }
3739 * ```
3740 *
3741 * Note: `mapEntries()` always returns a new instance, even if it produced
3742 * the same entry at every step.
3743 *
3744 * If the mapper function returns `undefined`, then the entry will be filtered
3745 */
3746 mapEntries<KM, VM>(
3747 mapper: (
3748 entry: [K, V],
3749 index: number,
3750 iter: this
3751 ) => [KM, VM] | undefined,
3752 context?: unknown
3753 ): Collection.Keyed<KM, VM>;
3754
3755 /**
3756 * Flat-maps the Collection, returning a Collection of the same type.
3757 *
3758 * Similar to `collection.map(...).flatten(true)`.
3759 */
3760 flatMap<KM, VM>(
3761 mapper: (value: V, key: K, iter: this) => Iterable<[KM, VM]>,
3762 context?: unknown
3763 ): Collection.Keyed<KM, VM>;
3764
3765 /**
3766 * Returns a new Collection with only the values for which the `predicate`
3767 * function returns true.
3768 *
3769 * Note: `filter()` always returns a new instance, even if it results in
3770 * not filtering out any values.
3771 */
3772 filter<F extends V>(
3773 predicate: (value: V, key: K, iter: this) => value is F,
3774 context?: unknown
3775 ): Collection.Keyed<K, F>;
3776 filter(
3777 predicate: (value: V, key: K, iter: this) => unknown,
3778 context?: unknown
3779 ): this;
3780
3781 /**
3782 * Returns a new keyed Collection with the values for which the
3783 * `predicate` function returns false and another for which is returns
3784 * true.
3785 */
3786 partition<F extends V, C>(
3787 predicate: (this: C, value: V, key: K, iter: this) => value is F,
3788 context?: C
3789 ): [Collection.Keyed<K, V>, Collection.Keyed<K, F>];
3790 partition<C>(
3791 predicate: (this: C, value: V, key: K, iter: this) => unknown,
3792 context?: C
3793 ): [this, this];
3794
3795 [Symbol.iterator](): IterableIterator<[K, V]>;
3796 }
3797
3798 /**
3799 * Indexed Collections have incrementing numeric keys. They exhibit
3800 * slightly different behavior than `Collection.Keyed` for some methods in order
3801 * to better mirror the behavior of JavaScript's `Array`, and add methods
3802 * which do not make sense on non-indexed Collections such as `indexOf`.
3803 *
3804 * Unlike JavaScript arrays, `Collection.Indexed`s are always dense. "Unset"
3805 * indices and `undefined` indices are indistinguishable, and all indices from
3806 * 0 to `size` are visited when iterated.
3807 *
3808 * All Collection.Indexed methods return re-indexed Collections. In other words,
3809 * indices always start at 0 and increment until size. If you wish to
3810 * preserve indices, using them as keys, convert to a Collection.Keyed by
3811 * calling `toKeyedSeq`.
3812 */
3813 namespace Indexed {}
3814
3815 /**
3816 * Creates a new Collection.Indexed.
3817 *
3818 * Note: `Collection.Indexed` is a conversion function and not a class, and
3819 * does not use the `new` keyword during construction.
3820 */
3821 function Indexed<T>(
3822 collection?: Iterable<T> | ArrayLike<T>
3823 ): Collection.Indexed<T>;
3824
3825 interface Indexed<T> extends Collection<number, T> {
3826 /**
3827 * Deeply converts this Indexed collection to equivalent native JavaScript Array.
3828 */
3829 toJS(): Array<DeepCopy<T>>;
3830
3831 /**
3832 * Shallowly converts this Indexed collection to equivalent native JavaScript Array.
3833 */
3834 toJSON(): Array<T>;
3835
3836 /**
3837 * Shallowly converts this collection to an Array.
3838 */
3839 toArray(): Array<T>;
3840
3841 // Reading values
3842
3843 /**
3844 * Returns the value associated with the provided index, or notSetValue if
3845 * the index is beyond the bounds of the Collection.
3846 *
3847 * `index` may be a negative number, which indexes back from the end of the
3848 * Collection. `s.get(-1)` gets the last item in the Collection.
3849 */
3850 get<NSV>(index: number, notSetValue: NSV): T | NSV;
3851 get(index: number): T | undefined;
3852
3853 // Conversion to Seq
3854
3855 /**
3856 * Returns Seq.Indexed.
3857 * @override
3858 */
3859 toSeq(): Seq.Indexed<T>;
3860
3861 /**
3862 * If this is a collection of [key, value] entry tuples, it will return a
3863 * Seq.Keyed of those entries.
3864 */
3865 fromEntrySeq(): Seq.Keyed<unknown, unknown>;
3866
3867 // Combination
3868
3869 /**
3870 * Returns a Collection of the same type with `separator` between each item
3871 * in this Collection.
3872 */
3873 interpose(separator: T): this;
3874
3875 /**
3876 * Returns a Collection of the same type with the provided `collections`
3877 * interleaved into this collection.
3878 *
3879 * The resulting Collection includes the first item from each, then the
3880 * second from each, etc.
3881 *
3882 * <!-- runkit:activate
3883 * { "preamble": "require('immutable')"}
3884 * -->
3885 * ```js
3886 * const { List } = require('immutable')
3887 * List([ 1, 2, 3 ]).interleave(List([ 'A', 'B', 'C' ]))
3888 * // List [ 1, "A", 2, "B", 3, "C" ]
3889 * ```
3890 *
3891 * The shortest Collection stops interleave.
3892 *
3893 * <!-- runkit:activate
3894 * { "preamble": "const { List } = require('immutable')" }
3895 * -->
3896 * ```js
3897 * List([ 1, 2, 3 ]).interleave(
3898 * List([ 'A', 'B' ]),
3899 * List([ 'X', 'Y', 'Z' ])
3900 * )
3901 * // List [ 1, "A", "X", 2, "B", "Y" ]
3902 * ```
3903 *
3904 * Since `interleave()` re-indexes values, it produces a complete copy,
3905 * which has `O(N)` complexity.
3906 *
3907 * Note: `interleave` *cannot* be used in `withMutations`.
3908 */
3909 interleave(...collections: Array<Collection<unknown, T>>): this;
3910
3911 /**
3912 * Splice returns a new indexed Collection by replacing a region of this
3913 * Collection with new values. If values are not provided, it only skips the
3914 * region to be removed.
3915 *
3916 * `index` may be a negative number, which indexes back from the end of the
3917 * Collection. `s.splice(-2)` splices after the second to last item.
3918 *
3919 * <!-- runkit:activate -->
3920 * ```js
3921 * const { List } = require('immutable')
3922 * List([ 'a', 'b', 'c', 'd' ]).splice(1, 2, 'q', 'r', 's')
3923 * // List [ "a", "q", "r", "s", "d" ]
3924 * ```
3925 *
3926 * Since `splice()` re-indexes values, it produces a complete copy, which
3927 * has `O(N)` complexity.
3928 *
3929 * Note: `splice` *cannot* be used in `withMutations`.
3930 */
3931 splice(index: number, removeNum: number, ...values: Array<T>): this;
3932
3933 /**
3934 * Returns a Collection of the same type "zipped" with the provided
3935 * collections.
3936 *
3937 * Like `zipWith`, but using the default `zipper`: creating an `Array`.
3938 *
3939 *
3940 * <!-- runkit:activate
3941 * { "preamble": "const { List } = require('immutable')" }
3942 * -->
3943 * ```js
3944 * const a = List([ 1, 2, 3 ]);
3945 * const b = List([ 4, 5, 6 ]);
3946 * const c = a.zip(b); // List [ [ 1, 4 ], [ 2, 5 ], [ 3, 6 ] ]
3947 * ```
3948 */
3949 zip<U>(other: Collection<unknown, U>): Collection.Indexed<[T, U]>;
3950 zip<U, V>(
3951 other: Collection<unknown, U>,
3952 other2: Collection<unknown, V>
3953 ): Collection.Indexed<[T, U, V]>;
3954 zip(
3955 ...collections: Array<Collection<unknown, unknown>>
3956 ): Collection.Indexed<unknown>;
3957
3958 /**
3959 * Returns a Collection "zipped" with the provided collections.
3960 *
3961 * Unlike `zip`, `zipAll` continues zipping until the longest collection is
3962 * exhausted. Missing values from shorter collections are filled with `undefined`.
3963 *
3964 * ```js
3965 * const a = List([ 1, 2 ]);
3966 * const b = List([ 3, 4, 5 ]);
3967 * const c = a.zipAll(b); // List [ [ 1, 3 ], [ 2, 4 ], [ undefined, 5 ] ]
3968 * ```
3969 */
3970 zipAll<U>(other: Collection<unknown, U>): Collection.Indexed<[T, U]>;
3971 zipAll<U, V>(
3972 other: Collection<unknown, U>,
3973 other2: Collection<unknown, V>
3974 ): Collection.Indexed<[T, U, V]>;
3975 zipAll(
3976 ...collections: Array<Collection<unknown, unknown>>
3977 ): Collection.Indexed<unknown>;
3978
3979 /**
3980 * Returns a Collection of the same type "zipped" with the provided
3981 * collections by using a custom `zipper` function.
3982 *
3983 * <!-- runkit:activate
3984 * { "preamble": "const { List } = require('immutable')" }
3985 * -->
3986 * ```js
3987 * const a = List([ 1, 2, 3 ]);
3988 * const b = List([ 4, 5, 6 ]);
3989 * const c = a.zipWith((a, b) => a + b, b);
3990 * // List [ 5, 7, 9 ]
3991 * ```
3992 */
3993 zipWith<U, Z>(
3994 zipper: (value: T, otherValue: U) => Z,
3995 otherCollection: Collection<unknown, U>
3996 ): Collection.Indexed<Z>;
3997 zipWith<U, V, Z>(
3998 zipper: (value: T, otherValue: U, thirdValue: V) => Z,
3999 otherCollection: Collection<unknown, U>,
4000 thirdCollection: Collection<unknown, V>
4001 ): Collection.Indexed<Z>;
4002 zipWith<Z>(
4003 zipper: (...values: Array<unknown>) => Z,
4004 ...collections: Array<Collection<unknown, unknown>>
4005 ): Collection.Indexed<Z>;
4006
4007 // Search for value
4008
4009 /**
4010 * Returns the first index at which a given value can be found in the
4011 * Collection, or -1 if it is not present.
4012 */
4013 indexOf(searchValue: T): number;
4014
4015 /**
4016 * Returns the last index at which a given value can be found in the
4017 * Collection, or -1 if it is not present.
4018 */
4019 lastIndexOf(searchValue: T): number;
4020
4021 /**
4022 * Returns the first index in the Collection where a value satisfies the
4023 * provided predicate function. Otherwise -1 is returned.
4024 */
4025 findIndex(
4026 predicate: (value: T, index: number, iter: this) => boolean,
4027 context?: unknown
4028 ): number;
4029
4030 /**
4031 * Returns the last index in the Collection where a value satisfies the
4032 * provided predicate function. Otherwise -1 is returned.
4033 */
4034 findLastIndex(
4035 predicate: (value: T, index: number, iter: this) => boolean,
4036 context?: unknown
4037 ): number;
4038
4039 // Sequence algorithms
4040
4041 /**
4042 * Returns a new Collection with other collections concatenated to this one.
4043 */
4044 concat<C>(
4045 ...valuesOrCollections: Array<Iterable<C> | C>
4046 ): Collection.Indexed<T | C>;
4047
4048 /**
4049 * Returns a new Collection.Indexed with values passed through a
4050 * `mapper` function.
4051 *
4052 * ```js
4053 * const { Collection } = require('immutable')
4054 * Collection.Indexed([1,2]).map(x => 10 * x)
4055 * // Seq [ 1, 2 ]
4056 * ```
4057 *
4058 * Note: `map()` always returns a new instance, even if it produced the
4059 * same value at every step.
4060 */
4061 map<M>(
4062 mapper: (value: T, key: number, iter: this) => M,
4063 context?: unknown
4064 ): Collection.Indexed<M>;
4065
4066 /**
4067 * Flat-maps the Collection, returning a Collection of the same type.
4068 *
4069 * Similar to `collection.map(...).flatten(true)`.
4070 */
4071 flatMap<M>(
4072 mapper: (value: T, key: number, iter: this) => Iterable<M>,
4073 context?: unknown
4074 ): Collection.Indexed<M>;
4075
4076 /**
4077 * Returns a new Collection with only the values for which the `predicate`
4078 * function returns true.
4079 *
4080 * Note: `filter()` always returns a new instance, even if it results in
4081 * not filtering out any values.
4082 */
4083 filter<F extends T>(
4084 predicate: (value: T, index: number, iter: this) => value is F,
4085 context?: unknown
4086 ): Collection.Indexed<F>;
4087 filter(
4088 predicate: (value: T, index: number, iter: this) => unknown,
4089 context?: unknown
4090 ): this;
4091
4092 /**
4093 * Returns a new indexed Collection with the values for which the
4094 * `predicate` function returns false and another for which is returns
4095 * true.
4096 */
4097 partition<F extends T, C>(
4098 predicate: (this: C, value: T, index: number, iter: this) => value is F,
4099 context?: C
4100 ): [Collection.Indexed<T>, Collection.Indexed<F>];
4101 partition<C>(
4102 predicate: (this: C, value: T, index: number, iter: this) => unknown,
4103 context?: C
4104 ): [this, this];
4105
4106 [Symbol.iterator](): IterableIterator<T>;
4107 }
4108
4109 /**
4110 * Set Collections only represent values. They have no associated keys or
4111 * indices. Duplicate values are possible in the lazy `Seq.Set`s, however
4112 * the concrete `Set` Collection does not allow duplicate values.
4113 *
4114 * Collection methods on Collection.Set such as `map` and `forEach` will provide
4115 * the value as both the first and second arguments to the provided function.
4116 *
4117 * ```js
4118 * const { Collection } = require('immutable')
4119 * const seq = Collection.Set([ 'A', 'B', 'C' ])
4120 * // Seq { "A", "B", "C" }
4121 * seq.forEach((v, k) =>
4122 * assert.equal(v, k)
4123 * )
4124 * ```
4125 */
4126 namespace Set {}
4127
4128 /**
4129 * Similar to `Collection()`, but always returns a Collection.Set.
4130 *
4131 * Note: `Collection.Set` is a factory function and not a class, and does
4132 * not use the `new` keyword during construction.
4133 */
4134 function Set<T>(collection?: Iterable<T> | ArrayLike<T>): Collection.Set<T>;
4135
4136 interface Set<T> extends Collection<T, T> {
4137 /**
4138 * Deeply converts this Set collection to equivalent native JavaScript Array.
4139 */
4140 toJS(): Array<DeepCopy<T>>;
4141
4142 /**
4143 * Shallowly converts this Set collection to equivalent native JavaScript Array.
4144 */
4145 toJSON(): Array<T>;
4146
4147 /**
4148 * Shallowly converts this collection to an Array.
4149 */
4150 toArray(): Array<T>;
4151
4152 /**
4153 * Returns Seq.Set.
4154 * @override
4155 */
4156 toSeq(): Seq.Set<T>;
4157
4158 // Sequence algorithms
4159
4160 /**
4161 * Returns a new Collection with other collections concatenated to this one.
4162 */
4163 concat<U>(...collections: Array<Iterable<U>>): Collection.Set<T | U>;
4164
4165 /**
4166 * Returns a new Collection.Set with values passed through a
4167 * `mapper` function.
4168 *
4169 * ```
4170 * Collection.Set([ 1, 2 ]).map(x => 10 * x)
4171 * // Seq { 1, 2 }
4172 * ```
4173 *
4174 * Note: `map()` always returns a new instance, even if it produced the
4175 * same value at every step.
4176 */
4177 map<M>(
4178 mapper: (value: T, key: T, iter: this) => M,
4179 context?: unknown
4180 ): Collection.Set<M>;
4181
4182 /**
4183 * Flat-maps the Collection, returning a Collection of the same type.
4184 *
4185 * Similar to `collection.map(...).flatten(true)`.
4186 */
4187 flatMap<M>(
4188 mapper: (value: T, key: T, iter: this) => Iterable<M>,
4189 context?: unknown
4190 ): Collection.Set<M>;
4191
4192 /**
4193 * Returns a new Collection with only the values for which the `predicate`
4194 * function returns true.
4195 *
4196 * Note: `filter()` always returns a new instance, even if it results in
4197 * not filtering out any values.
4198 */
4199 filter<F extends T>(
4200 predicate: (value: T, key: T, iter: this) => value is F,
4201 context?: unknown
4202 ): Collection.Set<F>;
4203 filter(
4204 predicate: (value: T, key: T, iter: this) => unknown,
4205 context?: unknown
4206 ): this;
4207
4208 /**
4209 * Returns a new set Collection with the values for which the
4210 * `predicate` function returns false and another for which is returns
4211 * true.
4212 */
4213 partition<F extends T, C>(
4214 predicate: (this: C, value: T, key: T, iter: this) => value is F,
4215 context?: C
4216 ): [Collection.Set<T>, Collection.Set<F>];
4217 partition<C>(
4218 predicate: (this: C, value: T, key: T, iter: this) => unknown,
4219 context?: C
4220 ): [this, this];
4221
4222 [Symbol.iterator](): IterableIterator<T>;
4223 }
4224 }
4225
4226 /**
4227 * Creates a Collection.
4228 *
4229 * The type of Collection created is based on the input.
4230 *
4231 * * If an `Collection`, that same `Collection`.
4232 * * If an Array-like, an `Collection.Indexed`.
4233 * * If an Object with an Iterator defined, an `Collection.Indexed`.
4234 * * If an Object, an `Collection.Keyed`.
4235 *
4236 * This methods forces the conversion of Objects and Strings to Collections.
4237 * If you want to ensure that a Collection of one item is returned, use
4238 * `Seq.of`.
4239 *
4240 * Note: An Iterator itself will be treated as an object, becoming a `Seq.Keyed`,
4241 * which is usually not what you want. You should turn your Iterator Object into
4242 * an iterable object by defining a Symbol.iterator (or @@iterator) method which
4243 * returns `this`.
4244 *
4245 * Note: `Collection` is a conversion function and not a class, and does not
4246 * use the `new` keyword during construction.
4247 */
4248 function Collection<I extends Collection<unknown, unknown>>(collection: I): I;
4249 function Collection<T>(
4250 collection: Iterable<T> | ArrayLike<T>
4251 ): Collection.Indexed<T>;
4252 function Collection<V>(obj: {
4253 [key: string]: V;
4254 }): Collection.Keyed<string, V>;
4255 function Collection<K = unknown, V = unknown>(): Collection<K, V>;
4256
4257 interface Collection<K, V> extends ValueObject {
4258 // Value equality
4259
4260 /**
4261 * True if this and the other Collection have value equality, as defined
4262 * by `Immutable.is()`.
4263 *
4264 * Note: This is equivalent to `Immutable.is(this, other)`, but provided to
4265 * allow for chained expressions.
4266 */
4267 equals(other: unknown): boolean;
4268
4269 /**
4270 * Computes and returns the hashed identity for this Collection.
4271 *
4272 * The `hashCode` of a Collection is used to determine potential equality,
4273 * and is used when adding this to a `Set` or as a key in a `Map`, enabling
4274 * lookup via a different instance.
4275 *
4276 * <!-- runkit:activate
4277 * { "preamble": "const { Set, List } = require('immutable')" }
4278 * -->
4279 * ```js
4280 * const a = List([ 1, 2, 3 ]);
4281 * const b = List([ 1, 2, 3 ]);
4282 * assert.notStrictEqual(a, b); // different instances
4283 * const set = Set([ a ]);
4284 * assert.equal(set.has(b), true);
4285 * ```
4286 *
4287 * If two values have the same `hashCode`, they are [not guaranteed
4288 * to be equal][Hash Collision]. If two values have different `hashCode`s,
4289 * they must not be equal.
4290 *
4291 * [Hash Collision]: https://en.wikipedia.org/wiki/Collision_(computer_science)
4292 */
4293 hashCode(): number;
4294
4295 // Reading values
4296
4297 /**
4298 * Returns the value associated with the provided key, or notSetValue if
4299 * the Collection does not contain this key.
4300 *
4301 * Note: it is possible a key may be associated with an `undefined` value,
4302 * so if `notSetValue` is not provided and this method returns `undefined`,
4303 * that does not guarantee the key was not found.
4304 */
4305 get<NSV>(key: K, notSetValue: NSV): V | NSV;
4306 get(key: K): V | undefined;
4307
4308 /**
4309 * True if a key exists within this `Collection`, using `Immutable.is`
4310 * to determine equality
4311 */
4312 has(key: K): boolean;
4313
4314 /**
4315 * True if a value exists within this `Collection`, using `Immutable.is`
4316 * to determine equality
4317 * @alias contains
4318 */
4319 includes(value: V): boolean;
4320 contains(value: V): boolean;
4321
4322 /**
4323 * In case the `Collection` is not empty returns the first element of the
4324 * `Collection`.
4325 * In case the `Collection` is empty returns the optional default
4326 * value if provided, if no default value is provided returns undefined.
4327 */
4328 first<NSV = undefined>(notSetValue?: NSV): V | NSV;
4329
4330 /**
4331 * In case the `Collection` is not empty returns the last element of the
4332 * `Collection`.
4333 * In case the `Collection` is empty returns the optional default
4334 * value if provided, if no default value is provided returns undefined.
4335 */
4336 last<NSV = undefined>(notSetValue?: NSV): V | NSV;
4337
4338 // Reading deep values
4339
4340 /**
4341 * Returns the value found by following a path of keys or indices through
4342 * nested Collections.
4343 *
4344 * <!-- runkit:activate -->
4345 * ```js
4346 * const { Map, List } = require('immutable')
4347 * const deepData = Map({ x: List([ Map({ y: 123 }) ]) });
4348 * deepData.getIn(['x', 0, 'y']) // 123
4349 * ```
4350 *
4351 * Plain JavaScript Object or Arrays may be nested within an Immutable.js
4352 * Collection, and getIn() can access those values as well:
4353 *
4354 * <!-- runkit:activate -->
4355 * ```js
4356 * const { Map, List } = require('immutable')
4357 * const deepData = Map({ x: [ { y: 123 } ] });
4358 * deepData.getIn(['x', 0, 'y']) // 123
4359 * ```
4360 */
4361 getIn(searchKeyPath: Iterable<unknown>, notSetValue?: unknown): unknown;
4362
4363 /**
4364 * True if the result of following a path of keys or indices through nested
4365 * Collections results in a set value.
4366 */
4367 hasIn(searchKeyPath: Iterable<unknown>): boolean;
4368
4369 // Persistent changes
4370
4371 /**
4372 * This can be very useful as a way to "chain" a normal function into a
4373 * sequence of methods. RxJS calls this "let" and lodash calls it "thru".
4374 *
4375 * For example, to sum a Seq after mapping and filtering:
4376 *
4377 * <!-- runkit:activate -->
4378 * ```js
4379 * const { Seq } = require('immutable')
4380 *
4381 * function sum(collection) {
4382 * return collection.reduce((sum, x) => sum + x, 0)
4383 * }
4384 *
4385 * Seq([ 1, 2, 3 ])
4386 * .map(x => x + 1)
4387 * .filter(x => x % 2 === 0)
4388 * .update(sum)
4389 * // 6
4390 * ```
4391 */
4392 update<R>(updater: (value: this) => R): R;
4393
4394 // Conversion to JavaScript types
4395
4396 /**
4397 * Deeply converts this Collection to equivalent native JavaScript Array or Object.
4398 *
4399 * `Collection.Indexed`, and `Collection.Set` become `Array`, while
4400 * `Collection.Keyed` become `Object`, converting keys to Strings.
4401 */
4402 toJS():
4403 | Array<DeepCopy<V>>
4404 | { [key in string | number | symbol]: DeepCopy<V> };
4405
4406 /**
4407 * Shallowly converts this Collection to equivalent native JavaScript Array or Object.
4408 *
4409 * `Collection.Indexed`, and `Collection.Set` become `Array`, while
4410 * `Collection.Keyed` become `Object`, converting keys to Strings.
4411 */
4412 toJSON(): Array<V> | { [key in string | number | symbol]: V };
4413
4414 /**
4415 * Shallowly converts this collection to an Array.
4416 *
4417 * `Collection.Indexed`, and `Collection.Set` produce an Array of values.
4418 * `Collection.Keyed` produce an Array of [key, value] tuples.
4419 */
4420 toArray(): Array<V> | Array<[K, V]>;
4421
4422 /**
4423 * Shallowly converts this Collection to an Object.
4424 *
4425 * Converts keys to Strings.
4426 */
4427 toObject(): { [key: string]: V };
4428
4429 // Conversion to Collections
4430
4431 /**
4432 * Converts this Collection to a Map, Throws if keys are not hashable.
4433 *
4434 * Note: This is equivalent to `Map(this.toKeyedSeq())`, but provided
4435 * for convenience and to allow for chained expressions.
4436 */
4437 toMap(): Map<K, V>;
4438
4439 /**
4440 * Converts this Collection to a Map, maintaining the order of iteration.
4441 *
4442 * Note: This is equivalent to `OrderedMap(this.toKeyedSeq())`, but
4443 * provided for convenience and to allow for chained expressions.
4444 */
4445 toOrderedMap(): OrderedMap<K, V>;
4446
4447 /**
4448 * Converts this Collection to a Set, discarding keys. Throws if values
4449 * are not hashable.
4450 *
4451 * Note: This is equivalent to `Set(this)`, but provided to allow for
4452 * chained expressions.
4453 */
4454 toSet(): Set<V>;
4455
4456 /**
4457 * Converts this Collection to a Set, maintaining the order of iteration and
4458 * discarding keys.
4459 *
4460 * Note: This is equivalent to `OrderedSet(this.valueSeq())`, but provided
4461 * for convenience and to allow for chained expressions.
4462 */
4463 toOrderedSet(): OrderedSet<V>;
4464
4465 /**
4466 * Converts this Collection to a List, discarding keys.
4467 *
4468 * This is similar to `List(collection)`, but provided to allow for chained
4469 * expressions. However, when called on `Map` or other keyed collections,
4470 * `collection.toList()` discards the keys and creates a list of only the
4471 * values, whereas `List(collection)` creates a list of entry tuples.
4472 *
4473 * <!-- runkit:activate -->
4474 * ```js
4475 * const { Map, List } = require('immutable')
4476 * var myMap = Map({ a: 'Apple', b: 'Banana' })
4477 * List(myMap) // List [ [ "a", "Apple" ], [ "b", "Banana" ] ]
4478 * myMap.toList() // List [ "Apple", "Banana" ]
4479 * ```
4480 */
4481 toList(): List<V>;
4482
4483 /**
4484 * Converts this Collection to a Stack, discarding keys. Throws if values
4485 * are not hashable.
4486 *
4487 * Note: This is equivalent to `Stack(this)`, but provided to allow for
4488 * chained expressions.
4489 */
4490 toStack(): Stack<V>;
4491
4492 // Conversion to Seq
4493
4494 /**
4495 * Converts this Collection to a Seq of the same kind (indexed,
4496 * keyed, or set).
4497 */
4498 toSeq(): Seq<K, V>;
4499
4500 /**
4501 * Returns a Seq.Keyed from this Collection where indices are treated as keys.
4502 *
4503 * This is useful if you want to operate on an
4504 * Collection.Indexed and preserve the [index, value] pairs.
4505 *
4506 * The returned Seq will have identical iteration order as
4507 * this Collection.
4508 *
4509 * <!-- runkit:activate -->
4510 * ```js
4511 * const { Seq } = require('immutable')
4512 * const indexedSeq = Seq([ 'A', 'B', 'C' ])
4513 * // Seq [ "A", "B", "C" ]
4514 * indexedSeq.filter(v => v === 'B')
4515 * // Seq [ "B" ]
4516 * const keyedSeq = indexedSeq.toKeyedSeq()
4517 * // Seq { 0: "A", 1: "B", 2: "C" }
4518 * keyedSeq.filter(v => v === 'B')
4519 * // Seq { 1: "B" }
4520 * ```
4521 */
4522 toKeyedSeq(): Seq.Keyed<K, V>;
4523
4524 /**
4525 * Returns an Seq.Indexed of the values of this Collection, discarding keys.
4526 */
4527 toIndexedSeq(): Seq.Indexed<V>;
4528
4529 /**
4530 * Returns a Seq.Set of the values of this Collection, discarding keys.
4531 */
4532 toSetSeq(): Seq.Set<V>;
4533
4534 // Iterators
4535
4536 /**
4537 * An iterator of this `Collection`'s keys.
4538 *
4539 * Note: this will return an ES6 iterator which does not support
4540 * Immutable.js sequence algorithms. Use `keySeq` instead, if this is
4541 * what you want.
4542 */
4543 keys(): IterableIterator<K>;
4544
4545 /**
4546 * An iterator of this `Collection`'s values.
4547 *
4548 * Note: this will return an ES6 iterator which does not support
4549 * Immutable.js sequence algorithms. Use `valueSeq` instead, if this is
4550 * what you want.
4551 */
4552 values(): IterableIterator<V>;
4553
4554 /**
4555 * An iterator of this `Collection`'s entries as `[ key, value ]` tuples.
4556 *
4557 * Note: this will return an ES6 iterator which does not support
4558 * Immutable.js sequence algorithms. Use `entrySeq` instead, if this is
4559 * what you want.
4560 */
4561 entries(): IterableIterator<[K, V]>;
4562
4563 [Symbol.iterator](): IterableIterator<unknown>;
4564
4565 // Collections (Seq)
4566
4567 /**
4568 * Returns a new Seq.Indexed of the keys of this Collection,
4569 * discarding values.
4570 */
4571 keySeq(): Seq.Indexed<K>;
4572
4573 /**
4574 * Returns an Seq.Indexed of the values of this Collection, discarding keys.
4575 */
4576 valueSeq(): Seq.Indexed<V>;
4577
4578 /**
4579 * Returns a new Seq.Indexed of [key, value] tuples.
4580 */
4581 entrySeq(): Seq.Indexed<[K, V]>;
4582
4583 // Sequence algorithms
4584
4585 /**
4586 * Returns a new Collection of the same type with values passed through a
4587 * `mapper` function.
4588 *
4589 * <!-- runkit:activate -->
4590 * ```js
4591 * const { Collection } = require('immutable')
4592 * Collection({ a: 1, b: 2 }).map(x => 10 * x)
4593 * // Seq { "a": 10, "b": 20 }
4594 * ```
4595 *
4596 * Note: `map()` always returns a new instance, even if it produced the same
4597 * value at every step.
4598 */
4599 map<M>(
4600 mapper: (value: V, key: K, iter: this) => M,
4601 context?: unknown
4602 ): Collection<K, M>;
4603
4604 /**
4605 * Note: used only for sets, which return Collection<M, M> but are otherwise
4606 * identical to normal `map()`.
4607 *
4608 * @ignore
4609 */
4610 map(...args: Array<never>): unknown;
4611
4612 /**
4613 * Returns a new Collection of the same type with only the entries for which
4614 * the `predicate` function returns true.
4615 *
4616 * <!-- runkit:activate -->
4617 * ```js
4618 * const { Map } = require('immutable')
4619 * Map({ a: 1, b: 2, c: 3, d: 4}).filter(x => x % 2 === 0)
4620 * // Map { "b": 2, "d": 4 }
4621 * ```
4622 *
4623 * Note: `filter()` always returns a new instance, even if it results in
4624 * not filtering out any values.
4625 */
4626 filter<F extends V>(
4627 predicate: (value: V, key: K, iter: this) => value is F,
4628 context?: unknown
4629 ): Collection<K, F>;
4630 filter(
4631 predicate: (value: V, key: K, iter: this) => unknown,
4632 context?: unknown
4633 ): this;
4634
4635 /**
4636 * Returns a new Collection of the same type with only the entries for which
4637 * the `predicate` function returns false.
4638 *
4639 * <!-- runkit:activate -->
4640 * ```js
4641 * const { Map } = require('immutable')
4642 * Map({ a: 1, b: 2, c: 3, d: 4}).filterNot(x => x % 2 === 0)
4643 * // Map { "a": 1, "c": 3 }
4644 * ```
4645 *
4646 * Note: `filterNot()` always returns a new instance, even if it results in
4647 * not filtering out any values.
4648 */
4649 filterNot(
4650 predicate: (value: V, key: K, iter: this) => boolean,
4651 context?: unknown
4652 ): this;
4653
4654 /**
4655 * Returns a new Collection with the values for which the `predicate`
4656 * function returns false and another for which is returns true.
4657 */
4658 partition<F extends V, C>(
4659 predicate: (this: C, value: V, key: K, iter: this) => value is F,
4660 context?: C
4661 ): [Collection<K, V>, Collection<K, F>];
4662 partition<C>(
4663 predicate: (this: C, value: V, key: K, iter: this) => unknown,
4664 context?: C
4665 ): [this, this];
4666
4667 /**
4668 * Returns a new Collection of the same type in reverse order.
4669 */
4670 reverse(): this;
4671
4672 /**
4673 * Returns a new Collection of the same type which includes the same entries,
4674 * stably sorted by using a `comparator`.
4675 *
4676 * If a `comparator` is not provided, a default comparator uses `<` and `>`.
4677 *
4678 * `comparator(valueA, valueB)`:
4679 *
4680 * * Returns `0` if the elements should not be swapped.
4681 * * Returns `-1` (or any negative number) if `valueA` comes before `valueB`
4682 * * Returns `1` (or any positive number) if `valueA` comes after `valueB`
4683 * * Alternatively, can return a value of the `PairSorting` enum type
4684 * * Is pure, i.e. it must always return the same value for the same pair
4685 * of values.
4686 *
4687 * When sorting collections which have no defined order, their ordered
4688 * equivalents will be returned. e.g. `map.sort()` returns OrderedMap.
4689 *
4690 * <!-- runkit:activate -->
4691 * ```js
4692 * const { Map } = require('immutable')
4693 * Map({ "c": 3, "a": 1, "b": 2 }).sort((a, b) => {
4694 * if (a < b) { return -1; }
4695 * if (a > b) { return 1; }
4696 * if (a === b) { return 0; }
4697 * });
4698 * // OrderedMap { "a": 1, "b": 2, "c": 3 }
4699 * ```
4700 *
4701 * Note: `sort()` Always returns a new instance, even if the original was
4702 * already sorted.
4703 *
4704 * Note: This is always an eager operation.
4705 */
4706 sort(comparator?: Comparator<V>): this;
4707
4708 /**
4709 * Like `sort`, but also accepts a `comparatorValueMapper` which allows for
4710 * sorting by more sophisticated means:
4711 *
4712 * <!-- runkit:activate -->
4713 * ```js
4714 * const { Map } = require('immutable')
4715 * const beattles = Map({
4716 * John: { name: "Lennon" },
4717 * Paul: { name: "McCartney" },
4718 * George: { name: "Harrison" },
4719 * Ringo: { name: "Starr" },
4720 * });
4721 * beattles.sortBy(member => member.name);
4722 * ```
4723 *
4724 * Note: `sortBy()` Always returns a new instance, even if the original was
4725 * already sorted.
4726 *
4727 * Note: This is always an eager operation.
4728 */
4729 sortBy<C>(
4730 comparatorValueMapper: (value: V, key: K, iter: this) => C,
4731 comparator?: Comparator<C>
4732 ): this;
4733
4734 /**
4735 * Returns a `Map` of `Collection`, grouped by the return
4736 * value of the `grouper` function.
4737 *
4738 * Note: This is always an eager operation.
4739 *
4740 * <!-- runkit:activate -->
4741 * ```js
4742 * const { List, Map } = require('immutable')
4743 * const listOfMaps = List([
4744 * Map({ v: 0 }),
4745 * Map({ v: 1 }),
4746 * Map({ v: 1 }),
4747 * Map({ v: 0 }),
4748 * Map({ v: 2 })
4749 * ])
4750 * const groupsOfMaps = listOfMaps.groupBy(x => x.get('v'))
4751 * // Map {
4752 * // 0: List [ Map{ "v": 0 }, Map { "v": 0 } ],
4753 * // 1: List [ Map{ "v": 1 }, Map { "v": 1 } ],
4754 * // 2: List [ Map{ "v": 2 } ],
4755 * // }
4756 * ```
4757 */
4758 groupBy<G>(
4759 grouper: (value: V, key: K, iter: this) => G,
4760 context?: unknown
4761 ): Map<G, this>;
4762
4763 // Side effects
4764
4765 /**
4766 * The `sideEffect` is executed for every entry in the Collection.
4767 *
4768 * Unlike `Array#forEach`, if any call of `sideEffect` returns
4769 * `false`, the iteration will stop. Returns the number of entries iterated
4770 * (including the last iteration which returned false).
4771 */
4772 forEach(
4773 sideEffect: (value: V, key: K, iter: this) => unknown,
4774 context?: unknown
4775 ): number;
4776
4777 // Creating subsets
4778
4779 /**
4780 * Returns a new Collection of the same type representing a portion of this
4781 * Collection from start up to but not including end.
4782 *
4783 * If begin is negative, it is offset from the end of the Collection. e.g.
4784 * `slice(-2)` returns a Collection of the last two entries. If it is not
4785 * provided the new Collection will begin at the beginning of this Collection.
4786 *
4787 * If end is negative, it is offset from the end of the Collection. e.g.
4788 * `slice(0, -1)` returns a Collection of everything but the last entry. If
4789 * it is not provided, the new Collection will continue through the end of
4790 * this Collection.
4791 *
4792 * If the requested slice is equivalent to the current Collection, then it
4793 * will return itself.
4794 */
4795 slice(begin?: number, end?: number): this;
4796
4797 /**
4798 * Returns a new Collection of the same type containing all entries except
4799 * the first.
4800 */
4801 rest(): this;
4802
4803 /**
4804 * Returns a new Collection of the same type containing all entries except
4805 * the last.
4806 */
4807 butLast(): this;
4808
4809 /**
4810 * Returns a new Collection of the same type which excludes the first `amount`
4811 * entries from this Collection.
4812 */
4813 skip(amount: number): this;
4814
4815 /**
4816 * Returns a new Collection of the same type which excludes the last `amount`
4817 * entries from this Collection.
4818 */
4819 skipLast(amount: number): this;
4820
4821 /**
4822 * Returns a new Collection of the same type which includes entries starting
4823 * from when `predicate` first returns false.
4824 *
4825 * <!-- runkit:activate -->
4826 * ```js
4827 * const { List } = require('immutable')
4828 * List([ 'dog', 'frog', 'cat', 'hat', 'god' ])
4829 * .skipWhile(x => x.match(/g/))
4830 * // List [ "cat", "hat", "god" ]
4831 * ```
4832 */
4833 skipWhile(
4834 predicate: (value: V, key: K, iter: this) => boolean,
4835 context?: unknown
4836 ): this;
4837
4838 /**
4839 * Returns a new Collection of the same type which includes entries starting
4840 * from when `predicate` first returns true.
4841 *
4842 * <!-- runkit:activate -->
4843 * ```js
4844 * const { List } = require('immutable')
4845 * List([ 'dog', 'frog', 'cat', 'hat', 'god' ])
4846 * .skipUntil(x => x.match(/hat/))
4847 * // List [ "hat", "god" ]
4848 * ```
4849 */
4850 skipUntil(
4851 predicate: (value: V, key: K, iter: this) => boolean,
4852 context?: unknown
4853 ): this;
4854
4855 /**
4856 * Returns a new Collection of the same type which includes the first `amount`
4857 * entries from this Collection.
4858 */
4859 take(amount: number): this;
4860
4861 /**
4862 * Returns a new Collection of the same type which includes the last `amount`
4863 * entries from this Collection.
4864 */
4865 takeLast(amount: number): this;
4866
4867 /**
4868 * Returns a new Collection of the same type which includes entries from this
4869 * Collection as long as the `predicate` returns true.
4870 *
4871 * <!-- runkit:activate -->
4872 * ```js
4873 * const { List } = require('immutable')
4874 * List([ 'dog', 'frog', 'cat', 'hat', 'god' ])
4875 * .takeWhile(x => x.match(/o/))
4876 * // List [ "dog", "frog" ]
4877 * ```
4878 */
4879 takeWhile(
4880 predicate: (value: V, key: K, iter: this) => boolean,
4881 context?: unknown
4882 ): this;
4883
4884 /**
4885 * Returns a new Collection of the same type which includes entries from this
4886 * Collection as long as the `predicate` returns false.
4887 *
4888 * <!-- runkit:activate -->
4889 * ```js
4890 * const { List } = require('immutable')
4891 * List([ 'dog', 'frog', 'cat', 'hat', 'god' ])
4892 * .takeUntil(x => x.match(/at/))
4893 * // List [ "dog", "frog" ]
4894 * ```
4895 */
4896 takeUntil(
4897 predicate: (value: V, key: K, iter: this) => boolean,
4898 context?: unknown
4899 ): this;
4900
4901 // Combination
4902
4903 /**
4904 * Returns a new Collection of the same type with other values and
4905 * collection-like concatenated to this one.
4906 *
4907 * For Seqs, all entries will be present in the resulting Seq, even if they
4908 * have the same key.
4909 */
4910 concat(
4911 ...valuesOrCollections: Array<unknown>
4912 ): Collection<unknown, unknown>;
4913
4914 /**
4915 * Flattens nested Collections.
4916 *
4917 * Will deeply flatten the Collection by default, returning a Collection of the
4918 * same type, but a `depth` can be provided in the form of a number or
4919 * boolean (where true means to shallowly flatten one level). A depth of 0
4920 * (or shallow: false) will deeply flatten.
4921 *
4922 * Flattens only others Collection, not Arrays or Objects.
4923 *
4924 * Note: `flatten(true)` operates on Collection<unknown, Collection<K, V>> and
4925 * returns Collection<K, V>
4926 */
4927 flatten(depth?: number): Collection<unknown, unknown>;
4928 // tslint:disable-next-line unified-signatures
4929 flatten(shallow?: boolean): Collection<unknown, unknown>;
4930
4931 /**
4932 * Flat-maps the Collection, returning a Collection of the same type.
4933 *
4934 * Similar to `collection.map(...).flatten(true)`.
4935 */
4936 flatMap<M>(
4937 mapper: (value: V, key: K, iter: this) => Iterable<M>,
4938 context?: unknown
4939 ): Collection<K, M>;
4940
4941 /**
4942 * Flat-maps the Collection, returning a Collection of the same type.
4943 *
4944 * Similar to `collection.map(...).flatten(true)`.
4945 * Used for Dictionaries only.
4946 */
4947 flatMap<KM, VM>(
4948 mapper: (value: V, key: K, iter: this) => Iterable<[KM, VM]>,
4949 context?: unknown
4950 ): Collection<KM, VM>;
4951
4952 // Reducing a value
4953
4954 /**
4955 * Reduces the Collection to a value by calling the `reducer` for every entry
4956 * in the Collection and passing along the reduced value.
4957 *
4958 * If `initialReduction` is not provided, the first item in the
4959 * Collection will be used.
4960 *
4961 * @see `Array#reduce`.
4962 */
4963 reduce<R>(
4964 reducer: (reduction: R, value: V, key: K, iter: this) => R,
4965 initialReduction: R,
4966 context?: unknown
4967 ): R;
4968 reduce<R>(
4969 reducer: (reduction: V | R, value: V, key: K, iter: this) => R
4970 ): R;
4971
4972 /**
4973 * Reduces the Collection in reverse (from the right side).
4974 *
4975 * Note: Similar to this.reverse().reduce(), and provided for parity
4976 * with `Array#reduceRight`.
4977 */
4978 reduceRight<R>(
4979 reducer: (reduction: R, value: V, key: K, iter: this) => R,
4980 initialReduction: R,
4981 context?: unknown
4982 ): R;
4983 reduceRight<R>(
4984 reducer: (reduction: V | R, value: V, key: K, iter: this) => R
4985 ): R;
4986
4987 /**
4988 * True if `predicate` returns true for all entries in the Collection.
4989 */
4990 every(
4991 predicate: (value: V, key: K, iter: this) => boolean,
4992 context?: unknown
4993 ): boolean;
4994
4995 /**
4996 * True if `predicate` returns true for any entry in the Collection.
4997 */
4998 some(
4999 predicate: (value: V, key: K, iter: this) => boolean,
5000 context?: unknown
5001 ): boolean;
5002
5003 /**
5004 * Joins values together as a string, inserting a separator between each.
5005 * The default separator is `","`.
5006 */
5007 join(separator?: string): string;
5008
5009 /**
5010 * Returns true if this Collection includes no values.
5011 *
5012 * For some lazy `Seq`, `isEmpty` might need to iterate to determine
5013 * emptiness. At most one iteration will occur.
5014 */
5015 isEmpty(): boolean;
5016
5017 /**
5018 * Returns the size of this Collection.
5019 *
5020 * Regardless of if this Collection can describe its size lazily (some Seqs
5021 * cannot), this method will always return the correct size. E.g. it
5022 * evaluates a lazy `Seq` if necessary.
5023 *
5024 * If `predicate` is provided, then this returns the count of entries in the
5025 * Collection for which the `predicate` returns true.
5026 */
5027 count(): number;
5028 count(
5029 predicate: (value: V, key: K, iter: this) => boolean,
5030 context?: unknown
5031 ): number;
5032
5033 /**
5034 * Returns a `Seq.Keyed` of counts, grouped by the return value of
5035 * the `grouper` function.
5036 *
5037 * Note: This is not a lazy operation.
5038 */
5039 countBy<G>(
5040 grouper: (value: V, key: K, iter: this) => G,
5041 context?: unknown
5042 ): Map<G, number>;
5043
5044 // Search for value
5045
5046 /**
5047 * Returns the first value for which the `predicate` returns true.
5048 */
5049 find(
5050 predicate: (value: V, key: K, iter: this) => boolean,
5051 context?: unknown,
5052 notSetValue?: V
5053 ): V | undefined;
5054
5055 /**
5056 * Returns the last value for which the `predicate` returns true.
5057 *
5058 * Note: `predicate` will be called for each entry in reverse.
5059 */
5060 findLast(
5061 predicate: (value: V, key: K, iter: this) => boolean,
5062 context?: unknown,
5063 notSetValue?: V
5064 ): V | undefined;
5065
5066 /**
5067 * Returns the first [key, value] entry for which the `predicate` returns true.
5068 */
5069 findEntry(
5070 predicate: (value: V, key: K, iter: this) => boolean,
5071 context?: unknown,
5072 notSetValue?: V
5073 ): [K, V] | undefined;
5074
5075 /**
5076 * Returns the last [key, value] entry for which the `predicate`
5077 * returns true.
5078 *
5079 * Note: `predicate` will be called for each entry in reverse.
5080 */
5081 findLastEntry(
5082 predicate: (value: V, key: K, iter: this) => boolean,
5083 context?: unknown,
5084 notSetValue?: V
5085 ): [K, V] | undefined;
5086
5087 /**
5088 * Returns the key for which the `predicate` returns true.
5089 */
5090 findKey(
5091 predicate: (value: V, key: K, iter: this) => boolean,
5092 context?: unknown
5093 ): K | undefined;
5094
5095 /**
5096 * Returns the last key for which the `predicate` returns true.
5097 *
5098 * Note: `predicate` will be called for each entry in reverse.
5099 */
5100 findLastKey(
5101 predicate: (value: V, key: K, iter: this) => boolean,
5102 context?: unknown
5103 ): K | undefined;
5104
5105 /**
5106 * Returns the key associated with the search value, or undefined.
5107 */
5108 keyOf(searchValue: V): K | undefined;
5109
5110 /**
5111 * Returns the last key associated with the search value, or undefined.
5112 */
5113 lastKeyOf(searchValue: V): K | undefined;
5114
5115 /**
5116 * Returns the maximum value in this collection. If any values are
5117 * comparatively equivalent, the first one found will be returned.
5118 *
5119 * The `comparator` is used in the same way as `Collection#sort`. If it is not
5120 * provided, the default comparator is `>`.
5121 *
5122 * When two values are considered equivalent, the first encountered will be
5123 * returned. Otherwise, `max` will operate independent of the order of input
5124 * as long as the comparator is commutative. The default comparator `>` is
5125 * commutative *only* when types do not differ.
5126 *
5127 * If `comparator` returns 0 and either value is NaN, undefined, or null,
5128 * that value will be returned.
5129 */
5130 max(comparator?: Comparator<V>): V | undefined;
5131
5132 /**
5133 * Like `max`, but also accepts a `comparatorValueMapper` which allows for
5134 * comparing by more sophisticated means:
5135 *
5136 * <!-- runkit:activate -->
5137 * ```js
5138 * const { List, } = require('immutable');
5139 * const l = List([
5140 * { name: 'Bob', avgHit: 1 },
5141 * { name: 'Max', avgHit: 3 },
5142 * { name: 'Lili', avgHit: 2 } ,
5143 * ]);
5144 * l.maxBy(i => i.avgHit); // will output { name: 'Max', avgHit: 3 }
5145 * ```
5146 */
5147 maxBy<C>(
5148 comparatorValueMapper: (value: V, key: K, iter: this) => C,
5149 comparator?: Comparator<C>
5150 ): V | undefined;
5151
5152 /**
5153 * Returns the minimum value in this collection. If any values are
5154 * comparatively equivalent, the first one found will be returned.
5155 *
5156 * The `comparator` is used in the same way as `Collection#sort`. If it is not
5157 * provided, the default comparator is `<`.
5158 *
5159 * When two values are considered equivalent, the first encountered will be
5160 * returned. Otherwise, `min` will operate independent of the order of input
5161 * as long as the comparator is commutative. The default comparator `<` is
5162 * commutative *only* when types do not differ.
5163 *
5164 * If `comparator` returns 0 and either value is NaN, undefined, or null,
5165 * that value will be returned.
5166 */
5167 min(comparator?: Comparator<V>): V | undefined;
5168
5169 /**
5170 * Like `min`, but also accepts a `comparatorValueMapper` which allows for
5171 * comparing by more sophisticated means:
5172 *
5173 * <!-- runkit:activate -->
5174 * ```js
5175 * const { List, } = require('immutable');
5176 * const l = List([
5177 * { name: 'Bob', avgHit: 1 },
5178 * { name: 'Max', avgHit: 3 },
5179 * { name: 'Lili', avgHit: 2 } ,
5180 * ]);
5181 * l.minBy(i => i.avgHit); // will output { name: 'Bob', avgHit: 1 }
5182 * ```
5183 */
5184 minBy<C>(
5185 comparatorValueMapper: (value: V, key: K, iter: this) => C,
5186 comparator?: Comparator<C>
5187 ): V | undefined;
5188
5189 // Comparison
5190
5191 /**
5192 * True if `iter` includes every value in this Collection.
5193 */
5194 isSubset(iter: Iterable<V>): boolean;
5195
5196 /**
5197 * True if this Collection includes every value in `iter`.
5198 */
5199 isSuperset(iter: Iterable<V>): boolean;
5200 }
5201
5202 /**
5203 * The interface to fulfill to qualify as a Value Object.
5204 */
5205 interface ValueObject {
5206 /**
5207 * True if this and the other Collection have value equality, as defined
5208 * by `Immutable.is()`.
5209 *
5210 * Note: This is equivalent to `Immutable.is(this, other)`, but provided to
5211 * allow for chained expressions.
5212 */
5213 equals(other: unknown): boolean;
5214
5215 /**
5216 * Computes and returns the hashed identity for this Collection.
5217 *
5218 * The `hashCode` of a Collection is used to determine potential equality,
5219 * and is used when adding this to a `Set` or as a key in a `Map`, enabling
5220 * lookup via a different instance.
5221 *
5222 * <!-- runkit:activate -->
5223 * ```js
5224 * const { List, Set } = require('immutable');
5225 * const a = List([ 1, 2, 3 ]);
5226 * const b = List([ 1, 2, 3 ]);
5227 * assert.notStrictEqual(a, b); // different instances
5228 * const set = Set([ a ]);
5229 * assert.equal(set.has(b), true);
5230 * ```
5231 *
5232 * Note: hashCode() MUST return a Uint32 number. The easiest way to
5233 * guarantee this is to return `myHash | 0` from a custom implementation.
5234 *
5235 * If two values have the same `hashCode`, they are [not guaranteed
5236 * to be equal][Hash Collision]. If two values have different `hashCode`s,
5237 * they must not be equal.
5238 *
5239 * Note: `hashCode()` is not guaranteed to always be called before
5240 * `equals()`. Most but not all Immutable.js collections use hash codes to
5241 * organize their internal data structures, while all Immutable.js
5242 * collections use equality during lookups.
5243 *
5244 * [Hash Collision]: https://en.wikipedia.org/wiki/Collision_(computer_science)
5245 */
5246 hashCode(): number;
5247 }
5248
5249 /**
5250 * Deeply converts plain JS objects and arrays to Immutable Maps and Lists.
5251 *
5252 * `fromJS` will convert Arrays and [array-like objects][2] to a List, and
5253 * plain objects (without a custom prototype) to a Map. [Iterable objects][3]
5254 * may be converted to List, Map, or Set.
5255 *
5256 * If a `reviver` is optionally provided, it will be called with every
5257 * collection as a Seq (beginning with the most nested collections
5258 * and proceeding to the top-level collection itself), along with the key
5259 * referring to each collection and the parent JS object provided as `this`.
5260 * For the top level, object, the key will be `""`. This `reviver` is expected
5261 * to return a new Immutable Collection, allowing for custom conversions from
5262 * deep JS objects. Finally, a `path` is provided which is the sequence of
5263 * keys to this value from the starting value.
5264 *
5265 * `reviver` acts similarly to the [same parameter in `JSON.parse`][1].
5266 *
5267 * If `reviver` is not provided, the default behavior will convert Objects
5268 * into Maps and Arrays into Lists like so:
5269 *
5270 * <!-- runkit:activate -->
5271 * ```js
5272 * const { fromJS, isKeyed } = require('immutable')
5273 * function (key, value) {
5274 * return isKeyed(value) ? value.toMap() : value.toList()
5275 * }
5276 * ```
5277 *
5278 * Accordingly, this example converts native JS data to OrderedMap and List:
5279 *
5280 * <!-- runkit:activate -->
5281 * ```js
5282 * const { fromJS, isKeyed } = require('immutable')
5283 * fromJS({ a: {b: [10, 20, 30]}, c: 40}, function (key, value, path) {
5284 * console.log(key, value, path)
5285 * return isKeyed(value) ? value.toOrderedMap() : value.toList()
5286 * })
5287 *
5288 * > "b", [ 10, 20, 30 ], [ "a", "b" ]
5289 * > "a", {b: [10, 20, 30]}, [ "a" ]
5290 * > "", {a: {b: [10, 20, 30]}, c: 40}, []
5291 * ```
5292 *
5293 * Keep in mind, when using JS objects to construct Immutable Maps, that
5294 * JavaScript Object properties are always strings, even if written in a
5295 * quote-less shorthand, while Immutable Maps accept keys of any type.
5296 *
5297 * <!-- runkit:activate -->
5298 * ```js
5299 * const { Map } = require('immutable')
5300 * let obj = { 1: "one" };
5301 * Object.keys(obj); // [ "1" ]
5302 * assert.equal(obj["1"], obj[1]); // "one" === "one"
5303 *
5304 * let map = Map(obj);
5305 * assert.notEqual(map.get("1"), map.get(1)); // "one" !== undefined
5306 * ```
5307 *
5308 * Property access for JavaScript Objects first converts the key to a string,
5309 * but since Immutable Map keys can be of any type the argument to `get()` is
5310 * not altered.
5311 *
5312 * [1]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/parse#Example.3A_Using_the_reviver_parameter
5313 * "Using the reviver parameter"
5314 * [2]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Indexed_collections#working_with_array-like_objects
5315 * "Working with array-like objects"
5316 * [3]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Iteration_protocols#the_iterable_protocol
5317 * "The iterable protocol"
5318 */
5319 function fromJS<JSValue>(
5320 jsValue: JSValue,
5321 reviver?: undefined
5322 ): FromJS<JSValue>;
5323 function fromJS(
5324 jsValue: unknown,
5325 reviver?: (
5326 key: string | number,
5327 sequence: Collection.Keyed<string, unknown> | Collection.Indexed<unknown>,
5328 path?: Array<string | number>
5329 ) => unknown
5330 ): Collection<unknown, unknown>;
5331
5332 type FromJS<JSValue> = JSValue extends FromJSNoTransform
5333 ? JSValue
5334 : JSValue extends Array<any>
5335 ? FromJSArray<JSValue>
5336 : JSValue extends {}
5337 ? FromJSObject<JSValue>
5338 : any;
5339
5340 type FromJSNoTransform =
5341 | Collection<any, any>
5342 | number
5343 | string
5344 | null
5345 | undefined;
5346
5347 type FromJSArray<JSValue> = JSValue extends Array<infer T>
5348 ? List<FromJS<T>>
5349 : never;
5350
5351 type FromJSObject<JSValue> = JSValue extends {}
5352 ? Map<keyof JSValue, FromJS<JSValue[keyof JSValue]>>
5353 : never;
5354
5355 /**
5356 * Value equality check with semantics similar to `Object.is`, but treats
5357 * Immutable `Collection`s as values, equal if the second `Collection` includes
5358 * equivalent values.
5359 *
5360 * It's used throughout Immutable when checking for equality, including `Map`
5361 * key equality and `Set` membership.
5362 *
5363 * <!-- runkit:activate -->
5364 * ```js
5365 * const { Map, is } = require('immutable')
5366 * const map1 = Map({ a: 1, b: 1, c: 1 })
5367 * const map2 = Map({ a: 1, b: 1, c: 1 })
5368 * assert.equal(map1 !== map2, true)
5369 * assert.equal(Object.is(map1, map2), false)
5370 * assert.equal(is(map1, map2), true)
5371 * ```
5372 *
5373 * `is()` compares primitive types like strings and numbers, Immutable.js
5374 * collections like `Map` and `List`, but also any custom object which
5375 * implements `ValueObject` by providing `equals()` and `hashCode()` methods.
5376 *
5377 * Note: Unlike `Object.is`, `Immutable.is` assumes `0` and `-0` are the same
5378 * value, matching the behavior of ES6 Map key equality.
5379 */
5380 function is(first: unknown, second: unknown): boolean;
5381
5382 /**
5383 * The `hash()` function is an important part of how Immutable determines if
5384 * two values are equivalent and is used to determine how to store those
5385 * values. Provided with any value, `hash()` will return a 31-bit integer.
5386 *
5387 * When designing Objects which may be equal, it's important that when a
5388 * `.equals()` method returns true, that both values `.hashCode()` method
5389 * return the same value. `hash()` may be used to produce those values.
5390 *
5391 * For non-Immutable Objects that do not provide a `.hashCode()` functions
5392 * (including plain Objects, plain Arrays, Date objects, etc), a unique hash
5393 * value will be created for each *instance*. That is, the create hash
5394 * represents referential equality, and not value equality for Objects. This
5395 * ensures that if that Object is mutated over time that its hash code will
5396 * remain consistent, allowing Objects to be used as keys and values in
5397 * Immutable.js collections.
5398 *
5399 * Note that `hash()` attempts to balance between speed and avoiding
5400 * collisions, however it makes no attempt to produce secure hashes.
5401 *
5402 * *New in Version 4.0*
5403 */
5404 function hash(value: unknown): number;
5405
5406 /**
5407 * True if `maybeImmutable` is an Immutable Collection or Record.
5408 *
5409 * Note: Still returns true even if the collections is within a `withMutations()`.
5410 *
5411 * <!-- runkit:activate -->
5412 * ```js
5413 * const { isImmutable, Map, List, Stack } = require('immutable');
5414 * isImmutable([]); // false
5415 * isImmutable({}); // false
5416 * isImmutable(Map()); // true
5417 * isImmutable(List()); // true
5418 * isImmutable(Stack()); // true
5419 * isImmutable(Map().asMutable()); // true
5420 * ```
5421 */
5422 function isImmutable(
5423 maybeImmutable: unknown
5424 ): maybeImmutable is Collection<unknown, unknown>;
5425
5426 /**
5427 * True if `maybeCollection` is a Collection, or any of its subclasses.
5428 *
5429 * <!-- runkit:activate -->
5430 * ```js
5431 * const { isCollection, Map, List, Stack } = require('immutable');
5432 * isCollection([]); // false
5433 * isCollection({}); // false
5434 * isCollection(Map()); // true
5435 * isCollection(List()); // true
5436 * isCollection(Stack()); // true
5437 * ```
5438 */
5439 function isCollection(
5440 maybeCollection: unknown
5441 ): maybeCollection is Collection<unknown, unknown>;
5442
5443 /**
5444 * True if `maybeKeyed` is a Collection.Keyed, or any of its subclasses.
5445 *
5446 * <!-- runkit:activate -->
5447 * ```js
5448 * const { isKeyed, Map, List, Stack } = require('immutable');
5449 * isKeyed([]); // false
5450 * isKeyed({}); // false
5451 * isKeyed(Map()); // true
5452 * isKeyed(List()); // false
5453 * isKeyed(Stack()); // false
5454 * ```
5455 */
5456 function isKeyed(
5457 maybeKeyed: unknown
5458 ): maybeKeyed is Collection.Keyed<unknown, unknown>;
5459
5460 /**
5461 * True if `maybeIndexed` is a Collection.Indexed, or any of its subclasses.
5462 *
5463 * <!-- runkit:activate -->
5464 * ```js
5465 * const { isIndexed, Map, List, Stack, Set } = require('immutable');
5466 * isIndexed([]); // false
5467 * isIndexed({}); // false
5468 * isIndexed(Map()); // false
5469 * isIndexed(List()); // true
5470 * isIndexed(Stack()); // true
5471 * isIndexed(Set()); // false
5472 * ```
5473 */
5474 function isIndexed(
5475 maybeIndexed: unknown
5476 ): maybeIndexed is Collection.Indexed<unknown>;
5477
5478 /**
5479 * True if `maybeAssociative` is either a Keyed or Indexed Collection.
5480 *
5481 * <!-- runkit:activate -->
5482 * ```js
5483 * const { isAssociative, Map, List, Stack, Set } = require('immutable');
5484 * isAssociative([]); // false
5485 * isAssociative({}); // false
5486 * isAssociative(Map()); // true
5487 * isAssociative(List()); // true
5488 * isAssociative(Stack()); // true
5489 * isAssociative(Set()); // false
5490 * ```
5491 */
5492 function isAssociative(
5493 maybeAssociative: unknown
5494 ): maybeAssociative is
5495 | Collection.Keyed<unknown, unknown>
5496 | Collection.Indexed<unknown>;
5497
5498 /**
5499 * True if `maybeOrdered` is a Collection where iteration order is well
5500 * defined. True for Collection.Indexed as well as OrderedMap and OrderedSet.
5501 *
5502 * <!-- runkit:activate -->
5503 * ```js
5504 * const { isOrdered, Map, OrderedMap, List, Set } = require('immutable');
5505 * isOrdered([]); // false
5506 * isOrdered({}); // false
5507 * isOrdered(Map()); // false
5508 * isOrdered(OrderedMap()); // true
5509 * isOrdered(List()); // true
5510 * isOrdered(Set()); // false
5511 * ```
5512 */
5513 function isOrdered(maybeOrdered: unknown): boolean;
5514
5515 /**
5516 * True if `maybeValue` is a JavaScript Object which has *both* `equals()`
5517 * and `hashCode()` methods.
5518 *
5519 * Any two instances of *value objects* can be compared for value equality with
5520 * `Immutable.is()` and can be used as keys in a `Map` or members in a `Set`.
5521 */
5522 function isValueObject(maybeValue: unknown): maybeValue is ValueObject;
5523
5524 /**
5525 * True if `maybeSeq` is a Seq.
5526 */
5527 function isSeq(
5528 maybeSeq: unknown
5529 ): maybeSeq is
5530 | Seq.Indexed<unknown>
5531 | Seq.Keyed<unknown, unknown>
5532 | Seq.Set<unknown>;
5533
5534 /**
5535 * True if `maybeList` is a List.
5536 */
5537 function isList(maybeList: unknown): maybeList is List<unknown>;
5538
5539 /**
5540 * True if `maybeMap` is a Map.
5541 *
5542 * Also true for OrderedMaps.
5543 */
5544 function isMap(maybeMap: unknown): maybeMap is Map<unknown, unknown>;
5545
5546 /**
5547 * True if `maybeOrderedMap` is an OrderedMap.
5548 */
5549 function isOrderedMap(
5550 maybeOrderedMap: unknown
5551 ): maybeOrderedMap is OrderedMap<unknown, unknown>;
5552
5553 /**
5554 * True if `maybeStack` is a Stack.
5555 */
5556 function isStack(maybeStack: unknown): maybeStack is Stack<unknown>;
5557
5558 /**
5559 * True if `maybeSet` is a Set.
5560 *
5561 * Also true for OrderedSets.
5562 */
5563 function isSet(maybeSet: unknown): maybeSet is Set<unknown>;
5564
5565 /**
5566 * True if `maybeOrderedSet` is an OrderedSet.
5567 */
5568 function isOrderedSet(
5569 maybeOrderedSet: unknown
5570 ): maybeOrderedSet is OrderedSet<unknown>;
5571
5572 /**
5573 * True if `maybeRecord` is a Record.
5574 */
5575 function isRecord(maybeRecord: unknown): maybeRecord is Record<{}>;
5576
5577 /**
5578 * Returns the value within the provided collection associated with the
5579 * provided key, or notSetValue if the key is not defined in the collection.
5580 *
5581 * A functional alternative to `collection.get(key)` which will also work on
5582 * plain Objects and Arrays as an alternative for `collection[key]`.
5583 *
5584 * <!-- runkit:activate -->
5585 * ```js
5586 * const { get } = require('immutable')
5587 * get([ 'dog', 'frog', 'cat' ], 2) // 'frog'
5588 * get({ x: 123, y: 456 }, 'x') // 123
5589 * get({ x: 123, y: 456 }, 'z', 'ifNotSet') // 'ifNotSet'
5590 * ```
5591 */
5592 function get<K, V>(collection: Collection<K, V>, key: K): V | undefined;
5593 function get<K, V, NSV>(
5594 collection: Collection<K, V>,
5595 key: K,
5596 notSetValue: NSV
5597 ): V | NSV;
5598 function get<TProps extends object, K extends keyof TProps>(
5599 record: Record<TProps>,
5600 key: K,
5601 notSetValue: unknown
5602 ): TProps[K];
5603 function get<V>(collection: Array<V>, key: number): V | undefined;
5604 function get<V, NSV>(
5605 collection: Array<V>,
5606 key: number,
5607 notSetValue: NSV
5608 ): V | NSV;
5609 function get<C extends object, K extends keyof C>(
5610 object: C,
5611 key: K,
5612 notSetValue: unknown
5613 ): C[K];
5614 function get<V>(collection: { [key: string]: V }, key: string): V | undefined;
5615 function get<V, NSV>(
5616 collection: { [key: string]: V },
5617 key: string,
5618 notSetValue: NSV
5619 ): V | NSV;
5620
5621 /**
5622 * Returns true if the key is defined in the provided collection.
5623 *
5624 * A functional alternative to `collection.has(key)` which will also work with
5625 * plain Objects and Arrays as an alternative for
5626 * `collection.hasOwnProperty(key)`.
5627 *
5628 * <!-- runkit:activate -->
5629 * ```js
5630 * const { has } = require('immutable')
5631 * has([ 'dog', 'frog', 'cat' ], 2) // true
5632 * has([ 'dog', 'frog', 'cat' ], 5) // false
5633 * has({ x: 123, y: 456 }, 'x') // true
5634 * has({ x: 123, y: 456 }, 'z') // false
5635 * ```
5636 */
5637 function has(collection: object, key: unknown): boolean;
5638
5639 /**
5640 * Returns a copy of the collection with the value at key removed.
5641 *
5642 * A functional alternative to `collection.remove(key)` which will also work
5643 * with plain Objects and Arrays as an alternative for
5644 * `delete collectionCopy[key]`.
5645 *
5646 * <!-- runkit:activate -->
5647 * ```js
5648 * const { remove } = require('immutable')
5649 * const originalArray = [ 'dog', 'frog', 'cat' ]
5650 * remove(originalArray, 1) // [ 'dog', 'cat' ]
5651 * console.log(originalArray) // [ 'dog', 'frog', 'cat' ]
5652 * const originalObject = { x: 123, y: 456 }
5653 * remove(originalObject, 'x') // { y: 456 }
5654 * console.log(originalObject) // { x: 123, y: 456 }
5655 * ```
5656 */
5657 function remove<K, C extends Collection<K, unknown>>(
5658 collection: C,
5659 key: K
5660 ): C;
5661 function remove<
5662 TProps extends object,
5663 C extends Record<TProps>,
5664 K extends keyof TProps
5665 >(collection: C, key: K): C;
5666 function remove<C extends Array<unknown>>(collection: C, key: number): C;
5667 function remove<C, K extends keyof C>(collection: C, key: K): C;
5668 function remove<C extends { [key: string]: unknown }, K extends keyof C>(
5669 collection: C,
5670 key: K
5671 ): C;
5672
5673 /**
5674 * Returns a copy of the collection with the value at key set to the provided
5675 * value.
5676 *
5677 * A functional alternative to `collection.set(key, value)` which will also
5678 * work with plain Objects and Arrays as an alternative for
5679 * `collectionCopy[key] = value`.
5680 *
5681 * <!-- runkit:activate -->
5682 * ```js
5683 * const { set } = require('immutable')
5684 * const originalArray = [ 'dog', 'frog', 'cat' ]
5685 * set(originalArray, 1, 'cow') // [ 'dog', 'cow', 'cat' ]
5686 * console.log(originalArray) // [ 'dog', 'frog', 'cat' ]
5687 * const originalObject = { x: 123, y: 456 }
5688 * set(originalObject, 'x', 789) // { x: 789, y: 456 }
5689 * console.log(originalObject) // { x: 123, y: 456 }
5690 * ```
5691 */
5692 function set<K, V, C extends Collection<K, V>>(
5693 collection: C,
5694 key: K,
5695 value: V
5696 ): C;
5697 function set<
5698 TProps extends object,
5699 C extends Record<TProps>,
5700 K extends keyof TProps
5701 >(record: C, key: K, value: TProps[K]): C;
5702 function set<V, C extends Array<V>>(collection: C, key: number, value: V): C;
5703 function set<C, K extends keyof C>(object: C, key: K, value: C[K]): C;
5704 function set<V, C extends { [key: string]: V }>(
5705 collection: C,
5706 key: string,
5707 value: V
5708 ): C;
5709
5710 /**
5711 * Returns a copy of the collection with the value at key set to the result of
5712 * providing the existing value to the updating function.
5713 *
5714 * A functional alternative to `collection.update(key, fn)` which will also
5715 * work with plain Objects and Arrays as an alternative for
5716 * `collectionCopy[key] = fn(collection[key])`.
5717 *
5718 * <!-- runkit:activate -->
5719 * ```js
5720 * const { update } = require('immutable')
5721 * const originalArray = [ 'dog', 'frog', 'cat' ]
5722 * update(originalArray, 1, val => val.toUpperCase()) // [ 'dog', 'FROG', 'cat' ]
5723 * console.log(originalArray) // [ 'dog', 'frog', 'cat' ]
5724 * const originalObject = { x: 123, y: 456 }
5725 * update(originalObject, 'x', val => val * 6) // { x: 738, y: 456 }
5726 * console.log(originalObject) // { x: 123, y: 456 }
5727 * ```
5728 */
5729 function update<K, V, C extends Collection<K, V>>(
5730 collection: C,
5731 key: K,
5732 updater: (value: V | undefined) => V | undefined
5733 ): C;
5734 function update<K, V, C extends Collection<K, V>, NSV>(
5735 collection: C,
5736 key: K,
5737 notSetValue: NSV,
5738 updater: (value: V | NSV) => V
5739 ): C;
5740 function update<
5741 TProps extends object,
5742 C extends Record<TProps>,
5743 K extends keyof TProps
5744 >(record: C, key: K, updater: (value: TProps[K]) => TProps[K]): C;
5745 function update<
5746 TProps extends object,
5747 C extends Record<TProps>,
5748 K extends keyof TProps,
5749 NSV
5750 >(
5751 record: C,
5752 key: K,
5753 notSetValue: NSV,
5754 updater: (value: TProps[K] | NSV) => TProps[K]
5755 ): C;
5756 function update<V>(
5757 collection: Array<V>,
5758 key: number,
5759 updater: (value: V | undefined) => V | undefined
5760 ): Array<V>;
5761 function update<V, NSV>(
5762 collection: Array<V>,
5763 key: number,
5764 notSetValue: NSV,
5765 updater: (value: V | NSV) => V
5766 ): Array<V>;
5767 function update<C, K extends keyof C>(
5768 object: C,
5769 key: K,
5770 updater: (value: C[K]) => C[K]
5771 ): C;
5772 function update<C, K extends keyof C, NSV>(
5773 object: C,
5774 key: K,
5775 notSetValue: NSV,
5776 updater: (value: C[K] | NSV) => C[K]
5777 ): C;
5778 function update<V, C extends { [key: string]: V }, K extends keyof C>(
5779 collection: C,
5780 key: K,
5781 updater: (value: V) => V
5782 ): { [key: string]: V };
5783 function update<V, C extends { [key: string]: V }, K extends keyof C, NSV>(
5784 collection: C,
5785 key: K,
5786 notSetValue: NSV,
5787 updater: (value: V | NSV) => V
5788 ): { [key: string]: V };
5789
5790 /**
5791 * Returns the value at the provided key path starting at the provided
5792 * collection, or notSetValue if the key path is not defined.
5793 *
5794 * A functional alternative to `collection.getIn(keypath)` which will also
5795 * work with plain Objects and Arrays.
5796 *
5797 * <!-- runkit:activate -->
5798 * ```js
5799 * const { getIn } = require('immutable')
5800 * getIn({ x: { y: { z: 123 }}}, ['x', 'y', 'z']) // 123
5801 * getIn({ x: { y: { z: 123 }}}, ['x', 'q', 'p'], 'ifNotSet') // 'ifNotSet'
5802 * ```
5803 */
5804 function getIn(
5805 collection: unknown,
5806 keyPath: Iterable<unknown>,
5807 notSetValue?: unknown
5808 ): unknown;
5809
5810 /**
5811 * Returns true if the key path is defined in the provided collection.
5812 *
5813 * A functional alternative to `collection.hasIn(keypath)` which will also
5814 * work with plain Objects and Arrays.
5815 *
5816 * <!-- runkit:activate -->
5817 * ```js
5818 * const { hasIn } = require('immutable')
5819 * hasIn({ x: { y: { z: 123 }}}, ['x', 'y', 'z']) // true
5820 * hasIn({ x: { y: { z: 123 }}}, ['x', 'q', 'p']) // false
5821 * ```
5822 */
5823 function hasIn(collection: unknown, keyPath: Iterable<unknown>): boolean;
5824
5825 /**
5826 * Returns a copy of the collection with the value at the key path removed.
5827 *
5828 * A functional alternative to `collection.removeIn(keypath)` which will also
5829 * work with plain Objects and Arrays.
5830 *
5831 * <!-- runkit:activate -->
5832 * ```js
5833 * const { removeIn } = require('immutable')
5834 * const original = { x: { y: { z: 123 }}}
5835 * removeIn(original, ['x', 'y', 'z']) // { x: { y: {}}}
5836 * console.log(original) // { x: { y: { z: 123 }}}
5837 * ```
5838 */
5839 function removeIn<C>(collection: C, keyPath: Iterable<unknown>): C;
5840
5841 /**
5842 * Returns a copy of the collection with the value at the key path set to the
5843 * provided value.
5844 *
5845 * A functional alternative to `collection.setIn(keypath)` which will also
5846 * work with plain Objects and Arrays.
5847 *
5848 * <!-- runkit:activate -->
5849 * ```js
5850 * const { setIn } = require('immutable')
5851 * const original = { x: { y: { z: 123 }}}
5852 * setIn(original, ['x', 'y', 'z'], 456) // { x: { y: { z: 456 }}}
5853 * console.log(original) // { x: { y: { z: 123 }}}
5854 * ```
5855 */
5856 function setIn<C>(
5857 collection: C,
5858 keyPath: Iterable<unknown>,
5859 value: unknown
5860 ): C;
5861
5862 /**
5863 * Returns a copy of the collection with the value at key path set to the
5864 * result of providing the existing value to the updating function.
5865 *
5866 * A functional alternative to `collection.updateIn(keypath)` which will also
5867 * work with plain Objects and Arrays.
5868 *
5869 * <!-- runkit:activate -->
5870 * ```js
5871 * const { updateIn } = require('immutable')
5872 * const original = { x: { y: { z: 123 }}}
5873 * updateIn(original, ['x', 'y', 'z'], val => val * 6) // { x: { y: { z: 738 }}}
5874 * console.log(original) // { x: { y: { z: 123 }}}
5875 * ```
5876 */
5877 function updateIn<C>(
5878 collection: C,
5879 keyPath: Iterable<unknown>,
5880 updater: (value: unknown) => unknown
5881 ): C;
5882 function updateIn<C>(
5883 collection: C,
5884 keyPath: Iterable<unknown>,
5885 notSetValue: unknown,
5886 updater: (value: unknown) => unknown
5887 ): C;
5888
5889 /**
5890 * Returns a copy of the collection with the remaining collections merged in.
5891 *
5892 * A functional alternative to `collection.merge()` which will also work with
5893 * plain Objects and Arrays.
5894 *
5895 * <!-- runkit:activate -->
5896 * ```js
5897 * const { merge } = require('immutable')
5898 * const original = { x: 123, y: 456 }
5899 * merge(original, { y: 789, z: 'abc' }) // { x: 123, y: 789, z: 'abc' }
5900 * console.log(original) // { x: 123, y: 456 }
5901 * ```
5902 */
5903 function merge<C>(
5904 collection: C,
5905 ...collections: Array<
5906 | Iterable<unknown>
5907 | Iterable<[unknown, unknown]>
5908 | { [key: string]: unknown }
5909 >
5910 ): C;
5911
5912 /**
5913 * Returns a copy of the collection with the remaining collections merged in,
5914 * calling the `merger` function whenever an existing value is encountered.
5915 *
5916 * A functional alternative to `collection.mergeWith()` which will also work
5917 * with plain Objects and Arrays.
5918 *
5919 * <!-- runkit:activate -->
5920 * ```js
5921 * const { mergeWith } = require('immutable')
5922 * const original = { x: 123, y: 456 }
5923 * mergeWith(
5924 * (oldVal, newVal) => oldVal + newVal,
5925 * original,
5926 * { y: 789, z: 'abc' }
5927 * ) // { x: 123, y: 1245, z: 'abc' }
5928 * console.log(original) // { x: 123, y: 456 }
5929 * ```
5930 */
5931 function mergeWith<C>(
5932 merger: (oldVal: unknown, newVal: unknown, key: unknown) => unknown,
5933 collection: C,
5934 ...collections: Array<
5935 | Iterable<unknown>
5936 | Iterable<[unknown, unknown]>
5937 | { [key: string]: unknown }
5938 >
5939 ): C;
5940
5941 /**
5942 * Like `merge()`, but when two compatible collections are encountered with
5943 * the same key, it merges them as well, recursing deeply through the nested
5944 * data. Two collections are considered to be compatible (and thus will be
5945 * merged together) if they both fall into one of three categories: keyed
5946 * (e.g., `Map`s, `Record`s, and objects), indexed (e.g., `List`s and
5947 * arrays), or set-like (e.g., `Set`s). If they fall into separate
5948 * categories, `mergeDeep` will replace the existing collection with the
5949 * collection being merged in. This behavior can be customized by using
5950 * `mergeDeepWith()`.
5951 *
5952 * Note: Indexed and set-like collections are merged using
5953 * `concat()`/`union()` and therefore do not recurse.
5954 *
5955 * A functional alternative to `collection.mergeDeep()` which will also work
5956 * with plain Objects and Arrays.
5957 *
5958 * <!-- runkit:activate -->
5959 * ```js
5960 * const { mergeDeep } = require('immutable')
5961 * const original = { x: { y: 123 }}
5962 * mergeDeep(original, { x: { z: 456 }}) // { x: { y: 123, z: 456 }}
5963 * console.log(original) // { x: { y: 123 }}
5964 * ```
5965 */
5966 function mergeDeep<C>(
5967 collection: C,
5968 ...collections: Array<
5969 | Iterable<unknown>
5970 | Iterable<[unknown, unknown]>
5971 | { [key: string]: unknown }
5972 >
5973 ): C;
5974
5975 /**
5976 * Like `mergeDeep()`, but when two non-collections or incompatible
5977 * collections are encountered at the same key, it uses the `merger` function
5978 * to determine the resulting value. Collections are considered incompatible
5979 * if they fall into separate categories between keyed, indexed, and set-like.
5980 *
5981 * A functional alternative to `collection.mergeDeepWith()` which will also
5982 * work with plain Objects and Arrays.
5983 *
5984 * <!-- runkit:activate -->
5985 * ```js
5986 * const { mergeDeepWith } = require('immutable')
5987 * const original = { x: { y: 123 }}
5988 * mergeDeepWith(
5989 * (oldVal, newVal) => oldVal + newVal,
5990 * original,
5991 * { x: { y: 456 }}
5992 * ) // { x: { y: 579 }}
5993 * console.log(original) // { x: { y: 123 }}
5994 * ```
5995 */
5996 function mergeDeepWith<C>(
5997 merger: (oldVal: unknown, newVal: unknown, key: unknown) => unknown,
5998 collection: C,
5999 ...collections: Array<
6000 | Iterable<unknown>
6001 | Iterable<[unknown, unknown]>
6002 | { [key: string]: unknown }
6003 >
6004 ): C;
6005}
6006
6007/**
6008 * Defines the main export of the immutable module to be the Immutable namespace
6009 * This supports many common module import patterns:
6010 *
6011 * const Immutable = require("immutable");
6012 * const { List } = require("immutable");
6013 * import Immutable from "immutable";
6014 * import * as Immutable from "immutable";
6015 * import { List } from "immutable";
6016 *
6017 */
6018export = Immutable;
6019
6020/**
6021 * A global "Immutable" namespace used by UMD modules which allows the use of
6022 * the full Immutable API.
6023 *
6024 * If using Immutable as an imported module, prefer using:
6025 *
6026 * import Immutable from 'immutable'
6027 *
6028 */
6029export as namespace Immutable;
6030
\No newline at end of file