immutable-nonambient.d.ts 170 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337
  1. /**
  2. * Copyright (c) 2014-present, Facebook, Inc.
  3. *
  4. * This source code is licensed under the MIT license found in the
  5. * LICENSE file in the root directory of this source tree.
  6. */
  7. /**
  8. * Immutable data encourages pure functions (data-in, data-out) and lends itself
  9. * to much simpler application development and enabling techniques from
  10. * functional programming such as lazy evaluation.
  11. *
  12. * While designed to bring these powerful functional concepts to JavaScript, it
  13. * presents an Object-Oriented API familiar to Javascript engineers and closely
  14. * mirroring that of Array, Map, and Set. It is easy and efficient to convert to
  15. * and from plain Javascript types.
  16. *
  17. * ## How to read these docs
  18. *
  19. * In order to better explain what kinds of values the Immutable.js API expects
  20. * and produces, this documentation is presented in a statically typed dialect of
  21. * JavaScript (like [Flow][] or [TypeScript][]). You *don't need* to use these
  22. * type checking tools in order to use Immutable.js, however becoming familiar
  23. * with their syntax will help you get a deeper understanding of this API.
  24. *
  25. * **A few examples and how to read them.**
  26. *
  27. * All methods describe the kinds of data they accept and the kinds of data
  28. * they return. For example a function which accepts two numbers and returns
  29. * a number would look like this:
  30. *
  31. * ```js
  32. * sum(first: number, second: number): number
  33. * ```
  34. *
  35. * Sometimes, methods can accept different kinds of data or return different
  36. * kinds of data, and this is described with a *type variable*, which is
  37. * typically in all-caps. For example, a function which always returns the same
  38. * kind of data it was provided would look like this:
  39. *
  40. * ```js
  41. * identity<T>(value: T): T
  42. * ```
  43. *
  44. * Type variables are defined with classes and referred to in methods. For
  45. * example, a class that holds onto a value for you might look like this:
  46. *
  47. * ```js
  48. * class Box<T> {
  49. * constructor(value: T)
  50. * getValue(): T
  51. * }
  52. * ```
  53. *
  54. * In order to manipulate Immutable data, methods that we're used to affecting
  55. * a Collection instead return a new Collection of the same type. The type
  56. * `this` refers to the same kind of class. For example, a List which returns
  57. * new Lists when you `push` a value onto it might look like:
  58. *
  59. * ```js
  60. * class List<T> {
  61. * push(value: T): this
  62. * }
  63. * ```
  64. *
  65. * Many methods in Immutable.js accept values which implement the JavaScript
  66. * [Iterable][] protocol, and might appear like `Iterable<string>` for something
  67. * which represents sequence of strings. Typically in JavaScript we use plain
  68. * Arrays (`[]`) when an Iterable is expected, but also all of the Immutable.js
  69. * collections are iterable themselves!
  70. *
  71. * For example, to get a value deep within a structure of data, we might use
  72. * `getIn` which expects an `Iterable` path:
  73. *
  74. * ```
  75. * getIn(path: Iterable<string | number>): any
  76. * ```
  77. *
  78. * To use this method, we could pass an array: `data.getIn([ "key", 2 ])`.
  79. *
  80. *
  81. * Note: All examples are presented in the modern [ES2015][] version of
  82. * JavaScript. Use tools like Babel to support older browsers.
  83. *
  84. * For example:
  85. *
  86. * ```js
  87. * // ES2015
  88. * const mappedFoo = foo.map(x => x * x);
  89. * // ES5
  90. * var mappedFoo = foo.map(function (x) { return x * x; });
  91. * ```
  92. *
  93. * [ES2015]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/New_in_JavaScript/ECMAScript_6_support_in_Mozilla
  94. * [TypeScript]: http://www.typescriptlang.org/
  95. * [Flow]: https://flowtype.org/
  96. * [Iterable]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Iteration_protocols
  97. */
  98. /**
  99. * Lists are ordered indexed dense collections, much like a JavaScript
  100. * Array.
  101. *
  102. * Lists are immutable and fully persistent with O(log32 N) gets and sets,
  103. * and O(1) push and pop.
  104. *
  105. * Lists implement Deque, with efficient addition and removal from both the
  106. * end (`push`, `pop`) and beginning (`unshift`, `shift`).
  107. *
  108. * Unlike a JavaScript Array, there is no distinction between an
  109. * "unset" index and an index set to `undefined`. `List#forEach` visits all
  110. * indices from 0 to size, regardless of whether they were explicitly defined.
  111. */
  112. export module List {
  113. /**
  114. * True if the provided value is a List
  115. *
  116. * <!-- runkit:activate -->
  117. * ```js
  118. * const { List } = require('immutable');
  119. * List.isList([]); // false
  120. * List.isList(List()); // true
  121. * ```
  122. */
  123. function isList(maybeList: any): maybeList is List<any>;
  124. /**
  125. * Creates a new List containing `values`.
  126. *
  127. * <!-- runkit:activate -->
  128. * ```js
  129. * const { List } = require('immutable');
  130. * List.of(1, 2, 3, 4)
  131. * // List [ 1, 2, 3, 4 ]
  132. * ```
  133. *
  134. * Note: Values are not altered or converted in any way.
  135. *
  136. * <!-- runkit:activate -->
  137. * ```js
  138. * const { List } = require('immutable');
  139. * List.of({x:1}, 2, [3], 4)
  140. * // List [ { x: 1 }, 2, [ 3 ], 4 ]
  141. * ```
  142. */
  143. function of<T>(...values: Array<T>): List<T>;
  144. }
  145. /**
  146. * Create a new immutable List containing the values of the provided
  147. * collection-like.
  148. *
  149. * Note: `List` is a factory function and not a class, and does not use the
  150. * `new` keyword during construction.
  151. *
  152. * <!-- runkit:activate -->
  153. * ```js
  154. * const { List, Set } = require('immutable')
  155. *
  156. * const emptyList = List()
  157. * // List []
  158. *
  159. * const plainArray = [ 1, 2, 3, 4 ]
  160. * const listFromPlainArray = List(plainArray)
  161. * // List [ 1, 2, 3, 4 ]
  162. *
  163. * const plainSet = Set([ 1, 2, 3, 4 ])
  164. * const listFromPlainSet = List(plainSet)
  165. * // List [ 1, 2, 3, 4 ]
  166. *
  167. * const arrayIterator = plainArray[Symbol.iterator]()
  168. * const listFromCollectionArray = List(arrayIterator)
  169. * // List [ 1, 2, 3, 4 ]
  170. *
  171. * listFromPlainArray.equals(listFromCollectionArray) // true
  172. * listFromPlainSet.equals(listFromCollectionArray) // true
  173. * listFromPlainSet.equals(listFromPlainArray) // true
  174. * ```
  175. */
  176. export function List(): List<any>;
  177. export function List<T>(): List<T>;
  178. export function List<T>(collection: Iterable<T>): List<T>;
  179. export interface List<T> extends Collection.Indexed<T> {
  180. /**
  181. * The number of items in this List.
  182. */
  183. readonly size: number;
  184. // Persistent changes
  185. /**
  186. * Returns a new List which includes `value` at `index`. If `index` already
  187. * exists in this List, it will be replaced.
  188. *
  189. * `index` may be a negative number, which indexes back from the end of the
  190. * List. `v.set(-1, "value")` sets the last item in the List.
  191. *
  192. * If `index` larger than `size`, the returned List's `size` will be large
  193. * enough to include the `index`.
  194. *
  195. * <!-- runkit:activate
  196. * { "preamble": "const { List } = require('immutable');" }
  197. * -->
  198. * ```js
  199. * const originalList = List([ 0 ]);
  200. * // List [ 0 ]
  201. * originalList.set(1, 1);
  202. * // List [ 0, 1 ]
  203. * originalList.set(0, 'overwritten');
  204. * // List [ "overwritten" ]
  205. * originalList.set(2, 2);
  206. * // List [ 0, undefined, 2 ]
  207. *
  208. * List().set(50000, 'value').size;
  209. * // 50001
  210. * ```
  211. *
  212. * Note: `set` can be used in `withMutations`.
  213. */
  214. set(index: number, value: T): List<T>;
  215. /**
  216. * Returns a new List which excludes this `index` and with a size 1 less
  217. * than this List. Values at indices above `index` are shifted down by 1 to
  218. * fill the position.
  219. *
  220. * This is synonymous with `list.splice(index, 1)`.
  221. *
  222. * `index` may be a negative number, which indexes back from the end of the
  223. * List. `v.delete(-1)` deletes the last item in the List.
  224. *
  225. * Note: `delete` cannot be safely used in IE8
  226. *
  227. * <!-- runkit:activate
  228. * { "preamble": "const { List } = require('immutable');" }
  229. * -->
  230. * ```js
  231. * List([ 0, 1, 2, 3, 4 ]).delete(0);
  232. * // List [ 1, 2, 3, 4 ]
  233. * ```
  234. *
  235. * Since `delete()` re-indexes values, it produces a complete copy, which
  236. * has `O(N)` complexity.
  237. *
  238. * Note: `delete` *cannot* be used in `withMutations`.
  239. *
  240. * @alias remove
  241. */
  242. delete(index: number): List<T>;
  243. remove(index: number): List<T>;
  244. /**
  245. * Returns a new List with `value` at `index` with a size 1 more than this
  246. * List. Values at indices above `index` are shifted over by 1.
  247. *
  248. * This is synonymous with `list.splice(index, 0, value)`.
  249. *
  250. * <!-- runkit:activate
  251. * { "preamble": "const { List } = require('immutable');" }
  252. * -->
  253. * ```js
  254. * List([ 0, 1, 2, 3, 4 ]).insert(6, 5)
  255. * // List [ 0, 1, 2, 3, 4, 5 ]
  256. * ```
  257. *
  258. * Since `insert()` re-indexes values, it produces a complete copy, which
  259. * has `O(N)` complexity.
  260. *
  261. * Note: `insert` *cannot* be used in `withMutations`.
  262. */
  263. insert(index: number, value: T): List<T>;
  264. /**
  265. * Returns a new List with 0 size and no values in constant time.
  266. *
  267. * <!-- runkit:activate
  268. * { "preamble": "const { List } = require('immutable');" }
  269. * -->
  270. * ```js
  271. * List([ 1, 2, 3, 4 ]).clear()
  272. * // List []
  273. * ```
  274. *
  275. * Note: `clear` can be used in `withMutations`.
  276. */
  277. clear(): List<T>;
  278. /**
  279. * Returns a new List with the provided `values` appended, starting at this
  280. * List's `size`.
  281. *
  282. * <!-- runkit:activate
  283. * { "preamble": "const { List } = require('immutable');" }
  284. * -->
  285. * ```js
  286. * List([ 1, 2, 3, 4 ]).push(5)
  287. * // List [ 1, 2, 3, 4, 5 ]
  288. * ```
  289. *
  290. * Note: `push` can be used in `withMutations`.
  291. */
  292. push(...values: Array<T>): List<T>;
  293. /**
  294. * Returns a new List with a size ones less than this List, excluding
  295. * the last index in this List.
  296. *
  297. * Note: this differs from `Array#pop` because it returns a new
  298. * List rather than the removed value. Use `last()` to get the last value
  299. * in this List.
  300. *
  301. * ```js
  302. * List([ 1, 2, 3, 4 ]).pop()
  303. * // List[ 1, 2, 3 ]
  304. * ```
  305. *
  306. * Note: `pop` can be used in `withMutations`.
  307. */
  308. pop(): List<T>;
  309. /**
  310. * Returns a new List with the provided `values` prepended, shifting other
  311. * values ahead to higher indices.
  312. *
  313. * <!-- runkit:activate
  314. * { "preamble": "const { List } = require('immutable');" }
  315. * -->
  316. * ```js
  317. * List([ 2, 3, 4]).unshift(1);
  318. * // List [ 1, 2, 3, 4 ]
  319. * ```
  320. *
  321. * Note: `unshift` can be used in `withMutations`.
  322. */
  323. unshift(...values: Array<T>): List<T>;
  324. /**
  325. * Returns a new List with a size ones less than this List, excluding
  326. * the first index in this List, shifting all other values to a lower index.
  327. *
  328. * Note: this differs from `Array#shift` because it returns a new
  329. * List rather than the removed value. Use `first()` to get the first
  330. * value in this List.
  331. *
  332. * <!-- runkit:activate
  333. * { "preamble": "const { List } = require('immutable');" }
  334. * -->
  335. * ```js
  336. * List([ 0, 1, 2, 3, 4 ]).shift();
  337. * // List [ 1, 2, 3, 4 ]
  338. * ```
  339. *
  340. * Note: `shift` can be used in `withMutations`.
  341. */
  342. shift(): List<T>;
  343. /**
  344. * Returns a new List with an updated value at `index` with the return
  345. * value of calling `updater` with the existing value, or `notSetValue` if
  346. * `index` was not set. If called with a single argument, `updater` is
  347. * called with the List itself.
  348. *
  349. * `index` may be a negative number, which indexes back from the end of the
  350. * List. `v.update(-1)` updates the last item in the List.
  351. *
  352. * <!-- runkit:activate
  353. * { "preamble": "const { List } = require('immutable');" }
  354. * -->
  355. * ```js
  356. * const list = List([ 'a', 'b', 'c' ])
  357. * const result = list.update(2, val => val.toUpperCase())
  358. * // List [ "a", "b", "C" ]
  359. * ```
  360. *
  361. * This can be very useful as a way to "chain" a normal function into a
  362. * sequence of methods. RxJS calls this "let" and lodash calls it "thru".
  363. *
  364. * For example, to sum a List after mapping and filtering:
  365. *
  366. * <!-- runkit:activate
  367. * { "preamble": "const { List } = require('immutable');" }
  368. * -->
  369. * ```js
  370. * function sum(collection) {
  371. * return collection.reduce((sum, x) => sum + x, 0)
  372. * }
  373. *
  374. * List([ 1, 2, 3 ])
  375. * .map(x => x + 1)
  376. * .filter(x => x % 2 === 0)
  377. * .update(sum)
  378. * // 6
  379. * ```
  380. *
  381. * Note: `update(index)` can be used in `withMutations`.
  382. *
  383. * @see `Map#update`
  384. */
  385. update(index: number, notSetValue: T, updater: (value: T) => T): this;
  386. update(index: number, updater: (value: T) => T): this;
  387. update<R>(updater: (value: this) => R): R;
  388. /**
  389. * Returns a new List with size `size`. If `size` is less than this
  390. * List's size, the new List will exclude values at the higher indices.
  391. * If `size` is greater than this List's size, the new List will have
  392. * undefined values for the newly available indices.
  393. *
  394. * When building a new List and the final size is known up front, `setSize`
  395. * used in conjunction with `withMutations` may result in the more
  396. * performant construction.
  397. */
  398. setSize(size: number): List<T>;
  399. // Deep persistent changes
  400. /**
  401. * Returns a new List having set `value` at this `keyPath`. If any keys in
  402. * `keyPath` do not exist, a new immutable Map will be created at that key.
  403. *
  404. * Index numbers are used as keys to determine the path to follow in
  405. * the List.
  406. *
  407. * <!-- runkit:activate -->
  408. * ```js
  409. * const { List } = require('immutable')
  410. * const list = List([ 0, 1, 2, List([ 3, 4 ])])
  411. * list.setIn([3, 0], 999);
  412. * // List [ 0, 1, 2, List [ 999, 4 ] ]
  413. * ```
  414. *
  415. * Plain JavaScript Object or Arrays may be nested within an Immutable.js
  416. * Collection, and setIn() can update those values as well, treating them
  417. * immutably by creating new copies of those values with the changes applied.
  418. *
  419. * <!-- runkit:activate -->
  420. * ```js
  421. * const { List } = require('immutable')
  422. * const list = List([ 0, 1, 2, { plain: 'object' }])
  423. * list.setIn([3, 'plain'], 'value');
  424. * // List([ 0, 1, 2, { plain: 'value' }])
  425. * ```
  426. *
  427. * Note: `setIn` can be used in `withMutations`.
  428. */
  429. setIn(keyPath: Iterable<any>, value: any): this;
  430. /**
  431. * Returns a new List having removed the value at this `keyPath`. If any
  432. * keys in `keyPath` do not exist, no change will occur.
  433. *
  434. * <!-- runkit:activate -->
  435. * ```js
  436. * const { List } = require('immutable')
  437. * const list = List([ 0, 1, 2, List([ 3, 4 ])])
  438. * list.deleteIn([3, 0]);
  439. * // List [ 0, 1, 2, List [ 4 ] ]
  440. * ```
  441. *
  442. * Plain JavaScript Object or Arrays may be nested within an Immutable.js
  443. * Collection, and removeIn() can update those values as well, treating them
  444. * immutably by creating new copies of those values with the changes applied.
  445. *
  446. * <!-- runkit:activate -->
  447. * ```js
  448. * const { List } = require('immutable')
  449. * const list = List([ 0, 1, 2, { plain: 'object' }])
  450. * list.removeIn([3, 'plain']);
  451. * // List([ 0, 1, 2, {}])
  452. * ```
  453. *
  454. * Note: `deleteIn` *cannot* be safely used in `withMutations`.
  455. *
  456. * @alias removeIn
  457. */
  458. deleteIn(keyPath: Iterable<any>): this;
  459. removeIn(keyPath: Iterable<any>): this;
  460. /**
  461. * Note: `updateIn` can be used in `withMutations`.
  462. *
  463. * @see `Map#updateIn`
  464. */
  465. updateIn(keyPath: Iterable<any>, notSetValue: any, updater: (value: any) => any): this;
  466. updateIn(keyPath: Iterable<any>, updater: (value: any) => any): this;
  467. /**
  468. * Note: `mergeIn` can be used in `withMutations`.
  469. *
  470. * @see `Map#mergeIn`
  471. */
  472. mergeIn(keyPath: Iterable<any>, ...collections: Array<any>): this;
  473. /**
  474. * Note: `mergeDeepIn` can be used in `withMutations`.
  475. *
  476. * @see `Map#mergeDeepIn`
  477. */
  478. mergeDeepIn(keyPath: Iterable<any>, ...collections: Array<any>): this;
  479. // Transient changes
  480. /**
  481. * Note: Not all methods can be safely used on a mutable collection or within
  482. * `withMutations`! Check the documentation for each method to see if it
  483. * allows being used in `withMutations`.
  484. *
  485. * @see `Map#withMutations`
  486. */
  487. withMutations(mutator: (mutable: this) => any): this;
  488. /**
  489. * An alternative API for withMutations()
  490. *
  491. * Note: Not all methods can be safely used on a mutable collection or within
  492. * `withMutations`! Check the documentation for each method to see if it
  493. * allows being used in `withMutations`.
  494. *
  495. * @see `Map#asMutable`
  496. */
  497. asMutable(): this;
  498. /**
  499. * @see `Map#wasAltered`
  500. */
  501. wasAltered(): boolean;
  502. /**
  503. * @see `Map#asImmutable`
  504. */
  505. asImmutable(): this;
  506. // Sequence algorithms
  507. /**
  508. * Returns a new List with other values or collections concatenated to this one.
  509. *
  510. * Note: `concat` can be used in `withMutations`.
  511. *
  512. * @alias merge
  513. */
  514. concat<C>(...valuesOrCollections: Array<Iterable<C> | C>): List<T | C>;
  515. merge<C>(...collections: Array<Iterable<C>>): List<T | C>;
  516. /**
  517. * Returns a new List with values passed through a
  518. * `mapper` function.
  519. *
  520. * <!-- runkit:activate
  521. * { "preamble": "const { List } = require('immutable');" }
  522. * -->
  523. * ```js
  524. * List([ 1, 2 ]).map(x => 10 * x)
  525. * // List [ 10, 20 ]
  526. * ```
  527. */
  528. map<M>(
  529. mapper: (value: T, key: number, iter: this) => M,
  530. context?: any
  531. ): List<M>;
  532. /**
  533. * Flat-maps the List, returning a new List.
  534. *
  535. * Similar to `list.map(...).flatten(true)`.
  536. */
  537. flatMap<M>(
  538. mapper: (value: T, key: number, iter: this) => Iterable<M>,
  539. context?: any
  540. ): List<M>;
  541. /**
  542. * Returns a new List with only the values for which the `predicate`
  543. * function returns true.
  544. *
  545. * Note: `filter()` always returns a new instance, even if it results in
  546. * not filtering out any values.
  547. */
  548. filter<F extends T>(
  549. predicate: (value: T, index: number, iter: this) => value is F,
  550. context?: any
  551. ): List<F>;
  552. filter(
  553. predicate: (value: T, index: number, iter: this) => any,
  554. context?: any
  555. ): this;
  556. /**
  557. * Returns a List "zipped" with the provided collection.
  558. *
  559. * Like `zipWith`, but using the default `zipper`: creating an `Array`.
  560. *
  561. * <!-- runkit:activate
  562. * { "preamble": "const { List } = require('immutable');" }
  563. * -->
  564. * ```js
  565. * const a = List([ 1, 2, 3 ]);
  566. * const b = List([ 4, 5, 6 ]);
  567. * const c = a.zip(b); // List [ [ 1, 4 ], [ 2, 5 ], [ 3, 6 ] ]
  568. * ```
  569. */
  570. zip<U>(other: Collection<any, U>): List<[T,U]>;
  571. zip<U,V>(other: Collection<any, U>, other2: Collection<any,V>): List<[T,U,V]>;
  572. zip(...collections: Array<Collection<any, any>>): List<any>;
  573. /**
  574. * Returns a List "zipped" with the provided collections.
  575. *
  576. * Unlike `zip`, `zipAll` continues zipping until the longest collection is
  577. * exhausted. Missing values from shorter collections are filled with `undefined`.
  578. *
  579. * <!-- runkit:activate
  580. * { "preamble": "const { List } = require('immutable');" }
  581. * -->
  582. * ```js
  583. * const a = List([ 1, 2 ]);
  584. * const b = List([ 3, 4, 5 ]);
  585. * const c = a.zipAll(b); // List [ [ 1, 3 ], [ 2, 4 ], [ undefined, 5 ] ]
  586. * ```
  587. *
  588. * Note: Since zipAll will return a collection as large as the largest
  589. * input, some results may contain undefined values. TypeScript cannot
  590. * account for these without cases (as of v2.5).
  591. */
  592. zipAll<U>(other: Collection<any, U>): List<[T,U]>;
  593. zipAll<U,V>(other: Collection<any, U>, other2: Collection<any,V>): List<[T,U,V]>;
  594. zipAll(...collections: Array<Collection<any, any>>): List<any>;
  595. /**
  596. * Returns a List "zipped" with the provided collections by using a
  597. * custom `zipper` function.
  598. *
  599. * <!-- runkit:activate
  600. * { "preamble": "const { List } = require('immutable');" }
  601. * -->
  602. * ```js
  603. * const a = List([ 1, 2, 3 ]);
  604. * const b = List([ 4, 5, 6 ]);
  605. * const c = a.zipWith((a, b) => a + b, b);
  606. * // List [ 5, 7, 9 ]
  607. * ```
  608. */
  609. zipWith<U, Z>(
  610. zipper: (value: T, otherValue: U) => Z,
  611. otherCollection: Collection<any, U>
  612. ): List<Z>;
  613. zipWith<U, V, Z>(
  614. zipper: (value: T, otherValue: U, thirdValue: V) => Z,
  615. otherCollection: Collection<any, U>,
  616. thirdCollection: Collection<any, V>
  617. ): List<Z>;
  618. zipWith<Z>(
  619. zipper: (...any: Array<any>) => Z,
  620. ...collections: Array<Collection<any, any>>
  621. ): List<Z>;
  622. }
  623. /**
  624. * Immutable Map is an unordered Collection.Keyed of (key, value) pairs with
  625. * `O(log32 N)` gets and `O(log32 N)` persistent sets.
  626. *
  627. * Iteration order of a Map is undefined, however is stable. Multiple
  628. * iterations of the same Map will iterate in the same order.
  629. *
  630. * Map's keys can be of any type, and use `Immutable.is` to determine key
  631. * equality. This allows the use of any value (including NaN) as a key.
  632. *
  633. * Because `Immutable.is` returns equality based on value semantics, and
  634. * Immutable collections are treated as values, any Immutable collection may
  635. * be used as a key.
  636. *
  637. * <!-- runkit:activate -->
  638. * ```js
  639. * const { Map, List } = require('immutable');
  640. * Map().set(List([ 1 ]), 'listofone').get(List([ 1 ]));
  641. * // 'listofone'
  642. * ```
  643. *
  644. * Any JavaScript object may be used as a key, however strict identity is used
  645. * to evaluate key equality. Two similar looking objects will represent two
  646. * different keys.
  647. *
  648. * Implemented by a hash-array mapped trie.
  649. */
  650. export module Map {
  651. /**
  652. * True if the provided value is a Map
  653. *
  654. * <!-- runkit:activate -->
  655. * ```js
  656. * const { Map } = require('immutable')
  657. * Map.isMap({}) // false
  658. * Map.isMap(Map()) // true
  659. * ```
  660. */
  661. function isMap(maybeMap: any): maybeMap is Map<any, any>;
  662. /**
  663. * Creates a new Map from alternating keys and values
  664. *
  665. * <!-- runkit:activate -->
  666. * ```js
  667. * const { Map } = require('immutable')
  668. * Map.of(
  669. * 'key', 'value',
  670. * 'numerical value', 3,
  671. * 0, 'numerical key'
  672. * )
  673. * // Map { 0: "numerical key", "key": "value", "numerical value": 3 }
  674. * ```
  675. *
  676. * @deprecated Use Map([ [ 'k', 'v' ] ]) or Map({ k: 'v' })
  677. */
  678. function of(...keyValues: Array<any>): Map<any, any>;
  679. }
  680. /**
  681. * Creates a new Immutable Map.
  682. *
  683. * Created with the same key value pairs as the provided Collection.Keyed or
  684. * JavaScript Object or expects a Collection of [K, V] tuple entries.
  685. *
  686. * Note: `Map` is a factory function and not a class, and does not use the
  687. * `new` keyword during construction.
  688. *
  689. * <!-- runkit:activate -->
  690. * ```js
  691. * const { Map } = require('immutable')
  692. * Map({ key: "value" })
  693. * Map([ [ "key", "value" ] ])
  694. * ```
  695. *
  696. * Keep in mind, when using JS objects to construct Immutable Maps, that
  697. * JavaScript Object properties are always strings, even if written in a
  698. * quote-less shorthand, while Immutable Maps accept keys of any type.
  699. *
  700. * <!-- runkit:activate
  701. * { "preamble": "const { Map } = require('immutable');" }
  702. * -->
  703. * ```js
  704. * let obj = { 1: "one" }
  705. * Object.keys(obj) // [ "1" ]
  706. * assert.equal(obj["1"], obj[1]) // "one" === "one"
  707. *
  708. * let map = Map(obj)
  709. * assert.notEqual(map.get("1"), map.get(1)) // "one" !== undefined
  710. * ```
  711. *
  712. * Property access for JavaScript Objects first converts the key to a string,
  713. * but since Immutable Map keys can be of any type the argument to `get()` is
  714. * not altered.
  715. */
  716. export function Map<K, V>(collection: Iterable<[K, V]>): Map<K, V>;
  717. export function Map<V>(obj: {[key: string]: V}): Map<string, V>;
  718. export function Map<K, V>(): Map<K, V>;
  719. export function Map(): Map<any, any>;
  720. export interface Map<K, V> extends Collection.Keyed<K, V> {
  721. /**
  722. * The number of entries in this Map.
  723. */
  724. readonly size: number;
  725. // Persistent changes
  726. /**
  727. * Returns a new Map also containing the new key, value pair. If an equivalent
  728. * key already exists in this Map, it will be replaced.
  729. *
  730. * <!-- runkit:activate -->
  731. * ```js
  732. * const { Map } = require('immutable')
  733. * const originalMap = Map()
  734. * const newerMap = originalMap.set('key', 'value')
  735. * const newestMap = newerMap.set('key', 'newer value')
  736. *
  737. * originalMap
  738. * // Map {}
  739. * newerMap
  740. * // Map { "key": "value" }
  741. * newestMap
  742. * // Map { "key": "newer value" }
  743. * ```
  744. *
  745. * Note: `set` can be used in `withMutations`.
  746. */
  747. set(key: K, value: V): this;
  748. /**
  749. * Returns a new Map which excludes this `key`.
  750. *
  751. * Note: `delete` cannot be safely used in IE8, but is provided to mirror
  752. * the ES6 collection API.
  753. *
  754. * <!-- runkit:activate -->
  755. * ```js
  756. * const { Map } = require('immutable')
  757. * const originalMap = Map({
  758. * key: 'value',
  759. * otherKey: 'other value'
  760. * })
  761. * // Map { "key": "value", "otherKey": "other value" }
  762. * originalMap.delete('otherKey')
  763. * // Map { "key": "value" }
  764. * ```
  765. *
  766. * Note: `delete` can be used in `withMutations`.
  767. *
  768. * @alias remove
  769. */
  770. delete(key: K): this;
  771. remove(key: K): this;
  772. /**
  773. * Returns a new Map which excludes the provided `keys`.
  774. *
  775. * <!-- runkit:activate -->
  776. * ```js
  777. * const { Map } = require('immutable')
  778. * const names = Map({ a: "Aaron", b: "Barry", c: "Connor" })
  779. * names.deleteAll([ 'a', 'c' ])
  780. * // Map { "b": "Barry" }
  781. * ```
  782. *
  783. * Note: `deleteAll` can be used in `withMutations`.
  784. *
  785. * @alias removeAll
  786. */
  787. deleteAll(keys: Iterable<K>): this;
  788. removeAll(keys: Iterable<K>): this;
  789. /**
  790. * Returns a new Map containing no keys or values.
  791. *
  792. * <!-- runkit:activate -->
  793. * ```js
  794. * const { Map } = require('immutable')
  795. * Map({ key: 'value' }).clear()
  796. * // Map {}
  797. * ```
  798. *
  799. * Note: `clear` can be used in `withMutations`.
  800. */
  801. clear(): this;
  802. /**
  803. * Returns a new Map having updated the value at this `key` with the return
  804. * value of calling `updater` with the existing value.
  805. *
  806. * Similar to: `map.set(key, updater(map.get(key)))`.
  807. *
  808. * <!-- runkit:activate -->
  809. * ```js
  810. * const { Map } = require('immutable')
  811. * const aMap = Map({ key: 'value' })
  812. * const newMap = aMap.update('key', value => value + value)
  813. * // Map { "key": "valuevalue" }
  814. * ```
  815. *
  816. * This is most commonly used to call methods on collections within a
  817. * structure of data. For example, in order to `.push()` onto a nested `List`,
  818. * `update` and `push` can be used together:
  819. *
  820. * <!-- runkit:activate
  821. * { "preamble": "const { Map, List } = require('immutable');" }
  822. * -->
  823. * ```js
  824. * const aMap = Map({ nestedList: List([ 1, 2, 3 ]) })
  825. * const newMap = aMap.update('nestedList', list => list.push(4))
  826. * // Map { "nestedList": List [ 1, 2, 3, 4 ] }
  827. * ```
  828. *
  829. * When a `notSetValue` is provided, it is provided to the `updater`
  830. * function when the value at the key does not exist in the Map.
  831. *
  832. * <!-- runkit:activate
  833. * { "preamble": "const { Map } = require('immutable');" }
  834. * -->
  835. * ```js
  836. * const aMap = Map({ key: 'value' })
  837. * const newMap = aMap.update('noKey', 'no value', value => value + value)
  838. * // Map { "key": "value", "noKey": "no valueno value" }
  839. * ```
  840. *
  841. * However, if the `updater` function returns the same value it was called
  842. * with, then no change will occur. This is still true if `notSetValue`
  843. * is provided.
  844. *
  845. * <!-- runkit:activate
  846. * { "preamble": "const { Map } = require('immutable');" }
  847. * -->
  848. * ```js
  849. * const aMap = Map({ apples: 10 })
  850. * const newMap = aMap.update('oranges', 0, val => val)
  851. * // Map { "apples": 10 }
  852. * assert.strictEqual(newMap, map);
  853. * ```
  854. *
  855. * For code using ES2015 or later, using `notSetValue` is discourged in
  856. * favor of function parameter default values. This helps to avoid any
  857. * potential confusion with identify functions as described above.
  858. *
  859. * The previous example behaves differently when written with default values:
  860. *
  861. * <!-- runkit:activate
  862. * { "preamble": "const { Map } = require('immutable');" }
  863. * -->
  864. * ```js
  865. * const aMap = Map({ apples: 10 })
  866. * const newMap = aMap.update('oranges', (val = 0) => val)
  867. * // Map { "apples": 10, "oranges": 0 }
  868. * ```
  869. *
  870. * If no key is provided, then the `updater` function return value is
  871. * returned as well.
  872. *
  873. * <!-- runkit:activate
  874. * { "preamble": "const { Map } = require('immutable');" }
  875. * -->
  876. * ```js
  877. * const aMap = Map({ key: 'value' })
  878. * const result = aMap.update(aMap => aMap.get('key'))
  879. * // "value"
  880. * ```
  881. *
  882. * This can be very useful as a way to "chain" a normal function into a
  883. * sequence of methods. RxJS calls this "let" and lodash calls it "thru".
  884. *
  885. * For example, to sum the values in a Map
  886. *
  887. * <!-- runkit:activate
  888. * { "preamble": "const { Map } = require('immutable');" }
  889. * -->
  890. * ```js
  891. * function sum(collection) {
  892. * return collection.reduce((sum, x) => sum + x, 0)
  893. * }
  894. *
  895. * Map({ x: 1, y: 2, z: 3 })
  896. * .map(x => x + 1)
  897. * .filter(x => x % 2 === 0)
  898. * .update(sum)
  899. * // 6
  900. * ```
  901. *
  902. * Note: `update(key)` can be used in `withMutations`.
  903. */
  904. update(key: K, notSetValue: V, updater: (value: V) => V): this;
  905. update(key: K, updater: (value: V) => V): this;
  906. update<R>(updater: (value: this) => R): R;
  907. /**
  908. * Returns a new Map resulting from merging the provided Collections
  909. * (or JS objects) into this Map. In other words, this takes each entry of
  910. * each collection and sets it on this Map.
  911. *
  912. * Note: Values provided to `merge` are shallowly converted before being
  913. * merged. No nested values are altered.
  914. *
  915. * <!-- runkit:activate -->
  916. * ```js
  917. * const { Map } = require('immutable')
  918. * const one = Map({ a: 10, b: 20, c: 30 })
  919. * const two = Map({ b: 40, a: 50, d: 60 })
  920. * one.merge(two) // Map { "a": 50, "b": 40, "c": 30, "d": 60 }
  921. * two.merge(one) // Map { "b": 20, "a": 10, "d": 60, "c": 30 }
  922. * ```
  923. *
  924. * Note: `merge` can be used in `withMutations`.
  925. *
  926. * @alias concat
  927. */
  928. merge<KC, VC>(...collections: Array<Iterable<[KC, VC]>>): Map<K | KC, V | VC>;
  929. merge<C>(...collections: Array<{[key: string]: C}>): Map<K | string, V | C>;
  930. concat<KC, VC>(...collections: Array<Iterable<[KC, VC]>>): Map<K | KC, V | VC>;
  931. concat<C>(...collections: Array<{[key: string]: C}>): Map<K | string, V | C>;
  932. /**
  933. * Like `merge()`, `mergeWith()` returns a new Map resulting from merging
  934. * the provided Collections (or JS objects) into this Map, but uses the
  935. * `merger` function for dealing with conflicts.
  936. *
  937. * <!-- runkit:activate -->
  938. * ```js
  939. * const { Map } = require('immutable')
  940. * const one = Map({ a: 10, b: 20, c: 30 })
  941. * const two = Map({ b: 40, a: 50, d: 60 })
  942. * one.mergeWith((oldVal, newVal) => oldVal / newVal, two)
  943. * // { "a": 0.2, "b": 0.5, "c": 30, "d": 60 }
  944. * two.mergeWith((oldVal, newVal) => oldVal / newVal, one)
  945. * // { "b": 2, "a": 5, "d": 60, "c": 30 }
  946. * ```
  947. *
  948. * Note: `mergeWith` can be used in `withMutations`.
  949. */
  950. mergeWith(
  951. merger: (oldVal: V, newVal: V, key: K) => V,
  952. ...collections: Array<Iterable<[K, V]> | {[key: string]: V}>
  953. ): this;
  954. /**
  955. * Like `merge()`, but when two Collections conflict, it merges them as well,
  956. * recursing deeply through the nested data.
  957. *
  958. * Note: Values provided to `merge` are shallowly converted before being
  959. * merged. No nested values are altered unless they will also be merged at
  960. * a deeper level.
  961. *
  962. * <!-- runkit:activate -->
  963. * ```js
  964. * const { Map } = require('immutable')
  965. * const one = Map({ a: Map({ x: 10, y: 10 }), b: Map({ x: 20, y: 50 }) })
  966. * const two = Map({ a: Map({ x: 2 }), b: Map({ y: 5 }), c: Map({ z: 3 }) })
  967. * one.mergeDeep(two)
  968. * // Map {
  969. * // "a": Map { "x": 2, "y": 10 },
  970. * // "b": Map { "x": 20, "y": 5 },
  971. * // "c": Map { "z": 3 }
  972. * // }
  973. * ```
  974. *
  975. * Note: `mergeDeep` can be used in `withMutations`.
  976. */
  977. mergeDeep(...collections: Array<Iterable<[K, V]> | {[key: string]: V}>): this;
  978. /**
  979. * Like `mergeDeep()`, but when two non-Collections conflict, it uses the
  980. * `merger` function to determine the resulting value.
  981. *
  982. * <!-- runkit:activate -->
  983. * ```js
  984. * const { Map } = require('immutable')
  985. * const one = Map({ a: Map({ x: 10, y: 10 }), b: Map({ x: 20, y: 50 }) })
  986. * const two = Map({ a: Map({ x: 2 }), b: Map({ y: 5 }), c: Map({ z: 3 }) })
  987. * one.mergeDeepWith((oldVal, newVal) => oldVal / newVal, two)
  988. * // Map {
  989. * // "a": Map { "x": 5, "y": 10 },
  990. * // "b": Map { "x": 20, "y": 10 },
  991. * // "c": Map { "z": 3 }
  992. * // }
  993. * ```
  994. * Note: `mergeDeepWith` can be used in `withMutations`.
  995. */
  996. mergeDeepWith(
  997. merger: (oldVal: any, newVal: any, key: any) => any,
  998. ...collections: Array<Iterable<[K, V]> | {[key: string]: V}>
  999. ): this;
  1000. // Deep persistent changes
  1001. /**
  1002. * Returns a new Map having set `value` at this `keyPath`. If any keys in
  1003. * `keyPath` do not exist, a new immutable Map will be created at that key.
  1004. *
  1005. * <!-- runkit:activate -->
  1006. * ```js
  1007. * const { Map } = require('immutable')
  1008. * const originalMap = Map({
  1009. * subObject: Map({
  1010. * subKey: 'subvalue',
  1011. * subSubObject: Map({
  1012. * subSubKey: 'subSubValue'
  1013. * })
  1014. * })
  1015. * })
  1016. *
  1017. * const newMap = originalMap.setIn(['subObject', 'subKey'], 'ha ha!')
  1018. * // Map {
  1019. * // "subObject": Map {
  1020. * // "subKey": "ha ha!",
  1021. * // "subSubObject": Map { "subSubKey": "subSubValue" }
  1022. * // }
  1023. * // }
  1024. *
  1025. * const newerMap = originalMap.setIn(
  1026. * ['subObject', 'subSubObject', 'subSubKey'],
  1027. * 'ha ha ha!'
  1028. * )
  1029. * // Map {
  1030. * // "subObject": Map {
  1031. * // "subKey": "subvalue",
  1032. * // "subSubObject": Map { "subSubKey": "ha ha ha!" }
  1033. * // }
  1034. * // }
  1035. * ```
  1036. *
  1037. * Plain JavaScript Object or Arrays may be nested within an Immutable.js
  1038. * Collection, and setIn() can update those values as well, treating them
  1039. * immutably by creating new copies of those values with the changes applied.
  1040. *
  1041. * <!-- runkit:activate -->
  1042. * ```js
  1043. * const { Map } = require('immutable')
  1044. * const originalMap = Map({
  1045. * subObject: {
  1046. * subKey: 'subvalue',
  1047. * subSubObject: {
  1048. * subSubKey: 'subSubValue'
  1049. * }
  1050. * }
  1051. * })
  1052. *
  1053. * originalMap.setIn(['subObject', 'subKey'], 'ha ha!')
  1054. * // Map {
  1055. * // "subObject": {
  1056. * // subKey: "ha ha!",
  1057. * // subSubObject: { subSubKey: "subSubValue" }
  1058. * // }
  1059. * // }
  1060. * ```
  1061. *
  1062. * If any key in the path exists but cannot be updated (such as a primitive
  1063. * like number or a custom Object like Date), an error will be thrown.
  1064. *
  1065. * Note: `setIn` can be used in `withMutations`.
  1066. */
  1067. setIn(keyPath: Iterable<any>, value: any): this;
  1068. /**
  1069. * Returns a new Map having removed the value at this `keyPath`. If any keys
  1070. * in `keyPath` do not exist, no change will occur.
  1071. *
  1072. * Note: `deleteIn` can be used in `withMutations`.
  1073. *
  1074. * @alias removeIn
  1075. */
  1076. deleteIn(keyPath: Iterable<any>): this;
  1077. removeIn(keyPath: Iterable<any>): this;
  1078. /**
  1079. * Returns a new Map having applied the `updater` to the entry found at the
  1080. * keyPath.
  1081. *
  1082. * This is most commonly used to call methods on collections nested within a
  1083. * structure of data. For example, in order to `.push()` onto a nested `List`,
  1084. * `updateIn` and `push` can be used together:
  1085. *
  1086. * <!-- runkit:activate -->
  1087. * ```js
  1088. * const { Map, List } = require('immutable')
  1089. * const map = Map({ inMap: Map({ inList: List([ 1, 2, 3 ]) }) })
  1090. * const newMap = map.updateIn(['inMap', 'inList'], list => list.push(4))
  1091. * // Map { "inMap": Map { "inList": List [ 1, 2, 3, 4 ] } }
  1092. * ```
  1093. *
  1094. * If any keys in `keyPath` do not exist, new Immutable `Map`s will
  1095. * be created at those keys. If the `keyPath` does not already contain a
  1096. * value, the `updater` function will be called with `notSetValue`, if
  1097. * provided, otherwise `undefined`.
  1098. *
  1099. * <!-- runkit:activate
  1100. * { "preamble": "const { Map } = require('immutable')" }
  1101. * -->
  1102. * ```js
  1103. * const map = Map({ a: Map({ b: Map({ c: 10 }) }) })
  1104. * const newMap = map.updateIn(['a', 'b', 'c'], val => val * 2)
  1105. * // Map { "a": Map { "b": Map { "c": 20 } } }
  1106. * ```
  1107. *
  1108. * If the `updater` function returns the same value it was called with, then
  1109. * no change will occur. This is still true if `notSetValue` is provided.
  1110. *
  1111. * <!-- runkit:activate
  1112. * { "preamble": "const { Map } = require('immutable')" }
  1113. * -->
  1114. * ```js
  1115. * const map = Map({ a: Map({ b: Map({ c: 10 }) }) })
  1116. * const newMap = map.updateIn(['a', 'b', 'x'], 100, val => val)
  1117. * // Map { "a": Map { "b": Map { "c": 10 } } }
  1118. * assert.strictEqual(newMap, aMap)
  1119. * ```
  1120. *
  1121. * For code using ES2015 or later, using `notSetValue` is discourged in
  1122. * favor of function parameter default values. This helps to avoid any
  1123. * potential confusion with identify functions as described above.
  1124. *
  1125. * The previous example behaves differently when written with default values:
  1126. *
  1127. * <!-- runkit:activate
  1128. * { "preamble": "const { Map } = require('immutable')" }
  1129. * -->
  1130. * ```js
  1131. * const map = Map({ a: Map({ b: Map({ c: 10 }) }) })
  1132. * const newMap = map.updateIn(['a', 'b', 'x'], (val = 100) => val)
  1133. * // Map { "a": Map { "b": Map { "c": 10, "x": 100 } } }
  1134. * ```
  1135. *
  1136. * Plain JavaScript Object or Arrays may be nested within an Immutable.js
  1137. * Collection, and updateIn() can update those values as well, treating them
  1138. * immutably by creating new copies of those values with the changes applied.
  1139. *
  1140. * <!-- runkit:activate
  1141. * { "preamble": "const { Map } = require('immutable')" }
  1142. * -->
  1143. * ```js
  1144. * const map = Map({ a: { b: { c: 10 } } })
  1145. * const newMap = map.updateIn(['a', 'b', 'c'], val => val * 2)
  1146. * // Map { "a": { b: { c: 20 } } }
  1147. * ```
  1148. *
  1149. * If any key in the path exists but cannot be updated (such as a primitive
  1150. * like number or a custom Object like Date), an error will be thrown.
  1151. *
  1152. * Note: `updateIn` can be used in `withMutations`.
  1153. */
  1154. updateIn(keyPath: Iterable<any>, notSetValue: any, updater: (value: any) => any): this;
  1155. updateIn(keyPath: Iterable<any>, updater: (value: any) => any): this;
  1156. /**
  1157. * A combination of `updateIn` and `merge`, returning a new Map, but
  1158. * performing the merge at a point arrived at by following the keyPath.
  1159. * In other words, these two lines are equivalent:
  1160. *
  1161. * ```js
  1162. * map.updateIn(['a', 'b', 'c'], abc => abc.merge(y))
  1163. * map.mergeIn(['a', 'b', 'c'], y)
  1164. * ```
  1165. *
  1166. * Note: `mergeIn` can be used in `withMutations`.
  1167. */
  1168. mergeIn(keyPath: Iterable<any>, ...collections: Array<any>): this;
  1169. /**
  1170. * A combination of `updateIn` and `mergeDeep`, returning a new Map, but
  1171. * performing the deep merge at a point arrived at by following the keyPath.
  1172. * In other words, these two lines are equivalent:
  1173. *
  1174. * ```js
  1175. * map.updateIn(['a', 'b', 'c'], abc => abc.mergeDeep(y))
  1176. * map.mergeDeepIn(['a', 'b', 'c'], y)
  1177. * ```
  1178. *
  1179. * Note: `mergeDeepIn` can be used in `withMutations`.
  1180. */
  1181. mergeDeepIn(keyPath: Iterable<any>, ...collections: Array<any>): this;
  1182. // Transient changes
  1183. /**
  1184. * Every time you call one of the above functions, a new immutable Map is
  1185. * created. If a pure function calls a number of these to produce a final
  1186. * return value, then a penalty on performance and memory has been paid by
  1187. * creating all of the intermediate immutable Maps.
  1188. *
  1189. * If you need to apply a series of mutations to produce a new immutable
  1190. * Map, `withMutations()` creates a temporary mutable copy of the Map which
  1191. * can apply mutations in a highly performant manner. In fact, this is
  1192. * exactly how complex mutations like `merge` are done.
  1193. *
  1194. * As an example, this results in the creation of 2, not 4, new Maps:
  1195. *
  1196. * <!-- runkit:activate -->
  1197. * ```js
  1198. * const { Map } = require('immutable')
  1199. * const map1 = Map()
  1200. * const map2 = map1.withMutations(map => {
  1201. * map.set('a', 1).set('b', 2).set('c', 3)
  1202. * })
  1203. * assert.equal(map1.size, 0)
  1204. * assert.equal(map2.size, 3)
  1205. * ```
  1206. *
  1207. * Note: Not all methods can be used on a mutable collection or within
  1208. * `withMutations`! Read the documentation for each method to see if it
  1209. * is safe to use in `withMutations`.
  1210. */
  1211. withMutations(mutator: (mutable: this) => any): this;
  1212. /**
  1213. * Another way to avoid creation of intermediate Immutable maps is to create
  1214. * a mutable copy of this collection. Mutable copies *always* return `this`,
  1215. * and thus shouldn't be used for equality. Your function should never return
  1216. * a mutable copy of a collection, only use it internally to create a new
  1217. * collection.
  1218. *
  1219. * If possible, use `withMutations` to work with temporary mutable copies as
  1220. * it provides an easier to use API and considers many common optimizations.
  1221. *
  1222. * Note: if the collection is already mutable, `asMutable` returns itself.
  1223. *
  1224. * Note: Not all methods can be used on a mutable collection or within
  1225. * `withMutations`! Read the documentation for each method to see if it
  1226. * is safe to use in `withMutations`.
  1227. *
  1228. * @see `Map#asImmutable`
  1229. */
  1230. asMutable(): this;
  1231. /**
  1232. * Returns true if this is a mutable copy (see `asMutable()`) and mutative
  1233. * alterations have been applied.
  1234. *
  1235. * @see `Map#asMutable`
  1236. */
  1237. wasAltered(): boolean;
  1238. /**
  1239. * The yin to `asMutable`'s yang. Because it applies to mutable collections,
  1240. * this operation is *mutable* and may return itself (though may not
  1241. * return itself, i.e. if the result is an empty collection). Once
  1242. * performed, the original mutable copy must no longer be mutated since it
  1243. * may be the immutable result.
  1244. *
  1245. * If possible, use `withMutations` to work with temporary mutable copies as
  1246. * it provides an easier to use API and considers many common optimizations.
  1247. *
  1248. * @see `Map#asMutable`
  1249. */
  1250. asImmutable(): this;
  1251. // Sequence algorithms
  1252. /**
  1253. * Returns a new Map with values passed through a
  1254. * `mapper` function.
  1255. *
  1256. * Map({ a: 1, b: 2 }).map(x => 10 * x)
  1257. * // Map { a: 10, b: 20 }
  1258. */
  1259. map<M>(
  1260. mapper: (value: V, key: K, iter: this) => M,
  1261. context?: any
  1262. ): Map<K, M>;
  1263. /**
  1264. * @see Collection.Keyed.mapKeys
  1265. */
  1266. mapKeys<M>(
  1267. mapper: (key: K, value: V, iter: this) => M,
  1268. context?: any
  1269. ): Map<M, V>;
  1270. /**
  1271. * @see Collection.Keyed.mapEntries
  1272. */
  1273. mapEntries<KM, VM>(
  1274. mapper: (entry: [K, V], index: number, iter: this) => [KM, VM],
  1275. context?: any
  1276. ): Map<KM, VM>;
  1277. /**
  1278. * Flat-maps the Map, returning a new Map.
  1279. *
  1280. * Similar to `data.map(...).flatten(true)`.
  1281. */
  1282. flatMap<KM, VM>(
  1283. mapper: (value: V, key: K, iter: this) => Iterable<[KM, VM]>,
  1284. context?: any
  1285. ): Map<KM, VM>;
  1286. /**
  1287. * Returns a new Map with only the entries for which the `predicate`
  1288. * function returns true.
  1289. *
  1290. * Note: `filter()` always returns a new instance, even if it results in
  1291. * not filtering out any values.
  1292. */
  1293. filter<F extends V>(
  1294. predicate: (value: V, key: K, iter: this) => value is F,
  1295. context?: any
  1296. ): Map<K, F>;
  1297. filter(
  1298. predicate: (value: V, key: K, iter: this) => any,
  1299. context?: any
  1300. ): this;
  1301. /**
  1302. * @see Collection.Keyed.flip
  1303. */
  1304. flip(): Map<V, K>;
  1305. }
  1306. /**
  1307. * A type of Map that has the additional guarantee that the iteration order of
  1308. * entries will be the order in which they were set().
  1309. *
  1310. * The iteration behavior of OrderedMap is the same as native ES6 Map and
  1311. * JavaScript Object.
  1312. *
  1313. * Note that `OrderedMap` are more expensive than non-ordered `Map` and may
  1314. * consume more memory. `OrderedMap#set` is amortized O(log32 N), but not
  1315. * stable.
  1316. */
  1317. export module OrderedMap {
  1318. /**
  1319. * True if the provided value is an OrderedMap.
  1320. */
  1321. function isOrderedMap(maybeOrderedMap: any): maybeOrderedMap is OrderedMap<any, any>;
  1322. }
  1323. /**
  1324. * Creates a new Immutable OrderedMap.
  1325. *
  1326. * Created with the same key value pairs as the provided Collection.Keyed or
  1327. * JavaScript Object or expects a Collection of [K, V] tuple entries.
  1328. *
  1329. * The iteration order of key-value pairs provided to this constructor will
  1330. * be preserved in the OrderedMap.
  1331. *
  1332. * let newOrderedMap = OrderedMap({key: "value"})
  1333. * let newOrderedMap = OrderedMap([["key", "value"]])
  1334. *
  1335. * Note: `OrderedMap` is a factory function and not a class, and does not use
  1336. * the `new` keyword during construction.
  1337. */
  1338. export function OrderedMap<K, V>(collection: Iterable<[K, V]>): OrderedMap<K, V>;
  1339. export function OrderedMap<V>(obj: {[key: string]: V}): OrderedMap<string, V>;
  1340. export function OrderedMap<K, V>(): OrderedMap<K, V>;
  1341. export function OrderedMap(): OrderedMap<any, any>;
  1342. export interface OrderedMap<K, V> extends Map<K, V> {
  1343. /**
  1344. * The number of entries in this OrderedMap.
  1345. */
  1346. readonly size: number;
  1347. /**
  1348. * Returns a new OrderedMap also containing the new key, value pair. If an
  1349. * equivalent key already exists in this OrderedMap, it will be replaced
  1350. * while maintaining the existing order.
  1351. *
  1352. * <!-- runkit:activate -->
  1353. * ```js
  1354. * const { OrderedMap } = require('immutable')
  1355. * const originalMap = OrderedMap({a:1, b:1, c:1})
  1356. * const updatedMap = originalMap.set('b', 2)
  1357. *
  1358. * originalMap
  1359. * // OrderedMap {a: 1, b: 1, c: 1}
  1360. * updatedMap
  1361. * // OrderedMap {a: 1, b: 2, c: 1}
  1362. * ```
  1363. *
  1364. * Note: `set` can be used in `withMutations`.
  1365. */
  1366. set(key: K, value: V): this;
  1367. /**
  1368. * Returns a new OrderedMap resulting from merging the provided Collections
  1369. * (or JS objects) into this OrderedMap. In other words, this takes each
  1370. * entry of each collection and sets it on this OrderedMap.
  1371. *
  1372. * Note: Values provided to `merge` are shallowly converted before being
  1373. * merged. No nested values are altered.
  1374. *
  1375. * <!-- runkit:activate -->
  1376. * ```js
  1377. * const { OrderedMap } = require('immutable')
  1378. * const one = OrderedMap({ a: 10, b: 20, c: 30 })
  1379. * const two = OrderedMap({ b: 40, a: 50, d: 60 })
  1380. * one.merge(two) // OrderedMap { "a": 50, "b": 40, "c": 30, "d": 60 }
  1381. * two.merge(one) // OrderedMap { "b": 20, "a": 10, "d": 60, "c": 30 }
  1382. * ```
  1383. *
  1384. * Note: `merge` can be used in `withMutations`.
  1385. *
  1386. * @alias concat
  1387. */
  1388. merge<KC, VC>(...collections: Array<Iterable<[KC, VC]>>): OrderedMap<K | KC, V | VC>;
  1389. merge<C>(...collections: Array<{[key: string]: C}>): OrderedMap<K | string, V | C>;
  1390. concat<KC, VC>(...collections: Array<Iterable<[KC, VC]>>): OrderedMap<K | KC, V | VC>;
  1391. concat<C>(...collections: Array<{[key: string]: C}>): OrderedMap<K | string, V | C>;
  1392. // Sequence algorithms
  1393. /**
  1394. * Returns a new OrderedMap with values passed through a
  1395. * `mapper` function.
  1396. *
  1397. * OrderedMap({ a: 1, b: 2 }).map(x => 10 * x)
  1398. * // OrderedMap { "a": 10, "b": 20 }
  1399. *
  1400. * Note: `map()` always returns a new instance, even if it produced the same
  1401. * value at every step.
  1402. */
  1403. map<M>(
  1404. mapper: (value: V, key: K, iter: this) => M,
  1405. context?: any
  1406. ): OrderedMap<K, M>;
  1407. /**
  1408. * @see Collection.Keyed.mapKeys
  1409. */
  1410. mapKeys<M>(
  1411. mapper: (key: K, value: V, iter: this) => M,
  1412. context?: any
  1413. ): OrderedMap<M, V>;
  1414. /**
  1415. * @see Collection.Keyed.mapEntries
  1416. */
  1417. mapEntries<KM, VM>(
  1418. mapper: (entry: [K, V], index: number, iter: this) => [KM, VM],
  1419. context?: any
  1420. ): OrderedMap<KM, VM>;
  1421. /**
  1422. * Flat-maps the OrderedMap, returning a new OrderedMap.
  1423. *
  1424. * Similar to `data.map(...).flatten(true)`.
  1425. */
  1426. flatMap<KM, VM>(
  1427. mapper: (value: V, key: K, iter: this) => Iterable<[KM, VM]>,
  1428. context?: any
  1429. ): OrderedMap<KM, VM>;
  1430. /**
  1431. * Returns a new OrderedMap with only the entries for which the `predicate`
  1432. * function returns true.
  1433. *
  1434. * Note: `filter()` always returns a new instance, even if it results in
  1435. * not filtering out any values.
  1436. */
  1437. filter<F extends V>(
  1438. predicate: (value: V, key: K, iter: this) => value is F,
  1439. context?: any
  1440. ): OrderedMap<K, F>;
  1441. filter(
  1442. predicate: (value: V, key: K, iter: this) => any,
  1443. context?: any
  1444. ): this;
  1445. /**
  1446. * @see Collection.Keyed.flip
  1447. */
  1448. flip(): OrderedMap<V, K>;
  1449. }
  1450. /**
  1451. * A Collection of unique values with `O(log32 N)` adds and has.
  1452. *
  1453. * When iterating a Set, the entries will be (value, value) pairs. Iteration
  1454. * order of a Set is undefined, however is stable. Multiple iterations of the
  1455. * same Set will iterate in the same order.
  1456. *
  1457. * Set values, like Map keys, may be of any type. Equality is determined using
  1458. * `Immutable.is`, enabling Sets to uniquely include other Immutable
  1459. * collections, custom value types, and NaN.
  1460. */
  1461. export module Set {
  1462. /**
  1463. * True if the provided value is a Set
  1464. */
  1465. function isSet(maybeSet: any): maybeSet is Set<any>;
  1466. /**
  1467. * Creates a new Set containing `values`.
  1468. */
  1469. function of<T>(...values: Array<T>): Set<T>;
  1470. /**
  1471. * `Set.fromKeys()` creates a new immutable Set containing the keys from
  1472. * this Collection or JavaScript Object.
  1473. */
  1474. function fromKeys<T>(iter: Collection<T, any>): Set<T>;
  1475. function fromKeys(obj: {[key: string]: any}): Set<string>;
  1476. /**
  1477. * `Set.intersect()` creates a new immutable Set that is the intersection of
  1478. * a collection of other sets.
  1479. *
  1480. * ```js
  1481. * const { Set } = require('immutable')
  1482. * const intersected = Set.intersect([
  1483. * Set([ 'a', 'b', 'c' ])
  1484. * Set([ 'c', 'a', 't' ])
  1485. * ])
  1486. * // Set [ "a", "c"" ]
  1487. * ```
  1488. */
  1489. function intersect<T>(sets: Iterable<Iterable<T>>): Set<T>;
  1490. /**
  1491. * `Set.union()` creates a new immutable Set that is the union of a
  1492. * collection of other sets.
  1493. *
  1494. * ```js
  1495. * const { Set } = require('immutable')
  1496. * const unioned = Set.union([
  1497. * Set([ 'a', 'b', 'c' ])
  1498. * Set([ 'c', 'a', 't' ])
  1499. * ])
  1500. * // Set [ "a", "b", "c", "t"" ]
  1501. * ```
  1502. */
  1503. function union<T>(sets: Iterable<Iterable<T>>): Set<T>;
  1504. }
  1505. /**
  1506. * Create a new immutable Set containing the values of the provided
  1507. * collection-like.
  1508. *
  1509. * Note: `Set` is a factory function and not a class, and does not use the
  1510. * `new` keyword during construction.
  1511. */
  1512. export function Set(): Set<any>;
  1513. export function Set<T>(): Set<T>;
  1514. export function Set<T>(collection: Iterable<T>): Set<T>;
  1515. export interface Set<T> extends Collection.Set<T> {
  1516. /**
  1517. * The number of items in this Set.
  1518. */
  1519. readonly size: number;
  1520. // Persistent changes
  1521. /**
  1522. * Returns a new Set which also includes this value.
  1523. *
  1524. * Note: `add` can be used in `withMutations`.
  1525. */
  1526. add(value: T): this;
  1527. /**
  1528. * Returns a new Set which excludes this value.
  1529. *
  1530. * Note: `delete` can be used in `withMutations`.
  1531. *
  1532. * Note: `delete` **cannot** be safely used in IE8, use `remove` if
  1533. * supporting old browsers.
  1534. *
  1535. * @alias remove
  1536. */
  1537. delete(value: T): this;
  1538. remove(value: T): this;
  1539. /**
  1540. * Returns a new Set containing no values.
  1541. *
  1542. * Note: `clear` can be used in `withMutations`.
  1543. */
  1544. clear(): this;
  1545. /**
  1546. * Returns a Set including any value from `collections` that does not already
  1547. * exist in this Set.
  1548. *
  1549. * Note: `union` can be used in `withMutations`.
  1550. * @alias merge
  1551. * @alias concat
  1552. */
  1553. union<C>(...collections: Array<Iterable<C>>): Set<T | C>;
  1554. merge<C>(...collections: Array<Iterable<C>>): Set<T | C>;
  1555. concat<C>(...collections: Array<Iterable<C>>): Set<T | C>;
  1556. /**
  1557. * Returns a Set which has removed any values not also contained
  1558. * within `collections`.
  1559. *
  1560. * Note: `intersect` can be used in `withMutations`.
  1561. */
  1562. intersect(...collections: Array<Iterable<T>>): this;
  1563. /**
  1564. * Returns a Set excluding any values contained within `collections`.
  1565. *
  1566. * <!-- runkit:activate -->
  1567. * ```js
  1568. * const { OrderedSet } = require('immutable')
  1569. * OrderedSet([ 1, 2, 3 ]).subtract([1, 3])
  1570. * // OrderedSet [2]
  1571. * ```
  1572. *
  1573. * Note: `subtract` can be used in `withMutations`.
  1574. */
  1575. subtract(...collections: Array<Iterable<T>>): this;
  1576. // Transient changes
  1577. /**
  1578. * Note: Not all methods can be used on a mutable collection or within
  1579. * `withMutations`! Check the documentation for each method to see if it
  1580. * mentions being safe to use in `withMutations`.
  1581. *
  1582. * @see `Map#withMutations`
  1583. */
  1584. withMutations(mutator: (mutable: this) => any): this;
  1585. /**
  1586. * Note: Not all methods can be used on a mutable collection or within
  1587. * `withMutations`! Check the documentation for each method to see if it
  1588. * mentions being safe to use in `withMutations`.
  1589. *
  1590. * @see `Map#asMutable`
  1591. */
  1592. asMutable(): this;
  1593. /**
  1594. * @see `Map#wasAltered`
  1595. */
  1596. wasAltered(): boolean;
  1597. /**
  1598. * @see `Map#asImmutable`
  1599. */
  1600. asImmutable(): this;
  1601. // Sequence algorithms
  1602. /**
  1603. * Returns a new Set with values passed through a
  1604. * `mapper` function.
  1605. *
  1606. * Set([1,2]).map(x => 10 * x)
  1607. * // Set [10,20]
  1608. */
  1609. map<M>(
  1610. mapper: (value: T, key: T, iter: this) => M,
  1611. context?: any
  1612. ): Set<M>;
  1613. /**
  1614. * Flat-maps the Set, returning a new Set.
  1615. *
  1616. * Similar to `set.map(...).flatten(true)`.
  1617. */
  1618. flatMap<M>(
  1619. mapper: (value: T, key: T, iter: this) => Iterable<M>,
  1620. context?: any
  1621. ): Set<M>;
  1622. /**
  1623. * Returns a new Set with only the values for which the `predicate`
  1624. * function returns true.
  1625. *
  1626. * Note: `filter()` always returns a new instance, even if it results in
  1627. * not filtering out any values.
  1628. */
  1629. filter<F extends T>(
  1630. predicate: (value: T, key: T, iter: this) => value is F,
  1631. context?: any
  1632. ): Set<F>;
  1633. filter(
  1634. predicate: (value: T, key: T, iter: this) => any,
  1635. context?: any
  1636. ): this;
  1637. }
  1638. /**
  1639. * A type of Set that has the additional guarantee that the iteration order of
  1640. * values will be the order in which they were `add`ed.
  1641. *
  1642. * The iteration behavior of OrderedSet is the same as native ES6 Set.
  1643. *
  1644. * Note that `OrderedSet` are more expensive than non-ordered `Set` and may
  1645. * consume more memory. `OrderedSet#add` is amortized O(log32 N), but not
  1646. * stable.
  1647. */
  1648. export module OrderedSet {
  1649. /**
  1650. * True if the provided value is an OrderedSet.
  1651. */
  1652. function isOrderedSet(maybeOrderedSet: any): boolean;
  1653. /**
  1654. * Creates a new OrderedSet containing `values`.
  1655. */
  1656. function of<T>(...values: Array<T>): OrderedSet<T>;
  1657. /**
  1658. * `OrderedSet.fromKeys()` creates a new immutable OrderedSet containing
  1659. * the keys from this Collection or JavaScript Object.
  1660. */
  1661. function fromKeys<T>(iter: Collection<T, any>): OrderedSet<T>;
  1662. function fromKeys(obj: {[key: string]: any}): OrderedSet<string>;
  1663. }
  1664. /**
  1665. * Create a new immutable OrderedSet containing the values of the provided
  1666. * collection-like.
  1667. *
  1668. * Note: `OrderedSet` is a factory function and not a class, and does not use
  1669. * the `new` keyword during construction.
  1670. */
  1671. export function OrderedSet(): OrderedSet<any>;
  1672. export function OrderedSet<T>(): OrderedSet<T>;
  1673. export function OrderedSet<T>(collection: Iterable<T>): OrderedSet<T>;
  1674. export interface OrderedSet<T> extends Set<T> {
  1675. /**
  1676. * The number of items in this OrderedSet.
  1677. */
  1678. readonly size: number;
  1679. /**
  1680. * Returns an OrderedSet including any value from `collections` that does
  1681. * not already exist in this OrderedSet.
  1682. *
  1683. * Note: `union` can be used in `withMutations`.
  1684. * @alias merge
  1685. * @alias concat
  1686. */
  1687. union<C>(...collections: Array<Iterable<C>>): OrderedSet<T | C>;
  1688. merge<C>(...collections: Array<Iterable<C>>): OrderedSet<T | C>;
  1689. concat<C>(...collections: Array<Iterable<C>>): OrderedSet<T | C>;
  1690. // Sequence algorithms
  1691. /**
  1692. * Returns a new Set with values passed through a
  1693. * `mapper` function.
  1694. *
  1695. * OrderedSet([ 1, 2 ]).map(x => 10 * x)
  1696. * // OrderedSet [10, 20]
  1697. */
  1698. map<M>(
  1699. mapper: (value: T, key: T, iter: this) => M,
  1700. context?: any
  1701. ): OrderedSet<M>;
  1702. /**
  1703. * Flat-maps the OrderedSet, returning a new OrderedSet.
  1704. *
  1705. * Similar to `set.map(...).flatten(true)`.
  1706. */
  1707. flatMap<M>(
  1708. mapper: (value: T, key: T, iter: this) => Iterable<M>,
  1709. context?: any
  1710. ): OrderedSet<M>;
  1711. /**
  1712. * Returns a new OrderedSet with only the values for which the `predicate`
  1713. * function returns true.
  1714. *
  1715. * Note: `filter()` always returns a new instance, even if it results in
  1716. * not filtering out any values.
  1717. */
  1718. filter<F extends T>(
  1719. predicate: (value: T, key: T, iter: this) => value is F,
  1720. context?: any
  1721. ): OrderedSet<F>;
  1722. filter(
  1723. predicate: (value: T, key: T, iter: this) => any,
  1724. context?: any
  1725. ): this;
  1726. /**
  1727. * Returns an OrderedSet of the same type "zipped" with the provided
  1728. * collections.
  1729. *
  1730. * Like `zipWith`, but using the default `zipper`: creating an `Array`.
  1731. *
  1732. * ```js
  1733. * const a = OrderedSet([ 1, 2, 3 ])
  1734. * const b = OrderedSet([ 4, 5, 6 ])
  1735. * const c = a.zip(b)
  1736. * // OrderedSet [ [ 1, 4 ], [ 2, 5 ], [ 3, 6 ] ]
  1737. * ```
  1738. */
  1739. zip<U>(other: Collection<any, U>): OrderedSet<[T,U]>;
  1740. zip<U,V>(other1: Collection<any, U>, other2: Collection<any, V>): OrderedSet<[T,U,V]>;
  1741. zip(...collections: Array<Collection<any, any>>): OrderedSet<any>;
  1742. /**
  1743. * Returns a OrderedSet of the same type "zipped" with the provided
  1744. * collections.
  1745. *
  1746. * Unlike `zip`, `zipAll` continues zipping until the longest collection is
  1747. * exhausted. Missing values from shorter collections are filled with `undefined`.
  1748. *
  1749. * ```js
  1750. * const a = OrderedSet([ 1, 2 ]);
  1751. * const b = OrderedSet([ 3, 4, 5 ]);
  1752. * const c = a.zipAll(b); // OrderedSet [ [ 1, 3 ], [ 2, 4 ], [ undefined, 5 ] ]
  1753. * ```
  1754. *
  1755. * Note: Since zipAll will return a collection as large as the largest
  1756. * input, some results may contain undefined values. TypeScript cannot
  1757. * account for these without cases (as of v2.5).
  1758. */
  1759. zipAll<U>(other: Collection<any, U>): OrderedSet<[T,U]>;
  1760. zipAll<U,V>(other1: Collection<any, U>, other2: Collection<any, V>): OrderedSet<[T,U,V]>;
  1761. zipAll(...collections: Array<Collection<any, any>>): OrderedSet<any>;
  1762. /**
  1763. * Returns an OrderedSet of the same type "zipped" with the provided
  1764. * collections by using a custom `zipper` function.
  1765. *
  1766. * @see Seq.Indexed.zipWith
  1767. */
  1768. zipWith<U, Z>(
  1769. zipper: (value: T, otherValue: U) => Z,
  1770. otherCollection: Collection<any, U>
  1771. ): OrderedSet<Z>;
  1772. zipWith<U, V, Z>(
  1773. zipper: (value: T, otherValue: U, thirdValue: V) => Z,
  1774. otherCollection: Collection<any, U>,
  1775. thirdCollection: Collection<any, V>
  1776. ): OrderedSet<Z>;
  1777. zipWith<Z>(
  1778. zipper: (...any: Array<any>) => Z,
  1779. ...collections: Array<Collection<any, any>>
  1780. ): OrderedSet<Z>;
  1781. }
  1782. /**
  1783. * Stacks are indexed collections which support very efficient O(1) addition
  1784. * and removal from the front using `unshift(v)` and `shift()`.
  1785. *
  1786. * For familiarity, Stack also provides `push(v)`, `pop()`, and `peek()`, but
  1787. * be aware that they also operate on the front of the list, unlike List or
  1788. * a JavaScript Array.
  1789. *
  1790. * Note: `reverse()` or any inherent reverse traversal (`reduceRight`,
  1791. * `lastIndexOf`, etc.) is not efficient with a Stack.
  1792. *
  1793. * Stack is implemented with a Single-Linked List.
  1794. */
  1795. export module Stack {
  1796. /**
  1797. * True if the provided value is a Stack
  1798. */
  1799. function isStack(maybeStack: any): maybeStack is Stack<any>;
  1800. /**
  1801. * Creates a new Stack containing `values`.
  1802. */
  1803. function of<T>(...values: Array<T>): Stack<T>;
  1804. }
  1805. /**
  1806. * Create a new immutable Stack containing the values of the provided
  1807. * collection-like.
  1808. *
  1809. * The iteration order of the provided collection is preserved in the
  1810. * resulting `Stack`.
  1811. *
  1812. * Note: `Stack` is a factory function and not a class, and does not use the
  1813. * `new` keyword during construction.
  1814. */
  1815. export function Stack(): Stack<any>;
  1816. export function Stack<T>(): Stack<T>;
  1817. export function Stack<T>(collection: Iterable<T>): Stack<T>;
  1818. export interface Stack<T> extends Collection.Indexed<T> {
  1819. /**
  1820. * The number of items in this Stack.
  1821. */
  1822. readonly size: number;
  1823. // Reading values
  1824. /**
  1825. * Alias for `Stack.first()`.
  1826. */
  1827. peek(): T | undefined;
  1828. // Persistent changes
  1829. /**
  1830. * Returns a new Stack with 0 size and no values.
  1831. *
  1832. * Note: `clear` can be used in `withMutations`.
  1833. */
  1834. clear(): Stack<T>;
  1835. /**
  1836. * Returns a new Stack with the provided `values` prepended, shifting other
  1837. * values ahead to higher indices.
  1838. *
  1839. * This is very efficient for Stack.
  1840. *
  1841. * Note: `unshift` can be used in `withMutations`.
  1842. */
  1843. unshift(...values: Array<T>): Stack<T>;
  1844. /**
  1845. * Like `Stack#unshift`, but accepts a collection rather than varargs.
  1846. *
  1847. * Note: `unshiftAll` can be used in `withMutations`.
  1848. */
  1849. unshiftAll(iter: Iterable<T>): Stack<T>;
  1850. /**
  1851. * Returns a new Stack with a size ones less than this Stack, excluding
  1852. * the first item in this Stack, shifting all other values to a lower index.
  1853. *
  1854. * Note: this differs from `Array#shift` because it returns a new
  1855. * Stack rather than the removed value. Use `first()` or `peek()` to get the
  1856. * first value in this Stack.
  1857. *
  1858. * Note: `shift` can be used in `withMutations`.
  1859. */
  1860. shift(): Stack<T>;
  1861. /**
  1862. * Alias for `Stack#unshift` and is not equivalent to `List#push`.
  1863. */
  1864. push(...values: Array<T>): Stack<T>;
  1865. /**
  1866. * Alias for `Stack#unshiftAll`.
  1867. */
  1868. pushAll(iter: Iterable<T>): Stack<T>;
  1869. /**
  1870. * Alias for `Stack#shift` and is not equivalent to `List#pop`.
  1871. */
  1872. pop(): Stack<T>;
  1873. // Transient changes
  1874. /**
  1875. * Note: Not all methods can be used on a mutable collection or within
  1876. * `withMutations`! Check the documentation for each method to see if it
  1877. * mentions being safe to use in `withMutations`.
  1878. *
  1879. * @see `Map#withMutations`
  1880. */
  1881. withMutations(mutator: (mutable: this) => any): this;
  1882. /**
  1883. * Note: Not all methods can be used on a mutable collection or within
  1884. * `withMutations`! Check the documentation for each method to see if it
  1885. * mentions being safe to use in `withMutations`.
  1886. *
  1887. * @see `Map#asMutable`
  1888. */
  1889. asMutable(): this;
  1890. /**
  1891. * @see `Map#wasAltered`
  1892. */
  1893. wasAltered(): boolean;
  1894. /**
  1895. * @see `Map#asImmutable`
  1896. */
  1897. asImmutable(): this;
  1898. // Sequence algorithms
  1899. /**
  1900. * Returns a new Stack with other collections concatenated to this one.
  1901. */
  1902. concat<C>(...valuesOrCollections: Array<Iterable<C> | C>): Stack<T | C>;
  1903. /**
  1904. * Returns a new Stack with values passed through a
  1905. * `mapper` function.
  1906. *
  1907. * Stack([ 1, 2 ]).map(x => 10 * x)
  1908. * // Stack [ 10, 20 ]
  1909. *
  1910. * Note: `map()` always returns a new instance, even if it produced the same
  1911. * value at every step.
  1912. */
  1913. map<M>(
  1914. mapper: (value: T, key: number, iter: this) => M,
  1915. context?: any
  1916. ): Stack<M>;
  1917. /**
  1918. * Flat-maps the Stack, returning a new Stack.
  1919. *
  1920. * Similar to `stack.map(...).flatten(true)`.
  1921. */
  1922. flatMap<M>(
  1923. mapper: (value: T, key: number, iter: this) => Iterable<M>,
  1924. context?: any
  1925. ): Stack<M>;
  1926. /**
  1927. * Returns a new Set with only the values for which the `predicate`
  1928. * function returns true.
  1929. *
  1930. * Note: `filter()` always returns a new instance, even if it results in
  1931. * not filtering out any values.
  1932. */
  1933. filter<F extends T>(
  1934. predicate: (value: T, index: number, iter: this) => value is F,
  1935. context?: any
  1936. ): Set<F>;
  1937. filter(
  1938. predicate: (value: T, index: number, iter: this) => any,
  1939. context?: any
  1940. ): this;
  1941. /**
  1942. * Returns a Stack "zipped" with the provided collections.
  1943. *
  1944. * Like `zipWith`, but using the default `zipper`: creating an `Array`.
  1945. *
  1946. * ```js
  1947. * const a = Stack([ 1, 2, 3 ]);
  1948. * const b = Stack([ 4, 5, 6 ]);
  1949. * const c = a.zip(b); // Stack [ [ 1, 4 ], [ 2, 5 ], [ 3, 6 ] ]
  1950. * ```
  1951. */
  1952. zip<U>(other: Collection<any, U>): Stack<[T,U]>;
  1953. zip<U,V>(other: Collection<any, U>, other2: Collection<any,V>): Stack<[T,U,V]>;
  1954. zip(...collections: Array<Collection<any, any>>): Stack<any>;
  1955. /**
  1956. * Returns a Stack "zipped" with the provided collections.
  1957. *
  1958. * Unlike `zip`, `zipAll` continues zipping until the longest collection is
  1959. * exhausted. Missing values from shorter collections are filled with `undefined`.
  1960. *
  1961. * ```js
  1962. * const a = Stack([ 1, 2 ]);
  1963. * const b = Stack([ 3, 4, 5 ]);
  1964. * const c = a.zipAll(b); // Stack [ [ 1, 3 ], [ 2, 4 ], [ undefined, 5 ] ]
  1965. * ```
  1966. *
  1967. * Note: Since zipAll will return a collection as large as the largest
  1968. * input, some results may contain undefined values. TypeScript cannot
  1969. * account for these without cases (as of v2.5).
  1970. */
  1971. zipAll<U>(other: Collection<any, U>): Stack<[T,U]>;
  1972. zipAll<U,V>(other: Collection<any, U>, other2: Collection<any,V>): Stack<[T,U,V]>;
  1973. zipAll(...collections: Array<Collection<any, any>>): Stack<any>;
  1974. /**
  1975. * Returns a Stack "zipped" with the provided collections by using a
  1976. * custom `zipper` function.
  1977. *
  1978. * ```js
  1979. * const a = Stack([ 1, 2, 3 ]);
  1980. * const b = Stack([ 4, 5, 6 ]);
  1981. * const c = a.zipWith((a, b) => a + b, b);
  1982. * // Stack [ 5, 7, 9 ]
  1983. * ```
  1984. */
  1985. zipWith<U, Z>(
  1986. zipper: (value: T, otherValue: U) => Z,
  1987. otherCollection: Collection<any, U>
  1988. ): Stack<Z>;
  1989. zipWith<U, V, Z>(
  1990. zipper: (value: T, otherValue: U, thirdValue: V) => Z,
  1991. otherCollection: Collection<any, U>,
  1992. thirdCollection: Collection<any, V>
  1993. ): Stack<Z>;
  1994. zipWith<Z>(
  1995. zipper: (...any: Array<any>) => Z,
  1996. ...collections: Array<Collection<any, any>>
  1997. ): Stack<Z>;
  1998. }
  1999. /**
  2000. * Returns a Seq.Indexed of numbers from `start` (inclusive) to `end`
  2001. * (exclusive), by `step`, where `start` defaults to 0, `step` to 1, and `end` to
  2002. * infinity. When `start` is equal to `end`, returns empty range.
  2003. *
  2004. * Note: `Range` is a factory function and not a class, and does not use the
  2005. * `new` keyword during construction.
  2006. *
  2007. * ```js
  2008. * const { Range } = require('immutable')
  2009. * Range() // [ 0, 1, 2, 3, ... ]
  2010. * Range(10) // [ 10, 11, 12, 13, ... ]
  2011. * Range(10, 15) // [ 10, 11, 12, 13, 14 ]
  2012. * Range(10, 30, 5) // [ 10, 15, 20, 25 ]
  2013. * Range(30, 10, 5) // [ 30, 25, 20, 15 ]
  2014. * Range(30, 30, 5) // []
  2015. * ```
  2016. */
  2017. export function Range(start?: number, end?: number, step?: number): Seq.Indexed<number>;
  2018. /**
  2019. * Returns a Seq.Indexed of `value` repeated `times` times. When `times` is
  2020. * not defined, returns an infinite `Seq` of `value`.
  2021. *
  2022. * Note: `Repeat` is a factory function and not a class, and does not use the
  2023. * `new` keyword during construction.
  2024. *
  2025. * ```js
  2026. * const { Repeat } = require('immutable')
  2027. * Repeat('foo') // [ 'foo', 'foo', 'foo', ... ]
  2028. * Repeat('bar', 4) // [ 'bar', 'bar', 'bar', 'bar' ]
  2029. * ```
  2030. */
  2031. export function Repeat<T>(value: T, times?: number): Seq.Indexed<T>;
  2032. /**
  2033. * A record is similar to a JS object, but enforces a specific set of allowed
  2034. * string keys, and has default values.
  2035. *
  2036. * The `Record()` function produces new Record Factories, which when called
  2037. * create Record instances.
  2038. *
  2039. * ```js
  2040. * const { Record } = require('immutable')
  2041. * const ABRecord = Record({ a: 1, b: 2 })
  2042. * const myRecord = ABRecord({ b: 3 })
  2043. * ```
  2044. *
  2045. * Records always have a value for the keys they define. `remove`ing a key
  2046. * from a record simply resets it to the default value for that key.
  2047. *
  2048. * ```js
  2049. * myRecord.size // 2
  2050. * myRecord.get('a') // 1
  2051. * myRecord.get('b') // 3
  2052. * const myRecordWithoutB = myRecord.remove('b')
  2053. * myRecordWithoutB.get('b') // 2
  2054. * myRecordWithoutB.size // 2
  2055. * ```
  2056. *
  2057. * Values provided to the constructor not found in the Record type will
  2058. * be ignored. For example, in this case, ABRecord is provided a key "x" even
  2059. * though only "a" and "b" have been defined. The value for "x" will be
  2060. * ignored for this record.
  2061. *
  2062. * ```js
  2063. * const myRecord = ABRecord({ b: 3, x: 10 })
  2064. * myRecord.get('x') // undefined
  2065. * ```
  2066. *
  2067. * Because Records have a known set of string keys, property get access works
  2068. * as expected, however property sets will throw an Error.
  2069. *
  2070. * Note: IE8 does not support property access. Only use `get()` when
  2071. * supporting IE8.
  2072. *
  2073. * ```js
  2074. * myRecord.b // 3
  2075. * myRecord.b = 5 // throws Error
  2076. * ```
  2077. *
  2078. * Record Types can be extended as well, allowing for custom methods on your
  2079. * Record. This is not a common pattern in functional environments, but is in
  2080. * many JS programs.
  2081. *
  2082. * However Record Types are more restricted than typical JavaScript classes.
  2083. * They do not use a class constructor, which also means they cannot use
  2084. * class properties (since those are technically part of a constructor).
  2085. *
  2086. * While Record Types can be syntactically created with the JavaScript `class`
  2087. * form, the resulting Record function is actually a factory function, not a
  2088. * class constructor. Even though Record Types are not classes, JavaScript
  2089. * currently requires the use of `new` when creating new Record instances if
  2090. * they are defined as a `class`.
  2091. *
  2092. * ```
  2093. * class ABRecord extends Record({ a: 1, b: 2 }) {
  2094. * getAB() {
  2095. * return this.a + this.b;
  2096. * }
  2097. * }
  2098. *
  2099. * var myRecord = new ABRecord({b: 3})
  2100. * myRecord.getAB() // 4
  2101. * ```
  2102. *
  2103. *
  2104. * **Flow Typing Records:**
  2105. *
  2106. * Immutable.js exports two Flow types designed to make it easier to use
  2107. * Records with flow typed code, `RecordOf<TProps>` and `RecordFactory<TProps>`.
  2108. *
  2109. * When defining a new kind of Record factory function, use a flow type that
  2110. * describes the values the record contains along with `RecordFactory<TProps>`.
  2111. * To type instances of the Record (which the factory function returns),
  2112. * use `RecordOf<TProps>`.
  2113. *
  2114. * Typically, new Record definitions will export both the Record factory
  2115. * function as well as the Record instance type for use in other code.
  2116. *
  2117. * ```js
  2118. * import type { RecordFactory, RecordOf } from 'immutable';
  2119. *
  2120. * // Use RecordFactory<TProps> for defining new Record factory functions.
  2121. * type Point3DProps = { x: number, y: number, z: number };
  2122. * const defaultValues: Point3DProps = { x: 0, y: 0, z: 0 };
  2123. * const makePoint3D: RecordFactory<Point3DProps> = Record(defaultValues);
  2124. * export makePoint3D;
  2125. *
  2126. * // Use RecordOf<T> for defining new instances of that Record.
  2127. * export type Point3D = RecordOf<Point3DProps>;
  2128. * const some3DPoint: Point3D = makePoint3D({ x: 10, y: 20, z: 30 });
  2129. * ```
  2130. *
  2131. * **Flow Typing Record Subclasses:**
  2132. *
  2133. * Records can be subclassed as a means to add additional methods to Record
  2134. * instances. This is generally discouraged in favor of a more functional API,
  2135. * since Subclasses have some minor overhead. However the ability to create
  2136. * a rich API on Record types can be quite valuable.
  2137. *
  2138. * When using Flow to type Subclasses, do not use `RecordFactory<TProps>`,
  2139. * instead apply the props type when subclassing:
  2140. *
  2141. * ```js
  2142. * type PersonProps = {name: string, age: number};
  2143. * const defaultValues: PersonProps = {name: 'Aristotle', age: 2400};
  2144. * const PersonRecord = Record(defaultValues);
  2145. * class Person extends PersonRecord<PersonProps> {
  2146. * getName(): string {
  2147. * return this.get('name')
  2148. * }
  2149. *
  2150. * setName(name: string): this {
  2151. * return this.set('name', name);
  2152. * }
  2153. * }
  2154. * ```
  2155. *
  2156. * **Choosing Records vs plain JavaScript objects**
  2157. *
  2158. * Records offer a persistently immutable alternative to plain JavaScript
  2159. * objects, however they're not required to be used within Immutable.js
  2160. * collections. In fact, the deep-access and deep-updating functions
  2161. * like `getIn()` and `setIn()` work with plain JavaScript Objects as well.
  2162. *
  2163. * Deciding to use Records or Objects in your application should be informed
  2164. * by the tradeoffs and relative benefits of each:
  2165. *
  2166. * - *Runtime immutability*: plain JS objects may be carefully treated as
  2167. * immutable, however Record instances will *throw* if attempted to be
  2168. * mutated directly. Records provide this additional guarantee, however at
  2169. * some marginal runtime cost. While JS objects are mutable by nature, the
  2170. * use of type-checking tools like [Flow](https://medium.com/@gcanti/immutability-with-flow-faa050a1aef4)
  2171. * can help gain confidence in code written to favor immutability.
  2172. *
  2173. * - *Value equality*: Records use value equality when compared with `is()`
  2174. * or `record.equals()`. That is, two Records with the same keys and values
  2175. * are equal. Plain objects use *reference equality*. Two objects with the
  2176. * same keys and values are not equal since they are different objects.
  2177. * This is important to consider when using objects as keys in a `Map` or
  2178. * values in a `Set`, which use equality when retrieving values.
  2179. *
  2180. * - *API methods*: Records have a full featured API, with methods like
  2181. * `.getIn()`, and `.equals()`. These can make working with these values
  2182. * easier, but comes at the cost of not allowing keys with those names.
  2183. *
  2184. * - *Default values*: Records provide default values for every key, which
  2185. * can be useful when constructing Records with often unchanging values.
  2186. * However default values can make using Flow and TypeScript more laborious.
  2187. *
  2188. * - *Serialization*: Records use a custom internal representation to
  2189. * efficiently store and update their values. Converting to and from this
  2190. * form isn't free. If converting Records to plain objects is common,
  2191. * consider sticking with plain objects to begin with.
  2192. */
  2193. export module Record {
  2194. /**
  2195. * True if `maybeRecord` is an instance of a Record.
  2196. */
  2197. export function isRecord(maybeRecord: any): maybeRecord is Record<any>;
  2198. /**
  2199. * Records allow passing a second parameter to supply a descriptive name
  2200. * that appears when converting a Record to a string or in any error
  2201. * messages. A descriptive name for any record can be accessed by using this
  2202. * method. If one was not provided, the string "Record" is returned.
  2203. *
  2204. * ```js
  2205. * const { Record } = require('immutable')
  2206. * const Person = Record({
  2207. * name: null
  2208. * }, 'Person')
  2209. *
  2210. * var me = Person({ name: 'My Name' })
  2211. * me.toString() // "Person { "name": "My Name" }"
  2212. * Record.getDescriptiveName(me) // "Person"
  2213. * ```
  2214. */
  2215. export function getDescriptiveName(record: Record<any>): string;
  2216. /**
  2217. * A Record.Factory is created by the `Record()` function. Record instances
  2218. * are created by passing it some of the accepted values for that Record
  2219. * type:
  2220. *
  2221. * <!-- runkit:activate
  2222. * { "preamble": "const { Record } = require('immutable')" }
  2223. * -->
  2224. * ```js
  2225. * // makePerson is a Record Factory function
  2226. * const makePerson = Record({ name: null, favoriteColor: 'unknown' });
  2227. *
  2228. * // alan is a Record instance
  2229. * const alan = makePerson({ name: 'Alan' });
  2230. * ```
  2231. *
  2232. * Note that Record Factories return `Record<TProps> & Readonly<TProps>`,
  2233. * this allows use of both the Record instance API, and direct property
  2234. * access on the resulting instances:
  2235. *
  2236. * <!-- runkit:activate
  2237. * { "preamble": "const { Record } = require('immutable');const makePerson = Record({ name: null, favoriteColor: 'unknown' });const alan = makePerson({ name: 'Alan' });" }
  2238. * -->
  2239. * ```js
  2240. * // Use the Record API
  2241. * console.log('Record API: ' + alan.get('name'))
  2242. *
  2243. * // Or direct property access (Readonly)
  2244. * console.log('property access: ' + alan.name)
  2245. * ```
  2246. *
  2247. * **Flow Typing Records:**
  2248. *
  2249. * Use the `RecordFactory<TProps>` Flow type to get high quality type checking of
  2250. * Records:
  2251. *
  2252. * ```js
  2253. * import type { RecordFactory, RecordOf } from 'immutable';
  2254. *
  2255. * // Use RecordFactory<TProps> for defining new Record factory functions.
  2256. * type PersonProps = { name: ?string, favoriteColor: string };
  2257. * const makePerson: RecordFactory<PersonProps> = Record({ name: null, favoriteColor: 'unknown' });
  2258. *
  2259. * // Use RecordOf<T> for defining new instances of that Record.
  2260. * type Person = RecordOf<PersonProps>;
  2261. * const alan: Person = makePerson({ name: 'Alan' });
  2262. * ```
  2263. */
  2264. export module Factory {}
  2265. export interface Factory<TProps extends Object> {
  2266. (values?: Partial<TProps> | Iterable<[string, any]>): Record<TProps> & Readonly<TProps>;
  2267. new (values?: Partial<TProps> | Iterable<[string, any]>): Record<TProps> & Readonly<TProps>;
  2268. /**
  2269. * The name provided to `Record(values, name)` can be accessed with
  2270. * `displayName`.
  2271. */
  2272. displayName: string;
  2273. }
  2274. export function Factory<TProps extends Object>(values?: Partial<TProps> | Iterable<[string, any]>): Record<TProps> & Readonly<TProps>;
  2275. }
  2276. /**
  2277. * Unlike other types in Immutable.js, the `Record()` function creates a new
  2278. * Record Factory, which is a function that creates Record instances.
  2279. *
  2280. * See above for examples of using `Record()`.
  2281. *
  2282. * Note: `Record` is a factory function and not a class, and does not use the
  2283. * `new` keyword during construction.
  2284. */
  2285. export function Record<TProps>(defaultValues: TProps, name?: string): Record.Factory<TProps>;
  2286. export interface Record<TProps extends Object> {
  2287. // Reading values
  2288. has(key: string): key is keyof TProps & string;
  2289. /**
  2290. * Returns the value associated with the provided key, which may be the
  2291. * default value defined when creating the Record factory function.
  2292. *
  2293. * If the requested key is not defined by this Record type, then
  2294. * notSetValue will be returned if provided. Note that this scenario would
  2295. * produce an error when using Flow or TypeScript.
  2296. */
  2297. get<K extends keyof TProps>(key: K, notSetValue?: any): TProps[K];
  2298. get<T>(key: string, notSetValue: T): T;
  2299. // Reading deep values
  2300. hasIn(keyPath: Iterable<any>): boolean;
  2301. getIn(keyPath: Iterable<any>): any;
  2302. // Value equality
  2303. equals(other: any): boolean;
  2304. hashCode(): number;
  2305. // Persistent changes
  2306. set<K extends keyof TProps>(key: K, value: TProps[K]): this;
  2307. update<K extends keyof TProps>(key: K, updater: (value: TProps[K]) => TProps[K]): this;
  2308. merge(...collections: Array<Partial<TProps> | Iterable<[string, any]>>): this;
  2309. mergeDeep(...collections: Array<Partial<TProps> | Iterable<[string, any]>>): this;
  2310. mergeWith(
  2311. merger: (oldVal: any, newVal: any, key: keyof TProps) => any,
  2312. ...collections: Array<Partial<TProps> | Iterable<[string, any]>>
  2313. ): this;
  2314. mergeDeepWith(
  2315. merger: (oldVal: any, newVal: any, key: any) => any,
  2316. ...collections: Array<Partial<TProps> | Iterable<[string, any]>>
  2317. ): this;
  2318. /**
  2319. * Returns a new instance of this Record type with the value for the
  2320. * specific key set to its default value.
  2321. *
  2322. * @alias remove
  2323. */
  2324. delete<K extends keyof TProps>(key: K): this;
  2325. remove<K extends keyof TProps>(key: K): this;
  2326. /**
  2327. * Returns a new instance of this Record type with all values set
  2328. * to their default values.
  2329. */
  2330. clear(): this;
  2331. // Deep persistent changes
  2332. setIn(keyPath: Iterable<any>, value: any): this;
  2333. updateIn(keyPath: Iterable<any>, updater: (value: any) => any): this;
  2334. mergeIn(keyPath: Iterable<any>, ...collections: Array<any>): this;
  2335. mergeDeepIn(keyPath: Iterable<any>, ...collections: Array<any>): this;
  2336. /**
  2337. * @alias removeIn
  2338. */
  2339. deleteIn(keyPath: Iterable<any>): this;
  2340. removeIn(keyPath: Iterable<any>): this;
  2341. // Conversion to JavaScript types
  2342. /**
  2343. * Deeply converts this Record to equivalent native JavaScript Object.
  2344. *
  2345. * Note: This method may not be overridden. Objects with custom
  2346. * serialization to plain JS may override toJSON() instead.
  2347. */
  2348. toJS(): { [K in keyof TProps]: any };
  2349. /**
  2350. * Shallowly converts this Record to equivalent native JavaScript Object.
  2351. */
  2352. toJSON(): TProps;
  2353. /**
  2354. * Shallowly converts this Record to equivalent JavaScript Object.
  2355. */
  2356. toObject(): TProps;
  2357. // Transient changes
  2358. /**
  2359. * Note: Not all methods can be used on a mutable collection or within
  2360. * `withMutations`! Only `set` may be used mutatively.
  2361. *
  2362. * @see `Map#withMutations`
  2363. */
  2364. withMutations(mutator: (mutable: this) => any): this;
  2365. /**
  2366. * @see `Map#asMutable`
  2367. */
  2368. asMutable(): this;
  2369. /**
  2370. * @see `Map#wasAltered`
  2371. */
  2372. wasAltered(): boolean;
  2373. /**
  2374. * @see `Map#asImmutable`
  2375. */
  2376. asImmutable(): this;
  2377. // Sequence algorithms
  2378. toSeq(): Seq.Keyed<keyof TProps, TProps[keyof TProps]>;
  2379. [Symbol.iterator](): IterableIterator<[keyof TProps, TProps[keyof TProps]]>;
  2380. }
  2381. /**
  2382. * RecordOf<T> is used in TypeScript to define interfaces expecting an
  2383. * instance of record with type T.
  2384. *
  2385. * This is equivalent to an instance of a record created by a Record Factory.
  2386. */
  2387. export type RecordOf<TProps extends Object> = Record<TProps> &
  2388. Readonly<TProps>;
  2389. /**
  2390. * `Seq` describes a lazy operation, allowing them to efficiently chain
  2391. * use of all the higher-order collection methods (such as `map` and `filter`)
  2392. * by not creating intermediate collections.
  2393. *
  2394. * **Seq is immutable** — Once a Seq is created, it cannot be
  2395. * changed, appended to, rearranged or otherwise modified. Instead, any
  2396. * mutative method called on a `Seq` will return a new `Seq`.
  2397. *
  2398. * **Seq is lazy** — `Seq` does as little work as necessary to respond to any
  2399. * method call. Values are often created during iteration, including implicit
  2400. * iteration when reducing or converting to a concrete data structure such as
  2401. * a `List` or JavaScript `Array`.
  2402. *
  2403. * For example, the following performs no work, because the resulting
  2404. * `Seq`'s values are never iterated:
  2405. *
  2406. * ```js
  2407. * const { Seq } = require('immutable')
  2408. * const oddSquares = Seq([ 1, 2, 3, 4, 5, 6, 7, 8 ])
  2409. * .filter(x => x % 2 !== 0)
  2410. * .map(x => x * x)
  2411. * ```
  2412. *
  2413. * Once the `Seq` is used, it performs only the work necessary. In this
  2414. * example, no intermediate arrays are ever created, filter is called three
  2415. * times, and map is only called once:
  2416. *
  2417. * ```js
  2418. * oddSquares.get(1); // 9
  2419. * ```
  2420. *
  2421. * Any collection can be converted to a lazy Seq with `Seq()`.
  2422. *
  2423. * <!-- runkit:activate -->
  2424. * ```js
  2425. * const { Map } = require('immutable')
  2426. * const map = Map({ a: 1, b: 2, c: 3 }
  2427. * const lazySeq = Seq(map)
  2428. * ```
  2429. *
  2430. * `Seq` allows for the efficient chaining of operations, allowing for the
  2431. * expression of logic that can otherwise be very tedious:
  2432. *
  2433. * ```js
  2434. * lazySeq
  2435. * .flip()
  2436. * .map(key => key.toUpperCase())
  2437. * .flip()
  2438. * // Seq { A: 1, B: 1, C: 1 }
  2439. * ```
  2440. *
  2441. * As well as expressing logic that would otherwise seem memory or time
  2442. * limited, for example `Range` is a special kind of Lazy sequence.
  2443. *
  2444. * <!-- runkit:activate -->
  2445. * ```js
  2446. * const { Range } = require('immutable')
  2447. * Range(1, Infinity)
  2448. * .skip(1000)
  2449. * .map(n => -n)
  2450. * .filter(n => n % 2 === 0)
  2451. * .take(2)
  2452. * .reduce((r, n) => r * n, 1)
  2453. * // 1006008
  2454. * ```
  2455. *
  2456. * Seq is often used to provide a rich collection API to JavaScript Object.
  2457. *
  2458. * ```js
  2459. * Seq({ x: 0, y: 1, z: 2 }).map(v => v * 2).toObject();
  2460. * // { x: 0, y: 2, z: 4 }
  2461. * ```
  2462. */
  2463. export module Seq {
  2464. /**
  2465. * True if `maybeSeq` is a Seq, it is not backed by a concrete
  2466. * structure such as Map, List, or Set.
  2467. */
  2468. function isSeq(maybeSeq: any): maybeSeq is Seq.Indexed<any> | Seq.Keyed<any, any> | Seq.Set<any>;
  2469. /**
  2470. * `Seq` which represents key-value pairs.
  2471. */
  2472. export module Keyed {}
  2473. /**
  2474. * Always returns a Seq.Keyed, if input is not keyed, expects an
  2475. * collection of [K, V] tuples.
  2476. *
  2477. * Note: `Seq.Keyed` is a conversion function and not a class, and does not
  2478. * use the `new` keyword during construction.
  2479. */
  2480. export function Keyed<K, V>(collection: Iterable<[K, V]>): Seq.Keyed<K, V>;
  2481. export function Keyed<V>(obj: {[key: string]: V}): Seq.Keyed<string, V>;
  2482. export function Keyed<K, V>(): Seq.Keyed<K, V>;
  2483. export function Keyed(): Seq.Keyed<any, any>;
  2484. export interface Keyed<K, V> extends Seq<K, V>, Collection.Keyed<K, V> {
  2485. /**
  2486. * Deeply converts this Keyed Seq to equivalent native JavaScript Object.
  2487. *
  2488. * Converts keys to Strings.
  2489. */
  2490. toJS(): Object;
  2491. /**
  2492. * Shallowly converts this Keyed Seq to equivalent native JavaScript Object.
  2493. *
  2494. * Converts keys to Strings.
  2495. */
  2496. toJSON(): { [key: string]: V };
  2497. /**
  2498. * Shallowly converts this collection to an Array.
  2499. */
  2500. toArray(): Array<[K, V]>;
  2501. /**
  2502. * Returns itself
  2503. */
  2504. toSeq(): this;
  2505. /**
  2506. * Returns a new Seq with other collections concatenated to this one.
  2507. *
  2508. * All entries will be present in the resulting Seq, even if they
  2509. * have the same key.
  2510. */
  2511. concat<KC, VC>(...collections: Array<Iterable<[KC, VC]>>): Seq.Keyed<K | KC, V | VC>;
  2512. concat<C>(...collections: Array<{[key: string]: C}>): Seq.Keyed<K | string, V | C>;
  2513. /**
  2514. * Returns a new Seq.Keyed with values passed through a
  2515. * `mapper` function.
  2516. *
  2517. * ```js
  2518. * const { Seq } = require('immutable')
  2519. * Seq.Keyed({ a: 1, b: 2 }).map(x => 10 * x)
  2520. * // Seq { "a": 10, "b": 20 }
  2521. * ```
  2522. *
  2523. * Note: `map()` always returns a new instance, even if it produced the
  2524. * same value at every step.
  2525. */
  2526. map<M>(
  2527. mapper: (value: V, key: K, iter: this) => M,
  2528. context?: any
  2529. ): Seq.Keyed<K, M>;
  2530. /**
  2531. * @see Collection.Keyed.mapKeys
  2532. */
  2533. mapKeys<M>(
  2534. mapper: (key: K, value: V, iter: this) => M,
  2535. context?: any
  2536. ): Seq.Keyed<M, V>;
  2537. /**
  2538. * @see Collection.Keyed.mapEntries
  2539. */
  2540. mapEntries<KM, VM>(
  2541. mapper: (entry: [K, V], index: number, iter: this) => [KM, VM],
  2542. context?: any
  2543. ): Seq.Keyed<KM, VM>;
  2544. /**
  2545. * Flat-maps the Seq, returning a Seq of the same type.
  2546. *
  2547. * Similar to `seq.map(...).flatten(true)`.
  2548. */
  2549. flatMap<KM, VM>(
  2550. mapper: (value: V, key: K, iter: this) => Iterable<[KM, VM]>,
  2551. context?: any
  2552. ): Seq.Keyed<KM, VM>;
  2553. /**
  2554. * Returns a new Seq with only the entries for which the `predicate`
  2555. * function returns true.
  2556. *
  2557. * Note: `filter()` always returns a new instance, even if it results in
  2558. * not filtering out any values.
  2559. */
  2560. filter<F extends V>(
  2561. predicate: (value: V, key: K, iter: this) => value is F,
  2562. context?: any
  2563. ): Seq.Keyed<K, F>;
  2564. filter(
  2565. predicate: (value: V, key: K, iter: this) => any,
  2566. context?: any
  2567. ): this;
  2568. /**
  2569. * @see Collection.Keyed.flip
  2570. */
  2571. flip(): Seq.Keyed<V, K>;
  2572. }
  2573. /**
  2574. * `Seq` which represents an ordered indexed list of values.
  2575. */
  2576. module Indexed {
  2577. /**
  2578. * Provides an Seq.Indexed of the values provided.
  2579. */
  2580. function of<T>(...values: Array<T>): Seq.Indexed<T>;
  2581. }
  2582. /**
  2583. * Always returns Seq.Indexed, discarding associated keys and
  2584. * supplying incrementing indices.
  2585. *
  2586. * Note: `Seq.Indexed` is a conversion function and not a class, and does
  2587. * not use the `new` keyword during construction.
  2588. */
  2589. export function Indexed(): Seq.Indexed<any>;
  2590. export function Indexed<T>(): Seq.Indexed<T>;
  2591. export function Indexed<T>(collection: Iterable<T>): Seq.Indexed<T>;
  2592. export interface Indexed<T> extends Seq<number, T>, Collection.Indexed<T> {
  2593. /**
  2594. * Deeply converts this Indexed Seq to equivalent native JavaScript Array.
  2595. */
  2596. toJS(): Array<any>;
  2597. /**
  2598. * Shallowly converts this Indexed Seq to equivalent native JavaScript Array.
  2599. */
  2600. toJSON(): Array<T>;
  2601. /**
  2602. * Shallowly converts this collection to an Array.
  2603. */
  2604. toArray(): Array<T>;
  2605. /**
  2606. * Returns itself
  2607. */
  2608. toSeq(): this
  2609. /**
  2610. * Returns a new Seq with other collections concatenated to this one.
  2611. */
  2612. concat<C>(...valuesOrCollections: Array<Iterable<C> | C>): Seq.Indexed<T | C>;
  2613. /**
  2614. * Returns a new Seq.Indexed with values passed through a
  2615. * `mapper` function.
  2616. *
  2617. * ```js
  2618. * const { Seq } = require('immutable')
  2619. * Seq.Indexed([ 1, 2 ]).map(x => 10 * x)
  2620. * // Seq [ 10, 20 ]
  2621. * ```
  2622. *
  2623. * Note: `map()` always returns a new instance, even if it produced the
  2624. * same value at every step.
  2625. */
  2626. map<M>(
  2627. mapper: (value: T, key: number, iter: this) => M,
  2628. context?: any
  2629. ): Seq.Indexed<M>;
  2630. /**
  2631. * Flat-maps the Seq, returning a a Seq of the same type.
  2632. *
  2633. * Similar to `seq.map(...).flatten(true)`.
  2634. */
  2635. flatMap<M>(
  2636. mapper: (value: T, key: number, iter: this) => Iterable<M>,
  2637. context?: any
  2638. ): Seq.Indexed<M>;
  2639. /**
  2640. * Returns a new Seq with only the values for which the `predicate`
  2641. * function returns true.
  2642. *
  2643. * Note: `filter()` always returns a new instance, even if it results in
  2644. * not filtering out any values.
  2645. */
  2646. filter<F extends T>(
  2647. predicate: (value: T, index: number, iter: this) => value is F,
  2648. context?: any
  2649. ): Seq.Indexed<F>;
  2650. filter(
  2651. predicate: (value: T, index: number, iter: this) => any,
  2652. context?: any
  2653. ): this;
  2654. /**
  2655. * Returns a Seq "zipped" with the provided collections.
  2656. *
  2657. * Like `zipWith`, but using the default `zipper`: creating an `Array`.
  2658. *
  2659. * ```js
  2660. * const a = Seq([ 1, 2, 3 ]);
  2661. * const b = Seq([ 4, 5, 6 ]);
  2662. * const c = a.zip(b); // Seq [ [ 1, 4 ], [ 2, 5 ], [ 3, 6 ] ]
  2663. * ```
  2664. */
  2665. zip<U>(other: Collection<any, U>): Seq.Indexed<[T,U]>;
  2666. zip<U,V>(other: Collection<any, U>, other2: Collection<any, V>): Seq.Indexed<[T,U,V]>;
  2667. zip(...collections: Array<Collection<any, any>>): Seq.Indexed<any>;
  2668. /**
  2669. * Returns a Seq "zipped" with the provided collections.
  2670. *
  2671. * Unlike `zip`, `zipAll` continues zipping until the longest collection is
  2672. * exhausted. Missing values from shorter collections are filled with `undefined`.
  2673. *
  2674. * ```js
  2675. * const a = Seq([ 1, 2 ]);
  2676. * const b = Seq([ 3, 4, 5 ]);
  2677. * const c = a.zipAll(b); // Seq [ [ 1, 3 ], [ 2, 4 ], [ undefined, 5 ] ]
  2678. * ```
  2679. */
  2680. zipAll<U>(other: Collection<any, U>): Seq.Indexed<[T,U]>;
  2681. zipAll<U,V>(other: Collection<any, U>, other2: Collection<any, V>): Seq.Indexed<[T,U,V]>;
  2682. zipAll(...collections: Array<Collection<any, any>>): Seq.Indexed<any>;
  2683. /**
  2684. * Returns a Seq "zipped" with the provided collections by using a
  2685. * custom `zipper` function.
  2686. *
  2687. * ```js
  2688. * const a = Seq([ 1, 2, 3 ]);
  2689. * const b = Seq([ 4, 5, 6 ]);
  2690. * const c = a.zipWith((a, b) => a + b, b);
  2691. * // Seq [ 5, 7, 9 ]
  2692. * ```
  2693. */
  2694. zipWith<U, Z>(
  2695. zipper: (value: T, otherValue: U) => Z,
  2696. otherCollection: Collection<any, U>
  2697. ): Seq.Indexed<Z>;
  2698. zipWith<U, V, Z>(
  2699. zipper: (value: T, otherValue: U, thirdValue: V) => Z,
  2700. otherCollection: Collection<any, U>,
  2701. thirdCollection: Collection<any, V>
  2702. ): Seq.Indexed<Z>;
  2703. zipWith<Z>(
  2704. zipper: (...any: Array<any>) => Z,
  2705. ...collections: Array<Collection<any, any>>
  2706. ): Seq.Indexed<Z>;
  2707. }
  2708. /**
  2709. * `Seq` which represents a set of values.
  2710. *
  2711. * Because `Seq` are often lazy, `Seq.Set` does not provide the same guarantee
  2712. * of value uniqueness as the concrete `Set`.
  2713. */
  2714. export module Set {
  2715. /**
  2716. * Returns a Seq.Set of the provided values
  2717. */
  2718. function of<T>(...values: Array<T>): Seq.Set<T>;
  2719. }
  2720. /**
  2721. * Always returns a Seq.Set, discarding associated indices or keys.
  2722. *
  2723. * Note: `Seq.Set` is a conversion function and not a class, and does not
  2724. * use the `new` keyword during construction.
  2725. */
  2726. export function Set(): Seq.Set<any>;
  2727. export function Set<T>(): Seq.Set<T>;
  2728. export function Set<T>(collection: Iterable<T>): Seq.Set<T>;
  2729. export interface Set<T> extends Seq<T, T>, Collection.Set<T> {
  2730. /**
  2731. * Deeply converts this Set Seq to equivalent native JavaScript Array.
  2732. */
  2733. toJS(): Array<any>;
  2734. /**
  2735. * Shallowly converts this Set Seq to equivalent native JavaScript Array.
  2736. */
  2737. toJSON(): Array<T>;
  2738. /**
  2739. * Shallowly converts this collection to an Array.
  2740. */
  2741. toArray(): Array<T>;
  2742. /**
  2743. * Returns itself
  2744. */
  2745. toSeq(): this
  2746. /**
  2747. * Returns a new Seq with other collections concatenated to this one.
  2748. *
  2749. * All entries will be present in the resulting Seq, even if they
  2750. * are duplicates.
  2751. */
  2752. concat<U>(...collections: Array<Iterable<U>>): Seq.Set<T | U>;
  2753. /**
  2754. * Returns a new Seq.Set with values passed through a
  2755. * `mapper` function.
  2756. *
  2757. * ```js
  2758. * Seq.Set([ 1, 2 ]).map(x => 10 * x)
  2759. * // Seq { 10, 20 }
  2760. * ```
  2761. *
  2762. * Note: `map()` always returns a new instance, even if it produced the
  2763. * same value at every step.
  2764. */
  2765. map<M>(
  2766. mapper: (value: T, key: T, iter: this) => M,
  2767. context?: any
  2768. ): Seq.Set<M>;
  2769. /**
  2770. * Flat-maps the Seq, returning a Seq of the same type.
  2771. *
  2772. * Similar to `seq.map(...).flatten(true)`.
  2773. */
  2774. flatMap<M>(
  2775. mapper: (value: T, key: T, iter: this) => Iterable<M>,
  2776. context?: any
  2777. ): Seq.Set<M>;
  2778. /**
  2779. * Returns a new Seq with only the values for which the `predicate`
  2780. * function returns true.
  2781. *
  2782. * Note: `filter()` always returns a new instance, even if it results in
  2783. * not filtering out any values.
  2784. */
  2785. filter<F extends T>(
  2786. predicate: (value: T, key: T, iter: this) => value is F,
  2787. context?: any
  2788. ): Seq.Set<F>;
  2789. filter(
  2790. predicate: (value: T, key: T, iter: this) => any,
  2791. context?: any
  2792. ): this;
  2793. }
  2794. }
  2795. /**
  2796. * Creates a Seq.
  2797. *
  2798. * Returns a particular kind of `Seq` based on the input.
  2799. *
  2800. * * If a `Seq`, that same `Seq`.
  2801. * * If an `Collection`, a `Seq` of the same kind (Keyed, Indexed, or Set).
  2802. * * If an Array-like, an `Seq.Indexed`.
  2803. * * If an Iterable Object, an `Seq.Indexed`.
  2804. * * If an Object, a `Seq.Keyed`.
  2805. *
  2806. * Note: An Iterator itself will be treated as an object, becoming a `Seq.Keyed`,
  2807. * which is usually not what you want. You should turn your Iterator Object into
  2808. * an iterable object by defining a Symbol.iterator (or @@iterator) method which
  2809. * returns `this`.
  2810. *
  2811. * Note: `Seq` is a conversion function and not a class, and does not use the
  2812. * `new` keyword during construction.
  2813. */
  2814. export function Seq<S extends Seq<any, any>>(seq: S): S;
  2815. export function Seq<K, V>(collection: Collection.Keyed<K, V>): Seq.Keyed<K, V>;
  2816. export function Seq<T>(collection: Collection.Indexed<T>): Seq.Indexed<T>;
  2817. export function Seq<T>(collection: Collection.Set<T>): Seq.Set<T>;
  2818. export function Seq<T>(collection: Iterable<T>): Seq.Indexed<T>;
  2819. export function Seq<V>(obj: {[key: string]: V}): Seq.Keyed<string, V>;
  2820. export function Seq(): Seq<any, any>;
  2821. export interface Seq<K, V> extends Collection<K, V> {
  2822. /**
  2823. * Some Seqs can describe their size lazily. When this is the case,
  2824. * size will be an integer. Otherwise it will be undefined.
  2825. *
  2826. * For example, Seqs returned from `map()` or `reverse()`
  2827. * preserve the size of the original `Seq` while `filter()` does not.
  2828. *
  2829. * Note: `Range`, `Repeat` and `Seq`s made from `Array`s and `Object`s will
  2830. * always have a size.
  2831. */
  2832. readonly size: number | undefined;
  2833. // Force evaluation
  2834. /**
  2835. * Because Sequences are lazy and designed to be chained together, they do
  2836. * not cache their results. For example, this map function is called a total
  2837. * of 6 times, as each `join` iterates the Seq of three values.
  2838. *
  2839. * var squares = Seq([ 1, 2, 3 ]).map(x => x * x)
  2840. * squares.join() + squares.join()
  2841. *
  2842. * If you know a `Seq` will be used multiple times, it may be more
  2843. * efficient to first cache it in memory. Here, the map function is called
  2844. * only 3 times.
  2845. *
  2846. * var squares = Seq([ 1, 2, 3 ]).map(x => x * x).cacheResult()
  2847. * squares.join() + squares.join()
  2848. *
  2849. * Use this method judiciously, as it must fully evaluate a Seq which can be
  2850. * a burden on memory and possibly performance.
  2851. *
  2852. * Note: after calling `cacheResult`, a Seq will always have a `size`.
  2853. */
  2854. cacheResult(): this;
  2855. // Sequence algorithms
  2856. /**
  2857. * Returns a new Seq with values passed through a
  2858. * `mapper` function.
  2859. *
  2860. * ```js
  2861. * const { Seq } = require('immutable')
  2862. * Seq([ 1, 2 ]).map(x => 10 * x)
  2863. * // Seq [ 10, 20 ]
  2864. * ```
  2865. *
  2866. * Note: `map()` always returns a new instance, even if it produced the same
  2867. * value at every step.
  2868. */
  2869. map<M>(
  2870. mapper: (value: V, key: K, iter: this) => M,
  2871. context?: any
  2872. ): Seq<K, M>;
  2873. /**
  2874. * Returns a new Seq with values passed through a
  2875. * `mapper` function.
  2876. *
  2877. * ```js
  2878. * const { Seq } = require('immutable')
  2879. * Seq([ 1, 2 ]).map(x => 10 * x)
  2880. * // Seq [ 10, 20 ]
  2881. * ```
  2882. *
  2883. * Note: `map()` always returns a new instance, even if it produced the same
  2884. * value at every step.
  2885. * Note: used only for sets.
  2886. */
  2887. map<M>(
  2888. mapper: (value: V, key: K, iter: this) => M,
  2889. context?: any
  2890. ): Seq<M, M>;
  2891. /**
  2892. * Flat-maps the Seq, returning a Seq of the same type.
  2893. *
  2894. * Similar to `seq.map(...).flatten(true)`.
  2895. */
  2896. flatMap<M>(
  2897. mapper: (value: V, key: K, iter: this) => Iterable<M>,
  2898. context?: any
  2899. ): Seq<K, M>;
  2900. /**
  2901. * Flat-maps the Seq, returning a Seq of the same type.
  2902. *
  2903. * Similar to `seq.map(...).flatten(true)`.
  2904. * Note: Used only for sets.
  2905. */
  2906. flatMap<M>(
  2907. mapper: (value: V, key: K, iter: this) => Iterable<M>,
  2908. context?: any
  2909. ): Seq<M, M>;
  2910. /**
  2911. * Returns a new Seq with only the values for which the `predicate`
  2912. * function returns true.
  2913. *
  2914. * Note: `filter()` always returns a new instance, even if it results in
  2915. * not filtering out any values.
  2916. */
  2917. filter<F extends V>(
  2918. predicate: (value: V, key: K, iter: this) => value is F,
  2919. context?: any
  2920. ): Seq<K, F>;
  2921. filter(
  2922. predicate: (value: V, key: K, iter: this) => any,
  2923. context?: any
  2924. ): this;
  2925. }
  2926. /**
  2927. * The `Collection` is a set of (key, value) entries which can be iterated, and
  2928. * is the base class for all collections in `immutable`, allowing them to
  2929. * make use of all the Collection methods (such as `map` and `filter`).
  2930. *
  2931. * Note: A collection is always iterated in the same order, however that order
  2932. * may not always be well defined, as is the case for the `Map` and `Set`.
  2933. *
  2934. * Collection is the abstract base class for concrete data structures. It
  2935. * cannot be constructed directly.
  2936. *
  2937. * Implementations should extend one of the subclasses, `Collection.Keyed`,
  2938. * `Collection.Indexed`, or `Collection.Set`.
  2939. */
  2940. export module Collection {
  2941. /**
  2942. * @deprecated use `const { isKeyed } = require('immutable')`
  2943. */
  2944. function isKeyed(maybeKeyed: any): maybeKeyed is Collection.Keyed<any, any>;
  2945. /**
  2946. * @deprecated use `const { isIndexed } = require('immutable')`
  2947. */
  2948. function isIndexed(maybeIndexed: any): maybeIndexed is Collection.Indexed<any>;
  2949. /**
  2950. * @deprecated use `const { isAssociative } = require('immutable')`
  2951. */
  2952. function isAssociative(maybeAssociative: any): maybeAssociative is Collection.Keyed<any, any> | Collection.Indexed<any>;
  2953. /**
  2954. * @deprecated use `const { isOrdered } = require('immutable')`
  2955. */
  2956. function isOrdered(maybeOrdered: any): boolean;
  2957. /**
  2958. * Keyed Collections have discrete keys tied to each value.
  2959. *
  2960. * When iterating `Collection.Keyed`, each iteration will yield a `[K, V]`
  2961. * tuple, in other words, `Collection#entries` is the default iterator for
  2962. * Keyed Collections.
  2963. */
  2964. export module Keyed {}
  2965. /**
  2966. * Creates a Collection.Keyed
  2967. *
  2968. * Similar to `Collection()`, however it expects collection-likes of [K, V]
  2969. * tuples if not constructed from a Collection.Keyed or JS Object.
  2970. *
  2971. * Note: `Collection.Keyed` is a conversion function and not a class, and
  2972. * does not use the `new` keyword during construction.
  2973. */
  2974. export function Keyed<K, V>(collection: Iterable<[K, V]>): Collection.Keyed<K, V>;
  2975. export function Keyed<V>(obj: {[key: string]: V}): Collection.Keyed<string, V>;
  2976. export interface Keyed<K, V> extends Collection<K, V> {
  2977. /**
  2978. * Deeply converts this Keyed collection to equivalent native JavaScript Object.
  2979. *
  2980. * Converts keys to Strings.
  2981. */
  2982. toJS(): Object;
  2983. /**
  2984. * Shallowly converts this Keyed collection to equivalent native JavaScript Object.
  2985. *
  2986. * Converts keys to Strings.
  2987. */
  2988. toJSON(): { [key: string]: V };
  2989. /**
  2990. * Shallowly converts this collection to an Array.
  2991. */
  2992. toArray(): Array<[K, V]>;
  2993. /**
  2994. * Returns Seq.Keyed.
  2995. * @override
  2996. */
  2997. toSeq(): Seq.Keyed<K, V>;
  2998. // Sequence functions
  2999. /**
  3000. * Returns a new Collection.Keyed of the same type where the keys and values
  3001. * have been flipped.
  3002. *
  3003. * <!-- runkit:activate -->
  3004. * ```js
  3005. * const { Map } = require('immutable')
  3006. * Map({ a: 'z', b: 'y' }).flip()
  3007. * // Map { "z": "a", "y": "b" }
  3008. * ```
  3009. */
  3010. flip(): Collection.Keyed<V, K>;
  3011. /**
  3012. * Returns a new Collection with other collections concatenated to this one.
  3013. */
  3014. concat<KC, VC>(...collections: Array<Iterable<[KC, VC]>>): Collection.Keyed<K | KC, V | VC>;
  3015. concat<C>(...collections: Array<{[key: string]: C}>): Collection.Keyed<K | string, V | C>;
  3016. /**
  3017. * Returns a new Collection.Keyed with values passed through a
  3018. * `mapper` function.
  3019. *
  3020. * ```js
  3021. * const { Collection } = require('immutable')
  3022. * Collection.Keyed({ a: 1, b: 2 }).map(x => 10 * x)
  3023. * // Seq { "a": 10, "b": 20 }
  3024. * ```
  3025. *
  3026. * Note: `map()` always returns a new instance, even if it produced the
  3027. * same value at every step.
  3028. */
  3029. map<M>(
  3030. mapper: (value: V, key: K, iter: this) => M,
  3031. context?: any
  3032. ): Collection.Keyed<K, M>;
  3033. /**
  3034. * Returns a new Collection.Keyed of the same type with keys passed through
  3035. * a `mapper` function.
  3036. *
  3037. * <!-- runkit:activate -->
  3038. * ```js
  3039. * const { Map } = require('immutable')
  3040. * Map({ a: 1, b: 2 }).mapKeys(x => x.toUpperCase())
  3041. * // Map { "A": 1, "B": 2 }
  3042. * ```
  3043. *
  3044. * Note: `mapKeys()` always returns a new instance, even if it produced
  3045. * the same key at every step.
  3046. */
  3047. mapKeys<M>(
  3048. mapper: (key: K, value: V, iter: this) => M,
  3049. context?: any
  3050. ): Collection.Keyed<M, V>;
  3051. /**
  3052. * Returns a new Collection.Keyed of the same type with entries
  3053. * ([key, value] tuples) passed through a `mapper` function.
  3054. *
  3055. * <!-- runkit:activate -->
  3056. * ```js
  3057. * const { Map } = require('immutable')
  3058. * Map({ a: 1, b: 2 })
  3059. * .mapEntries(([ k, v ]) => [ k.toUpperCase(), v * 2 ])
  3060. * // Map { "A": 2, "B": 4 }
  3061. * ```
  3062. *
  3063. * Note: `mapEntries()` always returns a new instance, even if it produced
  3064. * the same entry at every step.
  3065. */
  3066. mapEntries<KM, VM>(
  3067. mapper: (entry: [K, V], index: number, iter: this) => [KM, VM],
  3068. context?: any
  3069. ): Collection.Keyed<KM, VM>;
  3070. /**
  3071. * Flat-maps the Collection, returning a Collection of the same type.
  3072. *
  3073. * Similar to `collection.map(...).flatten(true)`.
  3074. */
  3075. flatMap<KM, VM>(
  3076. mapper: (value: V, key: K, iter: this) => Iterable<[KM, VM]>,
  3077. context?: any
  3078. ): Collection.Keyed<KM, VM>;
  3079. /**
  3080. * Returns a new Collection with only the values for which the `predicate`
  3081. * function returns true.
  3082. *
  3083. * Note: `filter()` always returns a new instance, even if it results in
  3084. * not filtering out any values.
  3085. */
  3086. filter<F extends V>(
  3087. predicate: (value: V, key: K, iter: this) => value is F,
  3088. context?: any
  3089. ): Collection.Keyed<K, F>;
  3090. filter(
  3091. predicate: (value: V, key: K, iter: this) => any,
  3092. context?: any
  3093. ): this;
  3094. [Symbol.iterator](): IterableIterator<[K, V]>;
  3095. }
  3096. /**
  3097. * Indexed Collections have incrementing numeric keys. They exhibit
  3098. * slightly different behavior than `Collection.Keyed` for some methods in order
  3099. * to better mirror the behavior of JavaScript's `Array`, and add methods
  3100. * which do not make sense on non-indexed Collections such as `indexOf`.
  3101. *
  3102. * Unlike JavaScript arrays, `Collection.Indexed`s are always dense. "Unset"
  3103. * indices and `undefined` indices are indistinguishable, and all indices from
  3104. * 0 to `size` are visited when iterated.
  3105. *
  3106. * All Collection.Indexed methods return re-indexed Collections. In other words,
  3107. * indices always start at 0 and increment until size. If you wish to
  3108. * preserve indices, using them as keys, convert to a Collection.Keyed by
  3109. * calling `toKeyedSeq`.
  3110. */
  3111. export module Indexed {}
  3112. /**
  3113. * Creates a new Collection.Indexed.
  3114. *
  3115. * Note: `Collection.Indexed` is a conversion function and not a class, and
  3116. * does not use the `new` keyword during construction.
  3117. */
  3118. export function Indexed<T>(collection: Iterable<T>): Collection.Indexed<T>;
  3119. export interface Indexed<T> extends Collection<number, T> {
  3120. /**
  3121. * Deeply converts this Indexed collection to equivalent native JavaScript Array.
  3122. */
  3123. toJS(): Array<any>;
  3124. /**
  3125. * Shallowly converts this Indexed collection to equivalent native JavaScript Array.
  3126. */
  3127. toJSON(): Array<T>;
  3128. /**
  3129. * Shallowly converts this collection to an Array.
  3130. */
  3131. toArray(): Array<T>;
  3132. // Reading values
  3133. /**
  3134. * Returns the value associated with the provided index, or notSetValue if
  3135. * the index is beyond the bounds of the Collection.
  3136. *
  3137. * `index` may be a negative number, which indexes back from the end of the
  3138. * Collection. `s.get(-1)` gets the last item in the Collection.
  3139. */
  3140. get<NSV>(index: number, notSetValue: NSV): T | NSV;
  3141. get(index: number): T | undefined;
  3142. // Conversion to Seq
  3143. /**
  3144. * Returns Seq.Indexed.
  3145. * @override
  3146. */
  3147. toSeq(): Seq.Indexed<T>;
  3148. /**
  3149. * If this is a collection of [key, value] entry tuples, it will return a
  3150. * Seq.Keyed of those entries.
  3151. */
  3152. fromEntrySeq(): Seq.Keyed<any, any>;
  3153. // Combination
  3154. /**
  3155. * Returns a Collection of the same type with `separator` between each item
  3156. * in this Collection.
  3157. */
  3158. interpose(separator: T): this;
  3159. /**
  3160. * Returns a Collection of the same type with the provided `collections`
  3161. * interleaved into this collection.
  3162. *
  3163. * The resulting Collection includes the first item from each, then the
  3164. * second from each, etc.
  3165. *
  3166. * <!-- runkit:activate
  3167. * { "preamble": "require('immutable')"}
  3168. * -->
  3169. * ```js
  3170. * const { List } = require('immutable')
  3171. * List([ 1, 2, 3 ]).interleave(List([ 'A', 'B', 'C' ]))
  3172. * // List [ 1, "A", 2, "B", 3, "C"" ]
  3173. * ```
  3174. *
  3175. * The shortest Collection stops interleave.
  3176. *
  3177. * <!-- runkit:activate
  3178. * { "preamble": "const { List } = require('immutable')" }
  3179. * -->
  3180. * ```js
  3181. * List([ 1, 2, 3 ]).interleave(
  3182. * List([ 'A', 'B' ]),
  3183. * List([ 'X', 'Y', 'Z' ])
  3184. * )
  3185. * // List [ 1, "A", "X", 2, "B", "Y"" ]
  3186. * ```
  3187. *
  3188. * Since `interleave()` re-indexes values, it produces a complete copy,
  3189. * which has `O(N)` complexity.
  3190. *
  3191. * Note: `interleave` *cannot* be used in `withMutations`.
  3192. */
  3193. interleave(...collections: Array<Collection<any, T>>): this;
  3194. /**
  3195. * Splice returns a new indexed Collection by replacing a region of this
  3196. * Collection with new values. If values are not provided, it only skips the
  3197. * region to be removed.
  3198. *
  3199. * `index` may be a negative number, which indexes back from the end of the
  3200. * Collection. `s.splice(-2)` splices after the second to last item.
  3201. *
  3202. * <!-- runkit:activate -->
  3203. * ```js
  3204. * const { List } = require('immutable')
  3205. * List([ 'a', 'b', 'c', 'd' ]).splice(1, 2, 'q', 'r', 's')
  3206. * // List [ "a", "q", "r", "s", "d" ]
  3207. * ```
  3208. *
  3209. * Since `splice()` re-indexes values, it produces a complete copy, which
  3210. * has `O(N)` complexity.
  3211. *
  3212. * Note: `splice` *cannot* be used in `withMutations`.
  3213. */
  3214. splice(
  3215. index: number,
  3216. removeNum: number,
  3217. ...values: Array<T>
  3218. ): this;
  3219. /**
  3220. * Returns a Collection of the same type "zipped" with the provided
  3221. * collections.
  3222. *
  3223. * Like `zipWith`, but using the default `zipper`: creating an `Array`.
  3224. *
  3225. *
  3226. * <!-- runkit:activate
  3227. * { "preamble": "const { List } = require('immutable')" }
  3228. * -->
  3229. * ```js
  3230. * const a = List([ 1, 2, 3 ]);
  3231. * const b = List([ 4, 5, 6 ]);
  3232. * const c = a.zip(b); // List [ [ 1, 4 ], [ 2, 5 ], [ 3, 6 ] ]
  3233. * ```
  3234. */
  3235. zip<U>(other: Collection<any, U>): Collection.Indexed<[T,U]>;
  3236. zip<U,V>(other: Collection<any, U>, other2: Collection<any, V>): Collection.Indexed<[T,U,V]>;
  3237. zip(...collections: Array<Collection<any, any>>): Collection.Indexed<any>;
  3238. /**
  3239. * Returns a Collection "zipped" with the provided collections.
  3240. *
  3241. * Unlike `zip`, `zipAll` continues zipping until the longest collection is
  3242. * exhausted. Missing values from shorter collections are filled with `undefined`.
  3243. *
  3244. * ```js
  3245. * const a = List([ 1, 2 ]);
  3246. * const b = List([ 3, 4, 5 ]);
  3247. * const c = a.zipAll(b); // List [ [ 1, 3 ], [ 2, 4 ], [ undefined, 5 ] ]
  3248. * ```
  3249. */
  3250. zipAll<U>(other: Collection<any, U>): Collection.Indexed<[T,U]>;
  3251. zipAll<U,V>(other: Collection<any, U>, other2: Collection<any, V>): Collection.Indexed<[T,U,V]>;
  3252. zipAll(...collections: Array<Collection<any, any>>): Collection.Indexed<any>;
  3253. /**
  3254. * Returns a Collection of the same type "zipped" with the provided
  3255. * collections by using a custom `zipper` function.
  3256. *
  3257. * <!-- runkit:activate
  3258. * { "preamble": "const { List } = require('immutable')" }
  3259. * -->
  3260. * ```js
  3261. * const a = List([ 1, 2, 3 ]);
  3262. * const b = List([ 4, 5, 6 ]);
  3263. * const c = a.zipWith((a, b) => a + b, b);
  3264. * // List [ 5, 7, 9 ]
  3265. * ```
  3266. */
  3267. zipWith<U, Z>(
  3268. zipper: (value: T, otherValue: U) => Z,
  3269. otherCollection: Collection<any, U>
  3270. ): Collection.Indexed<Z>;
  3271. zipWith<U, V, Z>(
  3272. zipper: (value: T, otherValue: U, thirdValue: V) => Z,
  3273. otherCollection: Collection<any, U>,
  3274. thirdCollection: Collection<any, V>
  3275. ): Collection.Indexed<Z>;
  3276. zipWith<Z>(
  3277. zipper: (...any: Array<any>) => Z,
  3278. ...collections: Array<Collection<any, any>>
  3279. ): Collection.Indexed<Z>;
  3280. // Search for value
  3281. /**
  3282. * Returns the first index at which a given value can be found in the
  3283. * Collection, or -1 if it is not present.
  3284. */
  3285. indexOf(searchValue: T): number;
  3286. /**
  3287. * Returns the last index at which a given value can be found in the
  3288. * Collection, or -1 if it is not present.
  3289. */
  3290. lastIndexOf(searchValue: T): number;
  3291. /**
  3292. * Returns the first index in the Collection where a value satisfies the
  3293. * provided predicate function. Otherwise -1 is returned.
  3294. */
  3295. findIndex(
  3296. predicate: (value: T, index: number, iter: this) => boolean,
  3297. context?: any
  3298. ): number;
  3299. /**
  3300. * Returns the last index in the Collection where a value satisfies the
  3301. * provided predicate function. Otherwise -1 is returned.
  3302. */
  3303. findLastIndex(
  3304. predicate: (value: T, index: number, iter: this) => boolean,
  3305. context?: any
  3306. ): number;
  3307. // Sequence algorithms
  3308. /**
  3309. * Returns a new Collection with other collections concatenated to this one.
  3310. */
  3311. concat<C>(...valuesOrCollections: Array<Iterable<C> | C>): Collection.Indexed<T | C>;
  3312. /**
  3313. * Returns a new Collection.Indexed with values passed through a
  3314. * `mapper` function.
  3315. *
  3316. * ```js
  3317. * const { Collection } = require('immutable')
  3318. * Collection.Indexed([1,2]).map(x => 10 * x)
  3319. * // Seq [ 1, 2 ]
  3320. * ```
  3321. *
  3322. * Note: `map()` always returns a new instance, even if it produced the
  3323. * same value at every step.
  3324. */
  3325. map<M>(
  3326. mapper: (value: T, key: number, iter: this) => M,
  3327. context?: any
  3328. ): Collection.Indexed<M>;
  3329. /**
  3330. * Flat-maps the Collection, returning a Collection of the same type.
  3331. *
  3332. * Similar to `collection.map(...).flatten(true)`.
  3333. */
  3334. flatMap<M>(
  3335. mapper: (value: T, key: number, iter: this) => Iterable<M>,
  3336. context?: any
  3337. ): Collection.Indexed<M>;
  3338. /**
  3339. * Returns a new Collection with only the values for which the `predicate`
  3340. * function returns true.
  3341. *
  3342. * Note: `filter()` always returns a new instance, even if it results in
  3343. * not filtering out any values.
  3344. */
  3345. filter<F extends T>(
  3346. predicate: (value: T, index: number, iter: this) => value is F,
  3347. context?: any
  3348. ): Collection.Indexed<F>;
  3349. filter(
  3350. predicate: (value: T, index: number, iter: this) => any,
  3351. context?: any
  3352. ): this;
  3353. [Symbol.iterator](): IterableIterator<T>;
  3354. }
  3355. /**
  3356. * Set Collections only represent values. They have no associated keys or
  3357. * indices. Duplicate values are possible in the lazy `Seq.Set`s, however
  3358. * the concrete `Set` Collection does not allow duplicate values.
  3359. *
  3360. * Collection methods on Collection.Set such as `map` and `forEach` will provide
  3361. * the value as both the first and second arguments to the provided function.
  3362. *
  3363. * ```js
  3364. * const { Collection } = require('immutable')
  3365. * const seq = Collection.Set([ 'A', 'B', 'C' ])
  3366. * // Seq { "A", "B", "C" }
  3367. * seq.forEach((v, k) =>
  3368. * assert.equal(v, k)
  3369. * )
  3370. * ```
  3371. */
  3372. export module Set {}
  3373. /**
  3374. * Similar to `Collection()`, but always returns a Collection.Set.
  3375. *
  3376. * Note: `Collection.Set` is a factory function and not a class, and does
  3377. * not use the `new` keyword during construction.
  3378. */
  3379. export function Set<T>(collection: Iterable<T>): Collection.Set<T>;
  3380. export interface Set<T> extends Collection<T, T> {
  3381. /**
  3382. * Deeply converts this Set collection to equivalent native JavaScript Array.
  3383. */
  3384. toJS(): Array<any>;
  3385. /**
  3386. * Shallowly converts this Set collection to equivalent native JavaScript Array.
  3387. */
  3388. toJSON(): Array<T>;
  3389. /**
  3390. * Shallowly converts this collection to an Array.
  3391. */
  3392. toArray(): Array<T>;
  3393. /**
  3394. * Returns Seq.Set.
  3395. * @override
  3396. */
  3397. toSeq(): Seq.Set<T>;
  3398. // Sequence algorithms
  3399. /**
  3400. * Returns a new Collection with other collections concatenated to this one.
  3401. */
  3402. concat<U>(...collections: Array<Iterable<U>>): Collection.Set<T | U>;
  3403. /**
  3404. * Returns a new Collection.Set with values passed through a
  3405. * `mapper` function.
  3406. *
  3407. * ```
  3408. * Collection.Set([ 1, 2 ]).map(x => 10 * x)
  3409. * // Seq { 1, 2 }
  3410. * ```
  3411. *
  3412. * Note: `map()` always returns a new instance, even if it produced the
  3413. * same value at every step.
  3414. */
  3415. map<M>(
  3416. mapper: (value: T, key: T, iter: this) => M,
  3417. context?: any
  3418. ): Collection.Set<M>;
  3419. /**
  3420. * Flat-maps the Collection, returning a Collection of the same type.
  3421. *
  3422. * Similar to `collection.map(...).flatten(true)`.
  3423. */
  3424. flatMap<M>(
  3425. mapper: (value: T, key: T, iter: this) => Iterable<M>,
  3426. context?: any
  3427. ): Collection.Set<M>;
  3428. /**
  3429. * Returns a new Collection with only the values for which the `predicate`
  3430. * function returns true.
  3431. *
  3432. * Note: `filter()` always returns a new instance, even if it results in
  3433. * not filtering out any values.
  3434. */
  3435. filter<F extends T>(
  3436. predicate: (value: T, key: T, iter: this) => value is F,
  3437. context?: any
  3438. ): Collection.Set<F>;
  3439. filter(
  3440. predicate: (value: T, key: T, iter: this) => any,
  3441. context?: any
  3442. ): this;
  3443. [Symbol.iterator](): IterableIterator<T>;
  3444. }
  3445. }
  3446. /**
  3447. * Creates a Collection.
  3448. *
  3449. * The type of Collection created is based on the input.
  3450. *
  3451. * * If an `Collection`, that same `Collection`.
  3452. * * If an Array-like, an `Collection.Indexed`.
  3453. * * If an Object with an Iterator defined, an `Collection.Indexed`.
  3454. * * If an Object, an `Collection.Keyed`.
  3455. *
  3456. * This methods forces the conversion of Objects and Strings to Collections.
  3457. * If you want to ensure that a Collection of one item is returned, use
  3458. * `Seq.of`.
  3459. *
  3460. * Note: An Iterator itself will be treated as an object, becoming a `Seq.Keyed`,
  3461. * which is usually not what you want. You should turn your Iterator Object into
  3462. * an iterable object by defining a Symbol.iterator (or @@iterator) method which
  3463. * returns `this`.
  3464. *
  3465. * Note: `Collection` is a conversion function and not a class, and does not
  3466. * use the `new` keyword during construction.
  3467. */
  3468. export function Collection<I extends Collection<any, any>>(collection: I): I;
  3469. export function Collection<T>(collection: Iterable<T>): Collection.Indexed<T>;
  3470. export function Collection<V>(obj: {[key: string]: V}): Collection.Keyed<string, V>;
  3471. export interface Collection<K, V> extends ValueObject {
  3472. // Value equality
  3473. /**
  3474. * True if this and the other Collection have value equality, as defined
  3475. * by `Immutable.is()`.
  3476. *
  3477. * Note: This is equivalent to `Immutable.is(this, other)`, but provided to
  3478. * allow for chained expressions.
  3479. */
  3480. equals(other: any): boolean;
  3481. /**
  3482. * Computes and returns the hashed identity for this Collection.
  3483. *
  3484. * The `hashCode` of a Collection is used to determine potential equality,
  3485. * and is used when adding this to a `Set` or as a key in a `Map`, enabling
  3486. * lookup via a different instance.
  3487. *
  3488. * <!-- runkit:activate
  3489. * { "preamble": "const { Set, List } = require('immutable')" }
  3490. * -->
  3491. * ```js
  3492. * const a = List([ 1, 2, 3 ]);
  3493. * const b = List([ 1, 2, 3 ]);
  3494. * assert.notStrictEqual(a, b); // different instances
  3495. * const set = Set([ a ]);
  3496. * assert.equal(set.has(b), true);
  3497. * ```
  3498. *
  3499. * If two values have the same `hashCode`, they are [not guaranteed
  3500. * to be equal][Hash Collision]. If two values have different `hashCode`s,
  3501. * they must not be equal.
  3502. *
  3503. * [Hash Collision]: http://en.wikipedia.org/wiki/Collision_(computer_science)
  3504. */
  3505. hashCode(): number;
  3506. // Reading values
  3507. /**
  3508. * Returns the value associated with the provided key, or notSetValue if
  3509. * the Collection does not contain this key.
  3510. *
  3511. * Note: it is possible a key may be associated with an `undefined` value,
  3512. * so if `notSetValue` is not provided and this method returns `undefined`,
  3513. * that does not guarantee the key was not found.
  3514. */
  3515. get<NSV>(key: K, notSetValue: NSV): V | NSV;
  3516. get(key: K): V | undefined;
  3517. /**
  3518. * True if a key exists within this `Collection`, using `Immutable.is`
  3519. * to determine equality
  3520. */
  3521. has(key: K): boolean;
  3522. /**
  3523. * True if a value exists within this `Collection`, using `Immutable.is`
  3524. * to determine equality
  3525. * @alias contains
  3526. */
  3527. includes(value: V): boolean;
  3528. contains(value: V): boolean;
  3529. /**
  3530. * In case the `Collection` is not empty returns the first element of the
  3531. * `Collection`.
  3532. * In case the `Collection` is empty returns the optional default
  3533. * value if provided, if no default value is provided returns undefined.
  3534. */
  3535. first<NSV>(notSetValue?: NSV): V | NSV;
  3536. /**
  3537. * In case the `Collection` is not empty returns the last element of the
  3538. * `Collection`.
  3539. * In case the `Collection` is empty returns the optional default
  3540. * value if provided, if no default value is provided returns undefined.
  3541. */
  3542. last<NSV>(notSetValue?: NSV): V | NSV;
  3543. // Reading deep values
  3544. /**
  3545. * Returns the value found by following a path of keys or indices through
  3546. * nested Collections.
  3547. *
  3548. * <!-- runkit:activate -->
  3549. * ```js
  3550. * const { Map, List } = require('immutable')
  3551. * const deepData = Map({ x: List([ Map({ y: 123 }) ]) });
  3552. * deepData.getIn(['x', 0, 'y']) // 123
  3553. * ```
  3554. *
  3555. * Plain JavaScript Object or Arrays may be nested within an Immutable.js
  3556. * Collection, and getIn() can access those values as well:
  3557. *
  3558. * <!-- runkit:activate -->
  3559. * ```js
  3560. * const { Map, List } = require('immutable')
  3561. * const deepData = Map({ x: [ { y: 123 } ] });
  3562. * deepData.getIn(['x', 0, 'y']) // 123
  3563. * ```
  3564. */
  3565. getIn(searchKeyPath: Iterable<any>, notSetValue?: any): any;
  3566. /**
  3567. * True if the result of following a path of keys or indices through nested
  3568. * Collections results in a set value.
  3569. */
  3570. hasIn(searchKeyPath: Iterable<any>): boolean;
  3571. // Persistent changes
  3572. /**
  3573. * This can be very useful as a way to "chain" a normal function into a
  3574. * sequence of methods. RxJS calls this "let" and lodash calls it "thru".
  3575. *
  3576. * For example, to sum a Seq after mapping and filtering:
  3577. *
  3578. * <!-- runkit:activate -->
  3579. * ```js
  3580. * const { Seq } = require('immutable')
  3581. *
  3582. * function sum(collection) {
  3583. * return collection.reduce((sum, x) => sum + x, 0)
  3584. * }
  3585. *
  3586. * Seq([ 1, 2, 3 ])
  3587. * .map(x => x + 1)
  3588. * .filter(x => x % 2 === 0)
  3589. * .update(sum)
  3590. * // 6
  3591. * ```
  3592. */
  3593. update<R>(updater: (value: this) => R): R;
  3594. // Conversion to JavaScript types
  3595. /**
  3596. * Deeply converts this Collection to equivalent native JavaScript Array or Object.
  3597. *
  3598. * `Collection.Indexed`, and `Collection.Set` become `Array`, while
  3599. * `Collection.Keyed` become `Object`, converting keys to Strings.
  3600. */
  3601. toJS(): Array<any> | { [key: string]: any };
  3602. /**
  3603. * Shallowly converts this Collection to equivalent native JavaScript Array or Object.
  3604. *
  3605. * `Collection.Indexed`, and `Collection.Set` become `Array`, while
  3606. * `Collection.Keyed` become `Object`, converting keys to Strings.
  3607. */
  3608. toJSON(): Array<V> | { [key: string]: V };
  3609. /**
  3610. * Shallowly converts this collection to an Array.
  3611. *
  3612. * `Collection.Indexed`, and `Collection.Set` produce an Array of values.
  3613. * `Collection.Keyed` produce an Array of [key, value] tuples.
  3614. */
  3615. toArray(): Array<V> | Array<[K, V]>;
  3616. /**
  3617. * Shallowly converts this Collection to an Object.
  3618. *
  3619. * Converts keys to Strings.
  3620. */
  3621. toObject(): { [key: string]: V };
  3622. // Conversion to Collections
  3623. /**
  3624. * Converts this Collection to a Map, Throws if keys are not hashable.
  3625. *
  3626. * Note: This is equivalent to `Map(this.toKeyedSeq())`, but provided
  3627. * for convenience and to allow for chained expressions.
  3628. */
  3629. toMap(): Map<K, V>;
  3630. /**
  3631. * Converts this Collection to a Map, maintaining the order of iteration.
  3632. *
  3633. * Note: This is equivalent to `OrderedMap(this.toKeyedSeq())`, but
  3634. * provided for convenience and to allow for chained expressions.
  3635. */
  3636. toOrderedMap(): OrderedMap<K, V>;
  3637. /**
  3638. * Converts this Collection to a Set, discarding keys. Throws if values
  3639. * are not hashable.
  3640. *
  3641. * Note: This is equivalent to `Set(this)`, but provided to allow for
  3642. * chained expressions.
  3643. */
  3644. toSet(): Set<V>;
  3645. /**
  3646. * Converts this Collection to a Set, maintaining the order of iteration and
  3647. * discarding keys.
  3648. *
  3649. * Note: This is equivalent to `OrderedSet(this.valueSeq())`, but provided
  3650. * for convenience and to allow for chained expressions.
  3651. */
  3652. toOrderedSet(): OrderedSet<V>;
  3653. /**
  3654. * Converts this Collection to a List, discarding keys.
  3655. *
  3656. * This is similar to `List(collection)`, but provided to allow for chained
  3657. * expressions. However, when called on `Map` or other keyed collections,
  3658. * `collection.toList()` discards the keys and creates a list of only the
  3659. * values, whereas `List(collection)` creates a list of entry tuples.
  3660. *
  3661. * <!-- runkit:activate -->
  3662. * ```js
  3663. * const { Map, List } = require('immutable')
  3664. * var myMap = Map({ a: 'Apple', b: 'Banana' })
  3665. * List(myMap) // List [ [ "a", "Apple" ], [ "b", "Banana" ] ]
  3666. * myMap.toList() // List [ "Apple", "Banana" ]
  3667. * ```
  3668. */
  3669. toList(): List<V>;
  3670. /**
  3671. * Converts this Collection to a Stack, discarding keys. Throws if values
  3672. * are not hashable.
  3673. *
  3674. * Note: This is equivalent to `Stack(this)`, but provided to allow for
  3675. * chained expressions.
  3676. */
  3677. toStack(): Stack<V>;
  3678. // Conversion to Seq
  3679. /**
  3680. * Converts this Collection to a Seq of the same kind (indexed,
  3681. * keyed, or set).
  3682. */
  3683. toSeq(): Seq<K, V>;
  3684. /**
  3685. * Returns a Seq.Keyed from this Collection where indices are treated as keys.
  3686. *
  3687. * This is useful if you want to operate on an
  3688. * Collection.Indexed and preserve the [index, value] pairs.
  3689. *
  3690. * The returned Seq will have identical iteration order as
  3691. * this Collection.
  3692. *
  3693. * <!-- runkit:activate -->
  3694. * ```js
  3695. * const { Seq } = require('immutable')
  3696. * const indexedSeq = Seq([ 'A', 'B', 'C' ])
  3697. * // Seq [ "A", "B", "C" ]
  3698. * indexedSeq.filter(v => v === 'B')
  3699. * // Seq [ "B" ]
  3700. * const keyedSeq = indexedSeq.toKeyedSeq()
  3701. * // Seq { 0: "A", 1: "B", 2: "C" }
  3702. * keyedSeq.filter(v => v === 'B')
  3703. * // Seq { 1: "B" }
  3704. * ```
  3705. */
  3706. toKeyedSeq(): Seq.Keyed<K, V>;
  3707. /**
  3708. * Returns an Seq.Indexed of the values of this Collection, discarding keys.
  3709. */
  3710. toIndexedSeq(): Seq.Indexed<V>;
  3711. /**
  3712. * Returns a Seq.Set of the values of this Collection, discarding keys.
  3713. */
  3714. toSetSeq(): Seq.Set<V>;
  3715. // Iterators
  3716. /**
  3717. * An iterator of this `Collection`'s keys.
  3718. *
  3719. * Note: this will return an ES6 iterator which does not support
  3720. * Immutable.js sequence algorithms. Use `keySeq` instead, if this is
  3721. * what you want.
  3722. */
  3723. keys(): IterableIterator<K>;
  3724. /**
  3725. * An iterator of this `Collection`'s values.
  3726. *
  3727. * Note: this will return an ES6 iterator which does not support
  3728. * Immutable.js sequence algorithms. Use `valueSeq` instead, if this is
  3729. * what you want.
  3730. */
  3731. values(): IterableIterator<V>;
  3732. /**
  3733. * An iterator of this `Collection`'s entries as `[ key, value ]` tuples.
  3734. *
  3735. * Note: this will return an ES6 iterator which does not support
  3736. * Immutable.js sequence algorithms. Use `entrySeq` instead, if this is
  3737. * what you want.
  3738. */
  3739. entries(): IterableIterator<[K, V]>;
  3740. // Collections (Seq)
  3741. /**
  3742. * Returns a new Seq.Indexed of the keys of this Collection,
  3743. * discarding values.
  3744. */
  3745. keySeq(): Seq.Indexed<K>;
  3746. /**
  3747. * Returns an Seq.Indexed of the values of this Collection, discarding keys.
  3748. */
  3749. valueSeq(): Seq.Indexed<V>;
  3750. /**
  3751. * Returns a new Seq.Indexed of [key, value] tuples.
  3752. */
  3753. entrySeq(): Seq.Indexed<[K, V]>;
  3754. // Sequence algorithms
  3755. /**
  3756. * Returns a new Collection of the same type with values passed through a
  3757. * `mapper` function.
  3758. *
  3759. * <!-- runkit:activate -->
  3760. * ```js
  3761. * const { Collection } = require('immutable')
  3762. * Collection({ a: 1, b: 2 }).map(x => 10 * x)
  3763. * // Seq { "a": 10, "b": 20 }
  3764. * ```
  3765. *
  3766. * Note: `map()` always returns a new instance, even if it produced the same
  3767. * value at every step.
  3768. */
  3769. map<M>(
  3770. mapper: (value: V, key: K, iter: this) => M,
  3771. context?: any
  3772. ): Collection<K, M>;
  3773. /**
  3774. * Note: used only for sets, which return Collection<M, M> but are otherwise
  3775. * identical to normal `map()`.
  3776. *
  3777. * @ignore
  3778. */
  3779. map<M>(...args: never[]): any;
  3780. /**
  3781. * Returns a new Collection of the same type with only the entries for which
  3782. * the `predicate` function returns true.
  3783. *
  3784. * <!-- runkit:activate -->
  3785. * ```js
  3786. * const { Map } = require('immutable')
  3787. * Map({ a: 1, b: 2, c: 3, d: 4}).filter(x => x % 2 === 0)
  3788. * // Map { "b": 2, "d": 4 }
  3789. * ```
  3790. *
  3791. * Note: `filter()` always returns a new instance, even if it results in
  3792. * not filtering out any values.
  3793. */
  3794. filter<F extends V>(
  3795. predicate: (value: V, key: K, iter: this) => value is F,
  3796. context?: any
  3797. ): Collection<K, F>;
  3798. filter(
  3799. predicate: (value: V, key: K, iter: this) => any,
  3800. context?: any
  3801. ): this;
  3802. /**
  3803. * Returns a new Collection of the same type with only the entries for which
  3804. * the `predicate` function returns false.
  3805. *
  3806. * <!-- runkit:activate -->
  3807. * ```js
  3808. * const { Map } = require('immutable')
  3809. * Map({ a: 1, b: 2, c: 3, d: 4}).filterNot(x => x % 2 === 0)
  3810. * // Map { "a": 1, "c": 3 }
  3811. * ```
  3812. *
  3813. * Note: `filterNot()` always returns a new instance, even if it results in
  3814. * not filtering out any values.
  3815. */
  3816. filterNot(
  3817. predicate: (value: V, key: K, iter: this) => boolean,
  3818. context?: any
  3819. ): this;
  3820. /**
  3821. * Returns a new Collection of the same type in reverse order.
  3822. */
  3823. reverse(): this;
  3824. /**
  3825. * Returns a new Collection of the same type which includes the same entries,
  3826. * stably sorted by using a `comparator`.
  3827. *
  3828. * If a `comparator` is not provided, a default comparator uses `<` and `>`.
  3829. *
  3830. * `comparator(valueA, valueB)`:
  3831. *
  3832. * * Returns `0` if the elements should not be swapped.
  3833. * * Returns `-1` (or any negative number) if `valueA` comes before `valueB`
  3834. * * Returns `1` (or any positive number) if `valueA` comes after `valueB`
  3835. * * Is pure, i.e. it must always return the same value for the same pair
  3836. * of values.
  3837. *
  3838. * When sorting collections which have no defined order, their ordered
  3839. * equivalents will be returned. e.g. `map.sort()` returns OrderedMap.
  3840. *
  3841. * <!-- runkit:activate -->
  3842. * ```js
  3843. * const { Map } = require('immutable')
  3844. * Map({ "c": 3, "a": 1, "b": 2 }).sort((a, b) => {
  3845. * if (a < b) { return -1; }
  3846. * if (a > b) { return 1; }
  3847. * if (a === b) { return 0; }
  3848. * });
  3849. * // OrderedMap { "a": 1, "b": 2, "c": 3 }
  3850. * ```
  3851. *
  3852. * Note: `sort()` Always returns a new instance, even if the original was
  3853. * already sorted.
  3854. *
  3855. * Note: This is always an eager operation.
  3856. */
  3857. sort(comparator?: (valueA: V, valueB: V) => number): this;
  3858. /**
  3859. * Like `sort`, but also accepts a `comparatorValueMapper` which allows for
  3860. * sorting by more sophisticated means:
  3861. *
  3862. * hitters.sortBy(hitter => hitter.avgHits)
  3863. *
  3864. * Note: `sortBy()` Always returns a new instance, even if the original was
  3865. * already sorted.
  3866. *
  3867. * Note: This is always an eager operation.
  3868. */
  3869. sortBy<C>(
  3870. comparatorValueMapper: (value: V, key: K, iter: this) => C,
  3871. comparator?: (valueA: C, valueB: C) => number
  3872. ): this;
  3873. /**
  3874. * Returns a `Collection.Keyed` of `Collection.Keyeds`, grouped by the return
  3875. * value of the `grouper` function.
  3876. *
  3877. * Note: This is always an eager operation.
  3878. *
  3879. * <!-- runkit:activate -->
  3880. * ```js
  3881. * const { List, Map } = require('immutable')
  3882. * const listOfMaps = List([
  3883. * Map({ v: 0 }),
  3884. * Map({ v: 1 }),
  3885. * Map({ v: 1 }),
  3886. * Map({ v: 0 }),
  3887. * Map({ v: 2 })
  3888. * ])
  3889. * const groupsOfMaps = listOfMaps.groupBy(x => x.get('v'))
  3890. * // Map {
  3891. * // 0: List [ Map{ "v": 0 }, Map { "v": 0 } ],
  3892. * // 1: List [ Map{ "v": 1 }, Map { "v": 1 } ],
  3893. * // 2: List [ Map{ "v": 2 } ],
  3894. * // }
  3895. * ```
  3896. */
  3897. groupBy<G>(
  3898. grouper: (value: V, key: K, iter: this) => G,
  3899. context?: any
  3900. ): /*Map*/Seq.Keyed<G, /*this*/Collection<K, V>>;
  3901. // Side effects
  3902. /**
  3903. * The `sideEffect` is executed for every entry in the Collection.
  3904. *
  3905. * Unlike `Array#forEach`, if any call of `sideEffect` returns
  3906. * `false`, the iteration will stop. Returns the number of entries iterated
  3907. * (including the last iteration which returned false).
  3908. */
  3909. forEach(
  3910. sideEffect: (value: V, key: K, iter: this) => any,
  3911. context?: any
  3912. ): number;
  3913. // Creating subsets
  3914. /**
  3915. * Returns a new Collection of the same type representing a portion of this
  3916. * Collection from start up to but not including end.
  3917. *
  3918. * If begin is negative, it is offset from the end of the Collection. e.g.
  3919. * `slice(-2)` returns a Collection of the last two entries. If it is not
  3920. * provided the new Collection will begin at the beginning of this Collection.
  3921. *
  3922. * If end is negative, it is offset from the end of the Collection. e.g.
  3923. * `slice(0, -1)` returns a Collection of everything but the last entry. If
  3924. * it is not provided, the new Collection will continue through the end of
  3925. * this Collection.
  3926. *
  3927. * If the requested slice is equivalent to the current Collection, then it
  3928. * will return itself.
  3929. */
  3930. slice(begin?: number, end?: number): this;
  3931. /**
  3932. * Returns a new Collection of the same type containing all entries except
  3933. * the first.
  3934. */
  3935. rest(): this;
  3936. /**
  3937. * Returns a new Collection of the same type containing all entries except
  3938. * the last.
  3939. */
  3940. butLast(): this;
  3941. /**
  3942. * Returns a new Collection of the same type which excludes the first `amount`
  3943. * entries from this Collection.
  3944. */
  3945. skip(amount: number): this;
  3946. /**
  3947. * Returns a new Collection of the same type which excludes the last `amount`
  3948. * entries from this Collection.
  3949. */
  3950. skipLast(amount: number): this;
  3951. /**
  3952. * Returns a new Collection of the same type which includes entries starting
  3953. * from when `predicate` first returns false.
  3954. *
  3955. * <!-- runkit:activate -->
  3956. * ```js
  3957. * const { List } = require('immutable')
  3958. * List([ 'dog', 'frog', 'cat', 'hat', 'god' ])
  3959. * .skipWhile(x => x.match(/g/))
  3960. * // List [ "cat", "hat", "god"" ]
  3961. * ```
  3962. */
  3963. skipWhile(
  3964. predicate: (value: V, key: K, iter: this) => boolean,
  3965. context?: any
  3966. ): this;
  3967. /**
  3968. * Returns a new Collection of the same type which includes entries starting
  3969. * from when `predicate` first returns true.
  3970. *
  3971. * <!-- runkit:activate -->
  3972. * ```js
  3973. * const { List } = require('immutable')
  3974. * List([ 'dog', 'frog', 'cat', 'hat', 'god' ])
  3975. * .skipUntil(x => x.match(/hat/))
  3976. * // List [ "hat", "god"" ]
  3977. * ```
  3978. */
  3979. skipUntil(
  3980. predicate: (value: V, key: K, iter: this) => boolean,
  3981. context?: any
  3982. ): this;
  3983. /**
  3984. * Returns a new Collection of the same type which includes the first `amount`
  3985. * entries from this Collection.
  3986. */
  3987. take(amount: number): this;
  3988. /**
  3989. * Returns a new Collection of the same type which includes the last `amount`
  3990. * entries from this Collection.
  3991. */
  3992. takeLast(amount: number): this;
  3993. /**
  3994. * Returns a new Collection of the same type which includes entries from this
  3995. * Collection as long as the `predicate` returns true.
  3996. *
  3997. * <!-- runkit:activate -->
  3998. * ```js
  3999. * const { List } = require('immutable')
  4000. * List([ 'dog', 'frog', 'cat', 'hat', 'god' ])
  4001. * .takeWhile(x => x.match(/o/))
  4002. * // List [ "dog", "frog" ]
  4003. * ```
  4004. */
  4005. takeWhile(
  4006. predicate: (value: V, key: K, iter: this) => boolean,
  4007. context?: any
  4008. ): this;
  4009. /**
  4010. * Returns a new Collection of the same type which includes entries from this
  4011. * Collection as long as the `predicate` returns false.
  4012. *
  4013. * <!-- runkit:activate -->
  4014. * ```js
  4015. * const { List } = require('immutable')
  4016. * List([ 'dog', 'frog', 'cat', 'hat', 'god' ])
  4017. * .takeUntil(x => x.match(/at/))
  4018. * // List [ "dog", "frog" ]
  4019. * ```
  4020. */
  4021. takeUntil(
  4022. predicate: (value: V, key: K, iter: this) => boolean,
  4023. context?: any
  4024. ): this;
  4025. // Combination
  4026. /**
  4027. * Returns a new Collection of the same type with other values and
  4028. * collection-like concatenated to this one.
  4029. *
  4030. * For Seqs, all entries will be present in the resulting Seq, even if they
  4031. * have the same key.
  4032. */
  4033. concat(...valuesOrCollections: Array<any>): Collection<any, any>;
  4034. /**
  4035. * Flattens nested Collections.
  4036. *
  4037. * Will deeply flatten the Collection by default, returning a Collection of the
  4038. * same type, but a `depth` can be provided in the form of a number or
  4039. * boolean (where true means to shallowly flatten one level). A depth of 0
  4040. * (or shallow: false) will deeply flatten.
  4041. *
  4042. * Flattens only others Collection, not Arrays or Objects.
  4043. *
  4044. * Note: `flatten(true)` operates on Collection<any, Collection<K, V>> and
  4045. * returns Collection<K, V>
  4046. */
  4047. flatten(depth?: number): Collection<any, any>;
  4048. flatten(shallow?: boolean): Collection<any, any>;
  4049. /**
  4050. * Flat-maps the Collection, returning a Collection of the same type.
  4051. *
  4052. * Similar to `collection.map(...).flatten(true)`.
  4053. */
  4054. flatMap<M>(
  4055. mapper: (value: V, key: K, iter: this) => Iterable<M>,
  4056. context?: any
  4057. ): Collection<K, M>;
  4058. /**
  4059. * Flat-maps the Collection, returning a Collection of the same type.
  4060. *
  4061. * Similar to `collection.map(...).flatten(true)`.
  4062. * Used for Dictionaries only.
  4063. */
  4064. flatMap<KM, VM>(
  4065. mapper: (value: V, key: K, iter: this) => Iterable<[KM, VM]>,
  4066. context?: any
  4067. ): Collection<KM, VM>;
  4068. // Reducing a value
  4069. /**
  4070. * Reduces the Collection to a value by calling the `reducer` for every entry
  4071. * in the Collection and passing along the reduced value.
  4072. *
  4073. * If `initialReduction` is not provided, the first item in the
  4074. * Collection will be used.
  4075. *
  4076. * @see `Array#reduce`.
  4077. */
  4078. reduce<R>(
  4079. reducer: (reduction: R, value: V, key: K, iter: this) => R,
  4080. initialReduction: R,
  4081. context?: any
  4082. ): R;
  4083. reduce<R>(
  4084. reducer: (reduction: V | R, value: V, key: K, iter: this) => R
  4085. ): R;
  4086. /**
  4087. * Reduces the Collection in reverse (from the right side).
  4088. *
  4089. * Note: Similar to this.reverse().reduce(), and provided for parity
  4090. * with `Array#reduceRight`.
  4091. */
  4092. reduceRight<R>(
  4093. reducer: (reduction: R, value: V, key: K, iter: this) => R,
  4094. initialReduction: R,
  4095. context?: any
  4096. ): R;
  4097. reduceRight<R>(
  4098. reducer: (reduction: V | R, value: V, key: K, iter: this) => R
  4099. ): R;
  4100. /**
  4101. * True if `predicate` returns true for all entries in the Collection.
  4102. */
  4103. every(
  4104. predicate: (value: V, key: K, iter: this) => boolean,
  4105. context?: any
  4106. ): boolean;
  4107. /**
  4108. * True if `predicate` returns true for any entry in the Collection.
  4109. */
  4110. some(
  4111. predicate: (value: V, key: K, iter: this) => boolean,
  4112. context?: any
  4113. ): boolean;
  4114. /**
  4115. * Joins values together as a string, inserting a separator between each.
  4116. * The default separator is `","`.
  4117. */
  4118. join(separator?: string): string;
  4119. /**
  4120. * Returns true if this Collection includes no values.
  4121. *
  4122. * For some lazy `Seq`, `isEmpty` might need to iterate to determine
  4123. * emptiness. At most one iteration will occur.
  4124. */
  4125. isEmpty(): boolean;
  4126. /**
  4127. * Returns the size of this Collection.
  4128. *
  4129. * Regardless of if this Collection can describe its size lazily (some Seqs
  4130. * cannot), this method will always return the correct size. E.g. it
  4131. * evaluates a lazy `Seq` if necessary.
  4132. *
  4133. * If `predicate` is provided, then this returns the count of entries in the
  4134. * Collection for which the `predicate` returns true.
  4135. */
  4136. count(): number;
  4137. count(
  4138. predicate: (value: V, key: K, iter: this) => boolean,
  4139. context?: any
  4140. ): number;
  4141. /**
  4142. * Returns a `Seq.Keyed` of counts, grouped by the return value of
  4143. * the `grouper` function.
  4144. *
  4145. * Note: This is not a lazy operation.
  4146. */
  4147. countBy<G>(
  4148. grouper: (value: V, key: K, iter: this) => G,
  4149. context?: any
  4150. ): Map<G, number>;
  4151. // Search for value
  4152. /**
  4153. * Returns the first value for which the `predicate` returns true.
  4154. */
  4155. find(
  4156. predicate: (value: V, key: K, iter: this) => boolean,
  4157. context?: any,
  4158. notSetValue?: V
  4159. ): V | undefined;
  4160. /**
  4161. * Returns the last value for which the `predicate` returns true.
  4162. *
  4163. * Note: `predicate` will be called for each entry in reverse.
  4164. */
  4165. findLast(
  4166. predicate: (value: V, key: K, iter: this) => boolean,
  4167. context?: any,
  4168. notSetValue?: V
  4169. ): V | undefined;
  4170. /**
  4171. * Returns the first [key, value] entry for which the `predicate` returns true.
  4172. */
  4173. findEntry(
  4174. predicate: (value: V, key: K, iter: this) => boolean,
  4175. context?: any,
  4176. notSetValue?: V
  4177. ): [K, V] | undefined;
  4178. /**
  4179. * Returns the last [key, value] entry for which the `predicate`
  4180. * returns true.
  4181. *
  4182. * Note: `predicate` will be called for each entry in reverse.
  4183. */
  4184. findLastEntry(
  4185. predicate: (value: V, key: K, iter: this) => boolean,
  4186. context?: any,
  4187. notSetValue?: V
  4188. ): [K, V] | undefined;
  4189. /**
  4190. * Returns the key for which the `predicate` returns true.
  4191. */
  4192. findKey(
  4193. predicate: (value: V, key: K, iter: this) => boolean,
  4194. context?: any
  4195. ): K | undefined;
  4196. /**
  4197. * Returns the last key for which the `predicate` returns true.
  4198. *
  4199. * Note: `predicate` will be called for each entry in reverse.
  4200. */
  4201. findLastKey(
  4202. predicate: (value: V, key: K, iter: this) => boolean,
  4203. context?: any
  4204. ): K | undefined;
  4205. /**
  4206. * Returns the key associated with the search value, or undefined.
  4207. */
  4208. keyOf(searchValue: V): K | undefined;
  4209. /**
  4210. * Returns the last key associated with the search value, or undefined.
  4211. */
  4212. lastKeyOf(searchValue: V): K | undefined;
  4213. /**
  4214. * Returns the maximum value in this collection. If any values are
  4215. * comparatively equivalent, the first one found will be returned.
  4216. *
  4217. * The `comparator` is used in the same way as `Collection#sort`. If it is not
  4218. * provided, the default comparator is `>`.
  4219. *
  4220. * When two values are considered equivalent, the first encountered will be
  4221. * returned. Otherwise, `max` will operate independent of the order of input
  4222. * as long as the comparator is commutative. The default comparator `>` is
  4223. * commutative *only* when types do not differ.
  4224. *
  4225. * If `comparator` returns 0 and either value is NaN, undefined, or null,
  4226. * that value will be returned.
  4227. */
  4228. max(comparator?: (valueA: V, valueB: V) => number): V | undefined;
  4229. /**
  4230. * Like `max`, but also accepts a `comparatorValueMapper` which allows for
  4231. * comparing by more sophisticated means:
  4232. *
  4233. * hitters.maxBy(hitter => hitter.avgHits);
  4234. *
  4235. */
  4236. maxBy<C>(
  4237. comparatorValueMapper: (value: V, key: K, iter: this) => C,
  4238. comparator?: (valueA: C, valueB: C) => number
  4239. ): V | undefined;
  4240. /**
  4241. * Returns the minimum value in this collection. If any values are
  4242. * comparatively equivalent, the first one found will be returned.
  4243. *
  4244. * The `comparator` is used in the same way as `Collection#sort`. If it is not
  4245. * provided, the default comparator is `<`.
  4246. *
  4247. * When two values are considered equivalent, the first encountered will be
  4248. * returned. Otherwise, `min` will operate independent of the order of input
  4249. * as long as the comparator is commutative. The default comparator `<` is
  4250. * commutative *only* when types do not differ.
  4251. *
  4252. * If `comparator` returns 0 and either value is NaN, undefined, or null,
  4253. * that value will be returned.
  4254. */
  4255. min(comparator?: (valueA: V, valueB: V) => number): V | undefined;
  4256. /**
  4257. * Like `min`, but also accepts a `comparatorValueMapper` which allows for
  4258. * comparing by more sophisticated means:
  4259. *
  4260. * hitters.minBy(hitter => hitter.avgHits);
  4261. *
  4262. */
  4263. minBy<C>(
  4264. comparatorValueMapper: (value: V, key: K, iter: this) => C,
  4265. comparator?: (valueA: C, valueB: C) => number
  4266. ): V | undefined;
  4267. // Comparison
  4268. /**
  4269. * True if `iter` includes every value in this Collection.
  4270. */
  4271. isSubset(iter: Iterable<V>): boolean;
  4272. /**
  4273. * True if this Collection includes every value in `iter`.
  4274. */
  4275. isSuperset(iter: Iterable<V>): boolean;
  4276. }
  4277. /**
  4278. * The interface to fulfill to qualify as a Value Object.
  4279. */
  4280. export interface ValueObject {
  4281. /**
  4282. * True if this and the other Collection have value equality, as defined
  4283. * by `Immutable.is()`.
  4284. *
  4285. * Note: This is equivalent to `Immutable.is(this, other)`, but provided to
  4286. * allow for chained expressions.
  4287. */
  4288. equals(other: any): boolean;
  4289. /**
  4290. * Computes and returns the hashed identity for this Collection.
  4291. *
  4292. * The `hashCode` of a Collection is used to determine potential equality,
  4293. * and is used when adding this to a `Set` or as a key in a `Map`, enabling
  4294. * lookup via a different instance.
  4295. *
  4296. * <!-- runkit:activate -->
  4297. * ```js
  4298. * const { List, Set } = require('immutable');
  4299. * const a = List([ 1, 2, 3 ]);
  4300. * const b = List([ 1, 2, 3 ]);
  4301. * assert.notStrictEqual(a, b); // different instances
  4302. * const set = Set([ a ]);
  4303. * assert.equal(set.has(b), true);
  4304. * ```
  4305. *
  4306. * Note: hashCode() MUST return a Uint32 number. The easiest way to
  4307. * guarantee this is to return `myHash | 0` from a custom implementation.
  4308. *
  4309. * If two values have the same `hashCode`, they are [not guaranteed
  4310. * to be equal][Hash Collision]. If two values have different `hashCode`s,
  4311. * they must not be equal.
  4312. *
  4313. * Note: `hashCode()` is not guaranteed to always be called before
  4314. * `equals()`. Most but not all Immutable.js collections use hash codes to
  4315. * organize their internal data structures, while all Immutable.js
  4316. * collections use equality during lookups.
  4317. *
  4318. * [Hash Collision]: http://en.wikipedia.org/wiki/Collision_(computer_science)
  4319. */
  4320. hashCode(): number;
  4321. }
  4322. /**
  4323. * Deeply converts plain JS objects and arrays to Immutable Maps and Lists.
  4324. *
  4325. * If a `reviver` is optionally provided, it will be called with every
  4326. * collection as a Seq (beginning with the most nested collections
  4327. * and proceeding to the top-level collection itself), along with the key
  4328. * referring to each collection and the parent JS object provided as `this`.
  4329. * For the top level, object, the key will be `""`. This `reviver` is expected
  4330. * to return a new Immutable Collection, allowing for custom conversions from
  4331. * deep JS objects. Finally, a `path` is provided which is the sequence of
  4332. * keys to this value from the starting value.
  4333. *
  4334. * `reviver` acts similarly to the [same parameter in `JSON.parse`][1].
  4335. *
  4336. * If `reviver` is not provided, the default behavior will convert Objects
  4337. * into Maps and Arrays into Lists like so:
  4338. *
  4339. * <!-- runkit:activate -->
  4340. * ```js
  4341. * const { fromJS, isKeyed } = require('immutable')
  4342. * function (key, value) {
  4343. * return isKeyed(value) ? value.toMap() : value.toList()
  4344. * }
  4345. * ```
  4346. *
  4347. * `fromJS` is conservative in its conversion. It will only convert
  4348. * arrays which pass `Array.isArray` to Lists, and only raw objects (no custom
  4349. * prototype) to Map.
  4350. *
  4351. * Accordingly, this example converts native JS data to OrderedMap and List:
  4352. *
  4353. * <!-- runkit:activate -->
  4354. * ```js
  4355. * const { fromJS, isKeyed } = require('immutable')
  4356. * fromJS({ a: {b: [10, 20, 30]}, c: 40}, function (key, value, path) {
  4357. * console.log(key, value, path)
  4358. * return isKeyed(value) ? value.toOrderedMap() : value.toList()
  4359. * })
  4360. *
  4361. * > "b", [ 10, 20, 30 ], [ "a", "b" ]
  4362. * > "a", {b: [10, 20, 30]}, [ "a" ]
  4363. * > "", {a: {b: [10, 20, 30]}, c: 40}, []
  4364. * ```
  4365. *
  4366. * Keep in mind, when using JS objects to construct Immutable Maps, that
  4367. * JavaScript Object properties are always strings, even if written in a
  4368. * quote-less shorthand, while Immutable Maps accept keys of any type.
  4369. *
  4370. * <!-- runkit:activate -->
  4371. * ```js
  4372. * const { Map } = require('immutable')
  4373. * let obj = { 1: "one" };
  4374. * Object.keys(obj); // [ "1" ]
  4375. * assert.equal(obj["1"], obj[1]); // "one" === "one"
  4376. *
  4377. * let map = Map(obj);
  4378. * assert.notEqual(map.get("1"), map.get(1)); // "one" !== undefined
  4379. * ```
  4380. *
  4381. * Property access for JavaScript Objects first converts the key to a string,
  4382. * but since Immutable Map keys can be of any type the argument to `get()` is
  4383. * not altered.
  4384. *
  4385. * [1]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/parse#Example.3A_Using_the_reviver_parameter
  4386. * "Using the reviver parameter"
  4387. */
  4388. export function fromJS(
  4389. jsValue: any,
  4390. reviver?: (
  4391. key: string | number,
  4392. sequence: Collection.Keyed<string, any> | Collection.Indexed<any>,
  4393. path?: Array<string | number>
  4394. ) => any
  4395. ): any;
  4396. /**
  4397. * Value equality check with semantics similar to `Object.is`, but treats
  4398. * Immutable `Collection`s as values, equal if the second `Collection` includes
  4399. * equivalent values.
  4400. *
  4401. * It's used throughout Immutable when checking for equality, including `Map`
  4402. * key equality and `Set` membership.
  4403. *
  4404. * <!-- runkit:activate -->
  4405. * ```js
  4406. * const { Map, is } = require('immutable')
  4407. * const map1 = Map({ a: 1, b: 1, c: 1 })
  4408. * const map2 = Map({ a: 1, b: 1, c: 1 })
  4409. * assert.equal(map1 !== map2, true)
  4410. * assert.equal(Object.is(map1, map2), false)
  4411. * assert.equal(is(map1, map2), true)
  4412. * ```
  4413. *
  4414. * `is()` compares primitive types like strings and numbers, Immutable.js
  4415. * collections like `Map` and `List`, but also any custom object which
  4416. * implements `ValueObject` by providing `equals()` and `hashCode()` methods.
  4417. *
  4418. * Note: Unlike `Object.is`, `Immutable.is` assumes `0` and `-0` are the same
  4419. * value, matching the behavior of ES6 Map key equality.
  4420. */
  4421. export function is(first: any, second: any): boolean;
  4422. /**
  4423. * The `hash()` function is an important part of how Immutable determines if
  4424. * two values are equivalent and is used to determine how to store those
  4425. * values. Provided with any value, `hash()` will return a 31-bit integer.
  4426. *
  4427. * When designing Objects which may be equal, it's important that when a
  4428. * `.equals()` method returns true, that both values `.hashCode()` method
  4429. * return the same value. `hash()` may be used to produce those values.
  4430. *
  4431. * For non-Immutable Objects that do not provide a `.hashCode()` functions
  4432. * (including plain Objects, plain Arrays, Date objects, etc), a unique hash
  4433. * value will be created for each *instance*. That is, the create hash
  4434. * represents referential equality, and not value equality for Objects. This
  4435. * ensures that if that Object is mutated over time that its hash code will
  4436. * remain consistent, allowing Objects to be used as keys and values in
  4437. * Immutable.js collections.
  4438. *
  4439. * Note that `hash()` attempts to balance between speed and avoiding
  4440. * collisions, however it makes no attempt to produce secure hashes.
  4441. *
  4442. * *New in Version 4.0*
  4443. */
  4444. export function hash(value: any): number;
  4445. /**
  4446. * True if `maybeImmutable` is an Immutable Collection or Record.
  4447. *
  4448. * Note: Still returns true even if the collections is within a `withMutations()`.
  4449. *
  4450. * <!-- runkit:activate -->
  4451. * ```js
  4452. * const { isImmutable, Map, List, Stack } = require('immutable');
  4453. * isImmutable([]); // false
  4454. * isImmutable({}); // false
  4455. * isImmutable(Map()); // true
  4456. * isImmutable(List()); // true
  4457. * isImmutable(Stack()); // true
  4458. * isImmutable(Map().asMutable()); // true
  4459. * ```
  4460. */
  4461. export function isImmutable(maybeImmutable: any): maybeImmutable is Collection<any, any>;
  4462. /**
  4463. * True if `maybeCollection` is a Collection, or any of its subclasses.
  4464. *
  4465. * <!-- runkit:activate -->
  4466. * ```js
  4467. * const { isCollection, Map, List, Stack } = require('immutable');
  4468. * isCollection([]); // false
  4469. * isCollection({}); // false
  4470. * isCollection(Map()); // true
  4471. * isCollection(List()); // true
  4472. * isCollection(Stack()); // true
  4473. * ```
  4474. */
  4475. export function isCollection(maybeCollection: any): maybeCollection is Collection<any, any>;
  4476. /**
  4477. * True if `maybeKeyed` is a Collection.Keyed, or any of its subclasses.
  4478. *
  4479. * <!-- runkit:activate -->
  4480. * ```js
  4481. * const { isKeyed, Map, List, Stack } = require('immutable');
  4482. * isKeyed([]); // false
  4483. * isKeyed({}); // false
  4484. * isKeyed(Map()); // true
  4485. * isKeyed(List()); // false
  4486. * isKeyed(Stack()); // false
  4487. * ```
  4488. */
  4489. export function isKeyed(maybeKeyed: any): maybeKeyed is Collection.Keyed<any, any>;
  4490. /**
  4491. * True if `maybeIndexed` is a Collection.Indexed, or any of its subclasses.
  4492. *
  4493. * <!-- runkit:activate -->
  4494. * ```js
  4495. * const { isIndexed, Map, List, Stack, Set } = require('immutable');
  4496. * isIndexed([]); // false
  4497. * isIndexed({}); // false
  4498. * isIndexed(Map()); // false
  4499. * isIndexed(List()); // true
  4500. * isIndexed(Stack()); // true
  4501. * isIndexed(Set()); // false
  4502. * ```
  4503. */
  4504. export function isIndexed(maybeIndexed: any): maybeIndexed is Collection.Indexed<any>;
  4505. /**
  4506. * True if `maybeAssociative` is either a Keyed or Indexed Collection.
  4507. *
  4508. * <!-- runkit:activate -->
  4509. * ```js
  4510. * const { isAssociative, Map, List, Stack, Set } = require('immutable');
  4511. * isAssociative([]); // false
  4512. * isAssociative({}); // false
  4513. * isAssociative(Map()); // true
  4514. * isAssociative(List()); // true
  4515. * isAssociative(Stack()); // true
  4516. * isAssociative(Set()); // false
  4517. * ```
  4518. */
  4519. export function isAssociative(maybeAssociative: any): maybeAssociative is Collection.Keyed<any, any> | Collection.Indexed<any>;
  4520. /**
  4521. * True if `maybeOrdered` is a Collection where iteration order is well
  4522. * defined. True for Collection.Indexed as well as OrderedMap and OrderedSet.
  4523. *
  4524. * <!-- runkit:activate -->
  4525. * ```js
  4526. * const { isOrdered, Map, OrderedMap, List, Set } = require('immutable');
  4527. * isOrdered([]); // false
  4528. * isOrdered({}); // false
  4529. * isOrdered(Map()); // false
  4530. * isOrdered(OrderedMap()); // true
  4531. * isOrdered(List()); // true
  4532. * isOrdered(Set()); // false
  4533. * ```
  4534. */
  4535. export function isOrdered(maybeOrdered: any): boolean;
  4536. /**
  4537. * True if `maybeValue` is a JavaScript Object which has *both* `equals()`
  4538. * and `hashCode()` methods.
  4539. *
  4540. * Any two instances of *value objects* can be compared for value equality with
  4541. * `Immutable.is()` and can be used as keys in a `Map` or members in a `Set`.
  4542. */
  4543. export function isValueObject(maybeValue: any): maybeValue is ValueObject;
  4544. /**
  4545. * True if `maybeSeq` is a Seq.
  4546. */
  4547. export function isSeq(maybeSeq: any): maybeSeq is Seq.Indexed<any> | Seq.Keyed<any, any> | Seq.Set<any>;
  4548. /**
  4549. * True if `maybeList` is a List.
  4550. */
  4551. export function isList(maybeList: any): maybeList is List<any>;
  4552. /**
  4553. * True if `maybeMap` is a Map.
  4554. *
  4555. * Also true for OrderedMaps.
  4556. */
  4557. export function isMap(maybeMap: any): maybeMap is Map<any, any>;
  4558. /**
  4559. * True if `maybeOrderedMap` is an OrderedMap.
  4560. */
  4561. export function isOrderedMap(maybeOrderedMap: any): maybeOrderedMap is OrderedMap<any, any>;
  4562. /**
  4563. * True if `maybeStack` is a Stack.
  4564. */
  4565. export function isStack(maybeStack: any): maybeStack is Stack<any>;
  4566. /**
  4567. * True if `maybeSet` is a Set.
  4568. *
  4569. * Also true for OrderedSets.
  4570. */
  4571. export function isSet(maybeSet: any): maybeSet is Set<any>;
  4572. /**
  4573. * True if `maybeOrderedSet` is an OrderedSet.
  4574. */
  4575. export function isOrderedSet(maybeOrderedSet: any): maybeOrderedSet is OrderedSet<any>;
  4576. /**
  4577. * True if `maybeRecord` is a Record.
  4578. */
  4579. export function isRecord(maybeRecord: any): maybeRecord is Record<any>;
  4580. /**
  4581. * Returns the value within the provided collection associated with the
  4582. * provided key, or notSetValue if the key is not defined in the collection.
  4583. *
  4584. * A functional alternative to `collection.get(key)` which will also work on
  4585. * plain Objects and Arrays as an alternative for `collection[key]`.
  4586. *
  4587. * <!-- runkit:activate -->
  4588. * ```js
  4589. * const { get } = require('immutable')
  4590. * get([ 'dog', 'frog', 'cat' ], 2) // 'frog'
  4591. * get({ x: 123, y: 456 }, 'x') // 123
  4592. * get({ x: 123, y: 456 }, 'z', 'ifNotSet') // 'ifNotSet'
  4593. * ```
  4594. */
  4595. export function get<K, V>(collection: Collection<K, V>, key: K): V | undefined;
  4596. export function get<K, V, NSV>(collection: Collection<K, V>, key: K, notSetValue: NSV): V | NSV;
  4597. export function get<TProps, K extends keyof TProps>(record: Record<TProps>, key: K, notSetValue: any): TProps[K];
  4598. export function get<V>(collection: Array<V>, key: number): V | undefined;
  4599. export function get<V, NSV>(collection: Array<V>, key: number, notSetValue: NSV): V | NSV;
  4600. export function get<C extends Object, K extends keyof C>(object: C, key: K, notSetValue: any): C[K];
  4601. export function get<V>(collection: {[key: string]: V}, key: string): V | undefined;
  4602. export function get<V, NSV>(collection: {[key: string]: V}, key: string, notSetValue: NSV): V | NSV;
  4603. /**
  4604. * Returns true if the key is defined in the provided collection.
  4605. *
  4606. * A functional alternative to `collection.has(key)` which will also work with
  4607. * plain Objects and Arrays as an alternative for
  4608. * `collection.hasOwnProperty(key)`.
  4609. *
  4610. * <!-- runkit:activate -->
  4611. * ```js
  4612. * const { has } = require('immutable')
  4613. * has([ 'dog', 'frog', 'cat' ], 2) // true
  4614. * has([ 'dog', 'frog', 'cat' ], 5) // false
  4615. * has({ x: 123, y: 456 }, 'x') // true
  4616. * has({ x: 123, y: 456 }, 'z') // false
  4617. * ```
  4618. */
  4619. export function has(collection: Object, key: any): boolean;
  4620. /**
  4621. * Returns a copy of the collection with the value at key removed.
  4622. *
  4623. * A functional alternative to `collection.remove(key)` which will also work
  4624. * with plain Objects and Arrays as an alternative for
  4625. * `delete collectionCopy[key]`.
  4626. *
  4627. * <!-- runkit:activate -->
  4628. * ```js
  4629. * const { remove } = require('immutable')
  4630. * const originalArray = [ 'dog', 'frog', 'cat' ]
  4631. * remove(originalArray, 1) // [ 'dog', 'cat' ]
  4632. * console.log(originalArray) // [ 'dog', 'frog', 'cat' ]
  4633. * const originalObject = { x: 123, y: 456 }
  4634. * remove(originalObject, 'x') // { y: 456 }
  4635. * console.log(originalObject) // { x: 123, y: 456 }
  4636. * ```
  4637. */
  4638. export function remove<K, C extends Collection<K, any>>(collection: C, key: K): C;
  4639. export function remove<TProps, C extends Record<TProps>, K extends keyof TProps>(collection: C, key: K): C;
  4640. export function remove<C extends Array<any>>(collection: C, key: number): C;
  4641. export function remove<C, K extends keyof C>(collection: C, key: K): C;
  4642. export function remove<C extends {[key: string]: any}, K extends keyof C>(collection: C, key: K): C;
  4643. /**
  4644. * Returns a copy of the collection with the value at key set to the provided
  4645. * value.
  4646. *
  4647. * A functional alternative to `collection.set(key, value)` which will also
  4648. * work with plain Objects and Arrays as an alternative for
  4649. * `collectionCopy[key] = value`.
  4650. *
  4651. * <!-- runkit:activate -->
  4652. * ```js
  4653. * const { set } = require('immutable')
  4654. * const originalArray = [ 'dog', 'frog', 'cat' ]
  4655. * set(originalArray, 1, 'cow') // [ 'dog', 'cow', 'cat' ]
  4656. * console.log(originalArray) // [ 'dog', 'frog', 'cat' ]
  4657. * const originalObject = { x: 123, y: 456 }
  4658. * set(originalObject, 'x', 789) // { x: 789, y: 456 }
  4659. * console.log(originalObject) // { x: 123, y: 456 }
  4660. * ```
  4661. */
  4662. export function set<K, V, C extends Collection<K, V>>(collection: C, key: K, value: V): C;
  4663. export function set<TProps, C extends Record<TProps>, K extends keyof TProps>(record: C, key: K, value: TProps[K]): C;
  4664. export function set<V, C extends Array<V>>(collection: C, key: number, value: V): C;
  4665. export function set<C, K extends keyof C>(object: C, key: K, value: C[K]): C;
  4666. export function set<V, C extends {[key: string]: V}>(collection: C, key: string, value: V): C;
  4667. /**
  4668. * Returns a copy of the collection with the value at key set to the result of
  4669. * providing the existing value to the updating function.
  4670. *
  4671. * A functional alternative to `collection.update(key, fn)` which will also
  4672. * work with plain Objects and Arrays as an alternative for
  4673. * `collectionCopy[key] = fn(collection[key])`.
  4674. *
  4675. * <!-- runkit:activate -->
  4676. * ```js
  4677. * const { update } = require('immutable')
  4678. * const originalArray = [ 'dog', 'frog', 'cat' ]
  4679. * update(originalArray, 1, val => val.toUpperCase()) // [ 'dog', 'FROG', 'cat' ]
  4680. * console.log(originalArray) // [ 'dog', 'frog', 'cat' ]
  4681. * const originalObject = { x: 123, y: 456 }
  4682. * update(originalObject, 'x', val => val * 6) // { x: 738, y: 456 }
  4683. * console.log(originalObject) // { x: 123, y: 456 }
  4684. * ```
  4685. */
  4686. export function update<K, V, C extends Collection<K, V>>(collection: C, key: K, updater: (value: V) => V): C;
  4687. export function update<K, V, C extends Collection<K, V>, NSV>(collection: C, key: K, notSetValue: NSV, updater: (value: V | NSV) => V): C;
  4688. export function update<TProps, C extends Record<TProps>, K extends keyof TProps>(record: C, key: K, updater: (value: TProps[K]) => TProps[K]): C;
  4689. export function update<TProps, C extends Record<TProps>, K extends keyof TProps, NSV>(record: C, key: K, notSetValue: NSV, updater: (value: TProps[K] | NSV) => TProps[K]): C;
  4690. export function update<V>(collection: Array<V>, key: number, updater: (value: V) => V): Array<V>;
  4691. export function update<V, NSV>(collection: Array<V>, key: number, notSetValue: NSV, updater: (value: V | NSV) => V): Array<V>;
  4692. export function update<C, K extends keyof C>(object: C, key: K, updater: (value: C[K]) => C[K]): C;
  4693. export function update<C, K extends keyof C, NSV>(object: C, key: K, notSetValue: NSV, updater: (value: C[K] | NSV) => C[K]): C;
  4694. export function update<V, C extends {[key: string]: V}, K extends keyof C>(collection: C, key: K, updater: (value: V) => V): {[key: string]: V};
  4695. export function update<V, C extends {[key: string]: V}, K extends keyof C, NSV>(collection: C, key: K, notSetValue: NSV, updater: (value: V | NSV) => V): {[key: string]: V};
  4696. /**
  4697. * Returns the value at the provided key path starting at the provided
  4698. * collection, or notSetValue if the key path is not defined.
  4699. *
  4700. * A functional alternative to `collection.getIn(keypath)` which will also
  4701. * work with plain Objects and Arrays.
  4702. *
  4703. * <!-- runkit:activate -->
  4704. * ```js
  4705. * const { getIn } = require('immutable')
  4706. * getIn({ x: { y: { z: 123 }}}, ['x', 'y', 'z']) // 123
  4707. * getIn({ x: { y: { z: 123 }}}, ['x', 'q', 'p'], 'ifNotSet') // 'ifNotSet'
  4708. * ```
  4709. */
  4710. export function getIn(collection: any, keyPath: Iterable<any>, notSetValue: any): any;
  4711. /**
  4712. * Returns true if the key path is defined in the provided collection.
  4713. *
  4714. * A functional alternative to `collection.hasIn(keypath)` which will also
  4715. * work with plain Objects and Arrays.
  4716. *
  4717. * <!-- runkit:activate -->
  4718. * ```js
  4719. * const { hasIn } = require('immutable')
  4720. * hasIn({ x: { y: { z: 123 }}}, ['x', 'y', 'z']) // true
  4721. * hasIn({ x: { y: { z: 123 }}}, ['x', 'q', 'p']) // false
  4722. * ```
  4723. */
  4724. export function hasIn(collection: any, keyPath: Iterable<any>): boolean;
  4725. /**
  4726. * Returns a copy of the collection with the value at the key path removed.
  4727. *
  4728. * A functional alternative to `collection.removeIn(keypath)` which will also
  4729. * work with plain Objects and Arrays.
  4730. *
  4731. * <!-- runkit:activate -->
  4732. * ```js
  4733. * const { removeIn } = require('immutable')
  4734. * const original = { x: { y: { z: 123 }}}
  4735. * removeIn(original, ['x', 'y', 'z']) // { x: { y: {}}}
  4736. * console.log(original) // { x: { y: { z: 123 }}}
  4737. * ```
  4738. */
  4739. export function removeIn<C>(collection: C, keyPath: Iterable<any>): C;
  4740. /**
  4741. * Returns a copy of the collection with the value at the key path set to the
  4742. * provided value.
  4743. *
  4744. * A functional alternative to `collection.setIn(keypath)` which will also
  4745. * work with plain Objects and Arrays.
  4746. *
  4747. * <!-- runkit:activate -->
  4748. * ```js
  4749. * const { setIn } = require('immutable')
  4750. * const original = { x: { y: { z: 123 }}}
  4751. * setIn(original, ['x', 'y', 'z'], 456) // { x: { y: { z: 456 }}}
  4752. * console.log(original) // { x: { y: { z: 123 }}}
  4753. * ```
  4754. */
  4755. export function setIn<C>(collection: C, keyPath: Iterable<any>, value: any): C;
  4756. /**
  4757. * Returns a copy of the collection with the value at key path set to the
  4758. * result of providing the existing value to the updating function.
  4759. *
  4760. * A functional alternative to `collection.updateIn(keypath)` which will also
  4761. * work with plain Objects and Arrays.
  4762. *
  4763. * <!-- runkit:activate -->
  4764. * ```js
  4765. * const { updateIn } = require('immutable')
  4766. * const original = { x: { y: { z: 123 }}}
  4767. * updateIn(original, ['x', 'y', 'z'], val => val * 6) // { x: { y: { z: 738 }}}
  4768. * console.log(original) // { x: { y: { z: 123 }}}
  4769. * ```
  4770. */
  4771. export function updateIn<C>(collection: C, keyPath: Iterable<any>, updater: (value: any) => any): C;
  4772. export function updateIn<C>(collection: C, keyPath: Iterable<any>, notSetValue: any, updater: (value: any) => any): C;
  4773. /**
  4774. * Returns a copy of the collection with the remaining collections merged in.
  4775. *
  4776. * A functional alternative to `collection.merge()` which will also work with
  4777. * plain Objects and Arrays.
  4778. *
  4779. * <!-- runkit:activate -->
  4780. * ```js
  4781. * const { merge } = require('immutable')
  4782. * const original = { x: 123, y: 456 }
  4783. * merge(original, { y: 789, z: 'abc' }) // { x: 123, y: 789, z: 'abc' }
  4784. * console.log(original) // { x: 123, y: 456 }
  4785. * ```
  4786. */
  4787. export function merge<C>(
  4788. collection: C,
  4789. ...collections: Array<Iterable<any> | Iterable<[any, any]> | {[key: string]: any}>
  4790. ): C;
  4791. /**
  4792. * Returns a copy of the collection with the remaining collections merged in,
  4793. * calling the `merger` function whenever an existing value is encountered.
  4794. *
  4795. * A functional alternative to `collection.mergeWith()` which will also work
  4796. * with plain Objects and Arrays.
  4797. *
  4798. * <!-- runkit:activate -->
  4799. * ```js
  4800. * const { mergeWith } = require('immutable')
  4801. * const original = { x: 123, y: 456 }
  4802. * mergeWith(
  4803. * (oldVal, newVal) => oldVal + newVal,
  4804. * original,
  4805. * { y: 789, z: 'abc' }
  4806. * ) // { x: 123, y: 1245, z: 'abc' }
  4807. * console.log(original) // { x: 123, y: 456 }
  4808. * ```
  4809. */
  4810. export function mergeWith<C>(
  4811. merger: (oldVal: any, newVal: any, key: any) => any,
  4812. collection: C,
  4813. ...collections: Array<Iterable<any> | Iterable<[any, any]> | {[key: string]: any}>
  4814. ): C;
  4815. /**
  4816. * Returns a copy of the collection with the remaining collections merged in
  4817. * deeply (recursively).
  4818. *
  4819. * A functional alternative to `collection.mergeDeep()` which will also work
  4820. * with plain Objects and Arrays.
  4821. *
  4822. * <!-- runkit:activate -->
  4823. * ```js
  4824. * const { mergeDeep } = require('immutable')
  4825. * const original = { x: { y: 123 }}
  4826. * mergeDeep(original, { x: { z: 456 }}) // { x: { y: 123, z: 456 }}
  4827. * console.log(original) // { x: { y: 123 }}
  4828. * ```
  4829. */
  4830. export function mergeDeep<C>(
  4831. collection: C,
  4832. ...collections: Array<Iterable<any> | Iterable<[any, any]> | {[key: string]: any}>
  4833. ): C;
  4834. /**
  4835. * Returns a copy of the collection with the remaining collections merged in
  4836. * deeply (recursively), calling the `merger` function whenever an existing
  4837. * value is encountered.
  4838. *
  4839. * A functional alternative to `collection.mergeDeepWith()` which will also
  4840. * work with plain Objects and Arrays.
  4841. *
  4842. * <!-- runkit:activate -->
  4843. * ```js
  4844. * const { mergeDeepWith } = require('immutable')
  4845. * const original = { x: { y: 123 }}
  4846. * mergeDeepWith(
  4847. * (oldVal, newVal) => oldVal + newVal,
  4848. * original,
  4849. * { x: { y: 456 }}
  4850. * ) // { x: { y: 579 }}
  4851. * console.log(original) // { x: { y: 123 }}
  4852. * ```
  4853. */
  4854. export function mergeDeepWith<C>(
  4855. merger: (oldVal: any, newVal: any, key: any) => any,
  4856. collection: C,
  4857. ...collections: Array<Iterable<any> | Iterable<[any, any]> | {[key: string]: any}>
  4858. ): C;