immutable-nonambient.d.ts 80 KB

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