immutable.d.ts 196 KB

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