immutable.d.ts 80 KB

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