forked from immutable-js/immutable-js
/
Immutable.d.ts
1651 lines (1448 loc) · 51.1 KB
/
Immutable.d.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
* Copyright (c) 2014, Facebook, Inc.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*/
/**
* Immutable Data
* ==============
*
* Immutable data encourages pure functions (data-in, data-out) and lends itself
* to much simpler application development and enabling techniques from
* functional programming such as lazy evaluation.
*
* While designed to bring these powerful functional concepts to JavaScript, it
* presents an Object-Oriented API familiar to Javascript engineers and closely
* mirroring that of Array, Map, and Set. It is easy and efficient to convert to
* and from plain Javascript types.
*/
declare module 'immutable' {
/**
* `Immutable.is()` has the same semantics as Object.is(), but treats immutable
* sequences as data, equal if the second immutable sequences contains
* equivalent data. It's used throughout when checking for equality.
*
* var map1 = Immutable.Map({a:1, b:1, c:1});
* var map2 = Immutable.Map({a:1, b:1, c:1});
* assert(map1 !== map2);
* assert(Object.is(map1, map2) === false);
* assert(Immutable.is(map1, map2) === true);
*
*/
export function is(first: any, second: any): boolean;
/**
* `Immutable.fromJS()` deeply converts plain JS objects and arrays to
* Immutable sequences.
*
* If a `converter` is optionally provided, it will be called with every
* sequence (beginning with the most nested sequences and proceeding to the
* original sequence itself), along with the key refering to this Sequence
* and the parent JS object provided as `this`. For the top level, object,
* the key will be "". This `converter` is expected to return a new Sequence,
* allowing for custom convertions from deep JS objects.
*
* This example converts JSON to Vector and OrderedMap:
*
* Immutable.fromJS({a: {b: [10, 20, 30]}, c: 40}, function (value, key) {
* var isIndexed = value instanceof IndexedSequence;
* console.log(isIndexed, key, this);
* return isIndexed ? value.toVector() : value.toOrderedMap();
* });
*
* // true, "b", {b: [10, 20, 30]}
* // false, "a", {a: {b: [10, 20, 30]}, c: 40}
* // false, "", {"": {a: {b: [10, 20, 30]}, c: 40}}
*
* If `converter` is not provided, the default behavior will convert Arrays into
* Vectors and Objects into Maps.
*
* Note: `converter` acts similarly to [`reviver`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/parse#Example.3A_Using_the_reviver_parameter)
* in `JSON.parse`.
*/
export function fromJS(
json: any,
converter?: (k: any, v: Sequence<any, any>) => any
): any;
/**
* Sequence
* --------
*
* The `Sequence` is a set of (key, value) entries which can be iterated, and
* is the base class for all collections in `immutable`, allowing them to
* make use of all the Sequence methods (such as `map` and `filter`).
*
* **Sequences are immutable** — Once a sequence is created, it cannot be
* changed, appended to, rearranged or otherwise modified. Instead, any mutative
* method called on a sequence will return a new immutable sequence.
*
* **Sequences are lazy** — Sequences do as little work as necessary to respond
* to any method call.
*
* For example, the following does no work, because the resulting sequence is
* never used:
*
* var oddSquares = Immutable.Sequence(1,2,3,4,5,6,7,8)
* .filter(x => x % 2).map(x => x * x);
*
* Once the sequence is used, it performs only the work necessary. In this
* example, no intermediate arrays are ever created, filter is only called
* twice, and map is only called once:
*
* console.log(evenSquares.last()); // 49
*
* Lazy Sequences allow for the efficient chaining of sequence operations,
* allowing for the expression of logic that can otherwise be very tedious:
*
* Immutable.Sequence({a:1, b:1, c:1})
* .flip().map(key => key.toUpperCase()).flip().toObject();
* // Map { A: 1, B: 1, C: 1 }
*
* As well as expressing logic that would otherwise seem memory-limited:
*
* Immutable.Range(1, Infinity)
* .skip(1000)
* .map(n => -n)
* .filter(n => n % 2 === 0)
* .take(2)
* .reduce((r, n) => r * n, 1);
* // 1006008
*
* Note: A sequence is always iterated in the same order, however that order may
* not always be well defined, as is the case for the `Map`.
*/
/**
* `Immutable.Sequence()` returns a sequence of its parameters.
*
* * If provided a single argument:
* * If a Sequence, that same Sequence is returned.
* * If an Array, an `IndexedSequence` is returned.
* * If a plain Object, a `Sequence` is returned, iterated in the same order
* as the for-in would iterate through the Object itself.
* * An `IndexedSequence` of all arguments is returned.
*
* Note: if a Sequence is created from a JavaScript Array or Object, then it can
* still possibly mutated if the underlying Array or Object is ever mutated.
*/
export function Sequence<T>(seq: IndexedSequence<T>): IndexedSequence<T>;
export function Sequence<T>(array: Array<T>): IndexedSequence<T>;
export function Sequence<K, V>(seq: Sequence<K, V>): Sequence<K, V>;
export function Sequence<V>(obj: {[key: string]: V}): Sequence<string, V>;
export function Sequence<T>(...values: T[]): IndexedSequence<T>;
export function Sequence(): Sequence<any, any>;
/**
* Like `Immutable.Sequence()`, `Immutable.Sequence.from()` returns a sequence,
* but always expects a single argument.
*/
export module Sequence {
function from<T>(seq: IndexedSequence<T>): IndexedSequence<T>;
function from<T>(array: Array<T>): IndexedSequence<T>;
function from<K, V>(seq: Sequence<K, V>): Sequence<K, V>;
function from<V>(obj: {[key: string]: V}): Sequence<string, V>;
}
export interface Sequence<K, V> {
/**
* Some sequences can describe their length lazily. When this is the case,
* length will be an integer. Otherwise it will be undefined.
*
* For example, the new Sequences returned from map() or reverse()
* preserve the length of the original sequence while filter() does not.
*
* Note: All original collections will have a length, including Maps, Vectors,
* Sets, Ranges, Repeats and Sequences made from Arrays and Objects.
*/
length: number;
/**
* Regardless of if this sequence can describe its length lazily, this method
* will always return the correct length. E.g. it evaluates the full sequence
* if necessary.
*
* If `predicate` is provided, then this returns the count of entries in the
* sequence for which the `predicate` returns true.
*/
count(): number;
count(
predicate: (value?: V, key?: K, seq?: Sequence<K, V>) => boolean,
thisArg?: any
): number;
/**
* Deeply converts this sequence to a string.
*/
toString(): string;
/**
* Deeply converts this sequence to equivalent JS.
*
* IndexedSequences, Vectors, Ranges, Repeats and Sets become Arrays, while
* other Sequences become Objects.
*/
toJS(): any;
/**
* Converts this sequence to an Array, discarding keys.
*/
toArray(): Array<V>;
/**
* Converts this sequence to an Object. Throws if keys are not strings.
*/
toObject(): Object;
/**
* Converts this sequence to a Vector, discarding keys.
*
* Note: This is equivalent to `Vector.from(this)`, but provided to allow for
* chained expressions.
*/
toVector(): Vector<V>;
/**
* Converts this sequence to a Map, Throws if keys are not hashable.
*
* Note: This is equivalent to `Map.from(this)`, but provided to allow for
* chained expressions.
*/
toMap(): Map<K, V>;
/**
* Converts this sequence to a Map, maintaining the order of iteration.
*
* Note: This is equivalent to `OrderedMap.from(this)`, but provided to allow
* for chained expressions.
*/
toOrderedMap(): Map<K, V>;
/**
* Converts this sequence to a Set, discarding keys. Throws if values
* are not hashable.
*
* Note: This is equivalent to `Set.from(this)`, but provided to allow for
* chained expressions.
*/
toSet(): Set<V>;
/**
* True if this and the other sequence have value equality, as defined
* by `Immutable.is()`.
*
* Note: This is equivalent to `Immutable.is(this, other)`, but provided to
* allow for chained expressions.
*/
equals(other: Sequence<K, V>): boolean;
/**
* Joins values together as a string, inserting a separator between each.
* The default separator is ",".
*/
join(separator?: string): string;
/**
* Returns a new sequence with other values and sequences concatenated to
* this one. All entries will be present in the resulting sequence, even if
* they have the same key.
*/
concat(...valuesOrSequences: any[]): Sequence<any, any>;
/**
* Returns a new sequence which iterates in reverse order of this sequence.
*/
reverse(): Sequence<K, V>;
/**
* Returns a new indexed sequence of the keys of this sequence,
* discarding values.
*/
keySeq(): IndexedSequence<K>;
/**
* Returns a new indexed sequence of the keys of this sequence,
* discarding keys.
*/
valueSeq(): IndexedSequence<V>;
/**
* Returns a new indexed sequence of [key, value] tuples.
*/
entrySeq(): IndexedSequence</*(K, V)*/Array<any>>;
/**
* The `sideEffect` is executed for every entry in the sequence.
*
* Unlike `Array.prototype.forEach`, if any call of `sideEffect` returns
* `false`, the iteration will stop. Returns the length of the sequence which
* was iterated.
*/
forEach(
sideEffect: (value?: V, key?: K, seq?: Sequence<K, V>) => any,
thisArg?: any
): number;
/**
* Reduces the sequence to a value by calling the `reducer` for every entry
* in the sequence and passing along the reduced value.
*/
reduce<R>(
reducer: (reduction?: R, value?: V, key?: K, seq?: Sequence<K, V>) => R,
initialReduction?: R,
thisArg?: any
): R;
/**
* Reduces the sequence in reverse (from the right side).
*
* Note: Equivalent to this.reverse().reduce(), but provided for parity
* with `Array.prototype.reduceRight`.
*/
reduceRight<R>(
reducer: (reduction?: R, value?: V, key?: K, seq?: Sequence<K, V>) => R,
initialReduction: R,
thisArg?: any
): R;
/**
* True if `predicate` returns true for all entries in the sequence.
*/
every(
predicate: (value?: V, key?: K, seq?: Sequence<K, V>) => boolean,
thisArg?: any
): boolean;
/**
* True if `predicate` returns true for any entry in the sequence.
*/
some(
predicate: (value?: V, key?: K, seq?: Sequence<K, V>) => boolean,
thisArg?: any
): boolean;
/**
* The first value in the sequence.
*/
first(): V;
/**
* The last value in the sequence.
*/
last(): V;
/**
* Returns a new Sequence containing all entries except the first.
*/
rest(): Sequence<K, V>
/**
* Returns a new Sequence containing all entries except the last.
*/
butLast(): Sequence<K, V>
/**
* True if a key exists within this Sequence.
*/
has(key: K): boolean;
/**
* Returns the value associated with the provided key, or notSetValue if
* the Sequence does not contain this key.
*
* Note: it is possible a key may be associated with an `undefined` value, so
* if `notSetValue` is not provided and this method returns `undefined`,
* that does not guarantee the key was not found.
*/
get(key: K, notSetValue?: V): V;
/**
* Returns the value found by following a key path through nested sequences.
*/
getIn(searchKeyPath: Array<K>, notSetValue?: V): V;
/**
* True if a value exists within this Sequence.
*/
contains(value: V): boolean;
/**
* Returns the value for which the `predicate` returns true.
*/
find(
predicate: (value?: V, key?: K, seq?: Sequence<K, V>) => boolean,
thisArg?: any,
notSetValue?: V
): V;
/**
* Returns the key for which the `predicate` returns true.
*/
findKey(
predicate: (value?: V, key?: K, seq?: Sequence<K, V>) => boolean,
thisArg?: any
): K;
/**
* Returns the last value for which the `predicate` returns true.
*
* Note: `predicate` will be called for each entry in reverse.
*/
findLast(
predicate: (value?: V, key?: K, seq?: Sequence<K, V>) => boolean,
thisArg?: any,
notSetValue?: V
): V;
/**
* Returns the last key for which the `predicate` returns true.
*
* Note: `predicate` will be called for each entry in reverse.
*/
findLastKey(
predicate: (value?: V, key?: K, seq?: Sequence<K, V>) => boolean,
thisArg?: any
): K;
/**
* Returns a new sequence with this sequences's keys as it's values, and this
* sequences's values as it's keys.
*
* Sequence({ a: 'z', b: 'y' }).flip() // { z: 'a', y: 'b' }
*
*/
flip(): Sequence<V, K>;
/**
* Returns a new sequence with values passed through a `mapper` function.
*
* Sequence({ a: 1, b: 2 }).map(x => 10 * x) // { a: 10, b: 20 }
*
*/
map<M>(
mapper: (value?: V, key?: K, seq?: Sequence<K, V>) => M,
thisArg?: any
): Sequence<K, M>;
/**
* Returns a new sequence with keys passed through a `mapper` function.
*
* Sequence({ a: 1, b: 2 }).mapKeys(x => x.toUpperCase()) // { A: 1, B: 2 }
*
*/
mapKeys<M>(
mapper: (key?: K, value?: V, seq?: Sequence<K, V>) => M,
thisArg?: any
): Sequence<M, V>;
/**
* Returns a new sequence with only the entries for which the `predicate`
* function returns true.
*
* Sequence({a:1,b:2,c:3,d:4}).filter(x => x % 2 === 0) // { b: 2, d: 4 }
*
*/
filter(
predicate: (value?: V, key?: K, seq?: Sequence<K, V>) => boolean,
thisArg?: any
): Sequence<K, V>;
/**
* Returns a new sequence representing a portion of this sequence from start
* up to but not including end.
*
* If begin is negative, it is offset from the end of the sequence. e.g.
* `slice(-2)` returns a sequence of the last two entries. If it is not
* provided the new sequence will begin at the beginning of this sequence.
*
* If end is negative, it is offset from the end of the sequence. e.g.
* `slice(0, -1)` returns a sequence of everything but the last entry. If it
* is not provided, the new sequence will continue through the end of
* this sequence.
*
* If the requested slice is equivalent to the current Sequence, then it will
* return itself.
*
* Note: unlike `Array.prototype.slice`, this function is O(1) and copies
* no data. The resulting sequence is also lazy, and a copy is only made when
* it is converted such as via `toArray()` or `toVector()`.
*/
slice(begin?: number, end?: number): Sequence<K, V>;
/**
* Returns a new sequence which contains the first `amount` entries from
* this sequence.
*/
take(amount: number): Sequence<K, V>;
/**
* Returns a new sequence which contains the last `amount` entries from
* this sequence.
*/
takeLast(amount: number): Sequence<K, V>;
/**
* Returns a new sequence which contains entries from this sequence as long
* as the `predicate` returns true.
*
* Sequence('dog','frog','cat','hat','god').takeWhile(x => x.match(/o/))
* // ['dog', 'frog']
*
*/
takeWhile(
predicate: (value?: V, key?: K, seq?: Sequence<K, V>) => boolean,
thisArg?: any
): Sequence<K, V>;
/**
* Returns a new sequence which contains entries from this sequence as long
* as the `predicate` returns false.
*
* Sequence('dog','frog','cat','hat','god').takeUntil(x => x.match(/at/))
* // ['dog', 'frog']
*
*/
takeUntil(
predicate: (value?: V, key?: K, seq?: Sequence<K, V>) => boolean,
thisArg?: any
): Sequence<K, V>;
/**
* Returns a new sequence which excludes the first `amount` entries from
* this sequence.
*/
skip(amount: number): Sequence<K, V>;
/**
* Returns a new sequence which excludes the last `amount` entries from
* this sequence.
*/
skipLast(amount: number): Sequence<K, V>;
/**
* Returns a new sequence which contains entries starting from when
* `predicate` first returns false.
*
* Sequence('dog','frog','cat','hat','god').skipWhile(x => x.match(/g/))
* // ['cat', 'hat', 'god']
*
*/
skipWhile(
predicate: (value?: V, key?: K, seq?: Sequence<K, V>) => boolean,
thisArg?: any
): Sequence<K, V>;
/**
* Returns a new sequence which contains entries starting from when
* `predicate` first returns true.
*
* Sequence('dog','frog','cat','hat','god').skipUntil(x => x.match(/hat/))
* // ['hat', 'god']
*
*/
skipUntil(
predicate: (value?: V, key?: K, seq?: Sequence<K, V>) => boolean,
thisArg?: any
): Sequence<K, V>;
/**
* Returns a `Map` of counts, grouped by the return value of the
* `grouper` function.
*
* Note: Because this returns a Map, this method is not lazy.
*/
countBy<G>(
grouper: (value?: V, key?: K, seq?: Sequence<K, V>) => G,
thisArg?: any
): Map<G, number>;
/**
* Returns a `Map` of sequences, grouped by the return value of the
* `grouper` function.
*
* Note: Because this returns a Map, this method is not lazy.
*/
groupBy<G>(
grouper: (value?: V, key?: K, seq?: Sequence<K, V>) => G,
thisArg?: any
): Map<G, Sequence<K, V>>;
sort(comparator?: (valueA: V, valueB: V) => number): Sequence<K, V>;
sortBy<S>(
sortValueMapper: (value?: V, key?: K, seq?: Sequence<K, V>) => S,
comparator?: (valueA: S, valueB: S) => number
): Sequence<K, V>;
/**
* Because Sequences are lazy and designed to be chained together, they do
* not cache their results. For example, this map function is called 6 times:
*
* var squares = Sequence(1,2,3).map(x => x * x);
* squares.join() + squares.join();
*
* If you know a derived sequence will be used multiple times, it may be more
* efficient to first cache it. Here, map is called 3 times:
*
* var squares = Sequence(1,2,3).map(x => x * x).cacheResult();
* squares.join() + squares.join();
*
* Use this method judiciously, as it must fully evaluate a lazy Sequence.
*
* Note: after calling `cacheResult()`, a Sequence will always have a length.
*/
cacheResult(): Sequence<K, V>;
}
/**
* Indexed Sequence
* ----------------
*
* Indexed Sequences have incrementing numeric keys. They exhibit
* slightly different behavior than `Sequence` for some methods in order to
* better mirror the behavior of JavaScript's `Array`, and add others which do
* not make sense on non-indexed sequences such as `indexOf`.
*
* Like JavaScript arrays, `IndexedSequence`s may be sparse, skipping over some
* indices and may have a length larger than the highest index.
*/
export interface IndexedSequence<T> extends Sequence<number, T> {
/**
* If this is a sequence of entries (key-value tuples), it will return a
* sequence of those entries.
*/
fromEntrySeq(): Sequence<any, any>;
/**
* Returns the first index at which a given value can be found in the
* sequence, or -1 if it is not present.
*/
indexOf(searchValue: T): number;
/**
* Returns the last index at which a given value can be found in the
* sequence, or -1 if it is not present.
*/
lastIndexOf(searchValue: T): number;
/**
* Returns the first index in the sequence where a value satisfies the
* provided predicate function. Otherwise -1 is returned.
*/
findIndex(
predicate: (value?: T, index?: number, seq?: IndexedSequence<T>) => boolean,
thisArg?: any
): number;
/**
* Returns the last index in the sequence where a value satisfies the
* provided predicate function. Otherwise -1 is returned.
*/
findLastIndex(
predicate: (value?: T, index?: number, seq?: IndexedSequence<T>) => boolean,
thisArg?: any
): number;
/**
* Splice returns a new indexed sequence by replacing a region of this sequence
* with new values. If values are not provided, it only skips the region to
* be removed.
*
* Sequence(['a','b','c','d']).splice(1, 2, 'q', 'r', 's')
* // ['a', 'q', 'r', 's', 'd']
*
*/
splice(index: number, removeNum: number, ...values: any[]): IndexedSequence<T>;
/**
* When IndexedSequence is converted to an array, the index keys are
* maintained. This differs from the behavior of Sequence which
* simply makes a dense array of all values.
* @override
*/
toArray(): Array<T>;
/**
* This has the same altered behavior as `toArray`.
* @override
*/
toVector(): Vector<T>;
/**
* This new behavior will iterate through the values and sequences with
* increasing indices.
* @override
*/
concat(...valuesOrSequences: any[]): IndexedSequence<any>;
/**
* This new behavior will not only iterate through the sequence in reverse,
* but it will also reverse the indices so the last value will report being
* at index 0. If you wish to preserve the original indices, set
* maintainIndices to true.
* @override
*/
reverse(maintainIndices?: boolean): IndexedSequence<T>;
/**
* Indexed sequences have a different `filter` behavior, where the filtered
* values have new indicies incrementing from 0. If you want to preserve the
* original indicies, set maintainIndices to true.
* @override
*/
filter(
predicate: (value?: T, index?: number, seq?: IndexedSequence<T>) => boolean,
thisArg?: any,
maintainIndices?: boolean
): IndexedSequence<T>;
/**
* Adds the ability to maintain original indices.
* @override
*/
slice(start: number, end?: number, maintainIndices?: boolean): IndexedSequence<T>;
/**
* Has the same altered behavior as `takeWhile`.
* @override
*/
take(amount: number, maintainIndices?: boolean): IndexedSequence<T>;
/**
* Has the same altered behavior as `takeWhile`.
* @override
*/
takeLast(amount: number, maintainIndices?: boolean): IndexedSequence<T>;
/**
* Indexed sequences have a different `takeWhile` behavior. The first
* value will have an index of 0 and the length of the sequence could be
* truncated. If you want to preserve the original indicies, set
* maintainIndices to true.
* @override
*/
takeWhile(
predicate: (value?: T, index?: number, seq?: IndexedSequence<T>) => boolean,
thisArg?: any,
maintainIndices?: boolean
): IndexedSequence<T>;
/**
* Has the same altered behavior as `takeWhile`.
* @override
*/
takeUntil(
predicate: (value?: T, index?: number, seq?: IndexedSequence<T>) => boolean,
thisArg?: any,
maintainIndices?: boolean
): IndexedSequence<T>;
/**
* Has the same altered behavior as `skipWhile`.
* @override
*/
skip(amount: number, maintainIndices?: boolean): IndexedSequence<T>;
/**
* Has the same altered behavior as `skipWhile`.
* @override
*/
skipLast(amount: number, maintainIndices?: boolean): IndexedSequence<T>;
/**
* Indexed sequences have a different `skipWhile` behavior. The first
* non-skipped value will have an index of 0. If you want to preserve the
* original indicies, set maintainIndices to true.
* @override
*/
skipWhile(
predicate: (value?: T, index?: number, seq?: IndexedSequence<T>) => boolean,
thisArg?: any,
maintainIndices?: boolean
): IndexedSequence<T>;
/**
* Has the same altered behavior as `skipWhile`.
* @override
*/
skipUntil(
predicate: (value?: T, index?: number, seq?: IndexedSequence<T>) => boolean,
thisArg?: any,
maintainIndices?: boolean
): IndexedSequence<T>;
/**
* Indexed sequences have a different `groupBy` behavior. Each group will be
* a new indexed sequence starting with an index of 0. If you want to preserve
* the original indicies, set maintainIndices to true.
* @override
*/
groupBy<G>(
grouper: (value?: T, index?: number, seq?: IndexedSequence<T>) => G,
thisArg?: any,
maintainIndices?: boolean
): Map<G, any/*IndexedSequence<T>*/>; // Bug: exposing this causes the type checker to implode.
sort(
comparator?: (valueA: T, valueB: T) => number,
maintainIndices?: boolean
): IndexedSequence<T>;
sortBy<S>(
sortValueMapper: (value?: T, index?: number, seq?: IndexedSequence<T>) => S,
comparator?: (valueA: S, valueB: S) => number,
maintainIndices?: boolean
): IndexedSequence<T>;
/**
* Returns an IndexedSequence
* @override
*/
map<M>(
mapper: (value?: T, index?: number, seq?: IndexedSequence<T>) => M,
thisArg?: any
): IndexedSequence<M>;
/**
* Returns an IndexedSequence
* @override
*/
cacheResult(): IndexedSequence<T>;
}
/**
* Range
* -----
*
* Returns a lazy indexed sequence of numbers from `start` (inclusive) to `end`
* (exclusive), by `step`, where `start` defaults to 0, `step` to 1, and `end` to
* infinity. When `start` is equal to `end`, returns empty range.
*
* Range() // [0,1,2,3,...]
* Range(10) // [10,11,12,13,...]
* Range(10,15) // [10,11,12,13,14]
* Range(10,30,5) // [10,15,20,25]
* Range(30,10,5) // [30,25,20,15]
* Range(30,30,5) // []
*
*/
export function Range(start?: number, end?: number, step?: number): IndexedSequence<number>;
/**
* Repeat
* ------
*
* Returns a lazy sequence of `value` repeated `times` times. When `times` is
* not defined, returns an infinite sequence of `value`.
*
* Repeat('foo') // ['foo','foo','foo',...]
* Repeat('bar',4) // ['bar','bar','bar','bar']
*
*/
export function Repeat<T>(value: T, times?: number): IndexedSequence<T>;
/**
* Map
* ---
*
* A Map is a Sequence of (key, value) pairs with `O(log32 N)` gets and sets.
*
* Map is a hash map and requires keys that are hashable, either a primitive
* (string or number) or an object with a `hashCode(): number` method.
*
* Iteration order of a Map is undefined, however is stable. Multiple iterations
* of the same Map will iterate in the same order.
*/
export module Map {
/**
* `Map.empty()` creates a new immutable map of length 0.
*/
function empty<K, V>(): Map<K, V>;
/**
* `Map.from()` creates a new immutable Map with the same key value pairs as
* the provided Sequence or JavaScript Object or Array.
*
* var newMap = Map.from({key: "value"});
* var newMap = Map.from([["key", "value"]]);
*
*/
function from<K, V>(sequence: Sequence<K, V>): Map<K, V>;
function from<V>(object: {[key: string]: V}): Map<string, V>;
function from<V>(entries: Array</*(K, V)*/Array<any>>): Map<any, any>;
}
/**
* Alias for `Map.empty()`.
*/
export function Map<K, V>(): Map<K, V>;
/**
* Alias for `Map.from()`.
*/
export function Map<K, V>(sequence: Sequence<K, V>): Map<K, V>;
export function Map<V>(object: {[key: string]: V}): Map<string, V>;
export function Map<V>(entries: Array</*(K, V)*/Array<any>>): Map<any, any>;
export interface Map<K, V> extends Sequence<K, V> {
/**
* Returns a new Map also containing the new key, value pair. If an equivalent
* key already exists in this Map, it will be replaced.
*/
set(key: K, value: V): Map<K, V>;
/**
* Returns a new Map which excludes this `key`.
*
* Note: `delete` cannot be safely used in IE8
* @alias delete
*/
remove(key: K): Map<K, V>;
delete(key: K): Map<K, V>;
/**
* Returns a new Map containing no keys or values.
*/
clear(): Map<K, V>;
/**
* An iterator of this Map's keys.
*/
keys(): Iterator<K>;
/**
* An iterator of this Map's values.
*/
values(): Iterator<V>;
/**
* An iterator of this Map's entries as [key, value] tuples.
*/
entries(): Iterator</*[K, V]*/Array<any>>;
/**
* When this cursor's (or any of its sub-cursors') `update` method is called,
* the resulting new data structure will be provided to the `onChange`
* function. Use this callback to keep track of the most current value or
* update the rest of your application.
*/
cursor(
onChange?: (newValue: Map<K, V>, oldValue?: Map<K, V>, keyPath?: Array<any>) => void
): Cursor<Map<K, V>>;
cursor(
keyPath: Array<any>,
onChange?: (newValue: Map<K, V>, oldValue?: Map<K, V>, keyPath?: Array<any>) => void
): Cursor<any>;
cursor(
key: K,
onChange?: (newValue: Map<K, V>, oldValue?: Map<K, V>, keyPath?: Array<any>) => void
): Cursor<V>;
/**
* Returns a new Map having updated the value at this `key` with the return
* value of calling `updater` with the existing value, or `notSetValue` if
* the key was not set. If called with only a single argument, `updater` is
* called with the Map itself.
*
* Equivalent to: `map.set(key, updater(map.get(key, notSetValue)))`.
*/
update(updater: (value: Map<K, V>) => Map<K, V>): Map<K, V>;
update(key: K, updater: (value: V) => V): Map<K, V>;
update(key: K, notSetValue: V, updater: (value: V) => V): Map<K, V>;
/**
* Returns a new Map having applied the `updater` to the entry found at the
* keyPath. If any keys in `keyPath` do not exist, a new immutable Map will
* be created at that key. If the `keyPath` was not previously set,
* `updater` is called with `notSetValue` (if provided).
*
* var data = Immutable.fromJS({ a: { b: { c: 10 } } });
* data.updateIn(['a', 'b'], map => map.set('d', 20));
* // { a: { b: { c: 10, d: 20 } } }
*
*/
updateIn(
keyPath: Array<any>,
updater: (value: any) => any
): Map<K, V>;
updateIn(
keyPath: Array<any>,
notSetValue: any,
updater: (value: any) => any
): Map<K, V>;
/**
* Returns a new Map resulting from merging the provided Sequences
* (or JS objects) into this Map. In other words, this takes each entry of
* each sequence and sets it on this Map.
*