Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

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