[d24f17c] | 1 | /**
|
---|
| 2 | * Copyright (c) 2014-present, Facebook, Inc.
|
---|
| 3 | *
|
---|
| 4 | * This source code is licensed under the MIT license found in the
|
---|
| 5 | * LICENSE file in the root directory of this source tree.
|
---|
| 6 | */
|
---|
| 7 |
|
---|
| 8 | /**
|
---|
| 9 | * Immutable data encourages pure functions (data-in, data-out) and lends itself
|
---|
| 10 | * to much simpler application development and enabling techniques from
|
---|
| 11 | * functional programming such as lazy evaluation.
|
---|
| 12 | *
|
---|
| 13 | * While designed to bring these powerful functional concepts to JavaScript, it
|
---|
| 14 | * presents an Object-Oriented API familiar to Javascript engineers and closely
|
---|
| 15 | * mirroring that of Array, Map, and Set. It is easy and efficient to convert to
|
---|
| 16 | * and from plain Javascript types.
|
---|
| 17 |
|
---|
| 18 | * Note: all examples are presented in [ES6][]. To run in all browsers, they
|
---|
| 19 | * need to be translated to ES3. For example:
|
---|
| 20 | *
|
---|
| 21 | * // ES6
|
---|
| 22 | * foo.map(x => x * x);
|
---|
| 23 | * // ES3
|
---|
| 24 | * foo.map(function (x) { return x * x; });
|
---|
| 25 | *
|
---|
| 26 | * [ES6]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/New_in_JavaScript/ECMAScript_6_support_in_Mozilla
|
---|
| 27 | */
|
---|
| 28 |
|
---|
| 29 |
|
---|
| 30 |
|
---|
| 31 | /**
|
---|
| 32 | * Deeply converts plain JS objects and arrays to Immutable Maps and Lists.
|
---|
| 33 | *
|
---|
| 34 | * If a `reviver` is optionally provided, it will be called with every
|
---|
| 35 | * collection as a Seq (beginning with the most nested collections
|
---|
| 36 | * and proceeding to the top-level collection itself), along with the key
|
---|
| 37 | * refering to each collection and the parent JS object provided as `this`.
|
---|
| 38 | * For the top level, object, the key will be `""`. This `reviver` is expected
|
---|
| 39 | * to return a new Immutable Iterable, allowing for custom conversions from
|
---|
| 40 | * deep JS objects.
|
---|
| 41 | *
|
---|
| 42 | * This example converts JSON to List and OrderedMap:
|
---|
| 43 | *
|
---|
| 44 | * Immutable.fromJS({a: {b: [10, 20, 30]}, c: 40}, function (key, value) {
|
---|
| 45 | * var isIndexed = Immutable.Iterable.isIndexed(value);
|
---|
| 46 | * return isIndexed ? value.toList() : value.toOrderedMap();
|
---|
| 47 | * });
|
---|
| 48 | *
|
---|
| 49 | * // true, "b", {b: [10, 20, 30]}
|
---|
| 50 | * // false, "a", {a: {b: [10, 20, 30]}, c: 40}
|
---|
| 51 | * // false, "", {"": {a: {b: [10, 20, 30]}, c: 40}}
|
---|
| 52 | *
|
---|
| 53 | * If `reviver` is not provided, the default behavior will convert Arrays into
|
---|
| 54 | * Lists and Objects into Maps.
|
---|
| 55 | *
|
---|
| 56 | * `reviver` acts similarly to the [same parameter in `JSON.parse`][1].
|
---|
| 57 | *
|
---|
| 58 | * `Immutable.fromJS` is conservative in its conversion. It will only convert
|
---|
| 59 | * arrays which pass `Array.isArray` to Lists, and only raw objects (no custom
|
---|
| 60 | * prototype) to Map.
|
---|
| 61 | *
|
---|
| 62 | * Keep in mind, when using JS objects to construct Immutable Maps, that
|
---|
| 63 | * JavaScript Object properties are always strings, even if written in a
|
---|
| 64 | * quote-less shorthand, while Immutable Maps accept keys of any type.
|
---|
| 65 | *
|
---|
| 66 | * ```js
|
---|
| 67 | * var obj = { 1: "one" };
|
---|
| 68 | * Object.keys(obj); // [ "1" ]
|
---|
| 69 | * obj["1"]; // "one"
|
---|
| 70 | * obj[1]; // "one"
|
---|
| 71 | *
|
---|
| 72 | * var map = Map(obj);
|
---|
| 73 | * map.get("1"); // "one"
|
---|
| 74 | * map.get(1); // undefined
|
---|
| 75 | * ```
|
---|
| 76 | *
|
---|
| 77 | * Property access for JavaScript Objects first converts the key to a string,
|
---|
| 78 | * but since Immutable Map keys can be of any type the argument to `get()` is
|
---|
| 79 | * not altered.
|
---|
| 80 | *
|
---|
| 81 | * [1]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/parse#Example.3A_Using_the_reviver_parameter
|
---|
| 82 | * "Using the reviver parameter"
|
---|
| 83 | */
|
---|
| 84 | export function fromJS(
|
---|
| 85 | json: any,
|
---|
| 86 | reviver?: (k: any, v: Iterable<any, any>) => any
|
---|
| 87 | ): any;
|
---|
| 88 |
|
---|
| 89 |
|
---|
| 90 | /**
|
---|
| 91 | * Value equality check with semantics similar to `Object.is`, but treats
|
---|
| 92 | * Immutable `Iterable`s as values, equal if the second `Iterable` includes
|
---|
| 93 | * equivalent values.
|
---|
| 94 | *
|
---|
| 95 | * It's used throughout Immutable when checking for equality, including `Map`
|
---|
| 96 | * key equality and `Set` membership.
|
---|
| 97 | *
|
---|
| 98 | * var map1 = Immutable.Map({a:1, b:1, c:1});
|
---|
| 99 | * var map2 = Immutable.Map({a:1, b:1, c:1});
|
---|
| 100 | * assert(map1 !== map2);
|
---|
| 101 | * assert(Object.is(map1, map2) === false);
|
---|
| 102 | * assert(Immutable.is(map1, map2) === true);
|
---|
| 103 | *
|
---|
| 104 | * Note: Unlike `Object.is`, `Immutable.is` assumes `0` and `-0` are the same
|
---|
| 105 | * value, matching the behavior of ES6 Map key equality.
|
---|
| 106 | */
|
---|
| 107 | export function is(first: any, second: any): boolean;
|
---|
| 108 |
|
---|
| 109 |
|
---|
| 110 | /**
|
---|
| 111 | * Lists are ordered indexed dense collections, much like a JavaScript
|
---|
| 112 | * Array.
|
---|
| 113 | *
|
---|
| 114 | * Lists are immutable and fully persistent with O(log32 N) gets and sets,
|
---|
| 115 | * and O(1) push and pop.
|
---|
| 116 | *
|
---|
| 117 | * Lists implement Deque, with efficient addition and removal from both the
|
---|
| 118 | * end (`push`, `pop`) and beginning (`unshift`, `shift`).
|
---|
| 119 | *
|
---|
| 120 | * Unlike a JavaScript Array, there is no distinction between an
|
---|
| 121 | * "unset" index and an index set to `undefined`. `List#forEach` visits all
|
---|
| 122 | * indices from 0 to size, regardless of whether they were explicitly defined.
|
---|
| 123 | */
|
---|
| 124 | export module List {
|
---|
| 125 |
|
---|
| 126 | /**
|
---|
| 127 | * True if the provided value is a List
|
---|
| 128 | */
|
---|
| 129 | function isList(maybeList: any): boolean;
|
---|
| 130 |
|
---|
| 131 | /**
|
---|
| 132 | * Creates a new List containing `values`.
|
---|
| 133 | */
|
---|
| 134 | function of<T>(...values: T[]): List<T>;
|
---|
| 135 | }
|
---|
| 136 |
|
---|
| 137 | /**
|
---|
| 138 | * Create a new immutable List containing the values of the provided
|
---|
| 139 | * iterable-like.
|
---|
| 140 | */
|
---|
| 141 | export function List<T>(): List<T>;
|
---|
| 142 | export function List<T>(iter: Iterable.Indexed<T>): List<T>;
|
---|
| 143 | export function List<T>(iter: Iterable.Set<T>): List<T>;
|
---|
| 144 | export function List<K, V>(iter: Iterable.Keyed<K, V>): List</*[K,V]*/any>;
|
---|
| 145 | export function List<T>(array: Array<T>): List<T>;
|
---|
| 146 | export function List<T>(iterator: Iterator<T>): List<T>;
|
---|
| 147 | export function List<T>(iterable: /*Iterable<T>*/Object): List<T>;
|
---|
| 148 |
|
---|
| 149 |
|
---|
| 150 | export interface List<T> extends Collection.Indexed<T> {
|
---|
| 151 |
|
---|
| 152 | // Persistent changes
|
---|
| 153 |
|
---|
| 154 | /**
|
---|
| 155 | * Returns a new List which includes `value` at `index`. If `index` already
|
---|
| 156 | * exists in this List, it will be replaced.
|
---|
| 157 | *
|
---|
| 158 | * `index` may be a negative number, which indexes back from the end of the
|
---|
| 159 | * List. `v.set(-1, "value")` sets the last item in the List.
|
---|
| 160 | *
|
---|
| 161 | * If `index` larger than `size`, the returned List's `size` will be large
|
---|
| 162 | * enough to include the `index`.
|
---|
| 163 | */
|
---|
| 164 | set(index: number, value: T): List<T>;
|
---|
| 165 |
|
---|
| 166 | /**
|
---|
| 167 | * Returns a new List which excludes this `index` and with a size 1 less
|
---|
| 168 | * than this List. Values at indices above `index` are shifted down by 1 to
|
---|
| 169 | * fill the position.
|
---|
| 170 | *
|
---|
| 171 | * This is synonymous with `list.splice(index, 1)`.
|
---|
| 172 | *
|
---|
| 173 | * `index` may be a negative number, which indexes back from the end of the
|
---|
| 174 | * List. `v.delete(-1)` deletes the last item in the List.
|
---|
| 175 | *
|
---|
| 176 | * Note: `delete` cannot be safely used in IE8
|
---|
| 177 | * @alias remove
|
---|
| 178 | */
|
---|
| 179 | delete(index: number): List<T>;
|
---|
| 180 | remove(index: number): List<T>;
|
---|
| 181 |
|
---|
| 182 | /**
|
---|
| 183 | * Returns a new List with `value` at `index` with a size 1 more than this
|
---|
| 184 | * List. Values at indices above `index` are shifted over by 1.
|
---|
| 185 | *
|
---|
| 186 | * This is synonymous with `list.splice(index, 0, value)
|
---|
| 187 | */
|
---|
| 188 | insert(index: number, value: T): List<T>;
|
---|
| 189 |
|
---|
| 190 | /**
|
---|
| 191 | * Returns a new List with 0 size and no values.
|
---|
| 192 | */
|
---|
| 193 | clear(): List<T>;
|
---|
| 194 |
|
---|
| 195 | /**
|
---|
| 196 | * Returns a new List with the provided `values` appended, starting at this
|
---|
| 197 | * List's `size`.
|
---|
| 198 | */
|
---|
| 199 | push(...values: T[]): List<T>;
|
---|
| 200 |
|
---|
| 201 | /**
|
---|
| 202 | * Returns a new List with a size ones less than this List, excluding
|
---|
| 203 | * the last index in this List.
|
---|
| 204 | *
|
---|
| 205 | * Note: this differs from `Array#pop` because it returns a new
|
---|
| 206 | * List rather than the removed value. Use `last()` to get the last value
|
---|
| 207 | * in this List.
|
---|
| 208 | */
|
---|
| 209 | pop(): List<T>;
|
---|
| 210 |
|
---|
| 211 | /**
|
---|
| 212 | * Returns a new List with the provided `values` prepended, shifting other
|
---|
| 213 | * values ahead to higher indices.
|
---|
| 214 | */
|
---|
| 215 | unshift(...values: T[]): List<T>;
|
---|
| 216 |
|
---|
| 217 | /**
|
---|
| 218 | * Returns a new List with a size ones less than this List, excluding
|
---|
| 219 | * the first index in this List, shifting all other values to a lower index.
|
---|
| 220 | *
|
---|
| 221 | * Note: this differs from `Array#shift` because it returns a new
|
---|
| 222 | * List rather than the removed value. Use `first()` to get the first
|
---|
| 223 | * value in this List.
|
---|
| 224 | */
|
---|
| 225 | shift(): List<T>;
|
---|
| 226 |
|
---|
| 227 | /**
|
---|
| 228 | * Returns a new List with an updated value at `index` with the return
|
---|
| 229 | * value of calling `updater` with the existing value, or `notSetValue` if
|
---|
| 230 | * `index` was not set. If called with a single argument, `updater` is
|
---|
| 231 | * called with the List itself.
|
---|
| 232 | *
|
---|
| 233 | * `index` may be a negative number, which indexes back from the end of the
|
---|
| 234 | * List. `v.update(-1)` updates the last item in the List.
|
---|
| 235 | *
|
---|
| 236 | * @see `Map#update`
|
---|
| 237 | */
|
---|
| 238 | update(updater: (value: List<T>) => List<T>): List<T>;
|
---|
| 239 | update(index: number, updater: (value: T) => T): List<T>;
|
---|
| 240 | update(index: number, notSetValue: T, updater: (value: T) => T): List<T>;
|
---|
| 241 |
|
---|
| 242 | /**
|
---|
| 243 | * @see `Map#merge`
|
---|
| 244 | */
|
---|
| 245 | merge(...iterables: Iterable.Indexed<T>[]): List<T>;
|
---|
| 246 | merge(...iterables: Array<T>[]): List<T>;
|
---|
| 247 |
|
---|
| 248 | /**
|
---|
| 249 | * @see `Map#mergeWith`
|
---|
| 250 | */
|
---|
| 251 | mergeWith(
|
---|
| 252 | merger: (previous?: T, next?: T, key?: number) => T,
|
---|
| 253 | ...iterables: Iterable.Indexed<T>[]
|
---|
| 254 | ): List<T>;
|
---|
| 255 | mergeWith(
|
---|
| 256 | merger: (previous?: T, next?: T, key?: number) => T,
|
---|
| 257 | ...iterables: Array<T>[]
|
---|
| 258 | ): List<T>;
|
---|
| 259 |
|
---|
| 260 | /**
|
---|
| 261 | * @see `Map#mergeDeep`
|
---|
| 262 | */
|
---|
| 263 | mergeDeep(...iterables: Iterable.Indexed<T>[]): List<T>;
|
---|
| 264 | mergeDeep(...iterables: Array<T>[]): List<T>;
|
---|
| 265 |
|
---|
| 266 | /**
|
---|
| 267 | * @see `Map#mergeDeepWith`
|
---|
| 268 | */
|
---|
| 269 | mergeDeepWith(
|
---|
| 270 | merger: (previous?: T, next?: T, key?: number) => T,
|
---|
| 271 | ...iterables: Iterable.Indexed<T>[]
|
---|
| 272 | ): List<T>;
|
---|
| 273 | mergeDeepWith(
|
---|
| 274 | merger: (previous?: T, next?: T, key?: number) => T,
|
---|
| 275 | ...iterables: Array<T>[]
|
---|
| 276 | ): List<T>;
|
---|
| 277 |
|
---|
| 278 | /**
|
---|
| 279 | * Returns a new List with size `size`. If `size` is less than this
|
---|
| 280 | * List's size, the new List will exclude values at the higher indices.
|
---|
| 281 | * If `size` is greater than this List's size, the new List will have
|
---|
| 282 | * undefined values for the newly available indices.
|
---|
| 283 | *
|
---|
| 284 | * When building a new List and the final size is known up front, `setSize`
|
---|
| 285 | * used in conjunction with `withMutations` may result in the more
|
---|
| 286 | * performant construction.
|
---|
| 287 | */
|
---|
| 288 | setSize(size: number): List<T>;
|
---|
| 289 |
|
---|
| 290 |
|
---|
| 291 | // Deep persistent changes
|
---|
| 292 |
|
---|
| 293 | /**
|
---|
| 294 | * Returns a new List having set `value` at this `keyPath`. If any keys in
|
---|
| 295 | * `keyPath` do not exist, a new immutable Map will be created at that key.
|
---|
| 296 | *
|
---|
| 297 | * Index numbers are used as keys to determine the path to follow in
|
---|
| 298 | * the List.
|
---|
| 299 | */
|
---|
| 300 | setIn(keyPath: Array<any>, value: any): List<T>;
|
---|
| 301 | setIn(keyPath: Iterable<any, any>, value: any): List<T>;
|
---|
| 302 |
|
---|
| 303 | /**
|
---|
| 304 | * Returns a new List having removed the value at this `keyPath`. If any
|
---|
| 305 | * keys in `keyPath` do not exist, no change will occur.
|
---|
| 306 | *
|
---|
| 307 | * @alias removeIn
|
---|
| 308 | */
|
---|
| 309 | deleteIn(keyPath: Array<any>): List<T>;
|
---|
| 310 | deleteIn(keyPath: Iterable<any, any>): List<T>;
|
---|
| 311 | removeIn(keyPath: Array<any>): List<T>;
|
---|
| 312 | removeIn(keyPath: Iterable<any, any>): List<T>;
|
---|
| 313 |
|
---|
| 314 | /**
|
---|
| 315 | * @see `Map#updateIn`
|
---|
| 316 | */
|
---|
| 317 | updateIn(
|
---|
| 318 | keyPath: Array<any>,
|
---|
| 319 | updater: (value: any) => any
|
---|
| 320 | ): List<T>;
|
---|
| 321 | updateIn(
|
---|
| 322 | keyPath: Array<any>,
|
---|
| 323 | notSetValue: any,
|
---|
| 324 | updater: (value: any) => any
|
---|
| 325 | ): List<T>;
|
---|
| 326 | updateIn(
|
---|
| 327 | keyPath: Iterable<any, any>,
|
---|
| 328 | updater: (value: any) => any
|
---|
| 329 | ): List<T>;
|
---|
| 330 | updateIn(
|
---|
| 331 | keyPath: Iterable<any, any>,
|
---|
| 332 | notSetValue: any,
|
---|
| 333 | updater: (value: any) => any
|
---|
| 334 | ): List<T>;
|
---|
| 335 |
|
---|
| 336 | /**
|
---|
| 337 | * @see `Map#mergeIn`
|
---|
| 338 | */
|
---|
| 339 | mergeIn(
|
---|
| 340 | keyPath: Iterable<any, any>,
|
---|
| 341 | ...iterables: Iterable.Indexed<T>[]
|
---|
| 342 | ): List<T>;
|
---|
| 343 | mergeIn(
|
---|
| 344 | keyPath: Array<any>,
|
---|
| 345 | ...iterables: Iterable.Indexed<T>[]
|
---|
| 346 | ): List<T>;
|
---|
| 347 | mergeIn(
|
---|
| 348 | keyPath: Array<any>,
|
---|
| 349 | ...iterables: Array<T>[]
|
---|
| 350 | ): List<T>;
|
---|
| 351 |
|
---|
| 352 | /**
|
---|
| 353 | * @see `Map#mergeDeepIn`
|
---|
| 354 | */
|
---|
| 355 | mergeDeepIn(
|
---|
| 356 | keyPath: Iterable<any, any>,
|
---|
| 357 | ...iterables: Iterable.Indexed<T>[]
|
---|
| 358 | ): List<T>;
|
---|
| 359 | mergeDeepIn(
|
---|
| 360 | keyPath: Array<any>,
|
---|
| 361 | ...iterables: Iterable.Indexed<T>[]
|
---|
| 362 | ): List<T>;
|
---|
| 363 | mergeDeepIn(
|
---|
| 364 | keyPath: Array<any>,
|
---|
| 365 | ...iterables: Array<T>[]
|
---|
| 366 | ): List<T>;
|
---|
| 367 |
|
---|
| 368 |
|
---|
| 369 | // Transient changes
|
---|
| 370 |
|
---|
| 371 | /**
|
---|
| 372 | * Note: Not all methods can be used on a mutable collection or within
|
---|
| 373 | * `withMutations`! Only `set`, `push`, `pop`, `shift`, `unshift` and
|
---|
| 374 | * `merge` may be used mutatively.
|
---|
| 375 | *
|
---|
| 376 | * @see `Map#withMutations`
|
---|
| 377 | */
|
---|
| 378 | withMutations(mutator: (mutable: List<T>) => any): List<T>;
|
---|
| 379 |
|
---|
| 380 | /**
|
---|
| 381 | * @see `Map#asMutable`
|
---|
| 382 | */
|
---|
| 383 | asMutable(): List<T>;
|
---|
| 384 |
|
---|
| 385 | /**
|
---|
| 386 | * @see `Map#asImmutable`
|
---|
| 387 | */
|
---|
| 388 | asImmutable(): List<T>;
|
---|
| 389 | }
|
---|
| 390 |
|
---|
| 391 |
|
---|
| 392 | /**
|
---|
| 393 | * Immutable Map is an unordered Iterable.Keyed of (key, value) pairs with
|
---|
| 394 | * `O(log32 N)` gets and `O(log32 N)` persistent sets.
|
---|
| 395 | *
|
---|
| 396 | * Iteration order of a Map is undefined, however is stable. Multiple
|
---|
| 397 | * iterations of the same Map will iterate in the same order.
|
---|
| 398 | *
|
---|
| 399 | * Map's keys can be of any type, and use `Immutable.is` to determine key
|
---|
| 400 | * equality. This allows the use of any value (including NaN) as a key.
|
---|
| 401 | *
|
---|
| 402 | * Because `Immutable.is` returns equality based on value semantics, and
|
---|
| 403 | * Immutable collections are treated as values, any Immutable collection may
|
---|
| 404 | * be used as a key.
|
---|
| 405 | *
|
---|
| 406 | * Map().set(List.of(1), 'listofone').get(List.of(1));
|
---|
| 407 | * // 'listofone'
|
---|
| 408 | *
|
---|
| 409 | * Any JavaScript object may be used as a key, however strict identity is used
|
---|
| 410 | * to evaluate key equality. Two similar looking objects will represent two
|
---|
| 411 | * different keys.
|
---|
| 412 | *
|
---|
| 413 | * Implemented by a hash-array mapped trie.
|
---|
| 414 | */
|
---|
| 415 | export module Map {
|
---|
| 416 |
|
---|
| 417 | /**
|
---|
| 418 | * True if the provided value is a Map
|
---|
| 419 | */
|
---|
| 420 | function isMap(maybeMap: any): boolean;
|
---|
| 421 |
|
---|
| 422 | /**
|
---|
| 423 | * Creates a new Map from alternating keys and values
|
---|
| 424 | */
|
---|
| 425 | function of(...keyValues: any[]): Map<any, any>;
|
---|
| 426 | }
|
---|
| 427 |
|
---|
| 428 | /**
|
---|
| 429 | * Creates a new Immutable Map.
|
---|
| 430 | *
|
---|
| 431 | * Created with the same key value pairs as the provided Iterable.Keyed or
|
---|
| 432 | * JavaScript Object or expects an Iterable of [K, V] tuple entries.
|
---|
| 433 | *
|
---|
| 434 | * var newMap = Map({key: "value"});
|
---|
| 435 | * var newMap = Map([["key", "value"]]);
|
---|
| 436 | *
|
---|
| 437 | * Keep in mind, when using JS objects to construct Immutable Maps, that
|
---|
| 438 | * JavaScript Object properties are always strings, even if written in a
|
---|
| 439 | * quote-less shorthand, while Immutable Maps accept keys of any type.
|
---|
| 440 | *
|
---|
| 441 | * ```js
|
---|
| 442 | * var obj = { 1: "one" };
|
---|
| 443 | * Object.keys(obj); // [ "1" ]
|
---|
| 444 | * obj["1"]; // "one"
|
---|
| 445 | * obj[1]; // "one"
|
---|
| 446 | *
|
---|
| 447 | * var map = Map(obj);
|
---|
| 448 | * map.get("1"); // "one"
|
---|
| 449 | * map.get(1); // undefined
|
---|
| 450 | * ```
|
---|
| 451 | *
|
---|
| 452 | * Property access for JavaScript Objects first converts the key to a string,
|
---|
| 453 | * but since Immutable Map keys can be of any type the argument to `get()` is
|
---|
| 454 | * not altered.
|
---|
| 455 | */
|
---|
| 456 | export function Map<K, V>(): Map<K, V>;
|
---|
| 457 | export function Map<K, V>(iter: Iterable.Keyed<K, V>): Map<K, V>;
|
---|
| 458 | export function Map<K, V>(iter: Iterable<any, /*[K,V]*/Array<any>>): Map<K, V>;
|
---|
| 459 | export function Map<K, V>(array: Array</*[K,V]*/Array<any>>): Map<K, V>;
|
---|
| 460 | export function Map<V>(obj: {[key: string]: V}): Map<string, V>;
|
---|
| 461 | export function Map<K, V>(iterator: Iterator</*[K,V]*/Array<any>>): Map<K, V>;
|
---|
| 462 | export function Map<K, V>(iterable: /*Iterable<[K,V]>*/Object): Map<K, V>;
|
---|
| 463 |
|
---|
| 464 | export interface Map<K, V> extends Collection.Keyed<K, V> {
|
---|
| 465 |
|
---|
| 466 | // Persistent changes
|
---|
| 467 |
|
---|
| 468 | /**
|
---|
| 469 | * Returns a new Map also containing the new key, value pair. If an equivalent
|
---|
| 470 | * key already exists in this Map, it will be replaced.
|
---|
| 471 | */
|
---|
| 472 | set(key: K, value: V): Map<K, V>;
|
---|
| 473 |
|
---|
| 474 | /**
|
---|
| 475 | * Returns a new Map which excludes this `key`.
|
---|
| 476 | *
|
---|
| 477 | * Note: `delete` cannot be safely used in IE8, but is provided to mirror
|
---|
| 478 | * the ES6 collection API.
|
---|
| 479 | * @alias remove
|
---|
| 480 | */
|
---|
| 481 | delete(key: K): Map<K, V>;
|
---|
| 482 | remove(key: K): Map<K, V>;
|
---|
| 483 |
|
---|
| 484 | /**
|
---|
| 485 | * Returns a new Map containing no keys or values.
|
---|
| 486 | */
|
---|
| 487 | clear(): Map<K, V>;
|
---|
| 488 |
|
---|
| 489 | /**
|
---|
| 490 | * Returns a new Map having updated the value at this `key` with the return
|
---|
| 491 | * value of calling `updater` with the existing value, or `notSetValue` if
|
---|
| 492 | * the key was not set. If called with only a single argument, `updater` is
|
---|
| 493 | * called with the Map itself.
|
---|
| 494 | *
|
---|
| 495 | * Equivalent to: `map.set(key, updater(map.get(key, notSetValue)))`.
|
---|
| 496 | */
|
---|
| 497 | update(updater: (value: Map<K, V>) => Map<K, V>): Map<K, V>;
|
---|
| 498 | update(key: K, updater: (value: V) => V): Map<K, V>;
|
---|
| 499 | update(key: K, notSetValue: V, updater: (value: V) => V): Map<K, V>;
|
---|
| 500 |
|
---|
| 501 | /**
|
---|
| 502 | * Returns a new Map resulting from merging the provided Iterables
|
---|
| 503 | * (or JS objects) into this Map. In other words, this takes each entry of
|
---|
| 504 | * each iterable and sets it on this Map.
|
---|
| 505 | *
|
---|
| 506 | * If any of the values provided to `merge` are not Iterable (would return
|
---|
| 507 | * false for `Immutable.Iterable.isIterable`) then they are deeply converted
|
---|
| 508 | * via `Immutable.fromJS` before being merged. However, if the value is an
|
---|
| 509 | * Iterable but includes non-iterable JS objects or arrays, those nested
|
---|
| 510 | * values will be preserved.
|
---|
| 511 | *
|
---|
| 512 | * var x = Immutable.Map({a: 10, b: 20, c: 30});
|
---|
| 513 | * var y = Immutable.Map({b: 40, a: 50, d: 60});
|
---|
| 514 | * x.merge(y) // { a: 50, b: 40, c: 30, d: 60 }
|
---|
| 515 | * y.merge(x) // { b: 20, a: 10, d: 60, c: 30 }
|
---|
| 516 | *
|
---|
| 517 | */
|
---|
| 518 | merge(...iterables: Iterable<K, V>[]): Map<K, V>;
|
---|
| 519 | merge(...iterables: {[key: string]: V}[]): Map<string, V>;
|
---|
| 520 |
|
---|
| 521 | /**
|
---|
| 522 | * Like `merge()`, `mergeWith()` returns a new Map resulting from merging
|
---|
| 523 | * the provided Iterables (or JS objects) into this Map, but uses the
|
---|
| 524 | * `merger` function for dealing with conflicts.
|
---|
| 525 | *
|
---|
| 526 | * var x = Immutable.Map({a: 10, b: 20, c: 30});
|
---|
| 527 | * var y = Immutable.Map({b: 40, a: 50, d: 60});
|
---|
| 528 | * x.mergeWith((prev, next) => prev / next, y) // { a: 0.2, b: 0.5, c: 30, d: 60 }
|
---|
| 529 | * y.mergeWith((prev, next) => prev / next, x) // { b: 2, a: 5, d: 60, c: 30 }
|
---|
| 530 | *
|
---|
| 531 | */
|
---|
| 532 | mergeWith(
|
---|
| 533 | merger: (previous?: V, next?: V, key?: K) => V,
|
---|
| 534 | ...iterables: Iterable<K, V>[]
|
---|
| 535 | ): Map<K, V>;
|
---|
| 536 | mergeWith(
|
---|
| 537 | merger: (previous?: V, next?: V, key?: K) => V,
|
---|
| 538 | ...iterables: {[key: string]: V}[]
|
---|
| 539 | ): Map<string, V>;
|
---|
| 540 |
|
---|
| 541 | /**
|
---|
| 542 | * Like `merge()`, but when two Iterables conflict, it merges them as well,
|
---|
| 543 | * recursing deeply through the nested data.
|
---|
| 544 | *
|
---|
| 545 | * var x = Immutable.fromJS({a: { x: 10, y: 10 }, b: { x: 20, y: 50 } });
|
---|
| 546 | * var y = Immutable.fromJS({a: { x: 2 }, b: { y: 5 }, c: { z: 3 } });
|
---|
| 547 | * x.mergeDeep(y) // {a: { x: 2, y: 10 }, b: { x: 20, y: 5 }, c: { z: 3 } }
|
---|
| 548 | *
|
---|
| 549 | */
|
---|
| 550 | mergeDeep(...iterables: Iterable<K, V>[]): Map<K, V>;
|
---|
| 551 | mergeDeep(...iterables: {[key: string]: V}[]): Map<string, V>;
|
---|
| 552 |
|
---|
| 553 | /**
|
---|
| 554 | * Like `mergeDeep()`, but when two non-Iterables conflict, it uses the
|
---|
| 555 | * `merger` function to determine the resulting value.
|
---|
| 556 | *
|
---|
| 557 | * var x = Immutable.fromJS({a: { x: 10, y: 10 }, b: { x: 20, y: 50 } });
|
---|
| 558 | * var y = Immutable.fromJS({a: { x: 2 }, b: { y: 5 }, c: { z: 3 } });
|
---|
| 559 | * x.mergeDeepWith((prev, next) => prev / next, y)
|
---|
| 560 | * // {a: { x: 5, y: 10 }, b: { x: 20, y: 10 }, c: { z: 3 } }
|
---|
| 561 | *
|
---|
| 562 | */
|
---|
| 563 | mergeDeepWith(
|
---|
| 564 | merger: (previous?: V, next?: V, key?: K) => V,
|
---|
| 565 | ...iterables: Iterable<K, V>[]
|
---|
| 566 | ): Map<K, V>;
|
---|
| 567 | mergeDeepWith(
|
---|
| 568 | merger: (previous?: V, next?: V, key?: K) => V,
|
---|
| 569 | ...iterables: {[key: string]: V}[]
|
---|
| 570 | ): Map<string, V>;
|
---|
| 571 |
|
---|
| 572 |
|
---|
| 573 | // Deep persistent changes
|
---|
| 574 |
|
---|
| 575 | /**
|
---|
| 576 | * Returns a new Map having set `value` at this `keyPath`. If any keys in
|
---|
| 577 | * `keyPath` do not exist, a new immutable Map will be created at that key.
|
---|
| 578 | */
|
---|
| 579 | setIn(keyPath: Array<any>, value: any): Map<K, V>;
|
---|
| 580 | setIn(KeyPath: Iterable<any, any>, value: any): Map<K, V>;
|
---|
| 581 |
|
---|
| 582 | /**
|
---|
| 583 | * Returns a new Map having removed the value at this `keyPath`. If any keys
|
---|
| 584 | * in `keyPath` do not exist, no change will occur.
|
---|
| 585 | *
|
---|
| 586 | * @alias removeIn
|
---|
| 587 | */
|
---|
| 588 | deleteIn(keyPath: Array<any>): Map<K, V>;
|
---|
| 589 | deleteIn(keyPath: Iterable<any, any>): Map<K, V>;
|
---|
| 590 | removeIn(keyPath: Array<any>): Map<K, V>;
|
---|
| 591 | removeIn(keyPath: Iterable<any, any>): Map<K, V>;
|
---|
| 592 |
|
---|
| 593 | /**
|
---|
| 594 | * Returns a new Map having applied the `updater` to the entry found at the
|
---|
| 595 | * keyPath.
|
---|
| 596 | *
|
---|
| 597 | * If any keys in `keyPath` do not exist, new Immutable `Map`s will
|
---|
| 598 | * be created at those keys. If the `keyPath` does not already contain a
|
---|
| 599 | * value, the `updater` function will be called with `notSetValue`, if
|
---|
| 600 | * provided, otherwise `undefined`.
|
---|
| 601 | *
|
---|
| 602 | * var data = Immutable.fromJS({ a: { b: { c: 10 } } });
|
---|
| 603 | * data = data.updateIn(['a', 'b', 'c'], val => val * 2);
|
---|
| 604 | * // { a: { b: { c: 20 } } }
|
---|
| 605 | *
|
---|
| 606 | * If the `updater` function returns the same value it was called with, then
|
---|
| 607 | * no change will occur. This is still true if `notSetValue` is provided.
|
---|
| 608 | *
|
---|
| 609 | * var data1 = Immutable.fromJS({ a: { b: { c: 10 } } });
|
---|
| 610 | * data2 = data1.updateIn(['x', 'y', 'z'], 100, val => val);
|
---|
| 611 | * assert(data2 === data1);
|
---|
| 612 | *
|
---|
| 613 | */
|
---|
| 614 | updateIn(
|
---|
| 615 | keyPath: Array<any>,
|
---|
| 616 | updater: (value: any) => any
|
---|
| 617 | ): Map<K, V>;
|
---|
| 618 | updateIn(
|
---|
| 619 | keyPath: Array<any>,
|
---|
| 620 | notSetValue: any,
|
---|
| 621 | updater: (value: any) => any
|
---|
| 622 | ): Map<K, V>;
|
---|
| 623 | updateIn(
|
---|
| 624 | keyPath: Iterable<any, any>,
|
---|
| 625 | updater: (value: any) => any
|
---|
| 626 | ): Map<K, V>;
|
---|
| 627 | updateIn(
|
---|
| 628 | keyPath: Iterable<any, any>,
|
---|
| 629 | notSetValue: any,
|
---|
| 630 | updater: (value: any) => any
|
---|
| 631 | ): Map<K, V>;
|
---|
| 632 |
|
---|
| 633 | /**
|
---|
| 634 | * A combination of `updateIn` and `merge`, returning a new Map, but
|
---|
| 635 | * performing the merge at a point arrived at by following the keyPath.
|
---|
| 636 | * In other words, these two lines are equivalent:
|
---|
| 637 | *
|
---|
| 638 | * x.updateIn(['a', 'b', 'c'], abc => abc.merge(y));
|
---|
| 639 | * x.mergeIn(['a', 'b', 'c'], y);
|
---|
| 640 | *
|
---|
| 641 | */
|
---|
| 642 | mergeIn(
|
---|
| 643 | keyPath: Iterable<any, any>,
|
---|
| 644 | ...iterables: Iterable<K, V>[]
|
---|
| 645 | ): Map<K, V>;
|
---|
| 646 | mergeIn(
|
---|
| 647 | keyPath: Array<any>,
|
---|
| 648 | ...iterables: Iterable<K, V>[]
|
---|
| 649 | ): Map<K, V>;
|
---|
| 650 | mergeIn(
|
---|
| 651 | keyPath: Array<any>,
|
---|
| 652 | ...iterables: {[key: string]: V}[]
|
---|
| 653 | ): Map<string, V>;
|
---|
| 654 |
|
---|
| 655 | /**
|
---|
| 656 | * A combination of `updateIn` and `mergeDeep`, returning a new Map, but
|
---|
| 657 | * performing the deep merge at a point arrived at by following the keyPath.
|
---|
| 658 | * In other words, these two lines are equivalent:
|
---|
| 659 | *
|
---|
| 660 | * x.updateIn(['a', 'b', 'c'], abc => abc.mergeDeep(y));
|
---|
| 661 | * x.mergeDeepIn(['a', 'b', 'c'], y);
|
---|
| 662 | *
|
---|
| 663 | */
|
---|
| 664 | mergeDeepIn(
|
---|
| 665 | keyPath: Iterable<any, any>,
|
---|
| 666 | ...iterables: Iterable<K, V>[]
|
---|
| 667 | ): Map<K, V>;
|
---|
| 668 | mergeDeepIn(
|
---|
| 669 | keyPath: Array<any>,
|
---|
| 670 | ...iterables: Iterable<K, V>[]
|
---|
| 671 | ): Map<K, V>;
|
---|
| 672 | mergeDeepIn(
|
---|
| 673 | keyPath: Array<any>,
|
---|
| 674 | ...iterables: {[key: string]: V}[]
|
---|
| 675 | ): Map<string, V>;
|
---|
| 676 |
|
---|
| 677 |
|
---|
| 678 | // Transient changes
|
---|
| 679 |
|
---|
| 680 | /**
|
---|
| 681 | * Every time you call one of the above functions, a new immutable Map is
|
---|
| 682 | * created. If a pure function calls a number of these to produce a final
|
---|
| 683 | * return value, then a penalty on performance and memory has been paid by
|
---|
| 684 | * creating all of the intermediate immutable Maps.
|
---|
| 685 | *
|
---|
| 686 | * If you need to apply a series of mutations to produce a new immutable
|
---|
| 687 | * Map, `withMutations()` creates a temporary mutable copy of the Map which
|
---|
| 688 | * can apply mutations in a highly performant manner. In fact, this is
|
---|
| 689 | * exactly how complex mutations like `merge` are done.
|
---|
| 690 | *
|
---|
| 691 | * As an example, this results in the creation of 2, not 4, new Maps:
|
---|
| 692 | *
|
---|
| 693 | * var map1 = Immutable.Map();
|
---|
| 694 | * var map2 = map1.withMutations(map => {
|
---|
| 695 | * map.set('a', 1).set('b', 2).set('c', 3);
|
---|
| 696 | * });
|
---|
| 697 | * assert(map1.size === 0);
|
---|
| 698 | * assert(map2.size === 3);
|
---|
| 699 | *
|
---|
| 700 | * Note: Not all methods can be used on a mutable collection or within
|
---|
| 701 | * `withMutations`! Only `set` and `merge` may be used mutatively.
|
---|
| 702 | *
|
---|
| 703 | */
|
---|
| 704 | withMutations(mutator: (mutable: Map<K, V>) => any): Map<K, V>;
|
---|
| 705 |
|
---|
| 706 | /**
|
---|
| 707 | * Another way to avoid creation of intermediate Immutable maps is to create
|
---|
| 708 | * a mutable copy of this collection. Mutable copies *always* return `this`,
|
---|
| 709 | * and thus shouldn't be used for equality. Your function should never return
|
---|
| 710 | * a mutable copy of a collection, only use it internally to create a new
|
---|
| 711 | * collection. If possible, use `withMutations` as it provides an easier to
|
---|
| 712 | * use API.
|
---|
| 713 | *
|
---|
| 714 | * Note: if the collection is already mutable, `asMutable` returns itself.
|
---|
| 715 | *
|
---|
| 716 | * Note: Not all methods can be used on a mutable collection or within
|
---|
| 717 | * `withMutations`! Only `set` and `merge` may be used mutatively.
|
---|
| 718 | */
|
---|
| 719 | asMutable(): Map<K, V>;
|
---|
| 720 |
|
---|
| 721 | /**
|
---|
| 722 | * The yin to `asMutable`'s yang. Because it applies to mutable collections,
|
---|
| 723 | * this operation is *mutable* and returns itself. Once performed, the mutable
|
---|
| 724 | * copy has become immutable and can be safely returned from a function.
|
---|
| 725 | */
|
---|
| 726 | asImmutable(): Map<K, V>;
|
---|
| 727 | }
|
---|
| 728 |
|
---|
| 729 |
|
---|
| 730 | /**
|
---|
| 731 | * A type of Map that has the additional guarantee that the iteration order of
|
---|
| 732 | * entries will be the order in which they were set().
|
---|
| 733 | *
|
---|
| 734 | * The iteration behavior of OrderedMap is the same as native ES6 Map and
|
---|
| 735 | * JavaScript Object.
|
---|
| 736 | *
|
---|
| 737 | * Note that `OrderedMap` are more expensive than non-ordered `Map` and may
|
---|
| 738 | * consume more memory. `OrderedMap#set` is amortized O(log32 N), but not
|
---|
| 739 | * stable.
|
---|
| 740 | */
|
---|
| 741 |
|
---|
| 742 | export module OrderedMap {
|
---|
| 743 |
|
---|
| 744 | /**
|
---|
| 745 | * True if the provided value is an OrderedMap.
|
---|
| 746 | */
|
---|
| 747 | function isOrderedMap(maybeOrderedMap: any): boolean;
|
---|
| 748 | }
|
---|
| 749 |
|
---|
| 750 | /**
|
---|
| 751 | * Creates a new Immutable OrderedMap.
|
---|
| 752 | *
|
---|
| 753 | * Created with the same key value pairs as the provided Iterable.Keyed or
|
---|
| 754 | * JavaScript Object or expects an Iterable of [K, V] tuple entries.
|
---|
| 755 | *
|
---|
| 756 | * The iteration order of key-value pairs provided to this constructor will
|
---|
| 757 | * be preserved in the OrderedMap.
|
---|
| 758 | *
|
---|
| 759 | * var newOrderedMap = OrderedMap({key: "value"});
|
---|
| 760 | * var newOrderedMap = OrderedMap([["key", "value"]]);
|
---|
| 761 | *
|
---|
| 762 | */
|
---|
| 763 | export function OrderedMap<K, V>(): OrderedMap<K, V>;
|
---|
| 764 | export function OrderedMap<K, V>(iter: Iterable.Keyed<K, V>): OrderedMap<K, V>;
|
---|
| 765 | export function OrderedMap<K, V>(iter: Iterable<any, /*[K,V]*/Array<any>>): OrderedMap<K, V>;
|
---|
| 766 | export function OrderedMap<K, V>(array: Array</*[K,V]*/Array<any>>): OrderedMap<K, V>;
|
---|
| 767 | export function OrderedMap<V>(obj: {[key: string]: V}): OrderedMap<string, V>;
|
---|
| 768 | export function OrderedMap<K, V>(iterator: Iterator</*[K,V]*/Array<any>>): OrderedMap<K, V>;
|
---|
| 769 | export function OrderedMap<K, V>(iterable: /*Iterable<[K,V]>*/Object): OrderedMap<K, V>;
|
---|
| 770 |
|
---|
| 771 | export interface OrderedMap<K, V> extends Map<K, V> {}
|
---|
| 772 |
|
---|
| 773 |
|
---|
| 774 | /**
|
---|
| 775 | * A Collection of unique values with `O(log32 N)` adds and has.
|
---|
| 776 | *
|
---|
| 777 | * When iterating a Set, the entries will be (value, value) pairs. Iteration
|
---|
| 778 | * order of a Set is undefined, however is stable. Multiple iterations of the
|
---|
| 779 | * same Set will iterate in the same order.
|
---|
| 780 | *
|
---|
| 781 | * Set values, like Map keys, may be of any type. Equality is determined using
|
---|
| 782 | * `Immutable.is`, enabling Sets to uniquely include other Immutable
|
---|
| 783 | * collections, custom value types, and NaN.
|
---|
| 784 | */
|
---|
| 785 | export module Set {
|
---|
| 786 |
|
---|
| 787 | /**
|
---|
| 788 | * True if the provided value is a Set
|
---|
| 789 | */
|
---|
| 790 | function isSet(maybeSet: any): boolean;
|
---|
| 791 |
|
---|
| 792 | /**
|
---|
| 793 | * Creates a new Set containing `values`.
|
---|
| 794 | */
|
---|
| 795 | function of<T>(...values: T[]): Set<T>;
|
---|
| 796 |
|
---|
| 797 | /**
|
---|
| 798 | * `Set.fromKeys()` creates a new immutable Set containing the keys from
|
---|
| 799 | * this Iterable or JavaScript Object.
|
---|
| 800 | */
|
---|
| 801 | function fromKeys<T>(iter: Iterable<T, any>): Set<T>;
|
---|
| 802 | function fromKeys(obj: {[key: string]: any}): Set<string>;
|
---|
| 803 | }
|
---|
| 804 |
|
---|
| 805 | /**
|
---|
| 806 | * Create a new immutable Set containing the values of the provided
|
---|
| 807 | * iterable-like.
|
---|
| 808 | */
|
---|
| 809 | export function Set<T>(): Set<T>;
|
---|
| 810 | export function Set<T>(iter: Iterable.Set<T>): Set<T>;
|
---|
| 811 | export function Set<T>(iter: Iterable.Indexed<T>): Set<T>;
|
---|
| 812 | export function Set<K, V>(iter: Iterable.Keyed<K, V>): Set</*[K,V]*/any>;
|
---|
| 813 | export function Set<T>(array: Array<T>): Set<T>;
|
---|
| 814 | export function Set<T>(iterator: Iterator<T>): Set<T>;
|
---|
| 815 | export function Set<T>(iterable: /*Iterable<T>*/Object): Set<T>;
|
---|
| 816 |
|
---|
| 817 | export interface Set<T> extends Collection.Set<T> {
|
---|
| 818 |
|
---|
| 819 | // Persistent changes
|
---|
| 820 |
|
---|
| 821 | /**
|
---|
| 822 | * Returns a new Set which also includes this value.
|
---|
| 823 | */
|
---|
| 824 | add(value: T): Set<T>;
|
---|
| 825 |
|
---|
| 826 | /**
|
---|
| 827 | * Returns a new Set which excludes this value.
|
---|
| 828 | *
|
---|
| 829 | * Note: `delete` cannot be safely used in IE8
|
---|
| 830 | * @alias remove
|
---|
| 831 | */
|
---|
| 832 | delete(value: T): Set<T>;
|
---|
| 833 | remove(value: T): Set<T>;
|
---|
| 834 |
|
---|
| 835 | /**
|
---|
| 836 | * Returns a new Set containing no values.
|
---|
| 837 | */
|
---|
| 838 | clear(): Set<T>;
|
---|
| 839 |
|
---|
| 840 | /**
|
---|
| 841 | * Returns a Set including any value from `iterables` that does not already
|
---|
| 842 | * exist in this Set.
|
---|
| 843 | * @alias merge
|
---|
| 844 | */
|
---|
| 845 | union(...iterables: Iterable<any, T>[]): Set<T>;
|
---|
| 846 | union(...iterables: Array<T>[]): Set<T>;
|
---|
| 847 | merge(...iterables: Iterable<any, T>[]): Set<T>;
|
---|
| 848 | merge(...iterables: Array<T>[]): Set<T>;
|
---|
| 849 |
|
---|
| 850 |
|
---|
| 851 | /**
|
---|
| 852 | * Returns a Set which has removed any values not also contained
|
---|
| 853 | * within `iterables`.
|
---|
| 854 | */
|
---|
| 855 | intersect(...iterables: Iterable<any, T>[]): Set<T>;
|
---|
| 856 | intersect(...iterables: Array<T>[]): Set<T>;
|
---|
| 857 |
|
---|
| 858 | /**
|
---|
| 859 | * Returns a Set excluding any values contained within `iterables`.
|
---|
| 860 | */
|
---|
| 861 | subtract(...iterables: Iterable<any, T>[]): Set<T>;
|
---|
| 862 | subtract(...iterables: Array<T>[]): Set<T>;
|
---|
| 863 |
|
---|
| 864 |
|
---|
| 865 | // Transient changes
|
---|
| 866 |
|
---|
| 867 | /**
|
---|
| 868 | * Note: Not all methods can be used on a mutable collection or within
|
---|
| 869 | * `withMutations`! Only `add` may be used mutatively.
|
---|
| 870 | *
|
---|
| 871 | * @see `Map#withMutations`
|
---|
| 872 | */
|
---|
| 873 | withMutations(mutator: (mutable: Set<T>) => any): Set<T>;
|
---|
| 874 |
|
---|
| 875 | /**
|
---|
| 876 | * @see `Map#asMutable`
|
---|
| 877 | */
|
---|
| 878 | asMutable(): Set<T>;
|
---|
| 879 |
|
---|
| 880 | /**
|
---|
| 881 | * @see `Map#asImmutable`
|
---|
| 882 | */
|
---|
| 883 | asImmutable(): Set<T>;
|
---|
| 884 | }
|
---|
| 885 |
|
---|
| 886 |
|
---|
| 887 | /**
|
---|
| 888 | * A type of Set that has the additional guarantee that the iteration order of
|
---|
| 889 | * values will be the order in which they were `add`ed.
|
---|
| 890 | *
|
---|
| 891 | * The iteration behavior of OrderedSet is the same as native ES6 Set.
|
---|
| 892 | *
|
---|
| 893 | * Note that `OrderedSet` are more expensive than non-ordered `Set` and may
|
---|
| 894 | * consume more memory. `OrderedSet#add` is amortized O(log32 N), but not
|
---|
| 895 | * stable.
|
---|
| 896 | */
|
---|
| 897 | export module OrderedSet {
|
---|
| 898 |
|
---|
| 899 | /**
|
---|
| 900 | * True if the provided value is an OrderedSet.
|
---|
| 901 | */
|
---|
| 902 | function isOrderedSet(maybeOrderedSet: any): boolean;
|
---|
| 903 |
|
---|
| 904 | /**
|
---|
| 905 | * Creates a new OrderedSet containing `values`.
|
---|
| 906 | */
|
---|
| 907 | function of<T>(...values: T[]): OrderedSet<T>;
|
---|
| 908 |
|
---|
| 909 | /**
|
---|
| 910 | * `OrderedSet.fromKeys()` creates a new immutable OrderedSet containing
|
---|
| 911 | * the keys from this Iterable or JavaScript Object.
|
---|
| 912 | */
|
---|
| 913 | function fromKeys<T>(iter: Iterable<T, any>): OrderedSet<T>;
|
---|
| 914 | function fromKeys(obj: {[key: string]: any}): OrderedSet<string>;
|
---|
| 915 | }
|
---|
| 916 |
|
---|
| 917 | /**
|
---|
| 918 | * Create a new immutable OrderedSet containing the values of the provided
|
---|
| 919 | * iterable-like.
|
---|
| 920 | */
|
---|
| 921 | export function OrderedSet<T>(): OrderedSet<T>;
|
---|
| 922 | export function OrderedSet<T>(iter: Iterable.Set<T>): OrderedSet<T>;
|
---|
| 923 | export function OrderedSet<T>(iter: Iterable.Indexed<T>): OrderedSet<T>;
|
---|
| 924 | export function OrderedSet<K, V>(iter: Iterable.Keyed<K, V>): OrderedSet</*[K,V]*/any>;
|
---|
| 925 | export function OrderedSet<T>(array: Array<T>): OrderedSet<T>;
|
---|
| 926 | export function OrderedSet<T>(iterator: Iterator<T>): OrderedSet<T>;
|
---|
| 927 | export function OrderedSet<T>(iterable: /*Iterable<T>*/Object): OrderedSet<T>;
|
---|
| 928 |
|
---|
| 929 | export interface OrderedSet<T> extends Set<T> {}
|
---|
| 930 |
|
---|
| 931 |
|
---|
| 932 | /**
|
---|
| 933 | * Stacks are indexed collections which support very efficient O(1) addition
|
---|
| 934 | * and removal from the front using `unshift(v)` and `shift()`.
|
---|
| 935 | *
|
---|
| 936 | * For familiarity, Stack also provides `push(v)`, `pop()`, and `peek()`, but
|
---|
| 937 | * be aware that they also operate on the front of the list, unlike List or
|
---|
| 938 | * a JavaScript Array.
|
---|
| 939 | *
|
---|
| 940 | * Note: `reverse()` or any inherent reverse traversal (`reduceRight`,
|
---|
| 941 | * `lastIndexOf`, etc.) is not efficient with a Stack.
|
---|
| 942 | *
|
---|
| 943 | * Stack is implemented with a Single-Linked List.
|
---|
| 944 | */
|
---|
| 945 | export module Stack {
|
---|
| 946 |
|
---|
| 947 | /**
|
---|
| 948 | * True if the provided value is a Stack
|
---|
| 949 | */
|
---|
| 950 | function isStack(maybeStack: any): boolean;
|
---|
| 951 |
|
---|
| 952 | /**
|
---|
| 953 | * Creates a new Stack containing `values`.
|
---|
| 954 | */
|
---|
| 955 | function of<T>(...values: T[]): Stack<T>;
|
---|
| 956 | }
|
---|
| 957 |
|
---|
| 958 | /**
|
---|
| 959 | * Create a new immutable Stack containing the values of the provided
|
---|
| 960 | * iterable-like.
|
---|
| 961 | *
|
---|
| 962 | * The iteration order of the provided iterable is preserved in the
|
---|
| 963 | * resulting `Stack`.
|
---|
| 964 | */
|
---|
| 965 | export function Stack<T>(): Stack<T>;
|
---|
| 966 | export function Stack<T>(iter: Iterable.Indexed<T>): Stack<T>;
|
---|
| 967 | export function Stack<T>(iter: Iterable.Set<T>): Stack<T>;
|
---|
| 968 | export function Stack<K, V>(iter: Iterable.Keyed<K, V>): Stack</*[K,V]*/any>;
|
---|
| 969 | export function Stack<T>(array: Array<T>): Stack<T>;
|
---|
| 970 | export function Stack<T>(iterator: Iterator<T>): Stack<T>;
|
---|
| 971 | export function Stack<T>(iterable: /*Iterable<T>*/Object): Stack<T>;
|
---|
| 972 |
|
---|
| 973 | export interface Stack<T> extends Collection.Indexed<T> {
|
---|
| 974 |
|
---|
| 975 | // Reading values
|
---|
| 976 |
|
---|
| 977 | /**
|
---|
| 978 | * Alias for `Stack.first()`.
|
---|
| 979 | */
|
---|
| 980 | peek(): T;
|
---|
| 981 |
|
---|
| 982 |
|
---|
| 983 | // Persistent changes
|
---|
| 984 |
|
---|
| 985 | /**
|
---|
| 986 | * Returns a new Stack with 0 size and no values.
|
---|
| 987 | */
|
---|
| 988 | clear(): Stack<T>;
|
---|
| 989 |
|
---|
| 990 | /**
|
---|
| 991 | * Returns a new Stack with the provided `values` prepended, shifting other
|
---|
| 992 | * values ahead to higher indices.
|
---|
| 993 | *
|
---|
| 994 | * This is very efficient for Stack.
|
---|
| 995 | */
|
---|
| 996 | unshift(...values: T[]): Stack<T>;
|
---|
| 997 |
|
---|
| 998 | /**
|
---|
| 999 | * Like `Stack#unshift`, but accepts a iterable rather than varargs.
|
---|
| 1000 | */
|
---|
| 1001 | unshiftAll(iter: Iterable<any, T>): Stack<T>;
|
---|
| 1002 | unshiftAll(iter: Array<T>): Stack<T>;
|
---|
| 1003 |
|
---|
| 1004 | /**
|
---|
| 1005 | * Returns a new Stack with a size ones less than this Stack, excluding
|
---|
| 1006 | * the first item in this Stack, shifting all other values to a lower index.
|
---|
| 1007 | *
|
---|
| 1008 | * Note: this differs from `Array#shift` because it returns a new
|
---|
| 1009 | * Stack rather than the removed value. Use `first()` or `peek()` to get the
|
---|
| 1010 | * first value in this Stack.
|
---|
| 1011 | */
|
---|
| 1012 | shift(): Stack<T>;
|
---|
| 1013 |
|
---|
| 1014 | /**
|
---|
| 1015 | * Alias for `Stack#unshift` and is not equivalent to `List#push`.
|
---|
| 1016 | */
|
---|
| 1017 | push(...values: T[]): Stack<T>;
|
---|
| 1018 |
|
---|
| 1019 | /**
|
---|
| 1020 | * Alias for `Stack#unshiftAll`.
|
---|
| 1021 | */
|
---|
| 1022 | pushAll(iter: Iterable<any, T>): Stack<T>;
|
---|
| 1023 | pushAll(iter: Array<T>): Stack<T>;
|
---|
| 1024 |
|
---|
| 1025 | /**
|
---|
| 1026 | * Alias for `Stack#shift` and is not equivalent to `List#pop`.
|
---|
| 1027 | */
|
---|
| 1028 | pop(): Stack<T>;
|
---|
| 1029 |
|
---|
| 1030 |
|
---|
| 1031 | // Transient changes
|
---|
| 1032 |
|
---|
| 1033 | /**
|
---|
| 1034 | * Note: Not all methods can be used on a mutable collection or within
|
---|
| 1035 | * `withMutations`! Only `set`, `push`, and `pop` may be used mutatively.
|
---|
| 1036 | *
|
---|
| 1037 | * @see `Map#withMutations`
|
---|
| 1038 | */
|
---|
| 1039 | withMutations(mutator: (mutable: Stack<T>) => any): Stack<T>;
|
---|
| 1040 |
|
---|
| 1041 | /**
|
---|
| 1042 | * @see `Map#asMutable`
|
---|
| 1043 | */
|
---|
| 1044 | asMutable(): Stack<T>;
|
---|
| 1045 |
|
---|
| 1046 | /**
|
---|
| 1047 | * @see `Map#asImmutable`
|
---|
| 1048 | */
|
---|
| 1049 | asImmutable(): Stack<T>;
|
---|
| 1050 | }
|
---|
| 1051 |
|
---|
| 1052 |
|
---|
| 1053 | /**
|
---|
| 1054 | * Returns a Seq.Indexed of numbers from `start` (inclusive) to `end`
|
---|
| 1055 | * (exclusive), by `step`, where `start` defaults to 0, `step` to 1, and `end` to
|
---|
| 1056 | * infinity. When `start` is equal to `end`, returns empty range.
|
---|
| 1057 | *
|
---|
| 1058 | * Range() // [0,1,2,3,...]
|
---|
| 1059 | * Range(10) // [10,11,12,13,...]
|
---|
| 1060 | * Range(10,15) // [10,11,12,13,14]
|
---|
| 1061 | * Range(10,30,5) // [10,15,20,25]
|
---|
| 1062 | * Range(30,10,5) // [30,25,20,15]
|
---|
| 1063 | * Range(30,30,5) // []
|
---|
| 1064 | *
|
---|
| 1065 | */
|
---|
| 1066 | export function Range(start?: number, end?: number, step?: number): Seq.Indexed<number>;
|
---|
| 1067 |
|
---|
| 1068 |
|
---|
| 1069 | /**
|
---|
| 1070 | * Returns a Seq.Indexed of `value` repeated `times` times. When `times` is
|
---|
| 1071 | * not defined, returns an infinite `Seq` of `value`.
|
---|
| 1072 | *
|
---|
| 1073 | * Repeat('foo') // ['foo','foo','foo',...]
|
---|
| 1074 | * Repeat('bar',4) // ['bar','bar','bar','bar']
|
---|
| 1075 | *
|
---|
| 1076 | */
|
---|
| 1077 | export function Repeat<T>(value: T, times?: number): Seq.Indexed<T>;
|
---|
| 1078 |
|
---|
| 1079 |
|
---|
| 1080 | /**
|
---|
| 1081 | * Creates a new Class which produces Record instances. A record is similar to
|
---|
| 1082 | * a JS object, but enforce a specific set of allowed string keys, and have
|
---|
| 1083 | * default values.
|
---|
| 1084 | *
|
---|
| 1085 | * var ABRecord = Record({a:1, b:2})
|
---|
| 1086 | * var myRecord = new ABRecord({b:3})
|
---|
| 1087 | *
|
---|
| 1088 | * Records always have a value for the keys they define. `remove`ing a key
|
---|
| 1089 | * from a record simply resets it to the default value for that key.
|
---|
| 1090 | *
|
---|
| 1091 | * myRecord.size // 2
|
---|
| 1092 | * myRecord.get('a') // 1
|
---|
| 1093 | * myRecord.get('b') // 3
|
---|
| 1094 | * myRecordWithoutB = myRecord.remove('b')
|
---|
| 1095 | * myRecordWithoutB.get('b') // 2
|
---|
| 1096 | * myRecordWithoutB.size // 2
|
---|
| 1097 | *
|
---|
| 1098 | * Values provided to the constructor not found in the Record type will
|
---|
| 1099 | * be ignored. For example, in this case, ABRecord is provided a key "x" even
|
---|
| 1100 | * though only "a" and "b" have been defined. The value for "x" will be
|
---|
| 1101 | * ignored for this record.
|
---|
| 1102 | *
|
---|
| 1103 | * var myRecord = new ABRecord({b:3, x:10})
|
---|
| 1104 | * myRecord.get('x') // undefined
|
---|
| 1105 | *
|
---|
| 1106 | * Because Records have a known set of string keys, property get access works
|
---|
| 1107 | * as expected, however property sets will throw an Error.
|
---|
| 1108 | *
|
---|
| 1109 | * Note: IE8 does not support property access. Only use `get()` when
|
---|
| 1110 | * supporting IE8.
|
---|
| 1111 | *
|
---|
| 1112 | * myRecord.b // 3
|
---|
| 1113 | * myRecord.b = 5 // throws Error
|
---|
| 1114 | *
|
---|
| 1115 | * Record Classes can be extended as well, allowing for custom methods on your
|
---|
| 1116 | * Record. This is not a common pattern in functional environments, but is in
|
---|
| 1117 | * many JS programs.
|
---|
| 1118 | *
|
---|
| 1119 | * Note: TypeScript does not support this type of subclassing.
|
---|
| 1120 | *
|
---|
| 1121 | * class ABRecord extends Record({a:1,b:2}) {
|
---|
| 1122 | * getAB() {
|
---|
| 1123 | * return this.a + this.b;
|
---|
| 1124 | * }
|
---|
| 1125 | * }
|
---|
| 1126 | *
|
---|
| 1127 | * var myRecord = new ABRecord({b: 3})
|
---|
| 1128 | * myRecord.getAB() // 4
|
---|
| 1129 | *
|
---|
| 1130 | */
|
---|
| 1131 | export module Record {
|
---|
| 1132 | export interface Class {
|
---|
| 1133 | new (): Map<string, any>;
|
---|
| 1134 | new (values: {[key: string]: any}): Map<string, any>;
|
---|
| 1135 | new (values: Iterable<string, any>): Map<string, any>; // deprecated
|
---|
| 1136 |
|
---|
| 1137 | (): Map<string, any>;
|
---|
| 1138 | (values: {[key: string]: any}): Map<string, any>;
|
---|
| 1139 | (values: Iterable<string, any>): Map<string, any>; // deprecated
|
---|
| 1140 | }
|
---|
| 1141 | }
|
---|
| 1142 |
|
---|
| 1143 | export function Record(
|
---|
| 1144 | defaultValues: {[key: string]: any}, name?: string
|
---|
| 1145 | ): Record.Class;
|
---|
| 1146 |
|
---|
| 1147 |
|
---|
| 1148 | /**
|
---|
| 1149 | * Represents a sequence of values, but may not be backed by a concrete data
|
---|
| 1150 | * structure.
|
---|
| 1151 | *
|
---|
| 1152 | * **Seq is immutable** — Once a Seq is created, it cannot be
|
---|
| 1153 | * changed, appended to, rearranged or otherwise modified. Instead, any
|
---|
| 1154 | * mutative method called on a `Seq` will return a new `Seq`.
|
---|
| 1155 | *
|
---|
| 1156 | * **Seq is lazy** — Seq does as little work as necessary to respond to any
|
---|
| 1157 | * method call. Values are often created during iteration, including implicit
|
---|
| 1158 | * iteration when reducing or converting to a concrete data structure such as
|
---|
| 1159 | * a `List` or JavaScript `Array`.
|
---|
| 1160 | *
|
---|
| 1161 | * For example, the following performs no work, because the resulting
|
---|
| 1162 | * Seq's values are never iterated:
|
---|
| 1163 | *
|
---|
| 1164 | * var oddSquares = Immutable.Seq.of(1,2,3,4,5,6,7,8)
|
---|
| 1165 | * .filter(x => x % 2).map(x => x * x);
|
---|
| 1166 | *
|
---|
| 1167 | * Once the Seq is used, it performs only the work necessary. In this
|
---|
| 1168 | * example, no intermediate data structures are ever created, filter is only
|
---|
| 1169 | * called three times, and map is only called once:
|
---|
| 1170 | *
|
---|
| 1171 | * console.log(oddSquares.get(1)); // 9
|
---|
| 1172 | *
|
---|
| 1173 | * Seq allows for the efficient chaining of operations,
|
---|
| 1174 | * allowing for the expression of logic that can otherwise be very tedious:
|
---|
| 1175 | *
|
---|
| 1176 | * Immutable.Seq({a:1, b:1, c:1})
|
---|
| 1177 | * .flip().map(key => key.toUpperCase()).flip().toObject();
|
---|
| 1178 | * // Map { A: 1, B: 1, C: 1 }
|
---|
| 1179 | *
|
---|
| 1180 | * As well as expressing logic that would otherwise be memory or time limited:
|
---|
| 1181 | *
|
---|
| 1182 | * Immutable.Range(1, Infinity)
|
---|
| 1183 | * .skip(1000)
|
---|
| 1184 | * .map(n => -n)
|
---|
| 1185 | * .filter(n => n % 2 === 0)
|
---|
| 1186 | * .take(2)
|
---|
| 1187 | * .reduce((r, n) => r * n, 1);
|
---|
| 1188 | * // 1006008
|
---|
| 1189 | *
|
---|
| 1190 | * Seq is often used to provide a rich collection API to JavaScript Object.
|
---|
| 1191 | *
|
---|
| 1192 | * Immutable.Seq({ x: 0, y: 1, z: 2 }).map(v => v * 2).toObject();
|
---|
| 1193 | * // { x: 0, y: 2, z: 4 }
|
---|
| 1194 | */
|
---|
| 1195 |
|
---|
| 1196 | export module Seq {
|
---|
| 1197 | /**
|
---|
| 1198 | * True if `maybeSeq` is a Seq, it is not backed by a concrete
|
---|
| 1199 | * structure such as Map, List, or Set.
|
---|
| 1200 | */
|
---|
| 1201 | function isSeq(maybeSeq: any): boolean;
|
---|
| 1202 |
|
---|
| 1203 | /**
|
---|
| 1204 | * Returns a Seq of the values provided. Alias for `Seq.Indexed.of()`.
|
---|
| 1205 | */
|
---|
| 1206 | function of<T>(...values: T[]): Seq.Indexed<T>;
|
---|
| 1207 |
|
---|
| 1208 |
|
---|
| 1209 | /**
|
---|
| 1210 | * `Seq` which represents key-value pairs.
|
---|
| 1211 | */
|
---|
| 1212 | export module Keyed {}
|
---|
| 1213 |
|
---|
| 1214 | /**
|
---|
| 1215 | * Always returns a Seq.Keyed, if input is not keyed, expects an
|
---|
| 1216 | * iterable of [K, V] tuples.
|
---|
| 1217 | */
|
---|
| 1218 | export function Keyed<K, V>(): Seq.Keyed<K, V>;
|
---|
| 1219 | export function Keyed<K, V>(seq: Iterable.Keyed<K, V>): Seq.Keyed<K, V>;
|
---|
| 1220 | export function Keyed<K, V>(seq: Iterable<any, /*[K,V]*/any>): Seq.Keyed<K, V>;
|
---|
| 1221 | export function Keyed<K, V>(array: Array</*[K,V]*/any>): Seq.Keyed<K, V>;
|
---|
| 1222 | export function Keyed<V>(obj: {[key: string]: V}): Seq.Keyed<string, V>;
|
---|
| 1223 | export function Keyed<K, V>(iterator: Iterator</*[K,V]*/any>): Seq.Keyed<K, V>;
|
---|
| 1224 | export function Keyed<K, V>(iterable: /*Iterable<[K,V]>*/Object): Seq.Keyed<K, V>;
|
---|
| 1225 |
|
---|
| 1226 | export interface Keyed<K, V> extends Seq<K, V>, Iterable.Keyed<K, V> {
|
---|
| 1227 |
|
---|
| 1228 | /**
|
---|
| 1229 | * Returns itself
|
---|
| 1230 | */
|
---|
| 1231 | toSeq(): /*this*/Seq.Keyed<K, V>
|
---|
| 1232 | }
|
---|
| 1233 |
|
---|
| 1234 |
|
---|
| 1235 | /**
|
---|
| 1236 | * `Seq` which represents an ordered indexed list of values.
|
---|
| 1237 | */
|
---|
| 1238 | module Indexed {
|
---|
| 1239 |
|
---|
| 1240 | /**
|
---|
| 1241 | * Provides an Seq.Indexed of the values provided.
|
---|
| 1242 | */
|
---|
| 1243 | function of<T>(...values: T[]): Seq.Indexed<T>;
|
---|
| 1244 | }
|
---|
| 1245 |
|
---|
| 1246 | /**
|
---|
| 1247 | * Always returns Seq.Indexed, discarding associated keys and
|
---|
| 1248 | * supplying incrementing indices.
|
---|
| 1249 | */
|
---|
| 1250 | export function Indexed<T>(): Seq.Indexed<T>;
|
---|
| 1251 | export function Indexed<T>(seq: Iterable.Indexed<T>): Seq.Indexed<T>;
|
---|
| 1252 | export function Indexed<T>(seq: Iterable.Set<T>): Seq.Indexed<T>;
|
---|
| 1253 | export function Indexed<K, V>(seq: Iterable.Keyed<K, V>): Seq.Indexed</*[K,V]*/any>;
|
---|
| 1254 | export function Indexed<T>(array: Array<T>): Seq.Indexed<T>;
|
---|
| 1255 | export function Indexed<T>(iterator: Iterator<T>): Seq.Indexed<T>;
|
---|
| 1256 | export function Indexed<T>(iterable: /*Iterable<T>*/Object): Seq.Indexed<T>;
|
---|
| 1257 |
|
---|
| 1258 | export interface Indexed<T> extends Seq<number, T>, Iterable.Indexed<T> {
|
---|
| 1259 |
|
---|
| 1260 | /**
|
---|
| 1261 | * Returns itself
|
---|
| 1262 | */
|
---|
| 1263 | toSeq(): /*this*/Seq.Indexed<T>
|
---|
| 1264 | }
|
---|
| 1265 |
|
---|
| 1266 |
|
---|
| 1267 | /**
|
---|
| 1268 | * `Seq` which represents a set of values.
|
---|
| 1269 | *
|
---|
| 1270 | * Because `Seq` are often lazy, `Seq.Set` does not provide the same guarantee
|
---|
| 1271 | * of value uniqueness as the concrete `Set`.
|
---|
| 1272 | */
|
---|
| 1273 | export module Set {
|
---|
| 1274 |
|
---|
| 1275 | /**
|
---|
| 1276 | * Returns a Seq.Set of the provided values
|
---|
| 1277 | */
|
---|
| 1278 | function of<T>(...values: T[]): Seq.Set<T>;
|
---|
| 1279 | }
|
---|
| 1280 |
|
---|
| 1281 | /**
|
---|
| 1282 | * Always returns a Seq.Set, discarding associated indices or keys.
|
---|
| 1283 | */
|
---|
| 1284 | export function Set<T>(): Seq.Set<T>;
|
---|
| 1285 | export function Set<T>(seq: Iterable.Set<T>): Seq.Set<T>;
|
---|
| 1286 | export function Set<T>(seq: Iterable.Indexed<T>): Seq.Set<T>;
|
---|
| 1287 | export function Set<K, V>(seq: Iterable.Keyed<K, V>): Seq.Set</*[K,V]*/any>;
|
---|
| 1288 | export function Set<T>(array: Array<T>): Seq.Set<T>;
|
---|
| 1289 | export function Set<T>(iterator: Iterator<T>): Seq.Set<T>;
|
---|
| 1290 | export function Set<T>(iterable: /*Iterable<T>*/Object): Seq.Set<T>;
|
---|
| 1291 |
|
---|
| 1292 | export interface Set<T> extends Seq<T, T>, Iterable.Set<T> {
|
---|
| 1293 |
|
---|
| 1294 | /**
|
---|
| 1295 | * Returns itself
|
---|
| 1296 | */
|
---|
| 1297 | toSeq(): /*this*/Seq.Set<T>
|
---|
| 1298 | }
|
---|
| 1299 |
|
---|
| 1300 | }
|
---|
| 1301 |
|
---|
| 1302 | /**
|
---|
| 1303 | * Creates a Seq.
|
---|
| 1304 | *
|
---|
| 1305 | * Returns a particular kind of `Seq` based on the input.
|
---|
| 1306 | *
|
---|
| 1307 | * * If a `Seq`, that same `Seq`.
|
---|
| 1308 | * * If an `Iterable`, a `Seq` of the same kind (Keyed, Indexed, or Set).
|
---|
| 1309 | * * If an Array-like, an `Seq.Indexed`.
|
---|
| 1310 | * * If an Object with an Iterator, an `Seq.Indexed`.
|
---|
| 1311 | * * If an Iterator, an `Seq.Indexed`.
|
---|
| 1312 | * * If an Object, a `Seq.Keyed`.
|
---|
| 1313 | *
|
---|
| 1314 | */
|
---|
| 1315 | export function Seq<K, V>(): Seq<K, V>;
|
---|
| 1316 | export function Seq<K, V>(seq: Seq<K, V>): Seq<K, V>;
|
---|
| 1317 | export function Seq<K, V>(iterable: Iterable<K, V>): Seq<K, V>;
|
---|
| 1318 | export function Seq<T>(array: Array<T>): Seq.Indexed<T>;
|
---|
| 1319 | export function Seq<V>(obj: {[key: string]: V}): Seq.Keyed<string, V>;
|
---|
| 1320 | export function Seq<T>(iterator: Iterator<T>): Seq.Indexed<T>;
|
---|
| 1321 | export function Seq<T>(iterable: /*ES6Iterable<T>*/Object): Seq.Indexed<T>;
|
---|
| 1322 |
|
---|
| 1323 | export interface Seq<K, V> extends Iterable<K, V> {
|
---|
| 1324 |
|
---|
| 1325 | /**
|
---|
| 1326 | * Some Seqs can describe their size lazily. When this is the case,
|
---|
| 1327 | * size will be an integer. Otherwise it will be undefined.
|
---|
| 1328 | *
|
---|
| 1329 | * For example, Seqs returned from `map()` or `reverse()`
|
---|
| 1330 | * preserve the size of the original `Seq` while `filter()` does not.
|
---|
| 1331 | *
|
---|
| 1332 | * Note: `Range`, `Repeat` and `Seq`s made from `Array`s and `Object`s will
|
---|
| 1333 | * always have a size.
|
---|
| 1334 | */
|
---|
| 1335 | size: number/*?*/;
|
---|
| 1336 |
|
---|
| 1337 |
|
---|
| 1338 | // Force evaluation
|
---|
| 1339 |
|
---|
| 1340 | /**
|
---|
| 1341 | * Because Sequences are lazy and designed to be chained together, they do
|
---|
| 1342 | * not cache their results. For example, this map function is called a total
|
---|
| 1343 | * of 6 times, as each `join` iterates the Seq of three values.
|
---|
| 1344 | *
|
---|
| 1345 | * var squares = Seq.of(1,2,3).map(x => x * x);
|
---|
| 1346 | * squares.join() + squares.join();
|
---|
| 1347 | *
|
---|
| 1348 | * If you know a `Seq` will be used multiple times, it may be more
|
---|
| 1349 | * efficient to first cache it in memory. Here, the map function is called
|
---|
| 1350 | * only 3 times.
|
---|
| 1351 | *
|
---|
| 1352 | * var squares = Seq.of(1,2,3).map(x => x * x).cacheResult();
|
---|
| 1353 | * squares.join() + squares.join();
|
---|
| 1354 | *
|
---|
| 1355 | * Use this method judiciously, as it must fully evaluate a Seq which can be
|
---|
| 1356 | * a burden on memory and possibly performance.
|
---|
| 1357 | *
|
---|
| 1358 | * Note: after calling `cacheResult`, a Seq will always have a `size`.
|
---|
| 1359 | */
|
---|
| 1360 | cacheResult(): /*this*/Seq<K, V>;
|
---|
| 1361 | }
|
---|
| 1362 |
|
---|
| 1363 | /**
|
---|
| 1364 | * The `Iterable` is a set of (key, value) entries which can be iterated, and
|
---|
| 1365 | * is the base class for all collections in `immutable`, allowing them to
|
---|
| 1366 | * make use of all the Iterable methods (such as `map` and `filter`).
|
---|
| 1367 | *
|
---|
| 1368 | * Note: An iterable is always iterated in the same order, however that order
|
---|
| 1369 | * may not always be well defined, as is the case for the `Map` and `Set`.
|
---|
| 1370 | */
|
---|
| 1371 | export module Iterable {
|
---|
| 1372 | /**
|
---|
| 1373 | * True if `maybeIterable` is an Iterable, or any of its subclasses.
|
---|
| 1374 | */
|
---|
| 1375 | function isIterable(maybeIterable: any): boolean;
|
---|
| 1376 |
|
---|
| 1377 | /**
|
---|
| 1378 | * True if `maybeKeyed` is an Iterable.Keyed, or any of its subclasses.
|
---|
| 1379 | */
|
---|
| 1380 | function isKeyed(maybeKeyed: any): boolean;
|
---|
| 1381 |
|
---|
| 1382 | /**
|
---|
| 1383 | * True if `maybeIndexed` is a Iterable.Indexed, or any of its subclasses.
|
---|
| 1384 | */
|
---|
| 1385 | function isIndexed(maybeIndexed: any): boolean;
|
---|
| 1386 |
|
---|
| 1387 | /**
|
---|
| 1388 | * True if `maybeAssociative` is either a keyed or indexed Iterable.
|
---|
| 1389 | */
|
---|
| 1390 | function isAssociative(maybeAssociative: any): boolean;
|
---|
| 1391 |
|
---|
| 1392 | /**
|
---|
| 1393 | * True if `maybeOrdered` is an Iterable where iteration order is well
|
---|
| 1394 | * defined. True for Iterable.Indexed as well as OrderedMap and OrderedSet.
|
---|
| 1395 | */
|
---|
| 1396 | function isOrdered(maybeOrdered: any): boolean;
|
---|
| 1397 |
|
---|
| 1398 |
|
---|
| 1399 | /**
|
---|
| 1400 | * Keyed Iterables have discrete keys tied to each value.
|
---|
| 1401 | *
|
---|
| 1402 | * When iterating `Iterable.Keyed`, each iteration will yield a `[K, V]`
|
---|
| 1403 | * tuple, in other words, `Iterable#entries` is the default iterator for
|
---|
| 1404 | * Keyed Iterables.
|
---|
| 1405 | */
|
---|
| 1406 | export module Keyed {}
|
---|
| 1407 |
|
---|
| 1408 | /**
|
---|
| 1409 | * Creates an Iterable.Keyed
|
---|
| 1410 | *
|
---|
| 1411 | * Similar to `Iterable()`, however it expects iterable-likes of [K, V]
|
---|
| 1412 | * tuples if not constructed from a Iterable.Keyed or JS Object.
|
---|
| 1413 | */
|
---|
| 1414 | export function Keyed<K, V>(iter: Iterable.Keyed<K, V>): Iterable.Keyed<K, V>;
|
---|
| 1415 | export function Keyed<K, V>(iter: Iterable<any, /*[K,V]*/any>): Iterable.Keyed<K, V>;
|
---|
| 1416 | export function Keyed<K, V>(array: Array</*[K,V]*/any>): Iterable.Keyed<K, V>;
|
---|
| 1417 | export function Keyed<V>(obj: {[key: string]: V}): Iterable.Keyed<string, V>;
|
---|
| 1418 | export function Keyed<K, V>(iterator: Iterator</*[K,V]*/any>): Iterable.Keyed<K, V>;
|
---|
| 1419 | export function Keyed<K, V>(iterable: /*Iterable<[K,V]>*/Object): Iterable.Keyed<K, V>;
|
---|
| 1420 |
|
---|
| 1421 | export interface Keyed<K, V> extends Iterable<K, V> {
|
---|
| 1422 |
|
---|
| 1423 | /**
|
---|
| 1424 | * Returns Seq.Keyed.
|
---|
| 1425 | * @override
|
---|
| 1426 | */
|
---|
| 1427 | toSeq(): Seq.Keyed<K, V>;
|
---|
| 1428 |
|
---|
| 1429 |
|
---|
| 1430 | // Sequence functions
|
---|
| 1431 |
|
---|
| 1432 | /**
|
---|
| 1433 | * Returns a new Iterable.Keyed of the same type where the keys and values
|
---|
| 1434 | * have been flipped.
|
---|
| 1435 | *
|
---|
| 1436 | * Seq({ a: 'z', b: 'y' }).flip() // { z: 'a', y: 'b' }
|
---|
| 1437 | *
|
---|
| 1438 | */
|
---|
| 1439 | flip(): /*this*/Iterable.Keyed<V, K>;
|
---|
| 1440 |
|
---|
| 1441 | /**
|
---|
| 1442 | * Returns a new Iterable.Keyed of the same type with keys passed through
|
---|
| 1443 | * a `mapper` function.
|
---|
| 1444 | *
|
---|
| 1445 | * Seq({ a: 1, b: 2 })
|
---|
| 1446 | * .mapKeys(x => x.toUpperCase())
|
---|
| 1447 | * // Seq { A: 1, B: 2 }
|
---|
| 1448 | *
|
---|
| 1449 | */
|
---|
| 1450 | mapKeys<M>(
|
---|
| 1451 | mapper: (key?: K, value?: V, iter?: /*this*/Iterable.Keyed<K, V>) => M,
|
---|
| 1452 | context?: any
|
---|
| 1453 | ): /*this*/Iterable.Keyed<M, V>;
|
---|
| 1454 |
|
---|
| 1455 | /**
|
---|
| 1456 | * Returns a new Iterable.Keyed of the same type with entries
|
---|
| 1457 | * ([key, value] tuples) passed through a `mapper` function.
|
---|
| 1458 | *
|
---|
| 1459 | * Seq({ a: 1, b: 2 })
|
---|
| 1460 | * .mapEntries(([k, v]) => [k.toUpperCase(), v * 2])
|
---|
| 1461 | * // Seq { A: 2, B: 4 }
|
---|
| 1462 | *
|
---|
| 1463 | */
|
---|
| 1464 | mapEntries<KM, VM>(
|
---|
| 1465 | mapper: (
|
---|
| 1466 | entry?: /*(K, V)*/Array<any>,
|
---|
| 1467 | index?: number,
|
---|
| 1468 | iter?: /*this*/Iterable.Keyed<K, V>
|
---|
| 1469 | ) => /*[KM, VM]*/Array<any>,
|
---|
| 1470 | context?: any
|
---|
| 1471 | ): /*this*/Iterable.Keyed<KM, VM>;
|
---|
| 1472 | }
|
---|
| 1473 |
|
---|
| 1474 |
|
---|
| 1475 | /**
|
---|
| 1476 | * Indexed Iterables have incrementing numeric keys. They exhibit
|
---|
| 1477 | * slightly different behavior than `Iterable.Keyed` for some methods in order
|
---|
| 1478 | * to better mirror the behavior of JavaScript's `Array`, and add methods
|
---|
| 1479 | * which do not make sense on non-indexed Iterables such as `indexOf`.
|
---|
| 1480 | *
|
---|
| 1481 | * Unlike JavaScript arrays, `Iterable.Indexed`s are always dense. "Unset"
|
---|
| 1482 | * indices and `undefined` indices are indistinguishable, and all indices from
|
---|
| 1483 | * 0 to `size` are visited when iterated.
|
---|
| 1484 | *
|
---|
| 1485 | * All Iterable.Indexed methods return re-indexed Iterables. In other words,
|
---|
| 1486 | * indices always start at 0 and increment until size. If you wish to
|
---|
| 1487 | * preserve indices, using them as keys, convert to a Iterable.Keyed by
|
---|
| 1488 | * calling `toKeyedSeq`.
|
---|
| 1489 | */
|
---|
| 1490 | export module Indexed {}
|
---|
| 1491 |
|
---|
| 1492 | /**
|
---|
| 1493 | * Creates a new Iterable.Indexed.
|
---|
| 1494 | */
|
---|
| 1495 | export function Indexed<T>(iter: Iterable.Indexed<T>): Iterable.Indexed<T>;
|
---|
| 1496 | export function Indexed<T>(iter: Iterable.Set<T>): Iterable.Indexed<T>;
|
---|
| 1497 | export function Indexed<K, V>(iter: Iterable.Keyed<K, V>): Iterable.Indexed</*[K,V]*/any>;
|
---|
| 1498 | export function Indexed<T>(array: Array<T>): Iterable.Indexed<T>;
|
---|
| 1499 | export function Indexed<T>(iterator: Iterator<T>): Iterable.Indexed<T>;
|
---|
| 1500 | export function Indexed<T>(iterable: /*Iterable<T>*/Object): Iterable.Indexed<T>;
|
---|
| 1501 |
|
---|
| 1502 | export interface Indexed<T> extends Iterable<number, T> {
|
---|
| 1503 |
|
---|
| 1504 | // Reading values
|
---|
| 1505 |
|
---|
| 1506 | /**
|
---|
| 1507 | * Returns the value associated with the provided index, or notSetValue if
|
---|
| 1508 | * the index is beyond the bounds of the Iterable.
|
---|
| 1509 | *
|
---|
| 1510 | * `index` may be a negative number, which indexes back from the end of the
|
---|
| 1511 | * Iterable. `s.get(-1)` gets the last item in the Iterable.
|
---|
| 1512 | */
|
---|
| 1513 | get(index: number, notSetValue?: T): T;
|
---|
| 1514 |
|
---|
| 1515 |
|
---|
| 1516 | // Conversion to Seq
|
---|
| 1517 |
|
---|
| 1518 | /**
|
---|
| 1519 | * Returns Seq.Indexed.
|
---|
| 1520 | * @override
|
---|
| 1521 | */
|
---|
| 1522 | toSeq(): Seq.Indexed<T>;
|
---|
| 1523 |
|
---|
| 1524 | /**
|
---|
| 1525 | * If this is an iterable of [key, value] entry tuples, it will return a
|
---|
| 1526 | * Seq.Keyed of those entries.
|
---|
| 1527 | */
|
---|
| 1528 | fromEntrySeq(): Seq.Keyed<any, any>;
|
---|
| 1529 |
|
---|
| 1530 |
|
---|
| 1531 | // Combination
|
---|
| 1532 |
|
---|
| 1533 | /**
|
---|
| 1534 | * Returns an Iterable of the same type with `separator` between each item
|
---|
| 1535 | * in this Iterable.
|
---|
| 1536 | */
|
---|
| 1537 | interpose(separator: T): /*this*/Iterable.Indexed<T>;
|
---|
| 1538 |
|
---|
| 1539 | /**
|
---|
| 1540 | * Returns an Iterable of the same type with the provided `iterables`
|
---|
| 1541 | * interleaved into this iterable.
|
---|
| 1542 | *
|
---|
| 1543 | * The resulting Iterable includes the first item from each, then the
|
---|
| 1544 | * second from each, etc.
|
---|
| 1545 | *
|
---|
| 1546 | * I.Seq.of(1,2,3).interleave(I.Seq.of('A','B','C'))
|
---|
| 1547 | * // Seq [ 1, 'A', 2, 'B', 3, 'C' ]
|
---|
| 1548 | *
|
---|
| 1549 | * The shortest Iterable stops interleave.
|
---|
| 1550 | *
|
---|
| 1551 | * I.Seq.of(1,2,3).interleave(
|
---|
| 1552 | * I.Seq.of('A','B'),
|
---|
| 1553 | * I.Seq.of('X','Y','Z')
|
---|
| 1554 | * )
|
---|
| 1555 | * // Seq [ 1, 'A', 'X', 2, 'B', 'Y' ]
|
---|
| 1556 | */
|
---|
| 1557 | interleave(...iterables: Array<Iterable<any, T>>): /*this*/Iterable.Indexed<T>;
|
---|
| 1558 |
|
---|
| 1559 | /**
|
---|
| 1560 | * Splice returns a new indexed Iterable by replacing a region of this
|
---|
| 1561 | * Iterable with new values. If values are not provided, it only skips the
|
---|
| 1562 | * region to be removed.
|
---|
| 1563 | *
|
---|
| 1564 | * `index` may be a negative number, which indexes back from the end of the
|
---|
| 1565 | * Iterable. `s.splice(-2)` splices after the second to last item.
|
---|
| 1566 | *
|
---|
| 1567 | * Seq(['a','b','c','d']).splice(1, 2, 'q', 'r', 's')
|
---|
| 1568 | * // Seq ['a', 'q', 'r', 's', 'd']
|
---|
| 1569 | *
|
---|
| 1570 | */
|
---|
| 1571 | splice(
|
---|
| 1572 | index: number,
|
---|
| 1573 | removeNum: number,
|
---|
| 1574 | ...values: /*Array<Iterable.Indexed<T> | T>*/any[]
|
---|
| 1575 | ): /*this*/Iterable.Indexed<T>;
|
---|
| 1576 |
|
---|
| 1577 | /**
|
---|
| 1578 | * Returns an Iterable of the same type "zipped" with the provided
|
---|
| 1579 | * iterables.
|
---|
| 1580 | *
|
---|
| 1581 | * Like `zipWith`, but using the default `zipper`: creating an `Array`.
|
---|
| 1582 | *
|
---|
| 1583 | * var a = Seq.of(1, 2, 3);
|
---|
| 1584 | * var b = Seq.of(4, 5, 6);
|
---|
| 1585 | * var c = a.zip(b); // Seq [ [ 1, 4 ], [ 2, 5 ], [ 3, 6 ] ]
|
---|
| 1586 | *
|
---|
| 1587 | */
|
---|
| 1588 | zip(...iterables: Array<Iterable<any, any>>): /*this*/Iterable.Indexed<any>;
|
---|
| 1589 |
|
---|
| 1590 | /**
|
---|
| 1591 | * Returns an Iterable of the same type "zipped" with the provided
|
---|
| 1592 | * iterables by using a custom `zipper` function.
|
---|
| 1593 | *
|
---|
| 1594 | * var a = Seq.of(1, 2, 3);
|
---|
| 1595 | * var b = Seq.of(4, 5, 6);
|
---|
| 1596 | * var c = a.zipWith((a, b) => a + b, b); // Seq [ 5, 7, 9 ]
|
---|
| 1597 | *
|
---|
| 1598 | */
|
---|
| 1599 | zipWith<U, Z>(
|
---|
| 1600 | zipper: (value: T, otherValue: U) => Z,
|
---|
| 1601 | otherIterable: Iterable<any, U>
|
---|
| 1602 | ): Iterable.Indexed<Z>;
|
---|
| 1603 | zipWith<U, V, Z>(
|
---|
| 1604 | zipper: (value: T, otherValue: U, thirdValue: V) => Z,
|
---|
| 1605 | otherIterable: Iterable<any, U>,
|
---|
| 1606 | thirdIterable: Iterable<any, V>
|
---|
| 1607 | ): Iterable.Indexed<Z>;
|
---|
| 1608 | zipWith<Z>(
|
---|
| 1609 | zipper: (...any: Array<any>) => Z,
|
---|
| 1610 | ...iterables: Array<Iterable<any, any>>
|
---|
| 1611 | ): Iterable.Indexed<Z>;
|
---|
| 1612 |
|
---|
| 1613 |
|
---|
| 1614 | // Search for value
|
---|
| 1615 |
|
---|
| 1616 | /**
|
---|
| 1617 | * Returns the first index at which a given value can be found in the
|
---|
| 1618 | * Iterable, or -1 if it is not present.
|
---|
| 1619 | */
|
---|
| 1620 | indexOf(searchValue: T): number;
|
---|
| 1621 |
|
---|
| 1622 | /**
|
---|
| 1623 | * Returns the last index at which a given value can be found in the
|
---|
| 1624 | * Iterable, or -1 if it is not present.
|
---|
| 1625 | */
|
---|
| 1626 | lastIndexOf(searchValue: T): number;
|
---|
| 1627 |
|
---|
| 1628 | /**
|
---|
| 1629 | * Returns the first index in the Iterable where a value satisfies the
|
---|
| 1630 | * provided predicate function. Otherwise -1 is returned.
|
---|
| 1631 | */
|
---|
| 1632 | findIndex(
|
---|
| 1633 | predicate: (value?: T, index?: number, iter?: /*this*/Iterable.Indexed<T>) => boolean,
|
---|
| 1634 | context?: any
|
---|
| 1635 | ): number;
|
---|
| 1636 |
|
---|
| 1637 | /**
|
---|
| 1638 | * Returns the last index in the Iterable where a value satisfies the
|
---|
| 1639 | * provided predicate function. Otherwise -1 is returned.
|
---|
| 1640 | */
|
---|
| 1641 | findLastIndex(
|
---|
| 1642 | predicate: (value?: T, index?: number, iter?: /*this*/Iterable.Indexed<T>) => boolean,
|
---|
| 1643 | context?: any
|
---|
| 1644 | ): number;
|
---|
| 1645 | }
|
---|
| 1646 |
|
---|
| 1647 |
|
---|
| 1648 | /**
|
---|
| 1649 | * Set Iterables only represent values. They have no associated keys or
|
---|
| 1650 | * indices. Duplicate values are possible in Seq.Sets, however the
|
---|
| 1651 | * concrete `Set` does not allow duplicate values.
|
---|
| 1652 | *
|
---|
| 1653 | * Iterable methods on Iterable.Set such as `map` and `forEach` will provide
|
---|
| 1654 | * the value as both the first and second arguments to the provided function.
|
---|
| 1655 | *
|
---|
| 1656 | * var seq = Seq.Set.of('A', 'B', 'C');
|
---|
| 1657 | * assert.equal(seq.every((v, k) => v === k), true);
|
---|
| 1658 | *
|
---|
| 1659 | */
|
---|
| 1660 | export module Set {}
|
---|
| 1661 |
|
---|
| 1662 | /**
|
---|
| 1663 | * Similar to `Iterable()`, but always returns a Iterable.Set.
|
---|
| 1664 | */
|
---|
| 1665 | export function Set<T>(iter: Iterable.Set<T>): Iterable.Set<T>;
|
---|
| 1666 | export function Set<T>(iter: Iterable.Indexed<T>): Iterable.Set<T>;
|
---|
| 1667 | export function Set<K, V>(iter: Iterable.Keyed<K, V>): Iterable.Set</*[K,V]*/any>;
|
---|
| 1668 | export function Set<T>(array: Array<T>): Iterable.Set<T>;
|
---|
| 1669 | export function Set<T>(iterator: Iterator<T>): Iterable.Set<T>;
|
---|
| 1670 | export function Set<T>(iterable: /*Iterable<T>*/Object): Iterable.Set<T>;
|
---|
| 1671 |
|
---|
| 1672 | export interface Set<T> extends Iterable<T, T> {
|
---|
| 1673 |
|
---|
| 1674 | /**
|
---|
| 1675 | * Returns Seq.Set.
|
---|
| 1676 | * @override
|
---|
| 1677 | */
|
---|
| 1678 | toSeq(): Seq.Set<T>;
|
---|
| 1679 | }
|
---|
| 1680 |
|
---|
| 1681 | }
|
---|
| 1682 |
|
---|
| 1683 | /**
|
---|
| 1684 | * Creates an Iterable.
|
---|
| 1685 | *
|
---|
| 1686 | * The type of Iterable created is based on the input.
|
---|
| 1687 | *
|
---|
| 1688 | * * If an `Iterable`, that same `Iterable`.
|
---|
| 1689 | * * If an Array-like, an `Iterable.Indexed`.
|
---|
| 1690 | * * If an Object with an Iterator, an `Iterable.Indexed`.
|
---|
| 1691 | * * If an Iterator, an `Iterable.Indexed`.
|
---|
| 1692 | * * If an Object, an `Iterable.Keyed`.
|
---|
| 1693 | *
|
---|
| 1694 | * This methods forces the conversion of Objects and Strings to Iterables.
|
---|
| 1695 | * If you want to ensure that a Iterable of one item is returned, use
|
---|
| 1696 | * `Seq.of`.
|
---|
| 1697 | */
|
---|
| 1698 | export function Iterable<K, V>(iterable: Iterable<K, V>): Iterable<K, V>;
|
---|
| 1699 | export function Iterable<T>(array: Array<T>): Iterable.Indexed<T>;
|
---|
| 1700 | export function Iterable<V>(obj: {[key: string]: V}): Iterable.Keyed<string, V>;
|
---|
| 1701 | export function Iterable<T>(iterator: Iterator<T>): Iterable.Indexed<T>;
|
---|
| 1702 | export function Iterable<T>(iterable: /*ES6Iterable<T>*/Object): Iterable.Indexed<T>;
|
---|
| 1703 | export function Iterable<V>(value: V): Iterable.Indexed<V>;
|
---|
| 1704 |
|
---|
| 1705 | export interface Iterable<K, V> {
|
---|
| 1706 |
|
---|
| 1707 | // Value equality
|
---|
| 1708 |
|
---|
| 1709 | /**
|
---|
| 1710 | * True if this and the other Iterable have value equality, as defined
|
---|
| 1711 | * by `Immutable.is()`.
|
---|
| 1712 | *
|
---|
| 1713 | * Note: This is equivalent to `Immutable.is(this, other)`, but provided to
|
---|
| 1714 | * allow for chained expressions.
|
---|
| 1715 | */
|
---|
| 1716 | equals(other: Iterable<K, V>): boolean;
|
---|
| 1717 |
|
---|
| 1718 | /**
|
---|
| 1719 | * Computes and returns the hashed identity for this Iterable.
|
---|
| 1720 | *
|
---|
| 1721 | * The `hashCode` of an Iterable is used to determine potential equality,
|
---|
| 1722 | * and is used when adding this to a `Set` or as a key in a `Map`, enabling
|
---|
| 1723 | * lookup via a different instance.
|
---|
| 1724 | *
|
---|
| 1725 | * var a = List.of(1, 2, 3);
|
---|
| 1726 | * var b = List.of(1, 2, 3);
|
---|
| 1727 | * assert(a !== b); // different instances
|
---|
| 1728 | * var set = Set.of(a);
|
---|
| 1729 | * assert(set.has(b) === true);
|
---|
| 1730 | *
|
---|
| 1731 | * If two values have the same `hashCode`, they are [not guaranteed
|
---|
| 1732 | * to be equal][Hash Collision]. If two values have different `hashCode`s,
|
---|
| 1733 | * they must not be equal.
|
---|
| 1734 | *
|
---|
| 1735 | * [Hash Collision]: http://en.wikipedia.org/wiki/Collision_(computer_science)
|
---|
| 1736 | */
|
---|
| 1737 | hashCode(): number;
|
---|
| 1738 |
|
---|
| 1739 |
|
---|
| 1740 | // Reading values
|
---|
| 1741 |
|
---|
| 1742 | /**
|
---|
| 1743 | * Returns the value associated with the provided key, or notSetValue if
|
---|
| 1744 | * the Iterable does not contain this key.
|
---|
| 1745 | *
|
---|
| 1746 | * Note: it is possible a key may be associated with an `undefined` value,
|
---|
| 1747 | * so if `notSetValue` is not provided and this method returns `undefined`,
|
---|
| 1748 | * that does not guarantee the key was not found.
|
---|
| 1749 | */
|
---|
| 1750 | get(key: K, notSetValue?: V): V;
|
---|
| 1751 |
|
---|
| 1752 | /**
|
---|
| 1753 | * True if a key exists within this `Iterable`, using `Immutable.is` to determine equality
|
---|
| 1754 | */
|
---|
| 1755 | has(key: K): boolean;
|
---|
| 1756 |
|
---|
| 1757 | /**
|
---|
| 1758 | * True if a value exists within this `Iterable`, using `Immutable.is` to determine equality
|
---|
| 1759 | * @alias contains
|
---|
| 1760 | */
|
---|
| 1761 | includes(value: V): boolean;
|
---|
| 1762 | contains(value: V): boolean;
|
---|
| 1763 |
|
---|
| 1764 | /**
|
---|
| 1765 | * The first value in the Iterable.
|
---|
| 1766 | */
|
---|
| 1767 | first(): V;
|
---|
| 1768 |
|
---|
| 1769 | /**
|
---|
| 1770 | * The last value in the Iterable.
|
---|
| 1771 | */
|
---|
| 1772 | last(): V;
|
---|
| 1773 |
|
---|
| 1774 |
|
---|
| 1775 | // Reading deep values
|
---|
| 1776 |
|
---|
| 1777 | /**
|
---|
| 1778 | * Returns the value found by following a path of keys or indices through
|
---|
| 1779 | * nested Iterables.
|
---|
| 1780 | */
|
---|
| 1781 | getIn(searchKeyPath: Array<any>, notSetValue?: any): any;
|
---|
| 1782 | getIn(searchKeyPath: Iterable<any, any>, notSetValue?: any): any;
|
---|
| 1783 |
|
---|
| 1784 | /**
|
---|
| 1785 | * True if the result of following a path of keys or indices through nested
|
---|
| 1786 | * Iterables results in a set value.
|
---|
| 1787 | */
|
---|
| 1788 | hasIn(searchKeyPath: Array<any>): boolean;
|
---|
| 1789 | hasIn(searchKeyPath: Iterable<any, any>): boolean;
|
---|
| 1790 |
|
---|
| 1791 |
|
---|
| 1792 | // Conversion to JavaScript types
|
---|
| 1793 |
|
---|
| 1794 | /**
|
---|
| 1795 | * Deeply converts this Iterable to equivalent JS.
|
---|
| 1796 | *
|
---|
| 1797 | * `Iterable.Indexeds`, and `Iterable.Sets` become Arrays, while
|
---|
| 1798 | * `Iterable.Keyeds` become Objects.
|
---|
| 1799 | *
|
---|
| 1800 | * @alias toJSON
|
---|
| 1801 | */
|
---|
| 1802 | toJS(): any;
|
---|
| 1803 |
|
---|
| 1804 | /**
|
---|
| 1805 | * Shallowly converts this iterable to an Array, discarding keys.
|
---|
| 1806 | */
|
---|
| 1807 | toArray(): Array<V>;
|
---|
| 1808 |
|
---|
| 1809 | /**
|
---|
| 1810 | * Shallowly converts this Iterable to an Object.
|
---|
| 1811 | *
|
---|
| 1812 | * Throws if keys are not strings.
|
---|
| 1813 | */
|
---|
| 1814 | toObject(): { [key: string]: V };
|
---|
| 1815 |
|
---|
| 1816 |
|
---|
| 1817 | // Conversion to Collections
|
---|
| 1818 |
|
---|
| 1819 | /**
|
---|
| 1820 | * Converts this Iterable to a Map, Throws if keys are not hashable.
|
---|
| 1821 | *
|
---|
| 1822 | * Note: This is equivalent to `Map(this.toKeyedSeq())`, but provided
|
---|
| 1823 | * for convenience and to allow for chained expressions.
|
---|
| 1824 | */
|
---|
| 1825 | toMap(): Map<K, V>;
|
---|
| 1826 |
|
---|
| 1827 | /**
|
---|
| 1828 | * Converts this Iterable to a Map, maintaining the order of iteration.
|
---|
| 1829 | *
|
---|
| 1830 | * Note: This is equivalent to `OrderedMap(this.toKeyedSeq())`, but
|
---|
| 1831 | * provided for convenience and to allow for chained expressions.
|
---|
| 1832 | */
|
---|
| 1833 | toOrderedMap(): OrderedMap<K, V>;
|
---|
| 1834 |
|
---|
| 1835 | /**
|
---|
| 1836 | * Converts this Iterable to a Set, discarding keys. Throws if values
|
---|
| 1837 | * are not hashable.
|
---|
| 1838 | *
|
---|
| 1839 | * Note: This is equivalent to `Set(this)`, but provided to allow for
|
---|
| 1840 | * chained expressions.
|
---|
| 1841 | */
|
---|
| 1842 | toSet(): Set<V>;
|
---|
| 1843 |
|
---|
| 1844 | /**
|
---|
| 1845 | * Converts this Iterable to a Set, maintaining the order of iteration and
|
---|
| 1846 | * discarding keys.
|
---|
| 1847 | *
|
---|
| 1848 | * Note: This is equivalent to `OrderedSet(this.valueSeq())`, but provided
|
---|
| 1849 | * for convenience and to allow for chained expressions.
|
---|
| 1850 | */
|
---|
| 1851 | toOrderedSet(): OrderedSet<V>;
|
---|
| 1852 |
|
---|
| 1853 | /**
|
---|
| 1854 | * Converts this Iterable to a List, discarding keys.
|
---|
| 1855 | *
|
---|
| 1856 | * Note: This is equivalent to `List(this)`, but provided to allow
|
---|
| 1857 | * for chained expressions.
|
---|
| 1858 | */
|
---|
| 1859 | toList(): List<V>;
|
---|
| 1860 |
|
---|
| 1861 | /**
|
---|
| 1862 | * Converts this Iterable to a Stack, discarding keys. Throws if values
|
---|
| 1863 | * are not hashable.
|
---|
| 1864 | *
|
---|
| 1865 | * Note: This is equivalent to `Stack(this)`, but provided to allow for
|
---|
| 1866 | * chained expressions.
|
---|
| 1867 | */
|
---|
| 1868 | toStack(): Stack<V>;
|
---|
| 1869 |
|
---|
| 1870 |
|
---|
| 1871 | // Conversion to Seq
|
---|
| 1872 |
|
---|
| 1873 | /**
|
---|
| 1874 | * Converts this Iterable to a Seq of the same kind (indexed,
|
---|
| 1875 | * keyed, or set).
|
---|
| 1876 | */
|
---|
| 1877 | toSeq(): Seq<K, V>;
|
---|
| 1878 |
|
---|
| 1879 | /**
|
---|
| 1880 | * Returns a Seq.Keyed from this Iterable where indices are treated as keys.
|
---|
| 1881 | *
|
---|
| 1882 | * This is useful if you want to operate on an
|
---|
| 1883 | * Iterable.Indexed and preserve the [index, value] pairs.
|
---|
| 1884 | *
|
---|
| 1885 | * The returned Seq will have identical iteration order as
|
---|
| 1886 | * this Iterable.
|
---|
| 1887 | *
|
---|
| 1888 | * Example:
|
---|
| 1889 | *
|
---|
| 1890 | * var indexedSeq = Immutable.Seq.of('A', 'B', 'C');
|
---|
| 1891 | * indexedSeq.filter(v => v === 'B').toString() // Seq [ 'B' ]
|
---|
| 1892 | * var keyedSeq = indexedSeq.toKeyedSeq();
|
---|
| 1893 | * keyedSeq.filter(v => v === 'B').toString() // Seq { 1: 'B' }
|
---|
| 1894 | *
|
---|
| 1895 | */
|
---|
| 1896 | toKeyedSeq(): Seq.Keyed<K, V>;
|
---|
| 1897 |
|
---|
| 1898 | /**
|
---|
| 1899 | * Returns an Seq.Indexed of the values of this Iterable, discarding keys.
|
---|
| 1900 | */
|
---|
| 1901 | toIndexedSeq(): Seq.Indexed<V>;
|
---|
| 1902 |
|
---|
| 1903 | /**
|
---|
| 1904 | * Returns a Seq.Set of the values of this Iterable, discarding keys.
|
---|
| 1905 | */
|
---|
| 1906 | toSetSeq(): Seq.Set<V>;
|
---|
| 1907 |
|
---|
| 1908 |
|
---|
| 1909 | // Iterators
|
---|
| 1910 |
|
---|
| 1911 | /**
|
---|
| 1912 | * An iterator of this `Iterable`'s keys.
|
---|
| 1913 | *
|
---|
| 1914 | * Note: this will return an ES6 iterator which does not support Immutable JS sequence algorithms. Use `keySeq` instead, if this is what you want.
|
---|
| 1915 | */
|
---|
| 1916 | keys(): Iterator<K>;
|
---|
| 1917 |
|
---|
| 1918 | /**
|
---|
| 1919 | * An iterator of this `Iterable`'s values.
|
---|
| 1920 | *
|
---|
| 1921 | * Note: this will return an ES6 iterator which does not support Immutable JS sequence algorithms. Use `valueSeq` instead, if this is what you want.
|
---|
| 1922 | */
|
---|
| 1923 | values(): Iterator<V>;
|
---|
| 1924 |
|
---|
| 1925 | /**
|
---|
| 1926 | * An iterator of this `Iterable`'s entries as `[key, value]` tuples.
|
---|
| 1927 | *
|
---|
| 1928 | * Note: this will return an ES6 iterator which does not support Immutable JS sequence algorithms. Use `entrySeq` instead, if this is what you want.
|
---|
| 1929 | */
|
---|
| 1930 | entries(): Iterator</*[K, V]*/Array<any>>;
|
---|
| 1931 |
|
---|
| 1932 |
|
---|
| 1933 | // Iterables (Seq)
|
---|
| 1934 |
|
---|
| 1935 | /**
|
---|
| 1936 | * Returns a new Seq.Indexed of the keys of this Iterable,
|
---|
| 1937 | * discarding values.
|
---|
| 1938 | */
|
---|
| 1939 | keySeq(): Seq.Indexed<K>;
|
---|
| 1940 |
|
---|
| 1941 | /**
|
---|
| 1942 | * Returns an Seq.Indexed of the values of this Iterable, discarding keys.
|
---|
| 1943 | */
|
---|
| 1944 | valueSeq(): Seq.Indexed<V>;
|
---|
| 1945 |
|
---|
| 1946 | /**
|
---|
| 1947 | * Returns a new Seq.Indexed of [key, value] tuples.
|
---|
| 1948 | */
|
---|
| 1949 | entrySeq(): Seq.Indexed</*(K, V)*/Array<any>>;
|
---|
| 1950 |
|
---|
| 1951 |
|
---|
| 1952 | // Sequence algorithms
|
---|
| 1953 |
|
---|
| 1954 | /**
|
---|
| 1955 | * Returns a new Iterable of the same type with values passed through a
|
---|
| 1956 | * `mapper` function.
|
---|
| 1957 | *
|
---|
| 1958 | * Seq({ a: 1, b: 2 }).map(x => 10 * x)
|
---|
| 1959 | * // Seq { a: 10, b: 20 }
|
---|
| 1960 | *
|
---|
| 1961 | */
|
---|
| 1962 | map<M>(
|
---|
| 1963 | mapper: (value?: V, key?: K, iter?: /*this*/Iterable<K, V>) => M,
|
---|
| 1964 | context?: any
|
---|
| 1965 | ): /*this*/Iterable<K, M>;
|
---|
| 1966 |
|
---|
| 1967 | /**
|
---|
| 1968 | * Returns a new Iterable of the same type with only the entries for which
|
---|
| 1969 | * the `predicate` function returns true.
|
---|
| 1970 | *
|
---|
| 1971 | * Seq({a:1,b:2,c:3,d:4}).filter(x => x % 2 === 0)
|
---|
| 1972 | * // Seq { b: 2, d: 4 }
|
---|
| 1973 | *
|
---|
| 1974 | */
|
---|
| 1975 | filter(
|
---|
| 1976 | predicate: (value?: V, key?: K, iter?: /*this*/Iterable<K, V>) => boolean,
|
---|
| 1977 | context?: any
|
---|
| 1978 | ): /*this*/Iterable<K, V>;
|
---|
| 1979 |
|
---|
| 1980 | /**
|
---|
| 1981 | * Returns a new Iterable of the same type with only the entries for which
|
---|
| 1982 | * the `predicate` function returns false.
|
---|
| 1983 | *
|
---|
| 1984 | * Seq({a:1,b:2,c:3,d:4}).filterNot(x => x % 2 === 0)
|
---|
| 1985 | * // Seq { a: 1, c: 3 }
|
---|
| 1986 | *
|
---|
| 1987 | */
|
---|
| 1988 | filterNot(
|
---|
| 1989 | predicate: (value?: V, key?: K, iter?: /*this*/Iterable<K, V>) => boolean,
|
---|
| 1990 | context?: any
|
---|
| 1991 | ): /*this*/Iterable<K, V>;
|
---|
| 1992 |
|
---|
| 1993 | /**
|
---|
| 1994 | * Returns a new Iterable of the same type in reverse order.
|
---|
| 1995 | */
|
---|
| 1996 | reverse(): /*this*/Iterable<K, V>;
|
---|
| 1997 |
|
---|
| 1998 | /**
|
---|
| 1999 | * Returns a new Iterable of the same type which includes the same entries,
|
---|
| 2000 | * stably sorted by using a `comparator`.
|
---|
| 2001 | *
|
---|
| 2002 | * If a `comparator` is not provided, a default comparator uses `<` and `>`.
|
---|
| 2003 | *
|
---|
| 2004 | * `comparator(valueA, valueB)`:
|
---|
| 2005 | *
|
---|
| 2006 | * * Returns `0` if the elements should not be swapped.
|
---|
| 2007 | * * Returns `-1` (or any negative number) if `valueA` comes before `valueB`
|
---|
| 2008 | * * Returns `1` (or any positive number) if `valueA` comes after `valueB`
|
---|
| 2009 | * * Is pure, i.e. it must always return the same value for the same pair
|
---|
| 2010 | * of values.
|
---|
| 2011 | *
|
---|
| 2012 | * When sorting collections which have no defined order, their ordered
|
---|
| 2013 | * equivalents will be returned. e.g. `map.sort()` returns OrderedMap.
|
---|
| 2014 | */
|
---|
| 2015 | sort(comparator?: (valueA: V, valueB: V) => number): /*this*/Iterable<K, V>;
|
---|
| 2016 |
|
---|
| 2017 | /**
|
---|
| 2018 | * Like `sort`, but also accepts a `comparatorValueMapper` which allows for
|
---|
| 2019 | * sorting by more sophisticated means:
|
---|
| 2020 | *
|
---|
| 2021 | * hitters.sortBy(hitter => hitter.avgHits);
|
---|
| 2022 | *
|
---|
| 2023 | */
|
---|
| 2024 | sortBy<C>(
|
---|
| 2025 | comparatorValueMapper: (value?: V, key?: K, iter?: /*this*/Iterable<K, V>) => C,
|
---|
| 2026 | comparator?: (valueA: C, valueB: C) => number
|
---|
| 2027 | ): /*this*/Iterable<K, V>;
|
---|
| 2028 |
|
---|
| 2029 | /**
|
---|
| 2030 | * Returns a `Iterable.Keyed` of `Iterable.Keyeds`, grouped by the return
|
---|
| 2031 | * value of the `grouper` function.
|
---|
| 2032 | *
|
---|
| 2033 | * Note: This is always an eager operation.
|
---|
| 2034 | */
|
---|
| 2035 | groupBy<G>(
|
---|
| 2036 | grouper: (value?: V, key?: K, iter?: /*this*/Iterable<K, V>) => G,
|
---|
| 2037 | context?: any
|
---|
| 2038 | ): /*Map*/Seq.Keyed<G, /*this*/Iterable<K, V>>;
|
---|
| 2039 |
|
---|
| 2040 |
|
---|
| 2041 | // Side effects
|
---|
| 2042 |
|
---|
| 2043 | /**
|
---|
| 2044 | * The `sideEffect` is executed for every entry in the Iterable.
|
---|
| 2045 | *
|
---|
| 2046 | * Unlike `Array#forEach`, if any call of `sideEffect` returns
|
---|
| 2047 | * `false`, the iteration will stop. Returns the number of entries iterated
|
---|
| 2048 | * (including the last iteration which returned false).
|
---|
| 2049 | */
|
---|
| 2050 | forEach(
|
---|
| 2051 | sideEffect: (value?: V, key?: K, iter?: /*this*/Iterable<K, V>) => any,
|
---|
| 2052 | context?: any
|
---|
| 2053 | ): number;
|
---|
| 2054 |
|
---|
| 2055 |
|
---|
| 2056 | // Creating subsets
|
---|
| 2057 |
|
---|
| 2058 | /**
|
---|
| 2059 | * Returns a new Iterable of the same type representing a portion of this
|
---|
| 2060 | * Iterable from start up to but not including end.
|
---|
| 2061 | *
|
---|
| 2062 | * If begin is negative, it is offset from the end of the Iterable. e.g.
|
---|
| 2063 | * `slice(-2)` returns a Iterable of the last two entries. If it is not
|
---|
| 2064 | * provided the new Iterable will begin at the beginning of this Iterable.
|
---|
| 2065 | *
|
---|
| 2066 | * If end is negative, it is offset from the end of the Iterable. e.g.
|
---|
| 2067 | * `slice(0, -1)` returns an Iterable of everything but the last entry. If
|
---|
| 2068 | * it is not provided, the new Iterable will continue through the end of
|
---|
| 2069 | * this Iterable.
|
---|
| 2070 | *
|
---|
| 2071 | * If the requested slice is equivalent to the current Iterable, then it
|
---|
| 2072 | * will return itself.
|
---|
| 2073 | */
|
---|
| 2074 | slice(begin?: number, end?: number): /*this*/Iterable<K, V>;
|
---|
| 2075 |
|
---|
| 2076 | /**
|
---|
| 2077 | * Returns a new Iterable of the same type containing all entries except
|
---|
| 2078 | * the first.
|
---|
| 2079 | */
|
---|
| 2080 | rest(): /*this*/Iterable<K, V>;
|
---|
| 2081 |
|
---|
| 2082 | /**
|
---|
| 2083 | * Returns a new Iterable of the same type containing all entries except
|
---|
| 2084 | * the last.
|
---|
| 2085 | */
|
---|
| 2086 | butLast(): /*this*/Iterable<K, V>;
|
---|
| 2087 |
|
---|
| 2088 | /**
|
---|
| 2089 | * Returns a new Iterable of the same type which excludes the first `amount`
|
---|
| 2090 | * entries from this Iterable.
|
---|
| 2091 | */
|
---|
| 2092 | skip(amount: number): /*this*/Iterable<K, V>;
|
---|
| 2093 |
|
---|
| 2094 | /**
|
---|
| 2095 | * Returns a new Iterable of the same type which excludes the last `amount`
|
---|
| 2096 | * entries from this Iterable.
|
---|
| 2097 | */
|
---|
| 2098 | skipLast(amount: number): /*this*/Iterable<K, V>;
|
---|
| 2099 |
|
---|
| 2100 | /**
|
---|
| 2101 | * Returns a new Iterable of the same type which includes entries starting
|
---|
| 2102 | * from when `predicate` first returns false.
|
---|
| 2103 | *
|
---|
| 2104 | * Seq.of('dog','frog','cat','hat','god')
|
---|
| 2105 | * .skipWhile(x => x.match(/g/))
|
---|
| 2106 | * // Seq [ 'cat', 'hat', 'god' ]
|
---|
| 2107 | *
|
---|
| 2108 | */
|
---|
| 2109 | skipWhile(
|
---|
| 2110 | predicate: (value?: V, key?: K, iter?: /*this*/Iterable<K, V>) => boolean,
|
---|
| 2111 | context?: any
|
---|
| 2112 | ): /*this*/Iterable<K, V>;
|
---|
| 2113 |
|
---|
| 2114 | /**
|
---|
| 2115 | * Returns a new Iterable of the same type which includes entries starting
|
---|
| 2116 | * from when `predicate` first returns true.
|
---|
| 2117 | *
|
---|
| 2118 | * Seq.of('dog','frog','cat','hat','god')
|
---|
| 2119 | * .skipUntil(x => x.match(/hat/))
|
---|
| 2120 | * // Seq [ 'hat', 'god' ]
|
---|
| 2121 | *
|
---|
| 2122 | */
|
---|
| 2123 | skipUntil(
|
---|
| 2124 | predicate: (value?: V, key?: K, iter?: /*this*/Iterable<K, V>) => boolean,
|
---|
| 2125 | context?: any
|
---|
| 2126 | ): /*this*/Iterable<K, V>;
|
---|
| 2127 |
|
---|
| 2128 | /**
|
---|
| 2129 | * Returns a new Iterable of the same type which includes the first `amount`
|
---|
| 2130 | * entries from this Iterable.
|
---|
| 2131 | */
|
---|
| 2132 | take(amount: number): /*this*/Iterable<K, V>;
|
---|
| 2133 |
|
---|
| 2134 | /**
|
---|
| 2135 | * Returns a new Iterable of the same type which includes the last `amount`
|
---|
| 2136 | * entries from this Iterable.
|
---|
| 2137 | */
|
---|
| 2138 | takeLast(amount: number): /*this*/Iterable<K, V>;
|
---|
| 2139 |
|
---|
| 2140 | /**
|
---|
| 2141 | * Returns a new Iterable of the same type which includes entries from this
|
---|
| 2142 | * Iterable as long as the `predicate` returns true.
|
---|
| 2143 | *
|
---|
| 2144 | * Seq.of('dog','frog','cat','hat','god')
|
---|
| 2145 | * .takeWhile(x => x.match(/o/))
|
---|
| 2146 | * // Seq [ 'dog', 'frog' ]
|
---|
| 2147 | *
|
---|
| 2148 | */
|
---|
| 2149 | takeWhile(
|
---|
| 2150 | predicate: (value?: V, key?: K, iter?: /*this*/Iterable<K, V>) => boolean,
|
---|
| 2151 | context?: any
|
---|
| 2152 | ): /*this*/Iterable<K, V>;
|
---|
| 2153 |
|
---|
| 2154 | /**
|
---|
| 2155 | * Returns a new Iterable of the same type which includes entries from this
|
---|
| 2156 | * Iterable as long as the `predicate` returns false.
|
---|
| 2157 | *
|
---|
| 2158 | * Seq.of('dog','frog','cat','hat','god').takeUntil(x => x.match(/at/))
|
---|
| 2159 | * // ['dog', 'frog']
|
---|
| 2160 | *
|
---|
| 2161 | */
|
---|
| 2162 | takeUntil(
|
---|
| 2163 | predicate: (value?: V, key?: K, iter?: /*this*/Iterable<K, V>) => boolean,
|
---|
| 2164 | context?: any
|
---|
| 2165 | ): /*this*/Iterable<K, V>;
|
---|
| 2166 |
|
---|
| 2167 |
|
---|
| 2168 | // Combination
|
---|
| 2169 |
|
---|
| 2170 | /**
|
---|
| 2171 | * Returns a new Iterable of the same type with other values and
|
---|
| 2172 | * iterable-like concatenated to this one.
|
---|
| 2173 | *
|
---|
| 2174 | * For Seqs, all entries will be present in
|
---|
| 2175 | * the resulting iterable, even if they have the same key.
|
---|
| 2176 | */
|
---|
| 2177 | concat(...valuesOrIterables: /*Array<Iterable<K, V>|V*/any[]): /*this*/Iterable<K, V>;
|
---|
| 2178 |
|
---|
| 2179 | /**
|
---|
| 2180 | * Flattens nested Iterables.
|
---|
| 2181 | *
|
---|
| 2182 | * Will deeply flatten the Iterable by default, returning an Iterable of the
|
---|
| 2183 | * same type, but a `depth` can be provided in the form of a number or
|
---|
| 2184 | * boolean (where true means to shallowly flatten one level). A depth of 0
|
---|
| 2185 | * (or shallow: false) will deeply flatten.
|
---|
| 2186 | *
|
---|
| 2187 | * Flattens only others Iterable, not Arrays or Objects.
|
---|
| 2188 | *
|
---|
| 2189 | * Note: `flatten(true)` operates on Iterable<any, Iterable<K, V>> and
|
---|
| 2190 | * returns Iterable<K, V>
|
---|
| 2191 | */
|
---|
| 2192 | flatten(depth?: number): /*this*/Iterable<any, any>;
|
---|
| 2193 | flatten(shallow?: boolean): /*this*/Iterable<any, any>;
|
---|
| 2194 |
|
---|
| 2195 | /**
|
---|
| 2196 | * Flat-maps the Iterable, returning an Iterable of the same type.
|
---|
| 2197 | *
|
---|
| 2198 | * Similar to `iter.map(...).flatten(true)`.
|
---|
| 2199 | */
|
---|
| 2200 | flatMap<MK, MV>(
|
---|
| 2201 | mapper: (value?: V, key?: K, iter?: /*this*/Iterable<K, V>) => Iterable<MK, MV>,
|
---|
| 2202 | context?: any
|
---|
| 2203 | ): /*this*/Iterable<MK, MV>;
|
---|
| 2204 | flatMap<MK, MV>(
|
---|
| 2205 | mapper: (value?: V, key?: K, iter?: /*this*/Iterable<K, V>) => /*iterable-like*/any,
|
---|
| 2206 | context?: any
|
---|
| 2207 | ): /*this*/Iterable<MK, MV>;
|
---|
| 2208 |
|
---|
| 2209 |
|
---|
| 2210 | // Reducing a value
|
---|
| 2211 |
|
---|
| 2212 | /**
|
---|
| 2213 | * Reduces the Iterable to a value by calling the `reducer` for every entry
|
---|
| 2214 | * in the Iterable and passing along the reduced value.
|
---|
| 2215 | *
|
---|
| 2216 | * If `initialReduction` is not provided, or is null, the first item in the
|
---|
| 2217 | * Iterable will be used.
|
---|
| 2218 | *
|
---|
| 2219 | * @see `Array#reduce`.
|
---|
| 2220 | */
|
---|
| 2221 | reduce<R>(
|
---|
| 2222 | reducer: (reduction?: R, value?: V, key?: K, iter?: /*this*/Iterable<K, V>) => R,
|
---|
| 2223 | initialReduction?: R,
|
---|
| 2224 | context?: any
|
---|
| 2225 | ): R;
|
---|
| 2226 |
|
---|
| 2227 | /**
|
---|
| 2228 | * Reduces the Iterable in reverse (from the right side).
|
---|
| 2229 | *
|
---|
| 2230 | * Note: Similar to this.reverse().reduce(), and provided for parity
|
---|
| 2231 | * with `Array#reduceRight`.
|
---|
| 2232 | */
|
---|
| 2233 | reduceRight<R>(
|
---|
| 2234 | reducer: (reduction?: R, value?: V, key?: K, iter?: /*this*/Iterable<K, V>) => R,
|
---|
| 2235 | initialReduction?: R,
|
---|
| 2236 | context?: any
|
---|
| 2237 | ): R;
|
---|
| 2238 |
|
---|
| 2239 | /**
|
---|
| 2240 | * True if `predicate` returns true for all entries in the Iterable.
|
---|
| 2241 | */
|
---|
| 2242 | every(
|
---|
| 2243 | predicate: (value?: V, key?: K, iter?: /*this*/Iterable<K, V>) => boolean,
|
---|
| 2244 | context?: any
|
---|
| 2245 | ): boolean;
|
---|
| 2246 |
|
---|
| 2247 | /**
|
---|
| 2248 | * True if `predicate` returns true for any entry in the Iterable.
|
---|
| 2249 | */
|
---|
| 2250 | some(
|
---|
| 2251 | predicate: (value?: V, key?: K, iter?: /*this*/Iterable<K, V>) => boolean,
|
---|
| 2252 | context?: any
|
---|
| 2253 | ): boolean;
|
---|
| 2254 |
|
---|
| 2255 | /**
|
---|
| 2256 | * Joins values together as a string, inserting a separator between each.
|
---|
| 2257 | * The default separator is `","`.
|
---|
| 2258 | */
|
---|
| 2259 | join(separator?: string): string;
|
---|
| 2260 |
|
---|
| 2261 | /**
|
---|
| 2262 | * Returns true if this Iterable includes no values.
|
---|
| 2263 | *
|
---|
| 2264 | * For some lazy `Seq`, `isEmpty` might need to iterate to determine
|
---|
| 2265 | * emptiness. At most one iteration will occur.
|
---|
| 2266 | */
|
---|
| 2267 | isEmpty(): boolean;
|
---|
| 2268 |
|
---|
| 2269 | /**
|
---|
| 2270 | * Returns the size of this Iterable.
|
---|
| 2271 | *
|
---|
| 2272 | * Regardless of if this Iterable can describe its size lazily (some Seqs
|
---|
| 2273 | * cannot), this method will always return the correct size. E.g. it
|
---|
| 2274 | * evaluates a lazy `Seq` if necessary.
|
---|
| 2275 | *
|
---|
| 2276 | * If `predicate` is provided, then this returns the count of entries in the
|
---|
| 2277 | * Iterable for which the `predicate` returns true.
|
---|
| 2278 | */
|
---|
| 2279 | count(): number;
|
---|
| 2280 | count(
|
---|
| 2281 | predicate: (value?: V, key?: K, iter?: /*this*/Iterable<K, V>) => boolean,
|
---|
| 2282 | context?: any
|
---|
| 2283 | ): number;
|
---|
| 2284 |
|
---|
| 2285 | /**
|
---|
| 2286 | * Returns a `Seq.Keyed` of counts, grouped by the return value of
|
---|
| 2287 | * the `grouper` function.
|
---|
| 2288 | *
|
---|
| 2289 | * Note: This is not a lazy operation.
|
---|
| 2290 | */
|
---|
| 2291 | countBy<G>(
|
---|
| 2292 | grouper: (value?: V, key?: K, iter?: /*this*/Iterable<K, V>) => G,
|
---|
| 2293 | context?: any
|
---|
| 2294 | ): Map<G, number>;
|
---|
| 2295 |
|
---|
| 2296 |
|
---|
| 2297 | // Search for value
|
---|
| 2298 |
|
---|
| 2299 | /**
|
---|
| 2300 | * Returns the first value for which the `predicate` returns true.
|
---|
| 2301 | */
|
---|
| 2302 | find(
|
---|
| 2303 | predicate: (value?: V, key?: K, iter?: /*this*/Iterable<K, V>) => boolean,
|
---|
| 2304 | context?: any,
|
---|
| 2305 | notSetValue?: V
|
---|
| 2306 | ): V;
|
---|
| 2307 |
|
---|
| 2308 | /**
|
---|
| 2309 | * Returns the last value for which the `predicate` returns true.
|
---|
| 2310 | *
|
---|
| 2311 | * Note: `predicate` will be called for each entry in reverse.
|
---|
| 2312 | */
|
---|
| 2313 | findLast(
|
---|
| 2314 | predicate: (value?: V, key?: K, iter?: /*this*/Iterable<K, V>) => boolean,
|
---|
| 2315 | context?: any,
|
---|
| 2316 | notSetValue?: V
|
---|
| 2317 | ): V;
|
---|
| 2318 |
|
---|
| 2319 | /**
|
---|
| 2320 | * Returns the first [key, value] entry for which the `predicate` returns true.
|
---|
| 2321 | */
|
---|
| 2322 | findEntry(
|
---|
| 2323 | predicate: (value?: V, key?: K, iter?: /*this*/Iterable<K, V>) => boolean,
|
---|
| 2324 | context?: any,
|
---|
| 2325 | notSetValue?: V
|
---|
| 2326 | ): /*[K, V]*/Array<any>;
|
---|
| 2327 |
|
---|
| 2328 | /**
|
---|
| 2329 | * Returns the last [key, value] entry for which the `predicate`
|
---|
| 2330 | * returns true.
|
---|
| 2331 | *
|
---|
| 2332 | * Note: `predicate` will be called for each entry in reverse.
|
---|
| 2333 | */
|
---|
| 2334 | findLastEntry(
|
---|
| 2335 | predicate: (value?: V, key?: K, iter?: /*this*/Iterable<K, V>) => boolean,
|
---|
| 2336 | context?: any,
|
---|
| 2337 | notSetValue?: V
|
---|
| 2338 | ): /*[K, V]*/Array<any>;
|
---|
| 2339 |
|
---|
| 2340 | /**
|
---|
| 2341 | * Returns the key for which the `predicate` returns true.
|
---|
| 2342 | */
|
---|
| 2343 | findKey(
|
---|
| 2344 | predicate: (value?: V, key?: K, iter?: /*this*/Iterable.Keyed<K, V>) => boolean,
|
---|
| 2345 | context?: any
|
---|
| 2346 | ): K;
|
---|
| 2347 |
|
---|
| 2348 | /**
|
---|
| 2349 | * Returns the last key for which the `predicate` returns true.
|
---|
| 2350 | *
|
---|
| 2351 | * Note: `predicate` will be called for each entry in reverse.
|
---|
| 2352 | */
|
---|
| 2353 | findLastKey(
|
---|
| 2354 | predicate: (value?: V, key?: K, iter?: /*this*/Iterable.Keyed<K, V>) => boolean,
|
---|
| 2355 | context?: any
|
---|
| 2356 | ): K;
|
---|
| 2357 |
|
---|
| 2358 | /**
|
---|
| 2359 | * Returns the key associated with the search value, or undefined.
|
---|
| 2360 | */
|
---|
| 2361 | keyOf(searchValue: V): K;
|
---|
| 2362 |
|
---|
| 2363 | /**
|
---|
| 2364 | * Returns the last key associated with the search value, or undefined.
|
---|
| 2365 | */
|
---|
| 2366 | lastKeyOf(searchValue: V): K;
|
---|
| 2367 |
|
---|
| 2368 | /**
|
---|
| 2369 | * Returns the maximum value in this collection. If any values are
|
---|
| 2370 | * comparatively equivalent, the first one found will be returned.
|
---|
| 2371 | *
|
---|
| 2372 | * The `comparator` is used in the same way as `Iterable#sort`. If it is not
|
---|
| 2373 | * provided, the default comparator is `>`.
|
---|
| 2374 | *
|
---|
| 2375 | * When two values are considered equivalent, the first encountered will be
|
---|
| 2376 | * returned. Otherwise, `max` will operate independent of the order of input
|
---|
| 2377 | * as long as the comparator is commutative. The default comparator `>` is
|
---|
| 2378 | * commutative *only* when types do not differ.
|
---|
| 2379 | *
|
---|
| 2380 | * If `comparator` returns 0 and either value is NaN, undefined, or null,
|
---|
| 2381 | * that value will be returned.
|
---|
| 2382 | */
|
---|
| 2383 | max(comparator?: (valueA: V, valueB: V) => number): V;
|
---|
| 2384 |
|
---|
| 2385 | /**
|
---|
| 2386 | * Like `max`, but also accepts a `comparatorValueMapper` which allows for
|
---|
| 2387 | * comparing by more sophisticated means:
|
---|
| 2388 | *
|
---|
| 2389 | * hitters.maxBy(hitter => hitter.avgHits);
|
---|
| 2390 | *
|
---|
| 2391 | */
|
---|
| 2392 | maxBy<C>(
|
---|
| 2393 | comparatorValueMapper: (value?: V, key?: K, iter?: /*this*/Iterable<K, V>) => C,
|
---|
| 2394 | comparator?: (valueA: C, valueB: C) => number
|
---|
| 2395 | ): V;
|
---|
| 2396 |
|
---|
| 2397 | /**
|
---|
| 2398 | * Returns the minimum value in this collection. If any values are
|
---|
| 2399 | * comparatively equivalent, the first one found will be returned.
|
---|
| 2400 | *
|
---|
| 2401 | * The `comparator` is used in the same way as `Iterable#sort`. If it is not
|
---|
| 2402 | * provided, the default comparator is `<`.
|
---|
| 2403 | *
|
---|
| 2404 | * When two values are considered equivalent, the first encountered will be
|
---|
| 2405 | * returned. Otherwise, `min` will operate independent of the order of input
|
---|
| 2406 | * as long as the comparator is commutative. The default comparator `<` is
|
---|
| 2407 | * commutative *only* when types do not differ.
|
---|
| 2408 | *
|
---|
| 2409 | * If `comparator` returns 0 and either value is NaN, undefined, or null,
|
---|
| 2410 | * that value will be returned.
|
---|
| 2411 | */
|
---|
| 2412 | min(comparator?: (valueA: V, valueB: V) => number): V;
|
---|
| 2413 |
|
---|
| 2414 | /**
|
---|
| 2415 | * Like `min`, but also accepts a `comparatorValueMapper` which allows for
|
---|
| 2416 | * comparing by more sophisticated means:
|
---|
| 2417 | *
|
---|
| 2418 | * hitters.minBy(hitter => hitter.avgHits);
|
---|
| 2419 | *
|
---|
| 2420 | */
|
---|
| 2421 | minBy<C>(
|
---|
| 2422 | comparatorValueMapper: (value?: V, key?: K, iter?: /*this*/Iterable<K, V>) => C,
|
---|
| 2423 | comparator?: (valueA: C, valueB: C) => number
|
---|
| 2424 | ): V;
|
---|
| 2425 |
|
---|
| 2426 |
|
---|
| 2427 | // Comparison
|
---|
| 2428 |
|
---|
| 2429 | /**
|
---|
| 2430 | * True if `iter` includes every value in this Iterable.
|
---|
| 2431 | */
|
---|
| 2432 | isSubset(iter: Iterable<any, V>): boolean;
|
---|
| 2433 | isSubset(iter: Array<V>): boolean;
|
---|
| 2434 |
|
---|
| 2435 | /**
|
---|
| 2436 | * True if this Iterable includes every value in `iter`.
|
---|
| 2437 | */
|
---|
| 2438 | isSuperset(iter: Iterable<any, V>): boolean;
|
---|
| 2439 | isSuperset(iter: Array<V>): boolean;
|
---|
| 2440 |
|
---|
| 2441 |
|
---|
| 2442 | /**
|
---|
| 2443 | * Note: this is here as a convenience to work around an issue with
|
---|
| 2444 | * TypeScript https://github.com/Microsoft/TypeScript/issues/285, but
|
---|
| 2445 | * Iterable does not define `size`, instead `Seq` defines `size` as
|
---|
| 2446 | * nullable number, and `Collection` defines `size` as always a number.
|
---|
| 2447 | *
|
---|
| 2448 | * @ignore
|
---|
| 2449 | */
|
---|
| 2450 | size: number;
|
---|
| 2451 | }
|
---|
| 2452 |
|
---|
| 2453 |
|
---|
| 2454 | /**
|
---|
| 2455 | * Collection is the abstract base class for concrete data structures. It
|
---|
| 2456 | * cannot be constructed directly.
|
---|
| 2457 | *
|
---|
| 2458 | * Implementations should extend one of the subclasses, `Collection.Keyed`,
|
---|
| 2459 | * `Collection.Indexed`, or `Collection.Set`.
|
---|
| 2460 | */
|
---|
| 2461 | export module Collection {
|
---|
| 2462 |
|
---|
| 2463 |
|
---|
| 2464 | /**
|
---|
| 2465 | * `Collection` which represents key-value pairs.
|
---|
| 2466 | */
|
---|
| 2467 | export module Keyed {}
|
---|
| 2468 |
|
---|
| 2469 | export interface Keyed<K, V> extends Collection<K, V>, Iterable.Keyed<K, V> {
|
---|
| 2470 |
|
---|
| 2471 | /**
|
---|
| 2472 | * Returns Seq.Keyed.
|
---|
| 2473 | * @override
|
---|
| 2474 | */
|
---|
| 2475 | toSeq(): Seq.Keyed<K, V>;
|
---|
| 2476 | }
|
---|
| 2477 |
|
---|
| 2478 |
|
---|
| 2479 | /**
|
---|
| 2480 | * `Collection` which represents ordered indexed values.
|
---|
| 2481 | */
|
---|
| 2482 | export module Indexed {}
|
---|
| 2483 |
|
---|
| 2484 | export interface Indexed<T> extends Collection<number, T>, Iterable.Indexed<T> {
|
---|
| 2485 |
|
---|
| 2486 | /**
|
---|
| 2487 | * Returns Seq.Indexed.
|
---|
| 2488 | * @override
|
---|
| 2489 | */
|
---|
| 2490 | toSeq(): Seq.Indexed<T>;
|
---|
| 2491 | }
|
---|
| 2492 |
|
---|
| 2493 |
|
---|
| 2494 | /**
|
---|
| 2495 | * `Collection` which represents values, unassociated with keys or indices.
|
---|
| 2496 | *
|
---|
| 2497 | * `Collection.Set` implementations should guarantee value uniqueness.
|
---|
| 2498 | */
|
---|
| 2499 | export module Set {}
|
---|
| 2500 |
|
---|
| 2501 | export interface Set<T> extends Collection<T, T>, Iterable.Set<T> {
|
---|
| 2502 |
|
---|
| 2503 | /**
|
---|
| 2504 | * Returns Seq.Set.
|
---|
| 2505 | * @override
|
---|
| 2506 | */
|
---|
| 2507 | toSeq(): Seq.Set<T>;
|
---|
| 2508 | }
|
---|
| 2509 |
|
---|
| 2510 | }
|
---|
| 2511 |
|
---|
| 2512 | export interface Collection<K, V> extends Iterable<K, V> {
|
---|
| 2513 |
|
---|
| 2514 | /**
|
---|
| 2515 | * All collections maintain their current `size` as an integer.
|
---|
| 2516 | */
|
---|
| 2517 | size: number;
|
---|
| 2518 | }
|
---|
| 2519 |
|
---|
| 2520 |
|
---|
| 2521 | /**
|
---|
| 2522 | * ES6 Iterator.
|
---|
| 2523 | *
|
---|
| 2524 | * This is not part of the Immutable library, but a common interface used by
|
---|
| 2525 | * many types in ES6 JavaScript.
|
---|
| 2526 | *
|
---|
| 2527 | * @ignore
|
---|
| 2528 | */
|
---|
| 2529 | export interface Iterator<T> {
|
---|
| 2530 | next(): { value: T; done: boolean; }
|
---|
| 2531 | }
|
---|
| 2532 |
|
---|
| 2533 |
|
---|