immutable.d.ts 183 KB

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