immutable 4.3.7 → 5.0.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -86,7 +86,7 @@ map1.get('b') + ' vs. ' + map2.get('b'); // 2 vs. 50
86
86
 
87
87
  Immutable.js has no dependencies, which makes it predictable to include in a Browser.
88
88
 
89
- It's highly recommended to use a module bundler like [webpack](https://webpack.github.io/),
89
+ It's highly recommended to use a module bundler like [webpack](https://webpack.js.org/),
90
90
  [rollup](https://rollupjs.org/), or
91
91
  [browserify](https://browserify.org/). The `immutable` npm module works
92
92
  without any additional consideration. All examples throughout the documentation
@@ -152,9 +152,9 @@ via relative path to the type definitions at the top of your file.
152
152
 
153
153
  ```js
154
154
  ///<reference path='./node_modules/immutable/dist/immutable.d.ts'/>
155
- import Immutable from 'immutable';
156
- var map1: Immutable.Map<string, number>;
157
- map1 = Immutable.Map({ a: 1, b: 2, c: 3 });
155
+ import { Map } from 'immutable';
156
+ var map1: Map<string, number>;
157
+ map1 = Map({ a: 1, b: 2, c: 3 });
158
158
  var map2 = map1.set('b', 50);
159
159
  map1.get('b'); // 2
160
160
  map2.get('b'); // 50
@@ -1,3 +1,5 @@
1
+ /** @ignore we should disable this rules, but let's activate it to enable eslint first */
2
+ /* eslint-disable @typescript-eslint/no-explicit-any, @typescript-eslint/ban-types */
1
3
  /**
2
4
  * Immutable data encourages pure functions (data-in, data-out) and lends itself
3
5
  * to much simpler application development and enabling techniques from
@@ -112,6 +114,11 @@ declare namespace Immutable {
112
114
  {
113
115
  [key in keyof R]: ContainObject<R[key]> extends true ? unknown : R[key];
114
116
  }
117
+ : T extends MapOf<infer R>
118
+ ? // convert MapOf to DeepCopy plain JS object
119
+ {
120
+ [key in keyof R]: ContainObject<R[key]> extends true ? unknown : R[key];
121
+ }
115
122
  : T extends Collection.Keyed<infer KeyedKey, infer V>
116
123
  ? // convert KeyedCollection to DeepCopy plain JS object
117
124
  {
@@ -120,6 +127,7 @@ declare namespace Immutable {
120
127
  : string]: V extends object ? unknown : V;
121
128
  }
122
129
  : // convert IndexedCollection or Immutable.Set to DeepCopy plain JS array
130
+ // eslint-disable-next-line @typescript-eslint/no-unused-vars
123
131
  T extends Collection<infer _, infer V>
124
132
  ? Array<DeepCopy<V>>
125
133
  : T extends string | number // Iterable scalar types : should be kept as is
@@ -784,24 +792,6 @@ declare namespace Immutable {
784
792
  * ```
785
793
  */
786
794
  function isMap(maybeMap: unknown): maybeMap is Map<unknown, unknown>;
787
-
788
- /**
789
- * Creates a new Map from alternating keys and values
790
- *
791
- * <!-- runkit:activate -->
792
- * ```js
793
- * const { Map } = require('immutable')
794
- * Map.of(
795
- * 'key', 'value',
796
- * 'numerical value', 3,
797
- * 0, 'numerical key'
798
- * )
799
- * // Map { 0: "numerical key", "key": "value", "numerical value": 3 }
800
- * ```
801
- *
802
- * @deprecated Use Map([ [ 'k', 'v' ] ]) or Map({ k: 'v' })
803
- */
804
- function of(...keyValues: Array<unknown>): Map<unknown, unknown>;
805
795
  }
806
796
 
807
797
  /**
@@ -841,9 +831,98 @@ declare namespace Immutable {
841
831
  * not altered.
842
832
  */
843
833
  function Map<K, V>(collection?: Iterable<[K, V]>): Map<K, V>;
834
+ function Map<R extends { [key in string | number | symbol]: unknown }>(
835
+ obj: R
836
+ ): MapOf<R>;
844
837
  function Map<V>(obj: { [key: string]: V }): Map<string, V>;
845
838
  function Map<K extends string | symbol, V>(obj: { [P in K]?: V }): Map<K, V>;
846
839
 
840
+ /**
841
+ * Represent a Map constructed by an object
842
+ *
843
+ * @ignore
844
+ */
845
+ interface MapOf<R extends { [key in string | number | symbol]: unknown }>
846
+ extends Map<keyof R, R[keyof R]> {
847
+ /**
848
+ * Returns the value associated with the provided key, or notSetValue if
849
+ * the Collection does not contain this key.
850
+ *
851
+ * Note: it is possible a key may be associated with an `undefined` value,
852
+ * so if `notSetValue` is not provided and this method returns `undefined`,
853
+ * that does not guarantee the key was not found.
854
+ */
855
+ get<K extends keyof R>(key: K, notSetValue?: unknown): R[K];
856
+ get<NSV>(key: any, notSetValue: NSV): NSV;
857
+
858
+ // TODO `<const P extends ...>` can be used after dropping support for TypeScript 4.x
859
+ // reference: https://www.typescriptlang.org/docs/handbook/release-notes/typescript-5-0.html#const-type-parameters
860
+ // after this change, `as const` assertions can be remove from the type tests
861
+ getIn<P extends ReadonlyArray<string | number | symbol>>(
862
+ searchKeyPath: [...P],
863
+ notSetValue?: unknown
864
+ ): RetrievePath<R, P>;
865
+
866
+ set<K extends keyof R>(key: K, value: R[K]): this;
867
+
868
+ update(updater: (value: this) => this): this;
869
+ update<K extends keyof R>(key: K, updater: (value: R[K]) => R[K]): this;
870
+ update<K extends keyof R, NSV extends R[K]>(
871
+ key: K,
872
+ notSetValue: NSV,
873
+ updater: (value: R[K]) => R[K]
874
+ ): this;
875
+
876
+ // Possible best type is MapOf<Omit<R, K>> but Omit seems to broke other function calls
877
+ // and generate recursion error with other methods (update, merge, etc.) until those functions are defined in MapOf
878
+ delete<K extends keyof R>(
879
+ key: K
880
+ ): Extract<R[K], undefined> extends never ? never : this;
881
+ remove<K extends keyof R>(
882
+ key: K
883
+ ): Extract<R[K], undefined> extends never ? never : this;
884
+
885
+ toJS(): { [K in keyof R]: DeepCopy<R[K]> };
886
+
887
+ toJSON(): { [K in keyof R]: R[K] };
888
+ }
889
+
890
+ // Loosely based off of this work.
891
+ // https://github.com/immutable-js/immutable-js/issues/1462#issuecomment-584123268
892
+
893
+ /** @ignore */
894
+ type GetMapType<S> = S extends MapOf<infer T> ? T : S;
895
+
896
+ /** @ignore */
897
+ type Head<T extends ReadonlyArray<any>> = T extends [
898
+ infer H,
899
+ ...Array<unknown>
900
+ ]
901
+ ? H
902
+ : never;
903
+
904
+ /** @ignore */
905
+ type Tail<T extends ReadonlyArray<any>> = T extends [unknown, ...infer I]
906
+ ? I
907
+ : Array<never>;
908
+
909
+ /** @ignore */
910
+ type RetrievePathReducer<
911
+ T,
912
+ C,
913
+ L extends ReadonlyArray<any>
914
+ > = C extends keyof GetMapType<T>
915
+ ? L extends []
916
+ ? GetMapType<T>[C]
917
+ : RetrievePathReducer<GetMapType<T>[C], Head<L>, Tail<L>>
918
+ : never;
919
+
920
+ /** @ignore */
921
+ type RetrievePath<
922
+ R,
923
+ P extends ReadonlyArray<string | number | symbol>
924
+ > = P extends [] ? P : RetrievePathReducer<R, Head<P>, Tail<P>>;
925
+
847
926
  interface Map<K, V> extends Collection.Keyed<K, V> {
848
927
  /**
849
928
  * The number of entries in this Map.
@@ -1061,16 +1140,17 @@ declare namespace Immutable {
1061
1140
  */
1062
1141
  merge<KC, VC>(
1063
1142
  ...collections: Array<Iterable<[KC, VC]>>
1064
- ): Map<K | KC, V | VC>;
1143
+ ): Map<K | KC, Exclude<V, VC> | VC>;
1065
1144
  merge<C>(
1066
1145
  ...collections: Array<{ [key: string]: C }>
1067
- ): Map<K | string, V | C>;
1146
+ ): Map<K | string, Exclude<V, C> | C>;
1147
+
1068
1148
  concat<KC, VC>(
1069
1149
  ...collections: Array<Iterable<[KC, VC]>>
1070
- ): Map<K | KC, V | VC>;
1150
+ ): Map<K | KC, Exclude<V, VC> | VC>;
1071
1151
  concat<C>(
1072
1152
  ...collections: Array<{ [key: string]: C }>
1073
- ): Map<K | string, V | C>;
1153
+ ): Map<K | string, Exclude<V, C> | C>;
1074
1154
 
1075
1155
  /**
1076
1156
  * Like `merge()`, `mergeWith()` returns a new Map resulting from merging
@@ -1090,10 +1170,14 @@ declare namespace Immutable {
1090
1170
  *
1091
1171
  * Note: `mergeWith` can be used in `withMutations`.
1092
1172
  */
1093
- mergeWith(
1094
- merger: (oldVal: V, newVal: V, key: K) => V,
1095
- ...collections: Array<Iterable<[K, V]> | { [key: string]: V }>
1096
- ): this;
1173
+ mergeWith<KC, VC, VCC>(
1174
+ merger: (oldVal: V, newVal: VC, key: K) => VCC,
1175
+ ...collections: Array<Iterable<[KC, VC]>>
1176
+ ): Map<K | KC, V | VC | VCC>;
1177
+ mergeWith<C, CC>(
1178
+ merger: (oldVal: V, newVal: C, key: string) => CC,
1179
+ ...collections: Array<{ [key: string]: C }>
1180
+ ): Map<K | string, V | C | CC>;
1097
1181
 
1098
1182
  /**
1099
1183
  * Like `merge()`, but when two compatible collections are encountered with
@@ -1124,9 +1208,12 @@ declare namespace Immutable {
1124
1208
  *
1125
1209
  * Note: `mergeDeep` can be used in `withMutations`.
1126
1210
  */
1127
- mergeDeep(
1128
- ...collections: Array<Iterable<[K, V]> | { [key: string]: V }>
1129
- ): this;
1211
+ mergeDeep<KC, VC>(
1212
+ ...collections: Array<Iterable<[KC, VC]>>
1213
+ ): Map<K | KC, V | VC>;
1214
+ mergeDeep<C>(
1215
+ ...collections: Array<{ [key: string]: C }>
1216
+ ): Map<K | string, V | C>;
1130
1217
 
1131
1218
  /**
1132
1219
  * Like `mergeDeep()`, but when two non-collections or incompatible
@@ -1504,6 +1591,65 @@ declare namespace Immutable {
1504
1591
  * @see Collection.Keyed.flip
1505
1592
  */
1506
1593
  flip(): Map<V, K>;
1594
+
1595
+ /**
1596
+ * Returns an OrderedMap of the same type which includes the same entries,
1597
+ * stably sorted by using a `comparator`.
1598
+ *
1599
+ * If a `comparator` is not provided, a default comparator uses `<` and `>`.
1600
+ *
1601
+ * `comparator(valueA, valueB)`:
1602
+ *
1603
+ * * Returns `0` if the elements should not be swapped.
1604
+ * * Returns `-1` (or any negative number) if `valueA` comes before `valueB`
1605
+ * * Returns `1` (or any positive number) if `valueA` comes after `valueB`
1606
+ * * Alternatively, can return a value of the `PairSorting` enum type
1607
+ * * Is pure, i.e. it must always return the same value for the same pair
1608
+ * of values.
1609
+ *
1610
+ * <!-- runkit:activate -->
1611
+ * ```js
1612
+ * const { Map } = require('immutable')
1613
+ * Map({ "c": 3, "a": 1, "b": 2 }).sort((a, b) => {
1614
+ * if (a < b) { return -1; }
1615
+ * if (a > b) { return 1; }
1616
+ * if (a === b) { return 0; }
1617
+ * });
1618
+ * // OrderedMap { "a": 1, "b": 2, "c": 3 }
1619
+ * ```
1620
+ *
1621
+ * Note: `sort()` Always returns a new instance, even if the original was
1622
+ * already sorted.
1623
+ *
1624
+ * Note: This is always an eager operation.
1625
+ */
1626
+ sort(comparator?: Comparator<V>): this & OrderedMap<K, V>;
1627
+
1628
+ /**
1629
+ * Like `sort`, but also accepts a `comparatorValueMapper` which allows for
1630
+ * sorting by more sophisticated means:
1631
+ *
1632
+ * <!-- runkit:activate -->
1633
+ * ```js
1634
+ * const { Map } = require('immutable')
1635
+ * const beattles = Map({
1636
+ * John: { name: "Lennon" },
1637
+ * Paul: { name: "McCartney" },
1638
+ * George: { name: "Harrison" },
1639
+ * Ringo: { name: "Starr" },
1640
+ * });
1641
+ * beattles.sortBy(member => member.name);
1642
+ * ```
1643
+ *
1644
+ * Note: `sortBy()` Always returns a new instance, even if the original was
1645
+ * already sorted.
1646
+ *
1647
+ * Note: This is always an eager operation.
1648
+ */
1649
+ sortBy<C>(
1650
+ comparatorValueMapper: (value: V, key: K, iter: this) => C,
1651
+ comparator?: (valueA: C, valueB: C) => number
1652
+ ): this & OrderedMap<K, V>;
1507
1653
  }
1508
1654
 
1509
1655
  /**
@@ -1594,15 +1740,32 @@ declare namespace Immutable {
1594
1740
  */
1595
1741
  merge<KC, VC>(
1596
1742
  ...collections: Array<Iterable<[KC, VC]>>
1597
- ): OrderedMap<K | KC, V | VC>;
1743
+ ): OrderedMap<K | KC, Exclude<V, VC> | VC>;
1598
1744
  merge<C>(
1599
1745
  ...collections: Array<{ [key: string]: C }>
1600
- ): OrderedMap<K | string, V | C>;
1746
+ ): OrderedMap<K | string, Exclude<V, C> | C>;
1747
+
1601
1748
  concat<KC, VC>(
1602
1749
  ...collections: Array<Iterable<[KC, VC]>>
1603
- ): OrderedMap<K | KC, V | VC>;
1750
+ ): OrderedMap<K | KC, Exclude<V, VC> | VC>;
1604
1751
  concat<C>(
1605
1752
  ...collections: Array<{ [key: string]: C }>
1753
+ ): OrderedMap<K | string, Exclude<V, C> | C>;
1754
+
1755
+ mergeWith<KC, VC, VCC>(
1756
+ merger: (oldVal: V, newVal: VC, key: K) => VCC,
1757
+ ...collections: Array<Iterable<[KC, VC]>>
1758
+ ): OrderedMap<K | KC, V | VC | VCC>;
1759
+ mergeWith<C, CC>(
1760
+ merger: (oldVal: V, newVal: C, key: string) => CC,
1761
+ ...collections: Array<{ [key: string]: C }>
1762
+ ): OrderedMap<K | string, V | C | CC>;
1763
+
1764
+ mergeDeep<KC, VC>(
1765
+ ...collections: Array<Iterable<[KC, VC]>>
1766
+ ): OrderedMap<K | KC, V | VC>;
1767
+ mergeDeep<C>(
1768
+ ...collections: Array<{ [key: string]: C }>
1606
1769
  ): OrderedMap<K | string, V | C>;
1607
1770
 
1608
1771
  // Sequence algorithms
@@ -1714,7 +1877,6 @@ declare namespace Immutable {
1714
1877
  * this Collection or JavaScript Object.
1715
1878
  */
1716
1879
  function fromKeys<T>(iter: Collection.Keyed<T, unknown>): Set<T>;
1717
- // tslint:disable-next-line unified-signatures
1718
1880
  function fromKeys<T>(iter: Collection<T, unknown>): Set<T>;
1719
1881
  function fromKeys(obj: { [key: string]: unknown }): Set<string>;
1720
1882
 
@@ -1909,6 +2071,65 @@ declare namespace Immutable {
1909
2071
  predicate: (this: C, value: T, key: T, iter: this) => unknown,
1910
2072
  context?: C
1911
2073
  ): [this, this];
2074
+
2075
+ /**
2076
+ * Returns an OrderedSet of the same type which includes the same entries,
2077
+ * stably sorted by using a `comparator`.
2078
+ *
2079
+ * If a `comparator` is not provided, a default comparator uses `<` and `>`.
2080
+ *
2081
+ * `comparator(valueA, valueB)`:
2082
+ *
2083
+ * * Returns `0` if the elements should not be swapped.
2084
+ * * Returns `-1` (or any negative number) if `valueA` comes before `valueB`
2085
+ * * Returns `1` (or any positive number) if `valueA` comes after `valueB`
2086
+ * * Alternatively, can return a value of the `PairSorting` enum type
2087
+ * * Is pure, i.e. it must always return the same value for the same pair
2088
+ * of values.
2089
+ *
2090
+ * <!-- runkit:activate -->
2091
+ * ```js
2092
+ * const { Set } = require('immutable')
2093
+ * Set(['b', 'a', 'c']).sort((a, b) => {
2094
+ * if (a < b) { return -1; }
2095
+ * if (a > b) { return 1; }
2096
+ * if (a === b) { return 0; }
2097
+ * });
2098
+ * // OrderedSet { "a":, "b", "c" }
2099
+ * ```
2100
+ *
2101
+ * Note: `sort()` Always returns a new instance, even if the original was
2102
+ * already sorted.
2103
+ *
2104
+ * Note: This is always an eager operation.
2105
+ */
2106
+ sort(comparator?: Comparator<T>): this & OrderedSet<T>;
2107
+
2108
+ /**
2109
+ * Like `sort`, but also accepts a `comparatorValueMapper` which allows for
2110
+ * sorting by more sophisticated means:
2111
+ *
2112
+ * <!-- runkit:activate -->
2113
+ * ```js
2114
+ * const { Set } = require('immutable')
2115
+ * const beattles = Set([
2116
+ * { name: "Lennon" },
2117
+ * { name: "McCartney" },
2118
+ * { name: "Harrison" },
2119
+ * { name: "Starr" },
2120
+ * ]);
2121
+ * beattles.sortBy(member => member.name);
2122
+ * ```
2123
+ *
2124
+ * Note: `sortBy()` Always returns a new instance, even if the original was
2125
+ * already sorted.
2126
+ *
2127
+ * Note: This is always an eager operation.
2128
+ */
2129
+ sortBy<C>(
2130
+ comparatorValueMapper: (value: T, key: T, iter: this) => C,
2131
+ comparator?: (valueA: C, valueB: C) => number
2132
+ ): this & OrderedSet<T>;
1912
2133
  }
1913
2134
 
1914
2135
  /**
@@ -1939,7 +2160,6 @@ declare namespace Immutable {
1939
2160
  * the keys from this Collection or JavaScript Object.
1940
2161
  */
1941
2162
  function fromKeys<T>(iter: Collection.Keyed<T, unknown>): OrderedSet<T>;
1942
- // tslint:disable-next-line unified-signatures
1943
2163
  function fromKeys<T>(iter: Collection<T, unknown>): OrderedSet<T>;
1944
2164
  function fromKeys(obj: { [key: string]: unknown }): OrderedSet<string>;
1945
2165
  }
@@ -2362,8 +2582,8 @@ declare namespace Immutable {
2362
2582
  * ```
2363
2583
  */
2364
2584
  function Range(
2365
- start?: number,
2366
- end?: number,
2585
+ start: number,
2586
+ end: number,
2367
2587
  step?: number
2368
2588
  ): Seq.Indexed<number>;
2369
2589
 
@@ -3472,34 +3692,6 @@ declare namespace Immutable {
3472
3692
  * `Collection.Indexed`, or `Collection.Set`.
3473
3693
  */
3474
3694
  namespace Collection {
3475
- /**
3476
- * @deprecated use `const { isKeyed } = require('immutable')`
3477
- */
3478
- function isKeyed(
3479
- maybeKeyed: unknown
3480
- ): maybeKeyed is Collection.Keyed<unknown, unknown>;
3481
-
3482
- /**
3483
- * @deprecated use `const { isIndexed } = require('immutable')`
3484
- */
3485
- function isIndexed(
3486
- maybeIndexed: unknown
3487
- ): maybeIndexed is Collection.Indexed<unknown>;
3488
-
3489
- /**
3490
- * @deprecated use `const { isAssociative } = require('immutable')`
3491
- */
3492
- function isAssociative(
3493
- maybeAssociative: unknown
3494
- ): maybeAssociative is
3495
- | Collection.Keyed<unknown, unknown>
3496
- | Collection.Indexed<unknown>;
3497
-
3498
- /**
3499
- * @deprecated use `const { isOrdered } = require('immutable')`
3500
- */
3501
- function isOrdered(maybeOrdered: unknown): boolean;
3502
-
3503
3695
  /**
3504
3696
  * Keyed Collections have discrete keys tied to each value.
3505
3697
  *
@@ -4208,7 +4400,8 @@ declare namespace Immutable {
4208
4400
  * In case the `Collection` is empty returns the optional default
4209
4401
  * value if provided, if no default value is provided returns undefined.
4210
4402
  */
4211
- first<NSV = undefined>(notSetValue?: NSV): V | NSV;
4403
+ first<NSV>(notSetValue: NSV): V | NSV;
4404
+ first(): V | undefined;
4212
4405
 
4213
4406
  /**
4214
4407
  * In case the `Collection` is not empty returns the last element of the
@@ -4216,7 +4409,8 @@ declare namespace Immutable {
4216
4409
  * In case the `Collection` is empty returns the optional default
4217
4410
  * value if provided, if no default value is provided returns undefined.
4218
4411
  */
4219
- last<NSV = undefined>(notSetValue?: NSV): V | NSV;
4412
+ last<NSV>(notSetValue: NSV): V | NSV;
4413
+ last(): V | undefined;
4220
4414
 
4221
4415
  // Reading deep values
4222
4416
 
@@ -4808,7 +5002,6 @@ declare namespace Immutable {
4808
5002
  * returns Collection<K, V>
4809
5003
  */
4810
5004
  flatten(depth?: number): Collection<unknown, unknown>;
4811
- // tslint:disable-next-line unified-signatures
4812
5005
  flatten(shallow?: boolean): Collection<unknown, unknown>;
4813
5006
 
4814
5007
  /**