immutable.es.js 172 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225
  1. /**
  2. * @license
  3. * MIT License
  4. *
  5. * Copyright (c) 2014-present, Lee Byron and other contributors.
  6. *
  7. * Permission is hereby granted, free of charge, to any person obtaining a copy
  8. * of this software and associated documentation files (the "Software"), to deal
  9. * in the Software without restriction, including without limitation the rights
  10. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  11. * copies of the Software, and to permit persons to whom the Software is
  12. * furnished to do so, subject to the following conditions:
  13. *
  14. * The above copyright notice and this permission notice shall be included in all
  15. * copies or substantial portions of the Software.
  16. *
  17. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  18. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  19. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  20. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  21. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  22. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  23. * SOFTWARE.
  24. */
  25. // Used for setting prototype methods that IE8 chokes on.
  26. var DELETE = 'delete';
  27. // Constants describing the size of trie nodes.
  28. var SHIFT = 5; // Resulted in best performance after ______?
  29. var SIZE = 1 << SHIFT;
  30. var MASK = SIZE - 1;
  31. // A consistent shared value representing "not set" which equals nothing other
  32. // than itself, and nothing that could be provided externally.
  33. var NOT_SET = {};
  34. // Boolean references, Rough equivalent of `bool &`.
  35. function MakeRef() {
  36. return { value: false };
  37. }
  38. function SetRef(ref) {
  39. if (ref) {
  40. ref.value = true;
  41. }
  42. }
  43. // A function which returns a value representing an "owner" for transient writes
  44. // to tries. The return value will only ever equal itself, and will not equal
  45. // the return of any subsequent call of this function.
  46. function OwnerID() { }
  47. function ensureSize(iter) {
  48. // @ts-expect-error size should exists on Collection
  49. if (iter.size === undefined) {
  50. // @ts-expect-error size should exists on Collection, __iterate does exist on Collection
  51. iter.size = iter.__iterate(returnTrue);
  52. }
  53. // @ts-expect-error size should exists on Collection
  54. return iter.size;
  55. }
  56. function wrapIndex(iter, index) {
  57. // This implements "is array index" which the ECMAString spec defines as:
  58. //
  59. // A String property name P is an array index if and only if
  60. // ToString(ToUint32(P)) is equal to P and ToUint32(P) is not equal
  61. // to 2^32−1.
  62. //
  63. // http://www.ecma-international.org/ecma-262/6.0/#sec-array-exotic-objects
  64. if (typeof index !== 'number') {
  65. var uint32Index = index >>> 0; // N >>> 0 is shorthand for ToUint32
  66. if ('' + uint32Index !== index || uint32Index === 4294967295) {
  67. return NaN;
  68. }
  69. index = uint32Index;
  70. }
  71. return index < 0 ? ensureSize(iter) + index : index;
  72. }
  73. function returnTrue() {
  74. return true;
  75. }
  76. function wholeSlice(begin, end, size) {
  77. return (((begin === 0 && !isNeg(begin)) ||
  78. (size !== undefined && begin <= -size)) &&
  79. (end === undefined || (size !== undefined && end >= size)));
  80. }
  81. function resolveBegin(begin, size) {
  82. return resolveIndex(begin, size, 0);
  83. }
  84. function resolveEnd(end, size) {
  85. return resolveIndex(end, size, size);
  86. }
  87. function resolveIndex(index, size, defaultIndex) {
  88. // Sanitize indices using this shorthand for ToInt32(argument)
  89. // http://www.ecma-international.org/ecma-262/6.0/#sec-toint32
  90. return index === undefined
  91. ? defaultIndex
  92. : isNeg(index)
  93. ? size === Infinity
  94. ? size
  95. : Math.max(0, size + index) | 0
  96. : size === undefined || size === index
  97. ? index
  98. : Math.min(size, index) | 0;
  99. }
  100. function isNeg(value) {
  101. // Account for -0 which is negative, but not less than 0.
  102. return value < 0 || (value === 0 && 1 / value === -Infinity);
  103. }
  104. // Note: value is unchanged to not break immutable-devtools.
  105. var IS_COLLECTION_SYMBOL = '@@__IMMUTABLE_ITERABLE__@@';
  106. /**
  107. * True if `maybeCollection` is a Collection, or any of its subclasses.
  108. *
  109. * ```js
  110. * import { isCollection, Map, List, Stack } from 'immutable';
  111. *
  112. * isCollection([]); // false
  113. * isCollection({}); // false
  114. * isCollection(Map()); // true
  115. * isCollection(List()); // true
  116. * isCollection(Stack()); // true
  117. * ```
  118. */
  119. function isCollection(maybeCollection) {
  120. return Boolean(maybeCollection &&
  121. // @ts-expect-error: maybeCollection is typed as `{}`, need to change in 6.0 to `maybeCollection && typeof maybeCollection === 'object' && IS_COLLECTION_SYMBOL in maybeCollection`
  122. maybeCollection[IS_COLLECTION_SYMBOL]);
  123. }
  124. var IS_KEYED_SYMBOL = '@@__IMMUTABLE_KEYED__@@';
  125. /**
  126. * True if `maybeKeyed` is a Collection.Keyed, or any of its subclasses.
  127. *
  128. * ```js
  129. * import { isKeyed, Map, List, Stack } from 'immutable';
  130. *
  131. * isKeyed([]); // false
  132. * isKeyed({}); // false
  133. * isKeyed(Map()); // true
  134. * isKeyed(List()); // false
  135. * isKeyed(Stack()); // false
  136. * ```
  137. */
  138. function isKeyed(maybeKeyed) {
  139. return Boolean(maybeKeyed &&
  140. // @ts-expect-error: maybeKeyed is typed as `{}`, need to change in 6.0 to `maybeKeyed && typeof maybeKeyed === 'object' && IS_KEYED_SYMBOL in maybeKeyed`
  141. maybeKeyed[IS_KEYED_SYMBOL]);
  142. }
  143. var IS_INDEXED_SYMBOL = '@@__IMMUTABLE_INDEXED__@@';
  144. /**
  145. * True if `maybeIndexed` is a Collection.Indexed, or any of its subclasses.
  146. *
  147. * ```js
  148. * import { isIndexed, Map, List, Stack, Set } from 'immutable';
  149. *
  150. * isIndexed([]); // false
  151. * isIndexed({}); // false
  152. * isIndexed(Map()); // false
  153. * isIndexed(List()); // true
  154. * isIndexed(Stack()); // true
  155. * isIndexed(Set()); // false
  156. * ```
  157. */
  158. function isIndexed(maybeIndexed) {
  159. return Boolean(maybeIndexed &&
  160. // @ts-expect-error: maybeIndexed is typed as `{}`, need to change in 6.0 to `maybeIndexed && typeof maybeIndexed === 'object' && IS_INDEXED_SYMBOL in maybeIndexed`
  161. maybeIndexed[IS_INDEXED_SYMBOL]);
  162. }
  163. /**
  164. * True if `maybeAssociative` is either a Keyed or Indexed Collection.
  165. *
  166. * ```js
  167. * import { isAssociative, Map, List, Stack, Set } from 'immutable';
  168. *
  169. * isAssociative([]); // false
  170. * isAssociative({}); // false
  171. * isAssociative(Map()); // true
  172. * isAssociative(List()); // true
  173. * isAssociative(Stack()); // true
  174. * isAssociative(Set()); // false
  175. * ```
  176. */
  177. function isAssociative(maybeAssociative) {
  178. return isKeyed(maybeAssociative) || isIndexed(maybeAssociative);
  179. }
  180. var Collection = function Collection(value) {
  181. // eslint-disable-next-line no-constructor-return
  182. return isCollection(value) ? value : Seq(value);
  183. };
  184. var KeyedCollection = /*@__PURE__*/(function (Collection) {
  185. function KeyedCollection(value) {
  186. // eslint-disable-next-line no-constructor-return
  187. return isKeyed(value) ? value : KeyedSeq(value);
  188. }
  189. if ( Collection ) KeyedCollection.__proto__ = Collection;
  190. KeyedCollection.prototype = Object.create( Collection && Collection.prototype );
  191. KeyedCollection.prototype.constructor = KeyedCollection;
  192. return KeyedCollection;
  193. }(Collection));
  194. var IndexedCollection = /*@__PURE__*/(function (Collection) {
  195. function IndexedCollection(value) {
  196. // eslint-disable-next-line no-constructor-return
  197. return isIndexed(value) ? value : IndexedSeq(value);
  198. }
  199. if ( Collection ) IndexedCollection.__proto__ = Collection;
  200. IndexedCollection.prototype = Object.create( Collection && Collection.prototype );
  201. IndexedCollection.prototype.constructor = IndexedCollection;
  202. return IndexedCollection;
  203. }(Collection));
  204. var SetCollection = /*@__PURE__*/(function (Collection) {
  205. function SetCollection(value) {
  206. // eslint-disable-next-line no-constructor-return
  207. return isCollection(value) && !isAssociative(value) ? value : SetSeq(value);
  208. }
  209. if ( Collection ) SetCollection.__proto__ = Collection;
  210. SetCollection.prototype = Object.create( Collection && Collection.prototype );
  211. SetCollection.prototype.constructor = SetCollection;
  212. return SetCollection;
  213. }(Collection));
  214. Collection.Keyed = KeyedCollection;
  215. Collection.Indexed = IndexedCollection;
  216. Collection.Set = SetCollection;
  217. var IS_SEQ_SYMBOL = '@@__IMMUTABLE_SEQ__@@';
  218. /**
  219. * True if `maybeSeq` is a Seq.
  220. */
  221. function isSeq(maybeSeq) {
  222. return Boolean(maybeSeq &&
  223. // @ts-expect-error: maybeSeq is typed as `{}`, need to change in 6.0 to `maybeSeq && typeof maybeSeq === 'object' && MAYBE_SEQ_SYMBOL in maybeSeq`
  224. maybeSeq[IS_SEQ_SYMBOL]);
  225. }
  226. var IS_RECORD_SYMBOL = '@@__IMMUTABLE_RECORD__@@';
  227. /**
  228. * True if `maybeRecord` is a Record.
  229. */
  230. function isRecord(maybeRecord) {
  231. return Boolean(maybeRecord &&
  232. // @ts-expect-error: maybeRecord is typed as `{}`, need to change in 6.0 to `maybeRecord && typeof maybeRecord === 'object' && IS_RECORD_SYMBOL in maybeRecord`
  233. maybeRecord[IS_RECORD_SYMBOL]);
  234. }
  235. /**
  236. * True if `maybeImmutable` is an Immutable Collection or Record.
  237. *
  238. * Note: Still returns true even if the collections is within a `withMutations()`.
  239. *
  240. * ```js
  241. * import { isImmutable, Map, List, Stack } from 'immutable';
  242. * isImmutable([]); // false
  243. * isImmutable({}); // false
  244. * isImmutable(Map()); // true
  245. * isImmutable(List()); // true
  246. * isImmutable(Stack()); // true
  247. * isImmutable(Map().asMutable()); // true
  248. * ```
  249. */
  250. function isImmutable(maybeImmutable) {
  251. return isCollection(maybeImmutable) || isRecord(maybeImmutable);
  252. }
  253. var IS_ORDERED_SYMBOL = '@@__IMMUTABLE_ORDERED__@@';
  254. function isOrdered(maybeOrdered) {
  255. return Boolean(maybeOrdered &&
  256. // @ts-expect-error: maybeOrdered is typed as `{}`, need to change in 6.0 to `maybeOrdered && typeof maybeOrdered === 'object' && IS_ORDERED_SYMBOL in maybeOrdered`
  257. maybeOrdered[IS_ORDERED_SYMBOL]);
  258. }
  259. var ITERATE_KEYS = 0;
  260. var ITERATE_VALUES = 1;
  261. var ITERATE_ENTRIES = 2;
  262. var REAL_ITERATOR_SYMBOL = typeof Symbol === 'function' && Symbol.iterator;
  263. var FAUX_ITERATOR_SYMBOL = '@@iterator';
  264. var ITERATOR_SYMBOL = REAL_ITERATOR_SYMBOL || FAUX_ITERATOR_SYMBOL;
  265. var Iterator = function Iterator(next) {
  266. this.next = next;
  267. };
  268. Iterator.prototype.toString = function toString () {
  269. return '[Iterator]';
  270. };
  271. Iterator.KEYS = ITERATE_KEYS;
  272. Iterator.VALUES = ITERATE_VALUES;
  273. Iterator.ENTRIES = ITERATE_ENTRIES;
  274. Iterator.prototype.inspect = Iterator.prototype.toSource = function () {
  275. return this.toString();
  276. };
  277. Iterator.prototype[ITERATOR_SYMBOL] = function () {
  278. return this;
  279. };
  280. function iteratorValue(type, k, v, iteratorResult) {
  281. var value =
  282. type === ITERATE_KEYS ? k : type === ITERATE_VALUES ? v : [k, v];
  283. // eslint-disable-next-line @typescript-eslint/no-unused-expressions -- TODO enable eslint here
  284. iteratorResult
  285. ? (iteratorResult.value = value)
  286. : (iteratorResult = {
  287. value: value,
  288. done: false,
  289. });
  290. return iteratorResult;
  291. }
  292. function iteratorDone() {
  293. return { value: undefined, done: true };
  294. }
  295. function hasIterator(maybeIterable) {
  296. if (Array.isArray(maybeIterable)) {
  297. // IE11 trick as it does not support `Symbol.iterator`
  298. return true;
  299. }
  300. return !!getIteratorFn(maybeIterable);
  301. }
  302. function isIterator(maybeIterator) {
  303. return maybeIterator && typeof maybeIterator.next === 'function';
  304. }
  305. function getIterator(iterable) {
  306. var iteratorFn = getIteratorFn(iterable);
  307. return iteratorFn && iteratorFn.call(iterable);
  308. }
  309. function getIteratorFn(iterable) {
  310. var iteratorFn =
  311. iterable &&
  312. ((REAL_ITERATOR_SYMBOL && iterable[REAL_ITERATOR_SYMBOL]) ||
  313. iterable[FAUX_ITERATOR_SYMBOL]);
  314. if (typeof iteratorFn === 'function') {
  315. return iteratorFn;
  316. }
  317. }
  318. function isEntriesIterable(maybeIterable) {
  319. var iteratorFn = getIteratorFn(maybeIterable);
  320. return iteratorFn && iteratorFn === maybeIterable.entries;
  321. }
  322. function isKeysIterable(maybeIterable) {
  323. var iteratorFn = getIteratorFn(maybeIterable);
  324. return iteratorFn && iteratorFn === maybeIterable.keys;
  325. }
  326. var hasOwnProperty = Object.prototype.hasOwnProperty;
  327. function isArrayLike(value) {
  328. if (Array.isArray(value) || typeof value === 'string') {
  329. return true;
  330. }
  331. // @ts-expect-error "Type 'unknown' is not assignable to type 'boolean'" : convert to Boolean
  332. return (value &&
  333. typeof value === 'object' &&
  334. // @ts-expect-error check that `'length' in value &&`
  335. Number.isInteger(value.length) &&
  336. // @ts-expect-error check that `'length' in value &&`
  337. value.length >= 0 &&
  338. // @ts-expect-error check that `'length' in value &&`
  339. (value.length === 0
  340. ? // Only {length: 0} is considered Array-like.
  341. Object.keys(value).length === 1
  342. : // An object is only Array-like if it has a property where the last value
  343. // in the array-like may be found (which could be undefined).
  344. // @ts-expect-error check that `'length' in value &&`
  345. value.hasOwnProperty(value.length - 1)));
  346. }
  347. var Seq = /*@__PURE__*/(function (Collection) {
  348. function Seq(value) {
  349. // eslint-disable-next-line no-constructor-return
  350. return value === undefined || value === null
  351. ? emptySequence()
  352. : isImmutable(value)
  353. ? value.toSeq()
  354. : seqFromValue(value);
  355. }
  356. if ( Collection ) Seq.__proto__ = Collection;
  357. Seq.prototype = Object.create( Collection && Collection.prototype );
  358. Seq.prototype.constructor = Seq;
  359. Seq.prototype.toSeq = function toSeq () {
  360. return this;
  361. };
  362. Seq.prototype.toString = function toString () {
  363. return this.__toString('Seq {', '}');
  364. };
  365. Seq.prototype.cacheResult = function cacheResult () {
  366. if (!this._cache && this.__iterateUncached) {
  367. this._cache = this.entrySeq().toArray();
  368. this.size = this._cache.length;
  369. }
  370. return this;
  371. };
  372. // abstract __iterateUncached(fn, reverse)
  373. Seq.prototype.__iterate = function __iterate (fn, reverse) {
  374. var cache = this._cache;
  375. if (cache) {
  376. var size = cache.length;
  377. var i = 0;
  378. while (i !== size) {
  379. var entry = cache[reverse ? size - ++i : i++];
  380. if (fn(entry[1], entry[0], this) === false) {
  381. break;
  382. }
  383. }
  384. return i;
  385. }
  386. return this.__iterateUncached(fn, reverse);
  387. };
  388. // abstract __iteratorUncached(type, reverse)
  389. Seq.prototype.__iterator = function __iterator (type, reverse) {
  390. var cache = this._cache;
  391. if (cache) {
  392. var size = cache.length;
  393. var i = 0;
  394. return new Iterator(function () {
  395. if (i === size) {
  396. return iteratorDone();
  397. }
  398. var entry = cache[reverse ? size - ++i : i++];
  399. return iteratorValue(type, entry[0], entry[1]);
  400. });
  401. }
  402. return this.__iteratorUncached(type, reverse);
  403. };
  404. return Seq;
  405. }(Collection));
  406. var KeyedSeq = /*@__PURE__*/(function (Seq) {
  407. function KeyedSeq(value) {
  408. // eslint-disable-next-line no-constructor-return
  409. return value === undefined || value === null
  410. ? emptySequence().toKeyedSeq()
  411. : isCollection(value)
  412. ? isKeyed(value)
  413. ? value.toSeq()
  414. : value.fromEntrySeq()
  415. : isRecord(value)
  416. ? value.toSeq()
  417. : keyedSeqFromValue(value);
  418. }
  419. if ( Seq ) KeyedSeq.__proto__ = Seq;
  420. KeyedSeq.prototype = Object.create( Seq && Seq.prototype );
  421. KeyedSeq.prototype.constructor = KeyedSeq;
  422. KeyedSeq.prototype.toKeyedSeq = function toKeyedSeq () {
  423. return this;
  424. };
  425. return KeyedSeq;
  426. }(Seq));
  427. var IndexedSeq = /*@__PURE__*/(function (Seq) {
  428. function IndexedSeq(value) {
  429. // eslint-disable-next-line no-constructor-return
  430. return value === undefined || value === null
  431. ? emptySequence()
  432. : isCollection(value)
  433. ? isKeyed(value)
  434. ? value.entrySeq()
  435. : value.toIndexedSeq()
  436. : isRecord(value)
  437. ? value.toSeq().entrySeq()
  438. : indexedSeqFromValue(value);
  439. }
  440. if ( Seq ) IndexedSeq.__proto__ = Seq;
  441. IndexedSeq.prototype = Object.create( Seq && Seq.prototype );
  442. IndexedSeq.prototype.constructor = IndexedSeq;
  443. IndexedSeq.of = function of (/*...values*/) {
  444. return IndexedSeq(arguments);
  445. };
  446. IndexedSeq.prototype.toIndexedSeq = function toIndexedSeq () {
  447. return this;
  448. };
  449. IndexedSeq.prototype.toString = function toString () {
  450. return this.__toString('Seq [', ']');
  451. };
  452. return IndexedSeq;
  453. }(Seq));
  454. var SetSeq = /*@__PURE__*/(function (Seq) {
  455. function SetSeq(value) {
  456. // eslint-disable-next-line no-constructor-return
  457. return (
  458. isCollection(value) && !isAssociative(value) ? value : IndexedSeq(value)
  459. ).toSetSeq();
  460. }
  461. if ( Seq ) SetSeq.__proto__ = Seq;
  462. SetSeq.prototype = Object.create( Seq && Seq.prototype );
  463. SetSeq.prototype.constructor = SetSeq;
  464. SetSeq.of = function of (/*...values*/) {
  465. return SetSeq(arguments);
  466. };
  467. SetSeq.prototype.toSetSeq = function toSetSeq () {
  468. return this;
  469. };
  470. return SetSeq;
  471. }(Seq));
  472. Seq.isSeq = isSeq;
  473. Seq.Keyed = KeyedSeq;
  474. Seq.Set = SetSeq;
  475. Seq.Indexed = IndexedSeq;
  476. Seq.prototype[IS_SEQ_SYMBOL] = true;
  477. // #pragma Root Sequences
  478. var ArraySeq = /*@__PURE__*/(function (IndexedSeq) {
  479. function ArraySeq(array) {
  480. this._array = array;
  481. this.size = array.length;
  482. }
  483. if ( IndexedSeq ) ArraySeq.__proto__ = IndexedSeq;
  484. ArraySeq.prototype = Object.create( IndexedSeq && IndexedSeq.prototype );
  485. ArraySeq.prototype.constructor = ArraySeq;
  486. ArraySeq.prototype.get = function get (index, notSetValue) {
  487. return this.has(index) ? this._array[wrapIndex(this, index)] : notSetValue;
  488. };
  489. ArraySeq.prototype.__iterate = function __iterate (fn, reverse) {
  490. var array = this._array;
  491. var size = array.length;
  492. var i = 0;
  493. while (i !== size) {
  494. var ii = reverse ? size - ++i : i++;
  495. if (fn(array[ii], ii, this) === false) {
  496. break;
  497. }
  498. }
  499. return i;
  500. };
  501. ArraySeq.prototype.__iterator = function __iterator (type, reverse) {
  502. var array = this._array;
  503. var size = array.length;
  504. var i = 0;
  505. return new Iterator(function () {
  506. if (i === size) {
  507. return iteratorDone();
  508. }
  509. var ii = reverse ? size - ++i : i++;
  510. return iteratorValue(type, ii, array[ii]);
  511. });
  512. };
  513. return ArraySeq;
  514. }(IndexedSeq));
  515. var ObjectSeq = /*@__PURE__*/(function (KeyedSeq) {
  516. function ObjectSeq(object) {
  517. var keys = Object.keys(object).concat(
  518. Object.getOwnPropertySymbols ? Object.getOwnPropertySymbols(object) : []
  519. );
  520. this._object = object;
  521. this._keys = keys;
  522. this.size = keys.length;
  523. }
  524. if ( KeyedSeq ) ObjectSeq.__proto__ = KeyedSeq;
  525. ObjectSeq.prototype = Object.create( KeyedSeq && KeyedSeq.prototype );
  526. ObjectSeq.prototype.constructor = ObjectSeq;
  527. ObjectSeq.prototype.get = function get (key, notSetValue) {
  528. if (notSetValue !== undefined && !this.has(key)) {
  529. return notSetValue;
  530. }
  531. return this._object[key];
  532. };
  533. ObjectSeq.prototype.has = function has (key) {
  534. return hasOwnProperty.call(this._object, key);
  535. };
  536. ObjectSeq.prototype.__iterate = function __iterate (fn, reverse) {
  537. var object = this._object;
  538. var keys = this._keys;
  539. var size = keys.length;
  540. var i = 0;
  541. while (i !== size) {
  542. var key = keys[reverse ? size - ++i : i++];
  543. if (fn(object[key], key, this) === false) {
  544. break;
  545. }
  546. }
  547. return i;
  548. };
  549. ObjectSeq.prototype.__iterator = function __iterator (type, reverse) {
  550. var object = this._object;
  551. var keys = this._keys;
  552. var size = keys.length;
  553. var i = 0;
  554. return new Iterator(function () {
  555. if (i === size) {
  556. return iteratorDone();
  557. }
  558. var key = keys[reverse ? size - ++i : i++];
  559. return iteratorValue(type, key, object[key]);
  560. });
  561. };
  562. return ObjectSeq;
  563. }(KeyedSeq));
  564. ObjectSeq.prototype[IS_ORDERED_SYMBOL] = true;
  565. var CollectionSeq = /*@__PURE__*/(function (IndexedSeq) {
  566. function CollectionSeq(collection) {
  567. this._collection = collection;
  568. this.size = collection.length || collection.size;
  569. }
  570. if ( IndexedSeq ) CollectionSeq.__proto__ = IndexedSeq;
  571. CollectionSeq.prototype = Object.create( IndexedSeq && IndexedSeq.prototype );
  572. CollectionSeq.prototype.constructor = CollectionSeq;
  573. CollectionSeq.prototype.__iterateUncached = function __iterateUncached (fn, reverse) {
  574. if (reverse) {
  575. return this.cacheResult().__iterate(fn, reverse);
  576. }
  577. var collection = this._collection;
  578. var iterator = getIterator(collection);
  579. var iterations = 0;
  580. if (isIterator(iterator)) {
  581. var step;
  582. while (!(step = iterator.next()).done) {
  583. if (fn(step.value, iterations++, this) === false) {
  584. break;
  585. }
  586. }
  587. }
  588. return iterations;
  589. };
  590. CollectionSeq.prototype.__iteratorUncached = function __iteratorUncached (type, reverse) {
  591. if (reverse) {
  592. return this.cacheResult().__iterator(type, reverse);
  593. }
  594. var collection = this._collection;
  595. var iterator = getIterator(collection);
  596. if (!isIterator(iterator)) {
  597. return new Iterator(iteratorDone);
  598. }
  599. var iterations = 0;
  600. return new Iterator(function () {
  601. var step = iterator.next();
  602. return step.done ? step : iteratorValue(type, iterations++, step.value);
  603. });
  604. };
  605. return CollectionSeq;
  606. }(IndexedSeq));
  607. // # pragma Helper functions
  608. var EMPTY_SEQ;
  609. function emptySequence() {
  610. return EMPTY_SEQ || (EMPTY_SEQ = new ArraySeq([]));
  611. }
  612. function keyedSeqFromValue(value) {
  613. var seq = maybeIndexedSeqFromValue(value);
  614. if (seq) {
  615. return seq.fromEntrySeq();
  616. }
  617. if (typeof value === 'object') {
  618. return new ObjectSeq(value);
  619. }
  620. throw new TypeError(
  621. 'Expected Array or collection object of [k, v] entries, or keyed object: ' +
  622. value
  623. );
  624. }
  625. function indexedSeqFromValue(value) {
  626. var seq = maybeIndexedSeqFromValue(value);
  627. if (seq) {
  628. return seq;
  629. }
  630. throw new TypeError(
  631. 'Expected Array or collection object of values: ' + value
  632. );
  633. }
  634. function seqFromValue(value) {
  635. var seq = maybeIndexedSeqFromValue(value);
  636. if (seq) {
  637. return isEntriesIterable(value)
  638. ? seq.fromEntrySeq()
  639. : isKeysIterable(value)
  640. ? seq.toSetSeq()
  641. : seq;
  642. }
  643. if (typeof value === 'object') {
  644. return new ObjectSeq(value);
  645. }
  646. throw new TypeError(
  647. 'Expected Array or collection object of values, or keyed object: ' + value
  648. );
  649. }
  650. function maybeIndexedSeqFromValue(value) {
  651. return isArrayLike(value)
  652. ? new ArraySeq(value)
  653. : hasIterator(value)
  654. ? new CollectionSeq(value)
  655. : undefined;
  656. }
  657. var IS_MAP_SYMBOL = '@@__IMMUTABLE_MAP__@@';
  658. /**
  659. * True if `maybeMap` is a Map.
  660. *
  661. * Also true for OrderedMaps.
  662. */
  663. function isMap(maybeMap) {
  664. return Boolean(maybeMap &&
  665. // @ts-expect-error: maybeMap is typed as `{}`, need to change in 6.0 to `maybeMap && typeof maybeMap === 'object' && IS_MAP_SYMBOL in maybeMap`
  666. maybeMap[IS_MAP_SYMBOL]);
  667. }
  668. /**
  669. * True if `maybeOrderedMap` is an OrderedMap.
  670. */
  671. function isOrderedMap(maybeOrderedMap) {
  672. return isMap(maybeOrderedMap) && isOrdered(maybeOrderedMap);
  673. }
  674. /**
  675. * True if `maybeValue` is a JavaScript Object which has *both* `equals()`
  676. * and `hashCode()` methods.
  677. *
  678. * Any two instances of *value objects* can be compared for value equality with
  679. * `Immutable.is()` and can be used as keys in a `Map` or members in a `Set`.
  680. */
  681. function isValueObject(maybeValue) {
  682. return Boolean(maybeValue &&
  683. // @ts-expect-error: maybeValue is typed as `{}`
  684. typeof maybeValue.equals === 'function' &&
  685. // @ts-expect-error: maybeValue is typed as `{}`
  686. typeof maybeValue.hashCode === 'function');
  687. }
  688. /**
  689. * An extension of the "same-value" algorithm as [described for use by ES6 Map
  690. * and Set](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map#Key_equality)
  691. *
  692. * NaN is considered the same as NaN, however -0 and 0 are considered the same
  693. * value, which is different from the algorithm described by
  694. * [`Object.is`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/is).
  695. *
  696. * This is extended further to allow Objects to describe the values they
  697. * represent, by way of `valueOf` or `equals` (and `hashCode`).
  698. *
  699. * Note: because of this extension, the key equality of Immutable.Map and the
  700. * value equality of Immutable.Set will differ from ES6 Map and Set.
  701. *
  702. * ### Defining custom values
  703. *
  704. * The easiest way to describe the value an object represents is by implementing
  705. * `valueOf`. For example, `Date` represents a value by returning a unix
  706. * timestamp for `valueOf`:
  707. *
  708. * var date1 = new Date(1234567890000); // Fri Feb 13 2009 ...
  709. * var date2 = new Date(1234567890000);
  710. * date1.valueOf(); // 1234567890000
  711. * assert( date1 !== date2 );
  712. * assert( Immutable.is( date1, date2 ) );
  713. *
  714. * Note: overriding `valueOf` may have other implications if you use this object
  715. * where JavaScript expects a primitive, such as implicit string coercion.
  716. *
  717. * For more complex types, especially collections, implementing `valueOf` may
  718. * not be performant. An alternative is to implement `equals` and `hashCode`.
  719. *
  720. * `equals` takes another object, presumably of similar type, and returns true
  721. * if it is equal. Equality is symmetrical, so the same result should be
  722. * returned if this and the argument are flipped.
  723. *
  724. * assert( a.equals(b) === b.equals(a) );
  725. *
  726. * `hashCode` returns a 32bit integer number representing the object which will
  727. * be used to determine how to store the value object in a Map or Set. You must
  728. * provide both or neither methods, one must not exist without the other.
  729. *
  730. * Also, an important relationship between these methods must be upheld: if two
  731. * values are equal, they *must* return the same hashCode. If the values are not
  732. * equal, they might have the same hashCode; this is called a hash collision,
  733. * and while undesirable for performance reasons, it is acceptable.
  734. *
  735. * if (a.equals(b)) {
  736. * assert( a.hashCode() === b.hashCode() );
  737. * }
  738. *
  739. * All Immutable collections are Value Objects: they implement `equals()`
  740. * and `hashCode()`.
  741. */
  742. function is(valueA, valueB) {
  743. if (valueA === valueB || (valueA !== valueA && valueB !== valueB)) {
  744. return true;
  745. }
  746. if (!valueA || !valueB) {
  747. return false;
  748. }
  749. if (typeof valueA.valueOf === 'function' &&
  750. typeof valueB.valueOf === 'function') {
  751. valueA = valueA.valueOf();
  752. valueB = valueB.valueOf();
  753. if (valueA === valueB || (valueA !== valueA && valueB !== valueB)) {
  754. return true;
  755. }
  756. if (!valueA || !valueB) {
  757. return false;
  758. }
  759. }
  760. return !!(isValueObject(valueA) &&
  761. isValueObject(valueB) &&
  762. valueA.equals(valueB));
  763. }
  764. var imul =
  765. typeof Math.imul === 'function' && Math.imul(0xffffffff, 2) === -2
  766. ? Math.imul
  767. : function imul(a, b) {
  768. a |= 0; // int
  769. b |= 0; // int
  770. var c = a & 0xffff;
  771. var d = b & 0xffff;
  772. // Shift by 0 fixes the sign on the high part.
  773. return (c * d + ((((a >>> 16) * d + c * (b >>> 16)) << 16) >>> 0)) | 0; // int
  774. };
  775. // v8 has an optimization for storing 31-bit signed numbers.
  776. // Values which have either 00 or 11 as the high order bits qualify.
  777. // This function drops the highest order bit in a signed number, maintaining
  778. // the sign bit.
  779. function smi(i32) {
  780. return ((i32 >>> 1) & 0x40000000) | (i32 & 0xbfffffff);
  781. }
  782. var defaultValueOf = Object.prototype.valueOf;
  783. function hash(o) {
  784. // eslint-disable-next-line eqeqeq
  785. if (o == null) {
  786. return hashNullish(o);
  787. }
  788. if (typeof o.hashCode === 'function') {
  789. // Drop any high bits from accidentally long hash codes.
  790. return smi(o.hashCode(o));
  791. }
  792. var v = valueOf(o);
  793. // eslint-disable-next-line eqeqeq
  794. if (v == null) {
  795. return hashNullish(v);
  796. }
  797. switch (typeof v) {
  798. case 'boolean':
  799. // The hash values for built-in constants are a 1 value for each 5-byte
  800. // shift region expect for the first, which encodes the value. This
  801. // reduces the odds of a hash collision for these common values.
  802. return v ? 0x42108421 : 0x42108420;
  803. case 'number':
  804. return hashNumber(v);
  805. case 'string':
  806. return v.length > STRING_HASH_CACHE_MIN_STRLEN
  807. ? cachedHashString(v)
  808. : hashString(v);
  809. case 'object':
  810. case 'function':
  811. return hashJSObj(v);
  812. case 'symbol':
  813. return hashSymbol(v);
  814. default:
  815. if (typeof v.toString === 'function') {
  816. return hashString(v.toString());
  817. }
  818. throw new Error('Value type ' + typeof v + ' cannot be hashed.');
  819. }
  820. }
  821. function hashNullish(nullish) {
  822. return nullish === null ? 0x42108422 : /* undefined */ 0x42108423;
  823. }
  824. // Compress arbitrarily large numbers into smi hashes.
  825. function hashNumber(n) {
  826. if (n !== n || n === Infinity) {
  827. return 0;
  828. }
  829. var hash = n | 0;
  830. if (hash !== n) {
  831. hash ^= n * 0xffffffff;
  832. }
  833. while (n > 0xffffffff) {
  834. n /= 0xffffffff;
  835. hash ^= n;
  836. }
  837. return smi(hash);
  838. }
  839. function cachedHashString(string) {
  840. var hashed = stringHashCache[string];
  841. if (hashed === undefined) {
  842. hashed = hashString(string);
  843. if (STRING_HASH_CACHE_SIZE === STRING_HASH_CACHE_MAX_SIZE) {
  844. STRING_HASH_CACHE_SIZE = 0;
  845. stringHashCache = {};
  846. }
  847. STRING_HASH_CACHE_SIZE++;
  848. stringHashCache[string] = hashed;
  849. }
  850. return hashed;
  851. }
  852. // http://jsperf.com/hashing-strings
  853. function hashString(string) {
  854. // This is the hash from JVM
  855. // The hash code for a string is computed as
  856. // s[0] * 31 ^ (n - 1) + s[1] * 31 ^ (n - 2) + ... + s[n - 1],
  857. // where s[i] is the ith character of the string and n is the length of
  858. // the string. We "mod" the result to make it between 0 (inclusive) and 2^31
  859. // (exclusive) by dropping high bits.
  860. var hashed = 0;
  861. for (var ii = 0; ii < string.length; ii++) {
  862. hashed = (31 * hashed + string.charCodeAt(ii)) | 0;
  863. }
  864. return smi(hashed);
  865. }
  866. function hashSymbol(sym) {
  867. var hashed = symbolMap[sym];
  868. if (hashed !== undefined) {
  869. return hashed;
  870. }
  871. hashed = nextHash();
  872. symbolMap[sym] = hashed;
  873. return hashed;
  874. }
  875. function hashJSObj(obj) {
  876. var hashed;
  877. if (usingWeakMap) {
  878. hashed = weakMap.get(obj);
  879. if (hashed !== undefined) {
  880. return hashed;
  881. }
  882. }
  883. hashed = obj[UID_HASH_KEY];
  884. if (hashed !== undefined) {
  885. return hashed;
  886. }
  887. if (!canDefineProperty) {
  888. hashed = obj.propertyIsEnumerable && obj.propertyIsEnumerable[UID_HASH_KEY];
  889. if (hashed !== undefined) {
  890. return hashed;
  891. }
  892. hashed = getIENodeHash(obj);
  893. if (hashed !== undefined) {
  894. return hashed;
  895. }
  896. }
  897. hashed = nextHash();
  898. if (usingWeakMap) {
  899. weakMap.set(obj, hashed);
  900. } else if (isExtensible !== undefined && isExtensible(obj) === false) {
  901. throw new Error('Non-extensible objects are not allowed as keys.');
  902. } else if (canDefineProperty) {
  903. Object.defineProperty(obj, UID_HASH_KEY, {
  904. enumerable: false,
  905. configurable: false,
  906. writable: false,
  907. value: hashed,
  908. });
  909. } else if (
  910. obj.propertyIsEnumerable !== undefined &&
  911. obj.propertyIsEnumerable === obj.constructor.prototype.propertyIsEnumerable
  912. ) {
  913. // Since we can't define a non-enumerable property on the object
  914. // we'll hijack one of the less-used non-enumerable properties to
  915. // save our hash on it. Since this is a function it will not show up in
  916. // `JSON.stringify` which is what we want.
  917. obj.propertyIsEnumerable = function () {
  918. return this.constructor.prototype.propertyIsEnumerable.apply(
  919. this,
  920. arguments
  921. );
  922. };
  923. obj.propertyIsEnumerable[UID_HASH_KEY] = hashed;
  924. } else if (obj.nodeType !== undefined) {
  925. // At this point we couldn't get the IE `uniqueID` to use as a hash
  926. // and we couldn't use a non-enumerable property to exploit the
  927. // dontEnum bug so we simply add the `UID_HASH_KEY` on the node
  928. // itself.
  929. obj[UID_HASH_KEY] = hashed;
  930. } else {
  931. throw new Error('Unable to set a non-enumerable property on object.');
  932. }
  933. return hashed;
  934. }
  935. // Get references to ES5 object methods.
  936. var isExtensible = Object.isExtensible;
  937. // True if Object.defineProperty works as expected. IE8 fails this test.
  938. var canDefineProperty = (function () {
  939. try {
  940. Object.defineProperty({}, '@', {});
  941. return true;
  942. // eslint-disable-next-line @typescript-eslint/no-unused-vars
  943. } catch (e) {
  944. return false;
  945. }
  946. })();
  947. // IE has a `uniqueID` property on DOM nodes. We can construct the hash from it
  948. // and avoid memory leaks from the IE cloneNode bug.
  949. function getIENodeHash(node) {
  950. if (node && node.nodeType > 0) {
  951. switch (node.nodeType) {
  952. case 1: // Element
  953. return node.uniqueID;
  954. case 9: // Document
  955. return node.documentElement && node.documentElement.uniqueID;
  956. }
  957. }
  958. }
  959. function valueOf(obj) {
  960. return obj.valueOf !== defaultValueOf && typeof obj.valueOf === 'function'
  961. ? obj.valueOf(obj)
  962. : obj;
  963. }
  964. function nextHash() {
  965. var nextHash = ++_objHashUID;
  966. if (_objHashUID & 0x40000000) {
  967. _objHashUID = 0;
  968. }
  969. return nextHash;
  970. }
  971. // If possible, use a WeakMap.
  972. var usingWeakMap = typeof WeakMap === 'function';
  973. var weakMap;
  974. if (usingWeakMap) {
  975. weakMap = new WeakMap();
  976. }
  977. var symbolMap = Object.create(null);
  978. var _objHashUID = 0;
  979. var UID_HASH_KEY = '__immutablehash__';
  980. if (typeof Symbol === 'function') {
  981. UID_HASH_KEY = Symbol(UID_HASH_KEY);
  982. }
  983. var STRING_HASH_CACHE_MIN_STRLEN = 16;
  984. var STRING_HASH_CACHE_MAX_SIZE = 255;
  985. var STRING_HASH_CACHE_SIZE = 0;
  986. var stringHashCache = {};
  987. var ToKeyedSequence = /*@__PURE__*/(function (KeyedSeq) {
  988. function ToKeyedSequence(indexed, useKeys) {
  989. this._iter = indexed;
  990. this._useKeys = useKeys;
  991. this.size = indexed.size;
  992. }
  993. if ( KeyedSeq ) ToKeyedSequence.__proto__ = KeyedSeq;
  994. ToKeyedSequence.prototype = Object.create( KeyedSeq && KeyedSeq.prototype );
  995. ToKeyedSequence.prototype.constructor = ToKeyedSequence;
  996. ToKeyedSequence.prototype.get = function get (key, notSetValue) {
  997. return this._iter.get(key, notSetValue);
  998. };
  999. ToKeyedSequence.prototype.has = function has (key) {
  1000. return this._iter.has(key);
  1001. };
  1002. ToKeyedSequence.prototype.valueSeq = function valueSeq () {
  1003. return this._iter.valueSeq();
  1004. };
  1005. ToKeyedSequence.prototype.reverse = function reverse () {
  1006. var this$1$1 = this;
  1007. var reversedSequence = reverseFactory(this, true);
  1008. if (!this._useKeys) {
  1009. reversedSequence.valueSeq = function () { return this$1$1._iter.toSeq().reverse(); };
  1010. }
  1011. return reversedSequence;
  1012. };
  1013. ToKeyedSequence.prototype.map = function map (mapper, context) {
  1014. var this$1$1 = this;
  1015. var mappedSequence = mapFactory(this, mapper, context);
  1016. if (!this._useKeys) {
  1017. mappedSequence.valueSeq = function () { return this$1$1._iter.toSeq().map(mapper, context); };
  1018. }
  1019. return mappedSequence;
  1020. };
  1021. ToKeyedSequence.prototype.__iterate = function __iterate (fn, reverse) {
  1022. var this$1$1 = this;
  1023. return this._iter.__iterate(function (v, k) { return fn(v, k, this$1$1); }, reverse);
  1024. };
  1025. ToKeyedSequence.prototype.__iterator = function __iterator (type, reverse) {
  1026. return this._iter.__iterator(type, reverse);
  1027. };
  1028. return ToKeyedSequence;
  1029. }(KeyedSeq));
  1030. ToKeyedSequence.prototype[IS_ORDERED_SYMBOL] = true;
  1031. var ToIndexedSequence = /*@__PURE__*/(function (IndexedSeq) {
  1032. function ToIndexedSequence(iter) {
  1033. this._iter = iter;
  1034. this.size = iter.size;
  1035. }
  1036. if ( IndexedSeq ) ToIndexedSequence.__proto__ = IndexedSeq;
  1037. ToIndexedSequence.prototype = Object.create( IndexedSeq && IndexedSeq.prototype );
  1038. ToIndexedSequence.prototype.constructor = ToIndexedSequence;
  1039. ToIndexedSequence.prototype.includes = function includes (value) {
  1040. return this._iter.includes(value);
  1041. };
  1042. ToIndexedSequence.prototype.__iterate = function __iterate (fn, reverse) {
  1043. var this$1$1 = this;
  1044. var i = 0;
  1045. // eslint-disable-next-line @typescript-eslint/no-unused-expressions -- TODO enable eslint here
  1046. reverse && ensureSize(this);
  1047. return this._iter.__iterate(
  1048. function (v) { return fn(v, reverse ? this$1$1.size - ++i : i++, this$1$1); },
  1049. reverse
  1050. );
  1051. };
  1052. ToIndexedSequence.prototype.__iterator = function __iterator (type, reverse) {
  1053. var this$1$1 = this;
  1054. var iterator = this._iter.__iterator(ITERATE_VALUES, reverse);
  1055. var i = 0;
  1056. // eslint-disable-next-line @typescript-eslint/no-unused-expressions -- TODO enable eslint here
  1057. reverse && ensureSize(this);
  1058. return new Iterator(function () {
  1059. var step = iterator.next();
  1060. return step.done
  1061. ? step
  1062. : iteratorValue(
  1063. type,
  1064. reverse ? this$1$1.size - ++i : i++,
  1065. step.value,
  1066. step
  1067. );
  1068. });
  1069. };
  1070. return ToIndexedSequence;
  1071. }(IndexedSeq));
  1072. var ToSetSequence = /*@__PURE__*/(function (SetSeq) {
  1073. function ToSetSequence(iter) {
  1074. this._iter = iter;
  1075. this.size = iter.size;
  1076. }
  1077. if ( SetSeq ) ToSetSequence.__proto__ = SetSeq;
  1078. ToSetSequence.prototype = Object.create( SetSeq && SetSeq.prototype );
  1079. ToSetSequence.prototype.constructor = ToSetSequence;
  1080. ToSetSequence.prototype.has = function has (key) {
  1081. return this._iter.includes(key);
  1082. };
  1083. ToSetSequence.prototype.__iterate = function __iterate (fn, reverse) {
  1084. var this$1$1 = this;
  1085. return this._iter.__iterate(function (v) { return fn(v, v, this$1$1); }, reverse);
  1086. };
  1087. ToSetSequence.prototype.__iterator = function __iterator (type, reverse) {
  1088. var iterator = this._iter.__iterator(ITERATE_VALUES, reverse);
  1089. return new Iterator(function () {
  1090. var step = iterator.next();
  1091. return step.done
  1092. ? step
  1093. : iteratorValue(type, step.value, step.value, step);
  1094. });
  1095. };
  1096. return ToSetSequence;
  1097. }(SetSeq));
  1098. var FromEntriesSequence = /*@__PURE__*/(function (KeyedSeq) {
  1099. function FromEntriesSequence(entries) {
  1100. this._iter = entries;
  1101. this.size = entries.size;
  1102. }
  1103. if ( KeyedSeq ) FromEntriesSequence.__proto__ = KeyedSeq;
  1104. FromEntriesSequence.prototype = Object.create( KeyedSeq && KeyedSeq.prototype );
  1105. FromEntriesSequence.prototype.constructor = FromEntriesSequence;
  1106. FromEntriesSequence.prototype.entrySeq = function entrySeq () {
  1107. return this._iter.toSeq();
  1108. };
  1109. FromEntriesSequence.prototype.__iterate = function __iterate (fn, reverse) {
  1110. var this$1$1 = this;
  1111. return this._iter.__iterate(function (entry) {
  1112. // Check if entry exists first so array access doesn't throw for holes
  1113. // in the parent iteration.
  1114. if (entry) {
  1115. validateEntry(entry);
  1116. var indexedCollection = isCollection(entry);
  1117. return fn(
  1118. indexedCollection ? entry.get(1) : entry[1],
  1119. indexedCollection ? entry.get(0) : entry[0],
  1120. this$1$1
  1121. );
  1122. }
  1123. }, reverse);
  1124. };
  1125. FromEntriesSequence.prototype.__iterator = function __iterator (type, reverse) {
  1126. var iterator = this._iter.__iterator(ITERATE_VALUES, reverse);
  1127. return new Iterator(function () {
  1128. while (true) {
  1129. var step = iterator.next();
  1130. if (step.done) {
  1131. return step;
  1132. }
  1133. var entry = step.value;
  1134. // Check if entry exists first so array access doesn't throw for holes
  1135. // in the parent iteration.
  1136. if (entry) {
  1137. validateEntry(entry);
  1138. var indexedCollection = isCollection(entry);
  1139. return iteratorValue(
  1140. type,
  1141. indexedCollection ? entry.get(0) : entry[0],
  1142. indexedCollection ? entry.get(1) : entry[1],
  1143. step
  1144. );
  1145. }
  1146. }
  1147. });
  1148. };
  1149. return FromEntriesSequence;
  1150. }(KeyedSeq));
  1151. ToIndexedSequence.prototype.cacheResult =
  1152. ToKeyedSequence.prototype.cacheResult =
  1153. ToSetSequence.prototype.cacheResult =
  1154. FromEntriesSequence.prototype.cacheResult =
  1155. cacheResultThrough;
  1156. function flipFactory(collection) {
  1157. var flipSequence = makeSequence(collection);
  1158. flipSequence._iter = collection;
  1159. flipSequence.size = collection.size;
  1160. flipSequence.flip = function () { return collection; };
  1161. flipSequence.reverse = function () {
  1162. var reversedSequence = collection.reverse.apply(this); // super.reverse()
  1163. reversedSequence.flip = function () { return collection.reverse(); };
  1164. return reversedSequence;
  1165. };
  1166. flipSequence.has = function (key) { return collection.includes(key); };
  1167. flipSequence.includes = function (key) { return collection.has(key); };
  1168. flipSequence.cacheResult = cacheResultThrough;
  1169. flipSequence.__iterateUncached = function (fn, reverse) {
  1170. var this$1$1 = this;
  1171. return collection.__iterate(function (v, k) { return fn(k, v, this$1$1) !== false; }, reverse);
  1172. };
  1173. flipSequence.__iteratorUncached = function (type, reverse) {
  1174. if (type === ITERATE_ENTRIES) {
  1175. var iterator = collection.__iterator(type, reverse);
  1176. return new Iterator(function () {
  1177. var step = iterator.next();
  1178. if (!step.done) {
  1179. var k = step.value[0];
  1180. step.value[0] = step.value[1];
  1181. step.value[1] = k;
  1182. }
  1183. return step;
  1184. });
  1185. }
  1186. return collection.__iterator(
  1187. type === ITERATE_VALUES ? ITERATE_KEYS : ITERATE_VALUES,
  1188. reverse
  1189. );
  1190. };
  1191. return flipSequence;
  1192. }
  1193. function mapFactory(collection, mapper, context) {
  1194. var mappedSequence = makeSequence(collection);
  1195. mappedSequence.size = collection.size;
  1196. mappedSequence.has = function (key) { return collection.has(key); };
  1197. mappedSequence.get = function (key, notSetValue) {
  1198. var v = collection.get(key, NOT_SET);
  1199. return v === NOT_SET
  1200. ? notSetValue
  1201. : mapper.call(context, v, key, collection);
  1202. };
  1203. mappedSequence.__iterateUncached = function (fn, reverse) {
  1204. var this$1$1 = this;
  1205. return collection.__iterate(
  1206. function (v, k, c) { return fn(mapper.call(context, v, k, c), k, this$1$1) !== false; },
  1207. reverse
  1208. );
  1209. };
  1210. mappedSequence.__iteratorUncached = function (type, reverse) {
  1211. var iterator = collection.__iterator(ITERATE_ENTRIES, reverse);
  1212. return new Iterator(function () {
  1213. var step = iterator.next();
  1214. if (step.done) {
  1215. return step;
  1216. }
  1217. var entry = step.value;
  1218. var key = entry[0];
  1219. return iteratorValue(
  1220. type,
  1221. key,
  1222. mapper.call(context, entry[1], key, collection),
  1223. step
  1224. );
  1225. });
  1226. };
  1227. return mappedSequence;
  1228. }
  1229. function reverseFactory(collection, useKeys) {
  1230. var this$1$1 = this;
  1231. var reversedSequence = makeSequence(collection);
  1232. reversedSequence._iter = collection;
  1233. reversedSequence.size = collection.size;
  1234. reversedSequence.reverse = function () { return collection; };
  1235. if (collection.flip) {
  1236. reversedSequence.flip = function () {
  1237. var flipSequence = flipFactory(collection);
  1238. flipSequence.reverse = function () { return collection.flip(); };
  1239. return flipSequence;
  1240. };
  1241. }
  1242. reversedSequence.get = function (key, notSetValue) { return collection.get(useKeys ? key : -1 - key, notSetValue); };
  1243. reversedSequence.has = function (key) { return collection.has(useKeys ? key : -1 - key); };
  1244. reversedSequence.includes = function (value) { return collection.includes(value); };
  1245. reversedSequence.cacheResult = cacheResultThrough;
  1246. reversedSequence.__iterate = function (fn, reverse) {
  1247. var this$1$1 = this;
  1248. var i = 0;
  1249. // eslint-disable-next-line @typescript-eslint/no-unused-expressions -- TODO enable eslint here
  1250. reverse && ensureSize(collection);
  1251. return collection.__iterate(
  1252. function (v, k) { return fn(v, useKeys ? k : reverse ? this$1$1.size - ++i : i++, this$1$1); },
  1253. !reverse
  1254. );
  1255. };
  1256. reversedSequence.__iterator = function (type, reverse) {
  1257. var i = 0;
  1258. // eslint-disable-next-line @typescript-eslint/no-unused-expressions -- TODO enable eslint here
  1259. reverse && ensureSize(collection);
  1260. var iterator = collection.__iterator(ITERATE_ENTRIES, !reverse);
  1261. return new Iterator(function () {
  1262. var step = iterator.next();
  1263. if (step.done) {
  1264. return step;
  1265. }
  1266. var entry = step.value;
  1267. return iteratorValue(
  1268. type,
  1269. useKeys ? entry[0] : reverse ? this$1$1.size - ++i : i++,
  1270. entry[1],
  1271. step
  1272. );
  1273. });
  1274. };
  1275. return reversedSequence;
  1276. }
  1277. function filterFactory(collection, predicate, context, useKeys) {
  1278. var filterSequence = makeSequence(collection);
  1279. if (useKeys) {
  1280. filterSequence.has = function (key) {
  1281. var v = collection.get(key, NOT_SET);
  1282. return v !== NOT_SET && !!predicate.call(context, v, key, collection);
  1283. };
  1284. filterSequence.get = function (key, notSetValue) {
  1285. var v = collection.get(key, NOT_SET);
  1286. return v !== NOT_SET && predicate.call(context, v, key, collection)
  1287. ? v
  1288. : notSetValue;
  1289. };
  1290. }
  1291. filterSequence.__iterateUncached = function (fn, reverse) {
  1292. var this$1$1 = this;
  1293. var iterations = 0;
  1294. collection.__iterate(function (v, k, c) {
  1295. if (predicate.call(context, v, k, c)) {
  1296. iterations++;
  1297. return fn(v, useKeys ? k : iterations - 1, this$1$1);
  1298. }
  1299. }, reverse);
  1300. return iterations;
  1301. };
  1302. filterSequence.__iteratorUncached = function (type, reverse) {
  1303. var iterator = collection.__iterator(ITERATE_ENTRIES, reverse);
  1304. var iterations = 0;
  1305. return new Iterator(function () {
  1306. while (true) {
  1307. var step = iterator.next();
  1308. if (step.done) {
  1309. return step;
  1310. }
  1311. var entry = step.value;
  1312. var key = entry[0];
  1313. var value = entry[1];
  1314. if (predicate.call(context, value, key, collection)) {
  1315. return iteratorValue(type, useKeys ? key : iterations++, value, step);
  1316. }
  1317. }
  1318. });
  1319. };
  1320. return filterSequence;
  1321. }
  1322. function countByFactory(collection, grouper, context) {
  1323. var groups = Map().asMutable();
  1324. collection.__iterate(function (v, k) {
  1325. groups.update(grouper.call(context, v, k, collection), 0, function (a) { return a + 1; });
  1326. });
  1327. return groups.asImmutable();
  1328. }
  1329. function groupByFactory(collection, grouper, context) {
  1330. var isKeyedIter = isKeyed(collection);
  1331. var groups = (isOrdered(collection) ? OrderedMap() : Map()).asMutable();
  1332. collection.__iterate(function (v, k) {
  1333. groups.update(
  1334. grouper.call(context, v, k, collection),
  1335. function (a) { return ((a = a || []), a.push(isKeyedIter ? [k, v] : v), a); }
  1336. );
  1337. });
  1338. var coerce = collectionClass(collection);
  1339. return groups.map(function (arr) { return reify(collection, coerce(arr)); }).asImmutable();
  1340. }
  1341. function partitionFactory(collection, predicate, context) {
  1342. var isKeyedIter = isKeyed(collection);
  1343. var groups = [[], []];
  1344. collection.__iterate(function (v, k) {
  1345. groups[predicate.call(context, v, k, collection) ? 1 : 0].push(
  1346. isKeyedIter ? [k, v] : v
  1347. );
  1348. });
  1349. var coerce = collectionClass(collection);
  1350. return groups.map(function (arr) { return reify(collection, coerce(arr)); });
  1351. }
  1352. function sliceFactory(collection, begin, end, useKeys) {
  1353. var originalSize = collection.size;
  1354. if (wholeSlice(begin, end, originalSize)) {
  1355. return collection;
  1356. }
  1357. // begin or end can not be resolved if they were provided as negative numbers and
  1358. // this collection's size is unknown. In that case, cache first so there is
  1359. // a known size and these do not resolve to NaN.
  1360. if (typeof originalSize === 'undefined' && (begin < 0 || end < 0)) {
  1361. return sliceFactory(collection.toSeq().cacheResult(), begin, end, useKeys);
  1362. }
  1363. var resolvedBegin = resolveBegin(begin, originalSize);
  1364. var resolvedEnd = resolveEnd(end, originalSize);
  1365. // Note: resolvedEnd is undefined when the original sequence's length is
  1366. // unknown and this slice did not supply an end and should contain all
  1367. // elements after resolvedBegin.
  1368. // In that case, resolvedSize will be NaN and sliceSize will remain undefined.
  1369. var resolvedSize = resolvedEnd - resolvedBegin;
  1370. var sliceSize;
  1371. if (resolvedSize === resolvedSize) {
  1372. sliceSize = resolvedSize < 0 ? 0 : resolvedSize;
  1373. }
  1374. var sliceSeq = makeSequence(collection);
  1375. // If collection.size is undefined, the size of the realized sliceSeq is
  1376. // unknown at this point unless the number of items to slice is 0
  1377. sliceSeq.size =
  1378. sliceSize === 0 ? sliceSize : (collection.size && sliceSize) || undefined;
  1379. if (!useKeys && isSeq(collection) && sliceSize >= 0) {
  1380. sliceSeq.get = function (index, notSetValue) {
  1381. index = wrapIndex(this, index);
  1382. return index >= 0 && index < sliceSize
  1383. ? collection.get(index + resolvedBegin, notSetValue)
  1384. : notSetValue;
  1385. };
  1386. }
  1387. sliceSeq.__iterateUncached = function (fn, reverse) {
  1388. var this$1$1 = this;
  1389. if (sliceSize === 0) {
  1390. return 0;
  1391. }
  1392. if (reverse) {
  1393. return this.cacheResult().__iterate(fn, reverse);
  1394. }
  1395. var skipped = 0;
  1396. var isSkipping = true;
  1397. var iterations = 0;
  1398. collection.__iterate(function (v, k) {
  1399. if (!(isSkipping && (isSkipping = skipped++ < resolvedBegin))) {
  1400. iterations++;
  1401. return (
  1402. fn(v, useKeys ? k : iterations - 1, this$1$1) !== false &&
  1403. iterations !== sliceSize
  1404. );
  1405. }
  1406. });
  1407. return iterations;
  1408. };
  1409. sliceSeq.__iteratorUncached = function (type, reverse) {
  1410. if (sliceSize !== 0 && reverse) {
  1411. return this.cacheResult().__iterator(type, reverse);
  1412. }
  1413. // Don't bother instantiating parent iterator if taking 0.
  1414. if (sliceSize === 0) {
  1415. return new Iterator(iteratorDone);
  1416. }
  1417. var iterator = collection.__iterator(type, reverse);
  1418. var skipped = 0;
  1419. var iterations = 0;
  1420. return new Iterator(function () {
  1421. while (skipped++ < resolvedBegin) {
  1422. iterator.next();
  1423. }
  1424. if (++iterations > sliceSize) {
  1425. return iteratorDone();
  1426. }
  1427. var step = iterator.next();
  1428. if (useKeys || type === ITERATE_VALUES || step.done) {
  1429. return step;
  1430. }
  1431. if (type === ITERATE_KEYS) {
  1432. return iteratorValue(type, iterations - 1, undefined, step);
  1433. }
  1434. return iteratorValue(type, iterations - 1, step.value[1], step);
  1435. });
  1436. };
  1437. return sliceSeq;
  1438. }
  1439. function takeWhileFactory(collection, predicate, context) {
  1440. var takeSequence = makeSequence(collection);
  1441. takeSequence.__iterateUncached = function (fn, reverse) {
  1442. var this$1$1 = this;
  1443. if (reverse) {
  1444. return this.cacheResult().__iterate(fn, reverse);
  1445. }
  1446. var iterations = 0;
  1447. collection.__iterate(
  1448. function (v, k, c) { return predicate.call(context, v, k, c) && ++iterations && fn(v, k, this$1$1); }
  1449. );
  1450. return iterations;
  1451. };
  1452. takeSequence.__iteratorUncached = function (type, reverse) {
  1453. var this$1$1 = this;
  1454. if (reverse) {
  1455. return this.cacheResult().__iterator(type, reverse);
  1456. }
  1457. var iterator = collection.__iterator(ITERATE_ENTRIES, reverse);
  1458. var iterating = true;
  1459. return new Iterator(function () {
  1460. if (!iterating) {
  1461. return iteratorDone();
  1462. }
  1463. var step = iterator.next();
  1464. if (step.done) {
  1465. return step;
  1466. }
  1467. var entry = step.value;
  1468. var k = entry[0];
  1469. var v = entry[1];
  1470. if (!predicate.call(context, v, k, this$1$1)) {
  1471. iterating = false;
  1472. return iteratorDone();
  1473. }
  1474. return type === ITERATE_ENTRIES ? step : iteratorValue(type, k, v, step);
  1475. });
  1476. };
  1477. return takeSequence;
  1478. }
  1479. function skipWhileFactory(collection, predicate, context, useKeys) {
  1480. var skipSequence = makeSequence(collection);
  1481. skipSequence.__iterateUncached = function (fn, reverse) {
  1482. var this$1$1 = this;
  1483. if (reverse) {
  1484. return this.cacheResult().__iterate(fn, reverse);
  1485. }
  1486. var isSkipping = true;
  1487. var iterations = 0;
  1488. collection.__iterate(function (v, k, c) {
  1489. if (!(isSkipping && (isSkipping = predicate.call(context, v, k, c)))) {
  1490. iterations++;
  1491. return fn(v, useKeys ? k : iterations - 1, this$1$1);
  1492. }
  1493. });
  1494. return iterations;
  1495. };
  1496. skipSequence.__iteratorUncached = function (type, reverse) {
  1497. var this$1$1 = this;
  1498. if (reverse) {
  1499. return this.cacheResult().__iterator(type, reverse);
  1500. }
  1501. var iterator = collection.__iterator(ITERATE_ENTRIES, reverse);
  1502. var skipping = true;
  1503. var iterations = 0;
  1504. return new Iterator(function () {
  1505. var step;
  1506. var k;
  1507. var v;
  1508. do {
  1509. step = iterator.next();
  1510. if (step.done) {
  1511. if (useKeys || type === ITERATE_VALUES) {
  1512. return step;
  1513. }
  1514. if (type === ITERATE_KEYS) {
  1515. return iteratorValue(type, iterations++, undefined, step);
  1516. }
  1517. return iteratorValue(type, iterations++, step.value[1], step);
  1518. }
  1519. var entry = step.value;
  1520. k = entry[0];
  1521. v = entry[1];
  1522. // eslint-disable-next-line @typescript-eslint/no-unused-expressions -- TODO enable eslint here
  1523. skipping && (skipping = predicate.call(context, v, k, this$1$1));
  1524. } while (skipping);
  1525. return type === ITERATE_ENTRIES ? step : iteratorValue(type, k, v, step);
  1526. });
  1527. };
  1528. return skipSequence;
  1529. }
  1530. var ConcatSeq = /*@__PURE__*/(function (Seq) {
  1531. function ConcatSeq(iterables) {
  1532. this._wrappedIterables = iterables.flatMap(function (iterable) {
  1533. if (iterable._wrappedIterables) {
  1534. return iterable._wrappedIterables;
  1535. }
  1536. return [iterable];
  1537. });
  1538. this.size = this._wrappedIterables.reduce(function (sum, iterable) {
  1539. if (sum !== undefined) {
  1540. var size = iterable.size;
  1541. if (size !== undefined) {
  1542. return sum + size;
  1543. }
  1544. }
  1545. }, 0);
  1546. this[IS_KEYED_SYMBOL] = this._wrappedIterables[0][IS_KEYED_SYMBOL];
  1547. this[IS_INDEXED_SYMBOL] = this._wrappedIterables[0][IS_INDEXED_SYMBOL];
  1548. this[IS_ORDERED_SYMBOL] = this._wrappedIterables[0][IS_ORDERED_SYMBOL];
  1549. }
  1550. if ( Seq ) ConcatSeq.__proto__ = Seq;
  1551. ConcatSeq.prototype = Object.create( Seq && Seq.prototype );
  1552. ConcatSeq.prototype.constructor = ConcatSeq;
  1553. ConcatSeq.prototype.__iterateUncached = function __iterateUncached (fn, reverse) {
  1554. if (this._wrappedIterables.length === 0) {
  1555. return;
  1556. }
  1557. if (reverse) {
  1558. return this.cacheResult().__iterate(fn, reverse);
  1559. }
  1560. var iterableIndex = 0;
  1561. var useKeys = isKeyed(this);
  1562. var iteratorType = useKeys ? ITERATE_ENTRIES : ITERATE_VALUES;
  1563. var currentIterator = this._wrappedIterables[iterableIndex].__iterator(
  1564. iteratorType,
  1565. reverse
  1566. );
  1567. var keepGoing = true;
  1568. var index = 0;
  1569. while (keepGoing) {
  1570. var next = currentIterator.next();
  1571. while (next.done) {
  1572. iterableIndex++;
  1573. if (iterableIndex === this._wrappedIterables.length) {
  1574. return index;
  1575. }
  1576. currentIterator = this._wrappedIterables[iterableIndex].__iterator(
  1577. iteratorType,
  1578. reverse
  1579. );
  1580. next = currentIterator.next();
  1581. }
  1582. var fnResult = useKeys
  1583. ? fn(next.value[1], next.value[0], this)
  1584. : fn(next.value, index, this);
  1585. keepGoing = fnResult !== false;
  1586. index++;
  1587. }
  1588. return index;
  1589. };
  1590. ConcatSeq.prototype.__iteratorUncached = function __iteratorUncached (type, reverse) {
  1591. var this$1$1 = this;
  1592. if (this._wrappedIterables.length === 0) {
  1593. return new Iterator(iteratorDone);
  1594. }
  1595. if (reverse) {
  1596. return this.cacheResult().__iterator(type, reverse);
  1597. }
  1598. var iterableIndex = 0;
  1599. var currentIterator = this._wrappedIterables[iterableIndex].__iterator(
  1600. type,
  1601. reverse
  1602. );
  1603. return new Iterator(function () {
  1604. var next = currentIterator.next();
  1605. while (next.done) {
  1606. iterableIndex++;
  1607. if (iterableIndex === this$1$1._wrappedIterables.length) {
  1608. return next;
  1609. }
  1610. currentIterator = this$1$1._wrappedIterables[iterableIndex].__iterator(
  1611. type,
  1612. reverse
  1613. );
  1614. next = currentIterator.next();
  1615. }
  1616. return next;
  1617. });
  1618. };
  1619. return ConcatSeq;
  1620. }(Seq));
  1621. function concatFactory(collection, values) {
  1622. var isKeyedCollection = isKeyed(collection);
  1623. var iters = [collection]
  1624. .concat(values)
  1625. .map(function (v) {
  1626. if (!isCollection(v)) {
  1627. v = isKeyedCollection
  1628. ? keyedSeqFromValue(v)
  1629. : indexedSeqFromValue(Array.isArray(v) ? v : [v]);
  1630. } else if (isKeyedCollection) {
  1631. v = KeyedCollection(v);
  1632. }
  1633. return v;
  1634. })
  1635. .filter(function (v) { return v.size !== 0; });
  1636. if (iters.length === 0) {
  1637. return collection;
  1638. }
  1639. if (iters.length === 1) {
  1640. var singleton = iters[0];
  1641. if (
  1642. singleton === collection ||
  1643. (isKeyedCollection && isKeyed(singleton)) ||
  1644. (isIndexed(collection) && isIndexed(singleton))
  1645. ) {
  1646. return singleton;
  1647. }
  1648. }
  1649. return new ConcatSeq(iters);
  1650. }
  1651. function flattenFactory(collection, depth, useKeys) {
  1652. var flatSequence = makeSequence(collection);
  1653. flatSequence.__iterateUncached = function (fn, reverse) {
  1654. if (reverse) {
  1655. return this.cacheResult().__iterate(fn, reverse);
  1656. }
  1657. var iterations = 0;
  1658. var stopped = false;
  1659. function flatDeep(iter, currentDepth) {
  1660. iter.__iterate(function (v, k) {
  1661. if ((!depth || currentDepth < depth) && isCollection(v)) {
  1662. flatDeep(v, currentDepth + 1);
  1663. } else {
  1664. iterations++;
  1665. if (fn(v, useKeys ? k : iterations - 1, flatSequence) === false) {
  1666. stopped = true;
  1667. }
  1668. }
  1669. return !stopped;
  1670. }, reverse);
  1671. }
  1672. flatDeep(collection, 0);
  1673. return iterations;
  1674. };
  1675. flatSequence.__iteratorUncached = function (type, reverse) {
  1676. if (reverse) {
  1677. return this.cacheResult().__iterator(type, reverse);
  1678. }
  1679. var iterator = collection.__iterator(type, reverse);
  1680. var stack = [];
  1681. var iterations = 0;
  1682. return new Iterator(function () {
  1683. while (iterator) {
  1684. var step = iterator.next();
  1685. if (step.done !== false) {
  1686. iterator = stack.pop();
  1687. continue;
  1688. }
  1689. var v = step.value;
  1690. if (type === ITERATE_ENTRIES) {
  1691. v = v[1];
  1692. }
  1693. if ((!depth || stack.length < depth) && isCollection(v)) {
  1694. stack.push(iterator);
  1695. iterator = v.__iterator(type, reverse);
  1696. } else {
  1697. return useKeys ? step : iteratorValue(type, iterations++, v, step);
  1698. }
  1699. }
  1700. return iteratorDone();
  1701. });
  1702. };
  1703. return flatSequence;
  1704. }
  1705. function flatMapFactory(collection, mapper, context) {
  1706. var coerce = collectionClass(collection);
  1707. return collection
  1708. .toSeq()
  1709. .map(function (v, k) { return coerce(mapper.call(context, v, k, collection)); })
  1710. .flatten(true);
  1711. }
  1712. function interposeFactory(collection, separator) {
  1713. var interposedSequence = makeSequence(collection);
  1714. interposedSequence.size = collection.size && collection.size * 2 - 1;
  1715. interposedSequence.__iterateUncached = function (fn, reverse) {
  1716. var this$1$1 = this;
  1717. var iterations = 0;
  1718. collection.__iterate(
  1719. function (v) { return (!iterations || fn(separator, iterations++, this$1$1) !== false) &&
  1720. fn(v, iterations++, this$1$1) !== false; },
  1721. reverse
  1722. );
  1723. return iterations;
  1724. };
  1725. interposedSequence.__iteratorUncached = function (type, reverse) {
  1726. var iterator = collection.__iterator(ITERATE_VALUES, reverse);
  1727. var iterations = 0;
  1728. var step;
  1729. return new Iterator(function () {
  1730. if (!step || iterations % 2) {
  1731. step = iterator.next();
  1732. if (step.done) {
  1733. return step;
  1734. }
  1735. }
  1736. return iterations % 2
  1737. ? iteratorValue(type, iterations++, separator)
  1738. : iteratorValue(type, iterations++, step.value, step);
  1739. });
  1740. };
  1741. return interposedSequence;
  1742. }
  1743. function sortFactory(collection, comparator, mapper) {
  1744. if (!comparator) {
  1745. comparator = defaultComparator;
  1746. }
  1747. var isKeyedCollection = isKeyed(collection);
  1748. var index = 0;
  1749. var entries = collection
  1750. .toSeq()
  1751. .map(function (v, k) { return [k, v, index++, mapper ? mapper(v, k, collection) : v]; })
  1752. .valueSeq()
  1753. .toArray();
  1754. entries
  1755. .sort(function (a, b) { return comparator(a[3], b[3]) || a[2] - b[2]; })
  1756. .forEach(
  1757. isKeyedCollection
  1758. ? function (v, i) {
  1759. entries[i].length = 2;
  1760. }
  1761. : function (v, i) {
  1762. entries[i] = v[1];
  1763. }
  1764. );
  1765. return isKeyedCollection
  1766. ? KeyedSeq(entries)
  1767. : isIndexed(collection)
  1768. ? IndexedSeq(entries)
  1769. : SetSeq(entries);
  1770. }
  1771. function maxFactory(collection, comparator, mapper) {
  1772. if (!comparator) {
  1773. comparator = defaultComparator;
  1774. }
  1775. if (mapper) {
  1776. var entry = collection
  1777. .toSeq()
  1778. .map(function (v, k) { return [v, mapper(v, k, collection)]; })
  1779. .reduce(function (a, b) { return (maxCompare(comparator, a[1], b[1]) ? b : a); });
  1780. return entry && entry[0];
  1781. }
  1782. return collection.reduce(function (a, b) { return (maxCompare(comparator, a, b) ? b : a); });
  1783. }
  1784. function maxCompare(comparator, a, b) {
  1785. var comp = comparator(b, a);
  1786. // b is considered the new max if the comparator declares them equal, but
  1787. // they are not equal and b is in fact a nullish value.
  1788. return (
  1789. (comp === 0 && b !== a && (b === undefined || b === null || b !== b)) ||
  1790. comp > 0
  1791. );
  1792. }
  1793. function zipWithFactory(keyIter, zipper, iters, zipAll) {
  1794. var zipSequence = makeSequence(keyIter);
  1795. var sizes = new ArraySeq(iters).map(function (i) { return i.size; });
  1796. zipSequence.size = zipAll ? sizes.max() : sizes.min();
  1797. // Note: this a generic base implementation of __iterate in terms of
  1798. // __iterator which may be more generically useful in the future.
  1799. zipSequence.__iterate = function (fn, reverse) {
  1800. /* generic:
  1801. var iterator = this.__iterator(ITERATE_ENTRIES, reverse);
  1802. var step;
  1803. var iterations = 0;
  1804. while (!(step = iterator.next()).done) {
  1805. iterations++;
  1806. if (fn(step.value[1], step.value[0], this) === false) {
  1807. break;
  1808. }
  1809. }
  1810. return iterations;
  1811. */
  1812. // indexed:
  1813. var iterator = this.__iterator(ITERATE_VALUES, reverse);
  1814. var step;
  1815. var iterations = 0;
  1816. while (!(step = iterator.next()).done) {
  1817. if (fn(step.value, iterations++, this) === false) {
  1818. break;
  1819. }
  1820. }
  1821. return iterations;
  1822. };
  1823. zipSequence.__iteratorUncached = function (type, reverse) {
  1824. var iterators = iters.map(
  1825. function (i) { return ((i = Collection(i)), getIterator(reverse ? i.reverse() : i)); }
  1826. );
  1827. var iterations = 0;
  1828. var isDone = false;
  1829. return new Iterator(function () {
  1830. var steps;
  1831. if (!isDone) {
  1832. steps = iterators.map(function (i) { return i.next(); });
  1833. isDone = zipAll
  1834. ? steps.every(function (s) { return s.done; })
  1835. : steps.some(function (s) { return s.done; });
  1836. }
  1837. if (isDone) {
  1838. return iteratorDone();
  1839. }
  1840. return iteratorValue(
  1841. type,
  1842. iterations++,
  1843. zipper.apply(
  1844. null,
  1845. steps.map(function (s) { return s.value; })
  1846. )
  1847. );
  1848. });
  1849. };
  1850. return zipSequence;
  1851. }
  1852. // #pragma Helper Functions
  1853. function reify(iter, seq) {
  1854. return iter === seq ? iter : isSeq(iter) ? seq : iter.constructor(seq);
  1855. }
  1856. function validateEntry(entry) {
  1857. if (entry !== Object(entry)) {
  1858. throw new TypeError('Expected [K, V] tuple: ' + entry);
  1859. }
  1860. }
  1861. function collectionClass(collection) {
  1862. return isKeyed(collection)
  1863. ? KeyedCollection
  1864. : isIndexed(collection)
  1865. ? IndexedCollection
  1866. : SetCollection;
  1867. }
  1868. function makeSequence(collection) {
  1869. return Object.create(
  1870. (isKeyed(collection)
  1871. ? KeyedSeq
  1872. : isIndexed(collection)
  1873. ? IndexedSeq
  1874. : SetSeq
  1875. ).prototype
  1876. );
  1877. }
  1878. function cacheResultThrough() {
  1879. if (this._iter.cacheResult) {
  1880. this._iter.cacheResult();
  1881. this.size = this._iter.size;
  1882. return this;
  1883. }
  1884. return Seq.prototype.cacheResult.call(this);
  1885. }
  1886. function defaultComparator(a, b) {
  1887. if (a === undefined && b === undefined) {
  1888. return 0;
  1889. }
  1890. if (a === undefined) {
  1891. return 1;
  1892. }
  1893. if (b === undefined) {
  1894. return -1;
  1895. }
  1896. return a > b ? 1 : a < b ? -1 : 0;
  1897. }
  1898. // http://jsperf.com/copy-array-inline
  1899. function arrCopy(arr, offset) {
  1900. offset = offset || 0;
  1901. var len = Math.max(0, arr.length - offset);
  1902. var newArr = new Array(len);
  1903. for (var ii = 0; ii < len; ii++) {
  1904. // @ts-expect-error We may want to guard for undefined values with `if (arr[ii + offset] !== undefined`, but ths should not happen by design
  1905. newArr[ii] = arr[ii + offset];
  1906. }
  1907. return newArr;
  1908. }
  1909. function invariant(condition, error) {
  1910. if (!condition)
  1911. { throw new Error(error); }
  1912. }
  1913. function assertNotInfinite(size) {
  1914. invariant(size !== Infinity, 'Cannot perform this action with an infinite size.');
  1915. }
  1916. function coerceKeyPath(keyPath) {
  1917. if (isArrayLike(keyPath) && typeof keyPath !== 'string') {
  1918. return keyPath;
  1919. }
  1920. if (isOrdered(keyPath)) {
  1921. return keyPath.toArray();
  1922. }
  1923. throw new TypeError('Invalid keyPath: expected Ordered Collection or Array: ' + keyPath);
  1924. }
  1925. var toString = Object.prototype.toString;
  1926. function isPlainObject(value) {
  1927. // The base prototype's toString deals with Argument objects and native namespaces like Math
  1928. if (!value ||
  1929. typeof value !== 'object' ||
  1930. toString.call(value) !== '[object Object]') {
  1931. return false;
  1932. }
  1933. var proto = Object.getPrototypeOf(value);
  1934. if (proto === null) {
  1935. return true;
  1936. }
  1937. // Iteratively going up the prototype chain is needed for cross-realm environments (differing contexts, iframes, etc)
  1938. var parentProto = proto;
  1939. var nextProto = Object.getPrototypeOf(proto);
  1940. while (nextProto !== null) {
  1941. parentProto = nextProto;
  1942. nextProto = Object.getPrototypeOf(parentProto);
  1943. }
  1944. return parentProto === proto;
  1945. }
  1946. /**
  1947. * Returns true if the value is a potentially-persistent data structure, either
  1948. * provided by Immutable.js or a plain Array or Object.
  1949. */
  1950. function isDataStructure(value) {
  1951. return (typeof value === 'object' &&
  1952. (isImmutable(value) || Array.isArray(value) || isPlainObject(value)));
  1953. }
  1954. /**
  1955. * Converts a value to a string, adding quotes if a string was provided.
  1956. */
  1957. function quoteString(value) {
  1958. try {
  1959. return typeof value === 'string' ? JSON.stringify(value) : String(value);
  1960. // eslint-disable-next-line @typescript-eslint/no-unused-vars
  1961. }
  1962. catch (_ignoreError) {
  1963. return JSON.stringify(value);
  1964. }
  1965. }
  1966. /**
  1967. * Returns true if the key is defined in the provided collection.
  1968. *
  1969. * A functional alternative to `collection.has(key)` which will also work with
  1970. * plain Objects and Arrays as an alternative for
  1971. * `collection.hasOwnProperty(key)`.
  1972. *
  1973. * <!-- runkit:activate -->
  1974. * ```js
  1975. * import { has } from 'immutable';
  1976. *
  1977. * has([ 'dog', 'frog', 'cat' ], 2) // true
  1978. * has([ 'dog', 'frog', 'cat' ], 5) // false
  1979. * has({ x: 123, y: 456 }, 'x') // true
  1980. * has({ x: 123, y: 456 }, 'z') // false
  1981. * ```
  1982. */
  1983. function has(collection, key) {
  1984. return isImmutable(collection)
  1985. ? // @ts-expect-error key might be a number or symbol, which is not handled be Record key type
  1986. collection.has(key)
  1987. : // @ts-expect-error key might be anything else than PropertyKey, and will return false in that case but runtime is OK
  1988. isDataStructure(collection) && hasOwnProperty.call(collection, key);
  1989. }
  1990. function get(collection, key, notSetValue) {
  1991. return isImmutable(collection)
  1992. ? collection.get(key, notSetValue)
  1993. : !has(collection, key)
  1994. ? notSetValue
  1995. : // @ts-expect-error weird "get" here,
  1996. typeof collection.get === 'function'
  1997. ? // @ts-expect-error weird "get" here,
  1998. collection.get(key)
  1999. : // @ts-expect-error key is unknown here,
  2000. collection[key];
  2001. }
  2002. function shallowCopy(from) {
  2003. if (Array.isArray(from)) {
  2004. return arrCopy(from);
  2005. }
  2006. var to = {};
  2007. for (var key in from) {
  2008. if (hasOwnProperty.call(from, key)) {
  2009. to[key] = from[key];
  2010. }
  2011. }
  2012. return to;
  2013. }
  2014. function remove(collection, key) {
  2015. if (!isDataStructure(collection)) {
  2016. throw new TypeError('Cannot update non-data-structure value: ' + collection);
  2017. }
  2018. if (isImmutable(collection)) {
  2019. // @ts-expect-error weird "remove" here,
  2020. if (!collection.remove) {
  2021. throw new TypeError('Cannot update immutable value without .remove() method: ' + collection);
  2022. }
  2023. // @ts-expect-error weird "remove" here,
  2024. return collection.remove(key);
  2025. }
  2026. if (!hasOwnProperty.call(collection, key)) {
  2027. return collection;
  2028. }
  2029. var collectionCopy = shallowCopy(collection);
  2030. if (Array.isArray(collectionCopy)) {
  2031. // @ts-expect-error assert that key is a number here
  2032. collectionCopy.splice(key, 1);
  2033. }
  2034. else {
  2035. delete collectionCopy[key];
  2036. }
  2037. return collectionCopy;
  2038. }
  2039. function set(collection, key, value) {
  2040. if (!isDataStructure(collection)) {
  2041. throw new TypeError('Cannot update non-data-structure value: ' + collection);
  2042. }
  2043. if (isImmutable(collection)) {
  2044. // @ts-expect-error weird "set" here,
  2045. if (!collection.set) {
  2046. throw new TypeError('Cannot update immutable value without .set() method: ' + collection);
  2047. }
  2048. // @ts-expect-error weird "set" here,
  2049. return collection.set(key, value);
  2050. }
  2051. // @ts-expect-error mix of key and string here. Probably need a more fine type here
  2052. if (hasOwnProperty.call(collection, key) && value === collection[key]) {
  2053. return collection;
  2054. }
  2055. var collectionCopy = shallowCopy(collection);
  2056. // @ts-expect-error mix of key and string here. Probably need a more fine type here
  2057. collectionCopy[key] = value;
  2058. return collectionCopy;
  2059. }
  2060. function updateIn$1(collection, keyPath, notSetValue, updater) {
  2061. if (!updater) {
  2062. // handle the fact that `notSetValue` is optional here, in that case `updater` is the updater function
  2063. // @ts-expect-error updater is a function here
  2064. updater = notSetValue;
  2065. notSetValue = undefined;
  2066. }
  2067. var updatedValue = updateInDeeply(isImmutable(collection),
  2068. // @ts-expect-error type issues with Record and mixed types
  2069. collection, coerceKeyPath(keyPath), 0, notSetValue, updater);
  2070. // @ts-expect-error mixed return type
  2071. return updatedValue === NOT_SET ? notSetValue : updatedValue;
  2072. }
  2073. function updateInDeeply(inImmutable, existing, keyPath, i, notSetValue, updater) {
  2074. var wasNotSet = existing === NOT_SET;
  2075. if (i === keyPath.length) {
  2076. var existingValue = wasNotSet ? notSetValue : existing;
  2077. // @ts-expect-error mixed type with optional value
  2078. var newValue = updater(existingValue);
  2079. // @ts-expect-error mixed type
  2080. return newValue === existingValue ? existing : newValue;
  2081. }
  2082. if (!wasNotSet && !isDataStructure(existing)) {
  2083. throw new TypeError('Cannot update within non-data-structure value in path [' +
  2084. Array.from(keyPath).slice(0, i).map(quoteString) +
  2085. ']: ' +
  2086. existing);
  2087. }
  2088. var key = keyPath[i];
  2089. if (typeof key === 'undefined') {
  2090. throw new TypeError('Index can not be undefined in updateIn(). This should not happen');
  2091. }
  2092. var nextExisting = wasNotSet ? NOT_SET : get(existing, key, NOT_SET);
  2093. var nextUpdated = updateInDeeply(nextExisting === NOT_SET ? inImmutable : isImmutable(nextExisting),
  2094. // @ts-expect-error mixed type
  2095. nextExisting, keyPath, i + 1, notSetValue, updater);
  2096. return nextUpdated === nextExisting
  2097. ? existing
  2098. : nextUpdated === NOT_SET
  2099. ? remove(existing, key)
  2100. : set(wasNotSet ? (inImmutable ? emptyMap() : {}) : existing, key, nextUpdated);
  2101. }
  2102. /**
  2103. * Returns a copy of the collection with the value at the key path set to the
  2104. * provided value.
  2105. *
  2106. * A functional alternative to `collection.setIn(keypath)` which will also
  2107. * work with plain Objects and Arrays.
  2108. *
  2109. * <!-- runkit:activate -->
  2110. * ```js
  2111. * import { setIn } from 'immutable';
  2112. *
  2113. * const original = { x: { y: { z: 123 }}}
  2114. * setIn(original, ['x', 'y', 'z'], 456) // { x: { y: { z: 456 }}}
  2115. * console.log(original) // { x: { y: { z: 123 }}}
  2116. * ```
  2117. */
  2118. function setIn$1(collection, keyPath, value) {
  2119. return updateIn$1(collection, keyPath, NOT_SET, function () { return value; });
  2120. }
  2121. function setIn(keyPath, v) {
  2122. return setIn$1(this, keyPath, v);
  2123. }
  2124. /**
  2125. * Returns a copy of the collection with the value at the key path removed.
  2126. *
  2127. * A functional alternative to `collection.removeIn(keypath)` which will also
  2128. * work with plain Objects and Arrays.
  2129. *
  2130. * <!-- runkit:activate -->
  2131. * ```js
  2132. * import { removeIn } from 'immutable';
  2133. *
  2134. * const original = { x: { y: { z: 123 }}}
  2135. * removeIn(original, ['x', 'y', 'z']) // { x: { y: {}}}
  2136. * console.log(original) // { x: { y: { z: 123 }}}
  2137. * ```
  2138. */
  2139. function removeIn(collection, keyPath) {
  2140. return updateIn$1(collection, keyPath, function () { return NOT_SET; });
  2141. }
  2142. function deleteIn(keyPath) {
  2143. return removeIn(this, keyPath);
  2144. }
  2145. function update$1(collection, key, notSetValue, updater) {
  2146. return updateIn$1(
  2147. // @ts-expect-error Index signature for type string is missing in type V[]
  2148. collection, [key], notSetValue, updater);
  2149. }
  2150. function update(key, notSetValue, updater) {
  2151. return arguments.length === 1
  2152. ? key(this)
  2153. : update$1(this, key, notSetValue, updater);
  2154. }
  2155. function updateIn(keyPath, notSetValue, updater) {
  2156. return updateIn$1(this, keyPath, notSetValue, updater);
  2157. }
  2158. function merge$1() {
  2159. var iters = [], len = arguments.length;
  2160. while ( len-- ) iters[ len ] = arguments[ len ];
  2161. return mergeIntoKeyedWith(this, iters);
  2162. }
  2163. function mergeWith$1(merger) {
  2164. var iters = [], len = arguments.length - 1;
  2165. while ( len-- > 0 ) iters[ len ] = arguments[ len + 1 ];
  2166. if (typeof merger !== 'function') {
  2167. throw new TypeError('Invalid merger function: ' + merger);
  2168. }
  2169. return mergeIntoKeyedWith(this, iters, merger);
  2170. }
  2171. function mergeIntoKeyedWith(collection, collections, merger) {
  2172. var iters = [];
  2173. for (var ii = 0; ii < collections.length; ii++) {
  2174. var collection$1 = KeyedCollection(collections[ii]);
  2175. if (collection$1.size !== 0) {
  2176. iters.push(collection$1);
  2177. }
  2178. }
  2179. if (iters.length === 0) {
  2180. return collection;
  2181. }
  2182. if (
  2183. collection.toSeq().size === 0 &&
  2184. !collection.__ownerID &&
  2185. iters.length === 1
  2186. ) {
  2187. return collection.constructor(iters[0]);
  2188. }
  2189. return collection.withMutations(function (collection) {
  2190. var mergeIntoCollection = merger
  2191. ? function (value, key) {
  2192. update$1(collection, key, NOT_SET, function (oldVal) { return oldVal === NOT_SET ? value : merger(oldVal, value, key); }
  2193. );
  2194. }
  2195. : function (value, key) {
  2196. collection.set(key, value);
  2197. };
  2198. for (var ii = 0; ii < iters.length; ii++) {
  2199. iters[ii].forEach(mergeIntoCollection);
  2200. }
  2201. });
  2202. }
  2203. function merge(collection) {
  2204. var sources = [], len = arguments.length - 1;
  2205. while ( len-- > 0 ) sources[ len ] = arguments[ len + 1 ];
  2206. return mergeWithSources(collection, sources);
  2207. }
  2208. function mergeWith(merger, collection) {
  2209. var sources = [], len = arguments.length - 2;
  2210. while ( len-- > 0 ) sources[ len ] = arguments[ len + 2 ];
  2211. return mergeWithSources(collection, sources, merger);
  2212. }
  2213. function mergeDeep$1(collection) {
  2214. var sources = [], len = arguments.length - 1;
  2215. while ( len-- > 0 ) sources[ len ] = arguments[ len + 1 ];
  2216. return mergeDeepWithSources(collection, sources);
  2217. }
  2218. function mergeDeepWith$1(merger, collection) {
  2219. var sources = [], len = arguments.length - 2;
  2220. while ( len-- > 0 ) sources[ len ] = arguments[ len + 2 ];
  2221. return mergeDeepWithSources(collection, sources, merger);
  2222. }
  2223. function mergeDeepWithSources(collection, sources, merger) {
  2224. return mergeWithSources(collection, sources, deepMergerWith(merger));
  2225. }
  2226. function mergeWithSources(collection, sources, merger) {
  2227. if (!isDataStructure(collection)) {
  2228. throw new TypeError(
  2229. 'Cannot merge into non-data-structure value: ' + collection
  2230. );
  2231. }
  2232. if (isImmutable(collection)) {
  2233. return typeof merger === 'function' && collection.mergeWith
  2234. ? collection.mergeWith.apply(collection, [ merger ].concat( sources ))
  2235. : collection.merge
  2236. ? collection.merge.apply(collection, sources)
  2237. : collection.concat.apply(collection, sources);
  2238. }
  2239. var isArray = Array.isArray(collection);
  2240. var merged = collection;
  2241. var Collection = isArray ? IndexedCollection : KeyedCollection;
  2242. var mergeItem = isArray
  2243. ? function (value) {
  2244. // Copy on write
  2245. if (merged === collection) {
  2246. merged = shallowCopy(merged);
  2247. }
  2248. merged.push(value);
  2249. }
  2250. : function (value, key) {
  2251. var hasVal = hasOwnProperty.call(merged, key);
  2252. var nextVal =
  2253. hasVal && merger ? merger(merged[key], value, key) : value;
  2254. if (!hasVal || nextVal !== merged[key]) {
  2255. // Copy on write
  2256. if (merged === collection) {
  2257. merged = shallowCopy(merged);
  2258. }
  2259. merged[key] = nextVal;
  2260. }
  2261. };
  2262. for (var i = 0; i < sources.length; i++) {
  2263. Collection(sources[i]).forEach(mergeItem);
  2264. }
  2265. return merged;
  2266. }
  2267. function deepMergerWith(merger) {
  2268. function deepMerger(oldValue, newValue, key) {
  2269. return isDataStructure(oldValue) &&
  2270. isDataStructure(newValue) &&
  2271. areMergeable(oldValue, newValue)
  2272. ? mergeWithSources(oldValue, [newValue], deepMerger)
  2273. : merger
  2274. ? merger(oldValue, newValue, key)
  2275. : newValue;
  2276. }
  2277. return deepMerger;
  2278. }
  2279. /**
  2280. * It's unclear what the desired behavior is for merging two collections that
  2281. * fall into separate categories between keyed, indexed, or set-like, so we only
  2282. * consider them mergeable if they fall into the same category.
  2283. */
  2284. function areMergeable(oldDataStructure, newDataStructure) {
  2285. var oldSeq = Seq(oldDataStructure);
  2286. var newSeq = Seq(newDataStructure);
  2287. // This logic assumes that a sequence can only fall into one of the three
  2288. // categories mentioned above (since there's no `isSetLike()` method).
  2289. return (
  2290. isIndexed(oldSeq) === isIndexed(newSeq) &&
  2291. isKeyed(oldSeq) === isKeyed(newSeq)
  2292. );
  2293. }
  2294. function mergeDeep() {
  2295. var iters = [], len = arguments.length;
  2296. while ( len-- ) iters[ len ] = arguments[ len ];
  2297. return mergeDeepWithSources(this, iters);
  2298. }
  2299. function mergeDeepWith(merger) {
  2300. var iters = [], len = arguments.length - 1;
  2301. while ( len-- > 0 ) iters[ len ] = arguments[ len + 1 ];
  2302. return mergeDeepWithSources(this, iters, merger);
  2303. }
  2304. function mergeIn(keyPath) {
  2305. var iters = [], len = arguments.length - 1;
  2306. while ( len-- > 0 ) iters[ len ] = arguments[ len + 1 ];
  2307. return updateIn$1(this, keyPath, emptyMap(), function (m) { return mergeWithSources(m, iters); });
  2308. }
  2309. function mergeDeepIn(keyPath) {
  2310. var iters = [], len = arguments.length - 1;
  2311. while ( len-- > 0 ) iters[ len ] = arguments[ len + 1 ];
  2312. return updateIn$1(this, keyPath, emptyMap(), function (m) { return mergeDeepWithSources(m, iters); }
  2313. );
  2314. }
  2315. function withMutations(fn) {
  2316. var mutable = this.asMutable();
  2317. fn(mutable);
  2318. return mutable.wasAltered() ? mutable.__ensureOwner(this.__ownerID) : this;
  2319. }
  2320. function asMutable() {
  2321. return this.__ownerID ? this : this.__ensureOwner(new OwnerID());
  2322. }
  2323. function asImmutable() {
  2324. return this.__ensureOwner();
  2325. }
  2326. function wasAltered() {
  2327. return this.__altered;
  2328. }
  2329. var Map = /*@__PURE__*/(function (KeyedCollection) {
  2330. function Map(value) {
  2331. // eslint-disable-next-line no-constructor-return
  2332. return value === undefined || value === null
  2333. ? emptyMap()
  2334. : isMap(value) && !isOrdered(value)
  2335. ? value
  2336. : emptyMap().withMutations(function (map) {
  2337. var iter = KeyedCollection(value);
  2338. assertNotInfinite(iter.size);
  2339. iter.forEach(function (v, k) { return map.set(k, v); });
  2340. });
  2341. }
  2342. if ( KeyedCollection ) Map.__proto__ = KeyedCollection;
  2343. Map.prototype = Object.create( KeyedCollection && KeyedCollection.prototype );
  2344. Map.prototype.constructor = Map;
  2345. Map.prototype.toString = function toString () {
  2346. return this.__toString('Map {', '}');
  2347. };
  2348. // @pragma Access
  2349. Map.prototype.get = function get (k, notSetValue) {
  2350. return this._root
  2351. ? this._root.get(0, undefined, k, notSetValue)
  2352. : notSetValue;
  2353. };
  2354. // @pragma Modification
  2355. Map.prototype.set = function set (k, v) {
  2356. return updateMap(this, k, v);
  2357. };
  2358. Map.prototype.remove = function remove (k) {
  2359. return updateMap(this, k, NOT_SET);
  2360. };
  2361. Map.prototype.deleteAll = function deleteAll (keys) {
  2362. var collection = Collection(keys);
  2363. if (collection.size === 0) {
  2364. return this;
  2365. }
  2366. return this.withMutations(function (map) {
  2367. collection.forEach(function (key) { return map.remove(key); });
  2368. });
  2369. };
  2370. Map.prototype.clear = function clear () {
  2371. if (this.size === 0) {
  2372. return this;
  2373. }
  2374. if (this.__ownerID) {
  2375. this.size = 0;
  2376. this._root = null;
  2377. this.__hash = undefined;
  2378. this.__altered = true;
  2379. return this;
  2380. }
  2381. return emptyMap();
  2382. };
  2383. // @pragma Composition
  2384. Map.prototype.sort = function sort (comparator) {
  2385. // Late binding
  2386. return OrderedMap(sortFactory(this, comparator));
  2387. };
  2388. Map.prototype.sortBy = function sortBy (mapper, comparator) {
  2389. // Late binding
  2390. return OrderedMap(sortFactory(this, comparator, mapper));
  2391. };
  2392. Map.prototype.map = function map (mapper, context) {
  2393. var this$1$1 = this;
  2394. return this.withMutations(function (map) {
  2395. map.forEach(function (value, key) {
  2396. map.set(key, mapper.call(context, value, key, this$1$1));
  2397. });
  2398. });
  2399. };
  2400. // @pragma Mutability
  2401. Map.prototype.__iterator = function __iterator (type, reverse) {
  2402. return new MapIterator(this, type, reverse);
  2403. };
  2404. Map.prototype.__iterate = function __iterate (fn, reverse) {
  2405. var this$1$1 = this;
  2406. var iterations = 0;
  2407. // eslint-disable-next-line @typescript-eslint/no-unused-expressions -- TODO enable eslint here
  2408. this._root &&
  2409. this._root.iterate(function (entry) {
  2410. iterations++;
  2411. return fn(entry[1], entry[0], this$1$1);
  2412. }, reverse);
  2413. return iterations;
  2414. };
  2415. Map.prototype.__ensureOwner = function __ensureOwner (ownerID) {
  2416. if (ownerID === this.__ownerID) {
  2417. return this;
  2418. }
  2419. if (!ownerID) {
  2420. if (this.size === 0) {
  2421. return emptyMap();
  2422. }
  2423. this.__ownerID = ownerID;
  2424. this.__altered = false;
  2425. return this;
  2426. }
  2427. return makeMap(this.size, this._root, ownerID, this.__hash);
  2428. };
  2429. return Map;
  2430. }(KeyedCollection));
  2431. Map.isMap = isMap;
  2432. var MapPrototype = Map.prototype;
  2433. MapPrototype[IS_MAP_SYMBOL] = true;
  2434. MapPrototype[DELETE] = MapPrototype.remove;
  2435. MapPrototype.removeAll = MapPrototype.deleteAll;
  2436. MapPrototype.setIn = setIn;
  2437. MapPrototype.removeIn = MapPrototype.deleteIn = deleteIn;
  2438. MapPrototype.update = update;
  2439. MapPrototype.updateIn = updateIn;
  2440. MapPrototype.merge = MapPrototype.concat = merge$1;
  2441. MapPrototype.mergeWith = mergeWith$1;
  2442. MapPrototype.mergeDeep = mergeDeep;
  2443. MapPrototype.mergeDeepWith = mergeDeepWith;
  2444. MapPrototype.mergeIn = mergeIn;
  2445. MapPrototype.mergeDeepIn = mergeDeepIn;
  2446. MapPrototype.withMutations = withMutations;
  2447. MapPrototype.wasAltered = wasAltered;
  2448. MapPrototype.asImmutable = asImmutable;
  2449. MapPrototype['@@transducer/init'] = MapPrototype.asMutable = asMutable;
  2450. MapPrototype['@@transducer/step'] = function (result, arr) {
  2451. return result.set(arr[0], arr[1]);
  2452. };
  2453. MapPrototype['@@transducer/result'] = function (obj) {
  2454. return obj.asImmutable();
  2455. };
  2456. // #pragma Trie Nodes
  2457. var ArrayMapNode = function ArrayMapNode(ownerID, entries) {
  2458. this.ownerID = ownerID;
  2459. this.entries = entries;
  2460. };
  2461. ArrayMapNode.prototype.get = function get (shift, keyHash, key, notSetValue) {
  2462. var entries = this.entries;
  2463. for (var ii = 0, len = entries.length; ii < len; ii++) {
  2464. if (is(key, entries[ii][0])) {
  2465. return entries[ii][1];
  2466. }
  2467. }
  2468. return notSetValue;
  2469. };
  2470. ArrayMapNode.prototype.update = function update (ownerID, shift, keyHash, key, value, didChangeSize, didAlter) {
  2471. var removed = value === NOT_SET;
  2472. var entries = this.entries;
  2473. var idx = 0;
  2474. var len = entries.length;
  2475. for (; idx < len; idx++) {
  2476. if (is(key, entries[idx][0])) {
  2477. break;
  2478. }
  2479. }
  2480. var exists = idx < len;
  2481. if (exists ? entries[idx][1] === value : removed) {
  2482. return this;
  2483. }
  2484. SetRef(didAlter);
  2485. // eslint-disable-next-line @typescript-eslint/no-unused-expressions -- TODO enable eslint here
  2486. (removed || !exists) && SetRef(didChangeSize);
  2487. if (removed && entries.length === 1) {
  2488. return; // undefined
  2489. }
  2490. if (!exists && !removed && entries.length >= MAX_ARRAY_MAP_SIZE) {
  2491. return createNodes(ownerID, entries, key, value);
  2492. }
  2493. var isEditable = ownerID && ownerID === this.ownerID;
  2494. var newEntries = isEditable ? entries : arrCopy(entries);
  2495. if (exists) {
  2496. if (removed) {
  2497. // eslint-disable-next-line @typescript-eslint/no-unused-expressions -- TODO enable eslint here
  2498. idx === len - 1
  2499. ? newEntries.pop()
  2500. : (newEntries[idx] = newEntries.pop());
  2501. } else {
  2502. newEntries[idx] = [key, value];
  2503. }
  2504. } else {
  2505. newEntries.push([key, value]);
  2506. }
  2507. if (isEditable) {
  2508. this.entries = newEntries;
  2509. return this;
  2510. }
  2511. return new ArrayMapNode(ownerID, newEntries);
  2512. };
  2513. var BitmapIndexedNode = function BitmapIndexedNode(ownerID, bitmap, nodes) {
  2514. this.ownerID = ownerID;
  2515. this.bitmap = bitmap;
  2516. this.nodes = nodes;
  2517. };
  2518. BitmapIndexedNode.prototype.get = function get (shift, keyHash, key, notSetValue) {
  2519. if (keyHash === undefined) {
  2520. keyHash = hash(key);
  2521. }
  2522. var bit = 1 << ((shift === 0 ? keyHash : keyHash >>> shift) & MASK);
  2523. var bitmap = this.bitmap;
  2524. return (bitmap & bit) === 0
  2525. ? notSetValue
  2526. : this.nodes[popCount(bitmap & (bit - 1))].get(
  2527. shift + SHIFT,
  2528. keyHash,
  2529. key,
  2530. notSetValue
  2531. );
  2532. };
  2533. BitmapIndexedNode.prototype.update = function update (ownerID, shift, keyHash, key, value, didChangeSize, didAlter) {
  2534. if (keyHash === undefined) {
  2535. keyHash = hash(key);
  2536. }
  2537. var keyHashFrag = (shift === 0 ? keyHash : keyHash >>> shift) & MASK;
  2538. var bit = 1 << keyHashFrag;
  2539. var bitmap = this.bitmap;
  2540. var exists = (bitmap & bit) !== 0;
  2541. if (!exists && value === NOT_SET) {
  2542. return this;
  2543. }
  2544. var idx = popCount(bitmap & (bit - 1));
  2545. var nodes = this.nodes;
  2546. var node = exists ? nodes[idx] : undefined;
  2547. var newNode = updateNode(
  2548. node,
  2549. ownerID,
  2550. shift + SHIFT,
  2551. keyHash,
  2552. key,
  2553. value,
  2554. didChangeSize,
  2555. didAlter
  2556. );
  2557. if (newNode === node) {
  2558. return this;
  2559. }
  2560. if (!exists && newNode && nodes.length >= MAX_BITMAP_INDEXED_SIZE) {
  2561. return expandNodes(ownerID, nodes, bitmap, keyHashFrag, newNode);
  2562. }
  2563. if (
  2564. exists &&
  2565. !newNode &&
  2566. nodes.length === 2 &&
  2567. isLeafNode(nodes[idx ^ 1])
  2568. ) {
  2569. return nodes[idx ^ 1];
  2570. }
  2571. if (exists && newNode && nodes.length === 1 && isLeafNode(newNode)) {
  2572. return newNode;
  2573. }
  2574. var isEditable = ownerID && ownerID === this.ownerID;
  2575. var newBitmap = exists ? (newNode ? bitmap : bitmap ^ bit) : bitmap | bit;
  2576. var newNodes = exists
  2577. ? newNode
  2578. ? setAt(nodes, idx, newNode, isEditable)
  2579. : spliceOut(nodes, idx, isEditable)
  2580. : spliceIn(nodes, idx, newNode, isEditable);
  2581. if (isEditable) {
  2582. this.bitmap = newBitmap;
  2583. this.nodes = newNodes;
  2584. return this;
  2585. }
  2586. return new BitmapIndexedNode(ownerID, newBitmap, newNodes);
  2587. };
  2588. var HashArrayMapNode = function HashArrayMapNode(ownerID, count, nodes) {
  2589. this.ownerID = ownerID;
  2590. this.count = count;
  2591. this.nodes = nodes;
  2592. };
  2593. HashArrayMapNode.prototype.get = function get (shift, keyHash, key, notSetValue) {
  2594. if (keyHash === undefined) {
  2595. keyHash = hash(key);
  2596. }
  2597. var idx = (shift === 0 ? keyHash : keyHash >>> shift) & MASK;
  2598. var node = this.nodes[idx];
  2599. return node
  2600. ? node.get(shift + SHIFT, keyHash, key, notSetValue)
  2601. : notSetValue;
  2602. };
  2603. HashArrayMapNode.prototype.update = function update (ownerID, shift, keyHash, key, value, didChangeSize, didAlter) {
  2604. if (keyHash === undefined) {
  2605. keyHash = hash(key);
  2606. }
  2607. var idx = (shift === 0 ? keyHash : keyHash >>> shift) & MASK;
  2608. var removed = value === NOT_SET;
  2609. var nodes = this.nodes;
  2610. var node = nodes[idx];
  2611. if (removed && !node) {
  2612. return this;
  2613. }
  2614. var newNode = updateNode(
  2615. node,
  2616. ownerID,
  2617. shift + SHIFT,
  2618. keyHash,
  2619. key,
  2620. value,
  2621. didChangeSize,
  2622. didAlter
  2623. );
  2624. if (newNode === node) {
  2625. return this;
  2626. }
  2627. var newCount = this.count;
  2628. if (!node) {
  2629. newCount++;
  2630. } else if (!newNode) {
  2631. newCount--;
  2632. if (newCount < MIN_HASH_ARRAY_MAP_SIZE) {
  2633. return packNodes(ownerID, nodes, newCount, idx);
  2634. }
  2635. }
  2636. var isEditable = ownerID && ownerID === this.ownerID;
  2637. var newNodes = setAt(nodes, idx, newNode, isEditable);
  2638. if (isEditable) {
  2639. this.count = newCount;
  2640. this.nodes = newNodes;
  2641. return this;
  2642. }
  2643. return new HashArrayMapNode(ownerID, newCount, newNodes);
  2644. };
  2645. var HashCollisionNode = function HashCollisionNode(ownerID, keyHash, entries) {
  2646. this.ownerID = ownerID;
  2647. this.keyHash = keyHash;
  2648. this.entries = entries;
  2649. };
  2650. HashCollisionNode.prototype.get = function get (shift, keyHash, key, notSetValue) {
  2651. var entries = this.entries;
  2652. for (var ii = 0, len = entries.length; ii < len; ii++) {
  2653. if (is(key, entries[ii][0])) {
  2654. return entries[ii][1];
  2655. }
  2656. }
  2657. return notSetValue;
  2658. };
  2659. HashCollisionNode.prototype.update = function update (ownerID, shift, keyHash, key, value, didChangeSize, didAlter) {
  2660. if (keyHash === undefined) {
  2661. keyHash = hash(key);
  2662. }
  2663. var removed = value === NOT_SET;
  2664. if (keyHash !== this.keyHash) {
  2665. if (removed) {
  2666. return this;
  2667. }
  2668. SetRef(didAlter);
  2669. SetRef(didChangeSize);
  2670. return mergeIntoNode(this, ownerID, shift, keyHash, [key, value]);
  2671. }
  2672. var entries = this.entries;
  2673. var idx = 0;
  2674. var len = entries.length;
  2675. for (; idx < len; idx++) {
  2676. if (is(key, entries[idx][0])) {
  2677. break;
  2678. }
  2679. }
  2680. var exists = idx < len;
  2681. if (exists ? entries[idx][1] === value : removed) {
  2682. return this;
  2683. }
  2684. SetRef(didAlter);
  2685. // eslint-disable-next-line @typescript-eslint/no-unused-expressions -- TODO enable eslint here
  2686. (removed || !exists) && SetRef(didChangeSize);
  2687. if (removed && len === 2) {
  2688. return new ValueNode(ownerID, this.keyHash, entries[idx ^ 1]);
  2689. }
  2690. var isEditable = ownerID && ownerID === this.ownerID;
  2691. var newEntries = isEditable ? entries : arrCopy(entries);
  2692. if (exists) {
  2693. if (removed) {
  2694. // eslint-disable-next-line @typescript-eslint/no-unused-expressions -- TODO enable eslint here
  2695. idx === len - 1
  2696. ? newEntries.pop()
  2697. : (newEntries[idx] = newEntries.pop());
  2698. } else {
  2699. newEntries[idx] = [key, value];
  2700. }
  2701. } else {
  2702. newEntries.push([key, value]);
  2703. }
  2704. if (isEditable) {
  2705. this.entries = newEntries;
  2706. return this;
  2707. }
  2708. return new HashCollisionNode(ownerID, this.keyHash, newEntries);
  2709. };
  2710. var ValueNode = function ValueNode(ownerID, keyHash, entry) {
  2711. this.ownerID = ownerID;
  2712. this.keyHash = keyHash;
  2713. this.entry = entry;
  2714. };
  2715. ValueNode.prototype.get = function get (shift, keyHash, key, notSetValue) {
  2716. return is(key, this.entry[0]) ? this.entry[1] : notSetValue;
  2717. };
  2718. ValueNode.prototype.update = function update (ownerID, shift, keyHash, key, value, didChangeSize, didAlter) {
  2719. var removed = value === NOT_SET;
  2720. var keyMatch = is(key, this.entry[0]);
  2721. if (keyMatch ? value === this.entry[1] : removed) {
  2722. return this;
  2723. }
  2724. SetRef(didAlter);
  2725. if (removed) {
  2726. SetRef(didChangeSize);
  2727. return; // undefined
  2728. }
  2729. if (keyMatch) {
  2730. if (ownerID && ownerID === this.ownerID) {
  2731. this.entry[1] = value;
  2732. return this;
  2733. }
  2734. return new ValueNode(ownerID, this.keyHash, [key, value]);
  2735. }
  2736. SetRef(didChangeSize);
  2737. return mergeIntoNode(this, ownerID, shift, hash(key), [key, value]);
  2738. };
  2739. // #pragma Iterators
  2740. ArrayMapNode.prototype.iterate = HashCollisionNode.prototype.iterate =
  2741. function (fn, reverse) {
  2742. var entries = this.entries;
  2743. for (var ii = 0, maxIndex = entries.length - 1; ii <= maxIndex; ii++) {
  2744. if (fn(entries[reverse ? maxIndex - ii : ii]) === false) {
  2745. return false;
  2746. }
  2747. }
  2748. };
  2749. BitmapIndexedNode.prototype.iterate = HashArrayMapNode.prototype.iterate =
  2750. function (fn, reverse) {
  2751. var nodes = this.nodes;
  2752. for (var ii = 0, maxIndex = nodes.length - 1; ii <= maxIndex; ii++) {
  2753. var node = nodes[reverse ? maxIndex - ii : ii];
  2754. if (node && node.iterate(fn, reverse) === false) {
  2755. return false;
  2756. }
  2757. }
  2758. };
  2759. // eslint-disable-next-line @typescript-eslint/no-unused-vars
  2760. ValueNode.prototype.iterate = function (fn, reverse) {
  2761. return fn(this.entry);
  2762. };
  2763. var MapIterator = /*@__PURE__*/(function (Iterator) {
  2764. function MapIterator(map, type, reverse) {
  2765. this._type = type;
  2766. this._reverse = reverse;
  2767. this._stack = map._root && mapIteratorFrame(map._root);
  2768. }
  2769. if ( Iterator ) MapIterator.__proto__ = Iterator;
  2770. MapIterator.prototype = Object.create( Iterator && Iterator.prototype );
  2771. MapIterator.prototype.constructor = MapIterator;
  2772. MapIterator.prototype.next = function next () {
  2773. var type = this._type;
  2774. var stack = this._stack;
  2775. while (stack) {
  2776. var node = stack.node;
  2777. var index = stack.index++;
  2778. var maxIndex = (void 0);
  2779. if (node.entry) {
  2780. if (index === 0) {
  2781. return mapIteratorValue(type, node.entry);
  2782. }
  2783. } else if (node.entries) {
  2784. maxIndex = node.entries.length - 1;
  2785. if (index <= maxIndex) {
  2786. return mapIteratorValue(
  2787. type,
  2788. node.entries[this._reverse ? maxIndex - index : index]
  2789. );
  2790. }
  2791. } else {
  2792. maxIndex = node.nodes.length - 1;
  2793. if (index <= maxIndex) {
  2794. var subNode = node.nodes[this._reverse ? maxIndex - index : index];
  2795. if (subNode) {
  2796. if (subNode.entry) {
  2797. return mapIteratorValue(type, subNode.entry);
  2798. }
  2799. stack = this._stack = mapIteratorFrame(subNode, stack);
  2800. }
  2801. continue;
  2802. }
  2803. }
  2804. stack = this._stack = this._stack.__prev;
  2805. }
  2806. return iteratorDone();
  2807. };
  2808. return MapIterator;
  2809. }(Iterator));
  2810. function mapIteratorValue(type, entry) {
  2811. return iteratorValue(type, entry[0], entry[1]);
  2812. }
  2813. function mapIteratorFrame(node, prev) {
  2814. return {
  2815. node: node,
  2816. index: 0,
  2817. __prev: prev,
  2818. };
  2819. }
  2820. function makeMap(size, root, ownerID, hash) {
  2821. var map = Object.create(MapPrototype);
  2822. map.size = size;
  2823. map._root = root;
  2824. map.__ownerID = ownerID;
  2825. map.__hash = hash;
  2826. map.__altered = false;
  2827. return map;
  2828. }
  2829. var EMPTY_MAP;
  2830. function emptyMap() {
  2831. return EMPTY_MAP || (EMPTY_MAP = makeMap(0));
  2832. }
  2833. function updateMap(map, k, v) {
  2834. var newRoot;
  2835. var newSize;
  2836. if (!map._root) {
  2837. if (v === NOT_SET) {
  2838. return map;
  2839. }
  2840. newSize = 1;
  2841. newRoot = new ArrayMapNode(map.__ownerID, [[k, v]]);
  2842. } else {
  2843. var didChangeSize = MakeRef();
  2844. var didAlter = MakeRef();
  2845. newRoot = updateNode(
  2846. map._root,
  2847. map.__ownerID,
  2848. 0,
  2849. undefined,
  2850. k,
  2851. v,
  2852. didChangeSize,
  2853. didAlter
  2854. );
  2855. if (!didAlter.value) {
  2856. return map;
  2857. }
  2858. newSize = map.size + (didChangeSize.value ? (v === NOT_SET ? -1 : 1) : 0);
  2859. }
  2860. if (map.__ownerID) {
  2861. map.size = newSize;
  2862. map._root = newRoot;
  2863. map.__hash = undefined;
  2864. map.__altered = true;
  2865. return map;
  2866. }
  2867. return newRoot ? makeMap(newSize, newRoot) : emptyMap();
  2868. }
  2869. function updateNode(
  2870. node,
  2871. ownerID,
  2872. shift,
  2873. keyHash,
  2874. key,
  2875. value,
  2876. didChangeSize,
  2877. didAlter
  2878. ) {
  2879. if (!node) {
  2880. if (value === NOT_SET) {
  2881. return node;
  2882. }
  2883. SetRef(didAlter);
  2884. SetRef(didChangeSize);
  2885. return new ValueNode(ownerID, keyHash, [key, value]);
  2886. }
  2887. return node.update(
  2888. ownerID,
  2889. shift,
  2890. keyHash,
  2891. key,
  2892. value,
  2893. didChangeSize,
  2894. didAlter
  2895. );
  2896. }
  2897. function isLeafNode(node) {
  2898. return (
  2899. node.constructor === ValueNode || node.constructor === HashCollisionNode
  2900. );
  2901. }
  2902. function mergeIntoNode(node, ownerID, shift, keyHash, entry) {
  2903. if (node.keyHash === keyHash) {
  2904. return new HashCollisionNode(ownerID, keyHash, [node.entry, entry]);
  2905. }
  2906. var idx1 = (shift === 0 ? node.keyHash : node.keyHash >>> shift) & MASK;
  2907. var idx2 = (shift === 0 ? keyHash : keyHash >>> shift) & MASK;
  2908. var newNode;
  2909. var nodes =
  2910. idx1 === idx2
  2911. ? [mergeIntoNode(node, ownerID, shift + SHIFT, keyHash, entry)]
  2912. : ((newNode = new ValueNode(ownerID, keyHash, entry)),
  2913. idx1 < idx2 ? [node, newNode] : [newNode, node]);
  2914. return new BitmapIndexedNode(ownerID, (1 << idx1) | (1 << idx2), nodes);
  2915. }
  2916. function createNodes(ownerID, entries, key, value) {
  2917. if (!ownerID) {
  2918. ownerID = new OwnerID();
  2919. }
  2920. var node = new ValueNode(ownerID, hash(key), [key, value]);
  2921. for (var ii = 0; ii < entries.length; ii++) {
  2922. var entry = entries[ii];
  2923. node = node.update(ownerID, 0, undefined, entry[0], entry[1]);
  2924. }
  2925. return node;
  2926. }
  2927. function packNodes(ownerID, nodes, count, excluding) {
  2928. var bitmap = 0;
  2929. var packedII = 0;
  2930. var packedNodes = new Array(count);
  2931. for (var ii = 0, bit = 1, len = nodes.length; ii < len; ii++, bit <<= 1) {
  2932. var node = nodes[ii];
  2933. if (node !== undefined && ii !== excluding) {
  2934. bitmap |= bit;
  2935. packedNodes[packedII++] = node;
  2936. }
  2937. }
  2938. return new BitmapIndexedNode(ownerID, bitmap, packedNodes);
  2939. }
  2940. function expandNodes(ownerID, nodes, bitmap, including, node) {
  2941. var count = 0;
  2942. var expandedNodes = new Array(SIZE);
  2943. for (var ii = 0; bitmap !== 0; ii++, bitmap >>>= 1) {
  2944. expandedNodes[ii] = bitmap & 1 ? nodes[count++] : undefined;
  2945. }
  2946. expandedNodes[including] = node;
  2947. return new HashArrayMapNode(ownerID, count + 1, expandedNodes);
  2948. }
  2949. function popCount(x) {
  2950. x -= (x >> 1) & 0x55555555;
  2951. x = (x & 0x33333333) + ((x >> 2) & 0x33333333);
  2952. x = (x + (x >> 4)) & 0x0f0f0f0f;
  2953. x += x >> 8;
  2954. x += x >> 16;
  2955. return x & 0x7f;
  2956. }
  2957. function setAt(array, idx, val, canEdit) {
  2958. var newArray = canEdit ? array : arrCopy(array);
  2959. newArray[idx] = val;
  2960. return newArray;
  2961. }
  2962. function spliceIn(array, idx, val, canEdit) {
  2963. var newLen = array.length + 1;
  2964. if (canEdit && idx + 1 === newLen) {
  2965. array[idx] = val;
  2966. return array;
  2967. }
  2968. var newArray = new Array(newLen);
  2969. var after = 0;
  2970. for (var ii = 0; ii < newLen; ii++) {
  2971. if (ii === idx) {
  2972. newArray[ii] = val;
  2973. after = -1;
  2974. } else {
  2975. newArray[ii] = array[ii + after];
  2976. }
  2977. }
  2978. return newArray;
  2979. }
  2980. function spliceOut(array, idx, canEdit) {
  2981. var newLen = array.length - 1;
  2982. if (canEdit && idx === newLen) {
  2983. array.pop();
  2984. return array;
  2985. }
  2986. var newArray = new Array(newLen);
  2987. var after = 0;
  2988. for (var ii = 0; ii < newLen; ii++) {
  2989. if (ii === idx) {
  2990. after = 1;
  2991. }
  2992. newArray[ii] = array[ii + after];
  2993. }
  2994. return newArray;
  2995. }
  2996. var MAX_ARRAY_MAP_SIZE = SIZE / 4;
  2997. var MAX_BITMAP_INDEXED_SIZE = SIZE / 2;
  2998. var MIN_HASH_ARRAY_MAP_SIZE = SIZE / 4;
  2999. var IS_LIST_SYMBOL = '@@__IMMUTABLE_LIST__@@';
  3000. /**
  3001. * True if `maybeList` is a List.
  3002. */
  3003. function isList(maybeList) {
  3004. return Boolean(maybeList &&
  3005. // @ts-expect-error: maybeList is typed as `{}`, need to change in 6.0 to `maybeList && typeof maybeList === 'object' && IS_LIST_SYMBOL in maybeList`
  3006. maybeList[IS_LIST_SYMBOL]);
  3007. }
  3008. var List = /*@__PURE__*/(function (IndexedCollection) {
  3009. function List(value) {
  3010. var empty = emptyList();
  3011. if (value === undefined || value === null) {
  3012. // eslint-disable-next-line no-constructor-return
  3013. return empty;
  3014. }
  3015. if (isList(value)) {
  3016. // eslint-disable-next-line no-constructor-return
  3017. return value;
  3018. }
  3019. var iter = IndexedCollection(value);
  3020. var size = iter.size;
  3021. if (size === 0) {
  3022. // eslint-disable-next-line no-constructor-return
  3023. return empty;
  3024. }
  3025. assertNotInfinite(size);
  3026. if (size > 0 && size < SIZE) {
  3027. // eslint-disable-next-line no-constructor-return
  3028. return makeList(0, size, SHIFT, null, new VNode(iter.toArray()));
  3029. }
  3030. // eslint-disable-next-line no-constructor-return
  3031. return empty.withMutations(function (list) {
  3032. list.setSize(size);
  3033. iter.forEach(function (v, i) { return list.set(i, v); });
  3034. });
  3035. }
  3036. if ( IndexedCollection ) List.__proto__ = IndexedCollection;
  3037. List.prototype = Object.create( IndexedCollection && IndexedCollection.prototype );
  3038. List.prototype.constructor = List;
  3039. List.of = function of (/*...values*/) {
  3040. return this(arguments);
  3041. };
  3042. List.prototype.toString = function toString () {
  3043. return this.__toString('List [', ']');
  3044. };
  3045. // @pragma Access
  3046. List.prototype.get = function get (index, notSetValue) {
  3047. index = wrapIndex(this, index);
  3048. if (index >= 0 && index < this.size) {
  3049. index += this._origin;
  3050. var node = listNodeFor(this, index);
  3051. return node && node.array[index & MASK];
  3052. }
  3053. return notSetValue;
  3054. };
  3055. // @pragma Modification
  3056. List.prototype.set = function set (index, value) {
  3057. return updateList(this, index, value);
  3058. };
  3059. List.prototype.remove = function remove (index) {
  3060. return !this.has(index)
  3061. ? this
  3062. : index === 0
  3063. ? this.shift()
  3064. : index === this.size - 1
  3065. ? this.pop()
  3066. : this.splice(index, 1);
  3067. };
  3068. List.prototype.insert = function insert (index, value) {
  3069. return this.splice(index, 0, value);
  3070. };
  3071. List.prototype.clear = function clear () {
  3072. if (this.size === 0) {
  3073. return this;
  3074. }
  3075. if (this.__ownerID) {
  3076. this.size = this._origin = this._capacity = 0;
  3077. this._level = SHIFT;
  3078. this._root = this._tail = this.__hash = undefined;
  3079. this.__altered = true;
  3080. return this;
  3081. }
  3082. return emptyList();
  3083. };
  3084. List.prototype.push = function push (/*...values*/) {
  3085. var values = arguments;
  3086. var oldSize = this.size;
  3087. return this.withMutations(function (list) {
  3088. setListBounds(list, 0, oldSize + values.length);
  3089. for (var ii = 0; ii < values.length; ii++) {
  3090. list.set(oldSize + ii, values[ii]);
  3091. }
  3092. });
  3093. };
  3094. List.prototype.pop = function pop () {
  3095. return setListBounds(this, 0, -1);
  3096. };
  3097. List.prototype.unshift = function unshift (/*...values*/) {
  3098. var values = arguments;
  3099. return this.withMutations(function (list) {
  3100. setListBounds(list, -values.length);
  3101. for (var ii = 0; ii < values.length; ii++) {
  3102. list.set(ii, values[ii]);
  3103. }
  3104. });
  3105. };
  3106. List.prototype.shift = function shift () {
  3107. return setListBounds(this, 1);
  3108. };
  3109. List.prototype.shuffle = function shuffle (random) {
  3110. if ( random === void 0 ) random = Math.random;
  3111. return this.withMutations(function (mutable) {
  3112. // implementation of the Fisher-Yates shuffle: https://en.wikipedia.org/wiki/Fisher%E2%80%93Yates_shuffle
  3113. var current = mutable.size;
  3114. var destination;
  3115. var tmp;
  3116. while (current) {
  3117. destination = Math.floor(random() * current--);
  3118. tmp = mutable.get(destination);
  3119. mutable.set(destination, mutable.get(current));
  3120. mutable.set(current, tmp);
  3121. }
  3122. });
  3123. };
  3124. // @pragma Composition
  3125. List.prototype.concat = function concat (/*...collections*/) {
  3126. var arguments$1 = arguments;
  3127. var seqs = [];
  3128. for (var i = 0; i < arguments.length; i++) {
  3129. var argument = arguments$1[i];
  3130. var seq = IndexedCollection(
  3131. typeof argument !== 'string' && hasIterator(argument)
  3132. ? argument
  3133. : [argument]
  3134. );
  3135. if (seq.size !== 0) {
  3136. seqs.push(seq);
  3137. }
  3138. }
  3139. if (seqs.length === 0) {
  3140. return this;
  3141. }
  3142. if (this.size === 0 && !this.__ownerID && seqs.length === 1) {
  3143. return this.constructor(seqs[0]);
  3144. }
  3145. return this.withMutations(function (list) {
  3146. seqs.forEach(function (seq) { return seq.forEach(function (value) { return list.push(value); }); });
  3147. });
  3148. };
  3149. List.prototype.setSize = function setSize (size) {
  3150. return setListBounds(this, 0, size);
  3151. };
  3152. List.prototype.map = function map (mapper, context) {
  3153. var this$1$1 = this;
  3154. return this.withMutations(function (list) {
  3155. for (var i = 0; i < this$1$1.size; i++) {
  3156. list.set(i, mapper.call(context, list.get(i), i, this$1$1));
  3157. }
  3158. });
  3159. };
  3160. // @pragma Iteration
  3161. List.prototype.slice = function slice (begin, end) {
  3162. var size = this.size;
  3163. if (wholeSlice(begin, end, size)) {
  3164. return this;
  3165. }
  3166. return setListBounds(
  3167. this,
  3168. resolveBegin(begin, size),
  3169. resolveEnd(end, size)
  3170. );
  3171. };
  3172. List.prototype.__iterator = function __iterator (type, reverse) {
  3173. var index = reverse ? this.size : 0;
  3174. var values = iterateList(this, reverse);
  3175. return new Iterator(function () {
  3176. var value = values();
  3177. return value === DONE
  3178. ? iteratorDone()
  3179. : iteratorValue(type, reverse ? --index : index++, value);
  3180. });
  3181. };
  3182. List.prototype.__iterate = function __iterate (fn, reverse) {
  3183. var index = reverse ? this.size : 0;
  3184. var values = iterateList(this, reverse);
  3185. var value;
  3186. while ((value = values()) !== DONE) {
  3187. if (fn(value, reverse ? --index : index++, this) === false) {
  3188. break;
  3189. }
  3190. }
  3191. return index;
  3192. };
  3193. List.prototype.__ensureOwner = function __ensureOwner (ownerID) {
  3194. if (ownerID === this.__ownerID) {
  3195. return this;
  3196. }
  3197. if (!ownerID) {
  3198. if (this.size === 0) {
  3199. return emptyList();
  3200. }
  3201. this.__ownerID = ownerID;
  3202. this.__altered = false;
  3203. return this;
  3204. }
  3205. return makeList(
  3206. this._origin,
  3207. this._capacity,
  3208. this._level,
  3209. this._root,
  3210. this._tail,
  3211. ownerID,
  3212. this.__hash
  3213. );
  3214. };
  3215. return List;
  3216. }(IndexedCollection));
  3217. List.isList = isList;
  3218. var ListPrototype = List.prototype;
  3219. ListPrototype[IS_LIST_SYMBOL] = true;
  3220. ListPrototype[DELETE] = ListPrototype.remove;
  3221. ListPrototype.merge = ListPrototype.concat;
  3222. ListPrototype.setIn = setIn;
  3223. ListPrototype.deleteIn = ListPrototype.removeIn = deleteIn;
  3224. ListPrototype.update = update;
  3225. ListPrototype.updateIn = updateIn;
  3226. ListPrototype.mergeIn = mergeIn;
  3227. ListPrototype.mergeDeepIn = mergeDeepIn;
  3228. ListPrototype.withMutations = withMutations;
  3229. ListPrototype.wasAltered = wasAltered;
  3230. ListPrototype.asImmutable = asImmutable;
  3231. ListPrototype['@@transducer/init'] = ListPrototype.asMutable = asMutable;
  3232. ListPrototype['@@transducer/step'] = function (result, arr) {
  3233. return result.push(arr);
  3234. };
  3235. ListPrototype['@@transducer/result'] = function (obj) {
  3236. return obj.asImmutable();
  3237. };
  3238. var VNode = function VNode(array, ownerID) {
  3239. this.array = array;
  3240. this.ownerID = ownerID;
  3241. };
  3242. // TODO: seems like these methods are very similar
  3243. VNode.prototype.removeBefore = function removeBefore (ownerID, level, index) {
  3244. if (
  3245. (index & ((1 << (level + SHIFT)) - 1)) === 0 ||
  3246. this.array.length === 0
  3247. ) {
  3248. return this;
  3249. }
  3250. var originIndex = (index >>> level) & MASK;
  3251. if (originIndex >= this.array.length) {
  3252. return new VNode([], ownerID);
  3253. }
  3254. var removingFirst = originIndex === 0;
  3255. var newChild;
  3256. if (level > 0) {
  3257. var oldChild = this.array[originIndex];
  3258. newChild =
  3259. oldChild && oldChild.removeBefore(ownerID, level - SHIFT, index);
  3260. if (newChild === oldChild && removingFirst) {
  3261. return this;
  3262. }
  3263. }
  3264. if (removingFirst && !newChild) {
  3265. return this;
  3266. }
  3267. var editable = editableVNode(this, ownerID);
  3268. if (!removingFirst) {
  3269. for (var ii = 0; ii < originIndex; ii++) {
  3270. editable.array[ii] = undefined;
  3271. }
  3272. }
  3273. if (newChild) {
  3274. editable.array[originIndex] = newChild;
  3275. }
  3276. return editable;
  3277. };
  3278. VNode.prototype.removeAfter = function removeAfter (ownerID, level, index) {
  3279. if (
  3280. index === (level ? 1 << (level + SHIFT) : SIZE) ||
  3281. this.array.length === 0
  3282. ) {
  3283. return this;
  3284. }
  3285. var sizeIndex = ((index - 1) >>> level) & MASK;
  3286. if (sizeIndex >= this.array.length) {
  3287. return this;
  3288. }
  3289. var newChild;
  3290. if (level > 0) {
  3291. var oldChild = this.array[sizeIndex];
  3292. newChild =
  3293. oldChild && oldChild.removeAfter(ownerID, level - SHIFT, index);
  3294. if (newChild === oldChild && sizeIndex === this.array.length - 1) {
  3295. return this;
  3296. }
  3297. }
  3298. var editable = editableVNode(this, ownerID);
  3299. editable.array.splice(sizeIndex + 1);
  3300. if (newChild) {
  3301. editable.array[sizeIndex] = newChild;
  3302. }
  3303. return editable;
  3304. };
  3305. var DONE = {};
  3306. function iterateList(list, reverse) {
  3307. var left = list._origin;
  3308. var right = list._capacity;
  3309. var tailPos = getTailOffset(right);
  3310. var tail = list._tail;
  3311. return iterateNodeOrLeaf(list._root, list._level, 0);
  3312. function iterateNodeOrLeaf(node, level, offset) {
  3313. return level === 0
  3314. ? iterateLeaf(node, offset)
  3315. : iterateNode(node, level, offset);
  3316. }
  3317. function iterateLeaf(node, offset) {
  3318. var array = offset === tailPos ? tail && tail.array : node && node.array;
  3319. var from = offset > left ? 0 : left - offset;
  3320. var to = right - offset;
  3321. if (to > SIZE) {
  3322. to = SIZE;
  3323. }
  3324. return function () {
  3325. if (from === to) {
  3326. return DONE;
  3327. }
  3328. var idx = reverse ? --to : from++;
  3329. return array && array[idx];
  3330. };
  3331. }
  3332. function iterateNode(node, level, offset) {
  3333. var values;
  3334. var array = node && node.array;
  3335. var from = offset > left ? 0 : (left - offset) >> level;
  3336. var to = ((right - offset) >> level) + 1;
  3337. if (to > SIZE) {
  3338. to = SIZE;
  3339. }
  3340. return function () {
  3341. while (true) {
  3342. if (values) {
  3343. var value = values();
  3344. if (value !== DONE) {
  3345. return value;
  3346. }
  3347. values = null;
  3348. }
  3349. if (from === to) {
  3350. return DONE;
  3351. }
  3352. var idx = reverse ? --to : from++;
  3353. values = iterateNodeOrLeaf(
  3354. array && array[idx],
  3355. level - SHIFT,
  3356. offset + (idx << level)
  3357. );
  3358. }
  3359. };
  3360. }
  3361. }
  3362. function makeList(origin, capacity, level, root, tail, ownerID, hash) {
  3363. var list = Object.create(ListPrototype);
  3364. list.size = capacity - origin;
  3365. list._origin = origin;
  3366. list._capacity = capacity;
  3367. list._level = level;
  3368. list._root = root;
  3369. list._tail = tail;
  3370. list.__ownerID = ownerID;
  3371. list.__hash = hash;
  3372. list.__altered = false;
  3373. return list;
  3374. }
  3375. function emptyList() {
  3376. return makeList(0, 0, SHIFT);
  3377. }
  3378. function updateList(list, index, value) {
  3379. index = wrapIndex(list, index);
  3380. if (index !== index) {
  3381. return list;
  3382. }
  3383. if (index >= list.size || index < 0) {
  3384. return list.withMutations(function (list) {
  3385. // eslint-disable-next-line @typescript-eslint/no-unused-expressions -- TODO enable eslint here
  3386. index < 0
  3387. ? setListBounds(list, index).set(0, value)
  3388. : setListBounds(list, 0, index + 1).set(index, value);
  3389. });
  3390. }
  3391. index += list._origin;
  3392. var newTail = list._tail;
  3393. var newRoot = list._root;
  3394. var didAlter = MakeRef();
  3395. if (index >= getTailOffset(list._capacity)) {
  3396. newTail = updateVNode(newTail, list.__ownerID, 0, index, value, didAlter);
  3397. } else {
  3398. newRoot = updateVNode(
  3399. newRoot,
  3400. list.__ownerID,
  3401. list._level,
  3402. index,
  3403. value,
  3404. didAlter
  3405. );
  3406. }
  3407. if (!didAlter.value) {
  3408. return list;
  3409. }
  3410. if (list.__ownerID) {
  3411. list._root = newRoot;
  3412. list._tail = newTail;
  3413. list.__hash = undefined;
  3414. list.__altered = true;
  3415. return list;
  3416. }
  3417. return makeList(list._origin, list._capacity, list._level, newRoot, newTail);
  3418. }
  3419. function updateVNode(node, ownerID, level, index, value, didAlter) {
  3420. var idx = (index >>> level) & MASK;
  3421. var nodeHas = node && idx < node.array.length;
  3422. if (!nodeHas && value === undefined) {
  3423. return node;
  3424. }
  3425. var newNode;
  3426. if (level > 0) {
  3427. var lowerNode = node && node.array[idx];
  3428. var newLowerNode = updateVNode(
  3429. lowerNode,
  3430. ownerID,
  3431. level - SHIFT,
  3432. index,
  3433. value,
  3434. didAlter
  3435. );
  3436. if (newLowerNode === lowerNode) {
  3437. return node;
  3438. }
  3439. newNode = editableVNode(node, ownerID);
  3440. newNode.array[idx] = newLowerNode;
  3441. return newNode;
  3442. }
  3443. if (nodeHas && node.array[idx] === value) {
  3444. return node;
  3445. }
  3446. if (didAlter) {
  3447. SetRef(didAlter);
  3448. }
  3449. newNode = editableVNode(node, ownerID);
  3450. if (value === undefined && idx === newNode.array.length - 1) {
  3451. newNode.array.pop();
  3452. } else {
  3453. newNode.array[idx] = value;
  3454. }
  3455. return newNode;
  3456. }
  3457. function editableVNode(node, ownerID) {
  3458. if (ownerID && node && ownerID === node.ownerID) {
  3459. return node;
  3460. }
  3461. return new VNode(node ? node.array.slice() : [], ownerID);
  3462. }
  3463. function listNodeFor(list, rawIndex) {
  3464. if (rawIndex >= getTailOffset(list._capacity)) {
  3465. return list._tail;
  3466. }
  3467. if (rawIndex < 1 << (list._level + SHIFT)) {
  3468. var node = list._root;
  3469. var level = list._level;
  3470. while (node && level > 0) {
  3471. node = node.array[(rawIndex >>> level) & MASK];
  3472. level -= SHIFT;
  3473. }
  3474. return node;
  3475. }
  3476. }
  3477. function setListBounds(list, begin, end) {
  3478. // Sanitize begin & end using this shorthand for ToInt32(argument)
  3479. // http://www.ecma-international.org/ecma-262/6.0/#sec-toint32
  3480. if (begin !== undefined) {
  3481. begin |= 0;
  3482. }
  3483. if (end !== undefined) {
  3484. end |= 0;
  3485. }
  3486. var owner = list.__ownerID || new OwnerID();
  3487. var oldOrigin = list._origin;
  3488. var oldCapacity = list._capacity;
  3489. var newOrigin = oldOrigin + begin;
  3490. var newCapacity =
  3491. end === undefined
  3492. ? oldCapacity
  3493. : end < 0
  3494. ? oldCapacity + end
  3495. : oldOrigin + end;
  3496. if (newOrigin === oldOrigin && newCapacity === oldCapacity) {
  3497. return list;
  3498. }
  3499. // If it's going to end after it starts, it's empty.
  3500. if (newOrigin >= newCapacity) {
  3501. return list.clear();
  3502. }
  3503. var newLevel = list._level;
  3504. var newRoot = list._root;
  3505. // New origin might need creating a higher root.
  3506. var offsetShift = 0;
  3507. while (newOrigin + offsetShift < 0) {
  3508. newRoot = new VNode(
  3509. newRoot && newRoot.array.length ? [undefined, newRoot] : [],
  3510. owner
  3511. );
  3512. newLevel += SHIFT;
  3513. offsetShift += 1 << newLevel;
  3514. }
  3515. if (offsetShift) {
  3516. newOrigin += offsetShift;
  3517. oldOrigin += offsetShift;
  3518. newCapacity += offsetShift;
  3519. oldCapacity += offsetShift;
  3520. }
  3521. var oldTailOffset = getTailOffset(oldCapacity);
  3522. var newTailOffset = getTailOffset(newCapacity);
  3523. // New size might need creating a higher root.
  3524. while (newTailOffset >= 1 << (newLevel + SHIFT)) {
  3525. newRoot = new VNode(
  3526. newRoot && newRoot.array.length ? [newRoot] : [],
  3527. owner
  3528. );
  3529. newLevel += SHIFT;
  3530. }
  3531. // Locate or create the new tail.
  3532. var oldTail = list._tail;
  3533. var newTail =
  3534. newTailOffset < oldTailOffset
  3535. ? listNodeFor(list, newCapacity - 1)
  3536. : newTailOffset > oldTailOffset
  3537. ? new VNode([], owner)
  3538. : oldTail;
  3539. // Merge Tail into tree.
  3540. if (
  3541. oldTail &&
  3542. newTailOffset > oldTailOffset &&
  3543. newOrigin < oldCapacity &&
  3544. oldTail.array.length
  3545. ) {
  3546. newRoot = editableVNode(newRoot, owner);
  3547. var node = newRoot;
  3548. for (var level = newLevel; level > SHIFT; level -= SHIFT) {
  3549. var idx = (oldTailOffset >>> level) & MASK;
  3550. node = node.array[idx] = editableVNode(node.array[idx], owner);
  3551. }
  3552. node.array[(oldTailOffset >>> SHIFT) & MASK] = oldTail;
  3553. }
  3554. // If the size has been reduced, there's a chance the tail needs to be trimmed.
  3555. if (newCapacity < oldCapacity) {
  3556. newTail = newTail && newTail.removeAfter(owner, 0, newCapacity);
  3557. }
  3558. // If the new origin is within the tail, then we do not need a root.
  3559. if (newOrigin >= newTailOffset) {
  3560. newOrigin -= newTailOffset;
  3561. newCapacity -= newTailOffset;
  3562. newLevel = SHIFT;
  3563. newRoot = null;
  3564. newTail = newTail && newTail.removeBefore(owner, 0, newOrigin);
  3565. // Otherwise, if the root has been trimmed, garbage collect.
  3566. } else if (newOrigin > oldOrigin || newTailOffset < oldTailOffset) {
  3567. offsetShift = 0;
  3568. // Identify the new top root node of the subtree of the old root.
  3569. while (newRoot) {
  3570. var beginIndex = (newOrigin >>> newLevel) & MASK;
  3571. if ((beginIndex !== newTailOffset >>> newLevel) & MASK) {
  3572. break;
  3573. }
  3574. if (beginIndex) {
  3575. offsetShift += (1 << newLevel) * beginIndex;
  3576. }
  3577. newLevel -= SHIFT;
  3578. newRoot = newRoot.array[beginIndex];
  3579. }
  3580. // Trim the new sides of the new root.
  3581. if (newRoot && newOrigin > oldOrigin) {
  3582. newRoot = newRoot.removeBefore(owner, newLevel, newOrigin - offsetShift);
  3583. }
  3584. if (newRoot && newTailOffset < oldTailOffset) {
  3585. newRoot = newRoot.removeAfter(
  3586. owner,
  3587. newLevel,
  3588. newTailOffset - offsetShift
  3589. );
  3590. }
  3591. if (offsetShift) {
  3592. newOrigin -= offsetShift;
  3593. newCapacity -= offsetShift;
  3594. }
  3595. }
  3596. if (list.__ownerID) {
  3597. list.size = newCapacity - newOrigin;
  3598. list._origin = newOrigin;
  3599. list._capacity = newCapacity;
  3600. list._level = newLevel;
  3601. list._root = newRoot;
  3602. list._tail = newTail;
  3603. list.__hash = undefined;
  3604. list.__altered = true;
  3605. return list;
  3606. }
  3607. return makeList(newOrigin, newCapacity, newLevel, newRoot, newTail);
  3608. }
  3609. function getTailOffset(size) {
  3610. return size < SIZE ? 0 : ((size - 1) >>> SHIFT) << SHIFT;
  3611. }
  3612. var OrderedMap = /*@__PURE__*/(function (Map) {
  3613. function OrderedMap(value) {
  3614. // eslint-disable-next-line no-constructor-return
  3615. return value === undefined || value === null
  3616. ? emptyOrderedMap()
  3617. : isOrderedMap(value)
  3618. ? value
  3619. : emptyOrderedMap().withMutations(function (map) {
  3620. var iter = KeyedCollection(value);
  3621. assertNotInfinite(iter.size);
  3622. iter.forEach(function (v, k) { return map.set(k, v); });
  3623. });
  3624. }
  3625. if ( Map ) OrderedMap.__proto__ = Map;
  3626. OrderedMap.prototype = Object.create( Map && Map.prototype );
  3627. OrderedMap.prototype.constructor = OrderedMap;
  3628. OrderedMap.of = function of (/*...values*/) {
  3629. return this(arguments);
  3630. };
  3631. OrderedMap.prototype.toString = function toString () {
  3632. return this.__toString('OrderedMap {', '}');
  3633. };
  3634. // @pragma Access
  3635. OrderedMap.prototype.get = function get (k, notSetValue) {
  3636. var index = this._map.get(k);
  3637. return index !== undefined ? this._list.get(index)[1] : notSetValue;
  3638. };
  3639. // @pragma Modification
  3640. OrderedMap.prototype.clear = function clear () {
  3641. if (this.size === 0) {
  3642. return this;
  3643. }
  3644. if (this.__ownerID) {
  3645. this.size = 0;
  3646. this._map.clear();
  3647. this._list.clear();
  3648. this.__altered = true;
  3649. return this;
  3650. }
  3651. return emptyOrderedMap();
  3652. };
  3653. OrderedMap.prototype.set = function set (k, v) {
  3654. return updateOrderedMap(this, k, v);
  3655. };
  3656. OrderedMap.prototype.remove = function remove (k) {
  3657. return updateOrderedMap(this, k, NOT_SET);
  3658. };
  3659. OrderedMap.prototype.__iterate = function __iterate (fn, reverse) {
  3660. var this$1$1 = this;
  3661. return this._list.__iterate(
  3662. function (entry) { return entry && fn(entry[1], entry[0], this$1$1); },
  3663. reverse
  3664. );
  3665. };
  3666. OrderedMap.prototype.__iterator = function __iterator (type, reverse) {
  3667. return this._list.fromEntrySeq().__iterator(type, reverse);
  3668. };
  3669. OrderedMap.prototype.__ensureOwner = function __ensureOwner (ownerID) {
  3670. if (ownerID === this.__ownerID) {
  3671. return this;
  3672. }
  3673. var newMap = this._map.__ensureOwner(ownerID);
  3674. var newList = this._list.__ensureOwner(ownerID);
  3675. if (!ownerID) {
  3676. if (this.size === 0) {
  3677. return emptyOrderedMap();
  3678. }
  3679. this.__ownerID = ownerID;
  3680. this.__altered = false;
  3681. this._map = newMap;
  3682. this._list = newList;
  3683. return this;
  3684. }
  3685. return makeOrderedMap(newMap, newList, ownerID, this.__hash);
  3686. };
  3687. return OrderedMap;
  3688. }(Map));
  3689. OrderedMap.isOrderedMap = isOrderedMap;
  3690. OrderedMap.prototype[IS_ORDERED_SYMBOL] = true;
  3691. OrderedMap.prototype[DELETE] = OrderedMap.prototype.remove;
  3692. function makeOrderedMap(map, list, ownerID, hash) {
  3693. var omap = Object.create(OrderedMap.prototype);
  3694. omap.size = map ? map.size : 0;
  3695. omap._map = map;
  3696. omap._list = list;
  3697. omap.__ownerID = ownerID;
  3698. omap.__hash = hash;
  3699. omap.__altered = false;
  3700. return omap;
  3701. }
  3702. var EMPTY_ORDERED_MAP;
  3703. function emptyOrderedMap() {
  3704. return (
  3705. EMPTY_ORDERED_MAP ||
  3706. (EMPTY_ORDERED_MAP = makeOrderedMap(emptyMap(), emptyList()))
  3707. );
  3708. }
  3709. function updateOrderedMap(omap, k, v) {
  3710. var map = omap._map;
  3711. var list = omap._list;
  3712. var i = map.get(k);
  3713. var has = i !== undefined;
  3714. var newMap;
  3715. var newList;
  3716. if (v === NOT_SET) {
  3717. // removed
  3718. if (!has) {
  3719. return omap;
  3720. }
  3721. if (list.size >= SIZE && list.size >= map.size * 2) {
  3722. newList = list.filter(function (entry, idx) { return entry !== undefined && i !== idx; });
  3723. newMap = newList
  3724. .toKeyedSeq()
  3725. .map(function (entry) { return entry[0]; })
  3726. .flip()
  3727. .toMap();
  3728. if (omap.__ownerID) {
  3729. newMap.__ownerID = newList.__ownerID = omap.__ownerID;
  3730. }
  3731. } else {
  3732. newMap = map.remove(k);
  3733. newList = i === list.size - 1 ? list.pop() : list.set(i, undefined);
  3734. }
  3735. } else if (has) {
  3736. if (v === list.get(i)[1]) {
  3737. return omap;
  3738. }
  3739. newMap = map;
  3740. newList = list.set(i, [k, v]);
  3741. } else {
  3742. newMap = map.set(k, list.size);
  3743. newList = list.set(list.size, [k, v]);
  3744. }
  3745. if (omap.__ownerID) {
  3746. omap.size = newMap.size;
  3747. omap._map = newMap;
  3748. omap._list = newList;
  3749. omap.__hash = undefined;
  3750. omap.__altered = true;
  3751. return omap;
  3752. }
  3753. return makeOrderedMap(newMap, newList);
  3754. }
  3755. var IS_STACK_SYMBOL = '@@__IMMUTABLE_STACK__@@';
  3756. /**
  3757. * True if `maybeStack` is a Stack.
  3758. */
  3759. function isStack(maybeStack) {
  3760. return Boolean(maybeStack &&
  3761. // @ts-expect-error: maybeStack is typed as `{}`, need to change in 6.0 to `maybeStack && typeof maybeStack === 'object' && MAYBE_STACK_SYMBOL in maybeStack`
  3762. maybeStack[IS_STACK_SYMBOL]);
  3763. }
  3764. var Stack = /*@__PURE__*/(function (IndexedCollection) {
  3765. function Stack(value) {
  3766. // eslint-disable-next-line no-constructor-return
  3767. return value === undefined || value === null
  3768. ? emptyStack()
  3769. : isStack(value)
  3770. ? value
  3771. : emptyStack().pushAll(value);
  3772. }
  3773. if ( IndexedCollection ) Stack.__proto__ = IndexedCollection;
  3774. Stack.prototype = Object.create( IndexedCollection && IndexedCollection.prototype );
  3775. Stack.prototype.constructor = Stack;
  3776. Stack.of = function of (/*...values*/) {
  3777. return this(arguments);
  3778. };
  3779. Stack.prototype.toString = function toString () {
  3780. return this.__toString('Stack [', ']');
  3781. };
  3782. // @pragma Access
  3783. Stack.prototype.get = function get (index, notSetValue) {
  3784. var head = this._head;
  3785. index = wrapIndex(this, index);
  3786. while (head && index--) {
  3787. head = head.next;
  3788. }
  3789. return head ? head.value : notSetValue;
  3790. };
  3791. Stack.prototype.peek = function peek () {
  3792. return this._head && this._head.value;
  3793. };
  3794. // @pragma Modification
  3795. Stack.prototype.push = function push (/*...values*/) {
  3796. var arguments$1 = arguments;
  3797. if (arguments.length === 0) {
  3798. return this;
  3799. }
  3800. var newSize = this.size + arguments.length;
  3801. var head = this._head;
  3802. for (var ii = arguments.length - 1; ii >= 0; ii--) {
  3803. head = {
  3804. value: arguments$1[ii],
  3805. next: head,
  3806. };
  3807. }
  3808. if (this.__ownerID) {
  3809. this.size = newSize;
  3810. this._head = head;
  3811. this.__hash = undefined;
  3812. this.__altered = true;
  3813. return this;
  3814. }
  3815. return makeStack(newSize, head);
  3816. };
  3817. Stack.prototype.pushAll = function pushAll (iter) {
  3818. iter = IndexedCollection(iter);
  3819. if (iter.size === 0) {
  3820. return this;
  3821. }
  3822. if (this.size === 0 && isStack(iter)) {
  3823. return iter;
  3824. }
  3825. assertNotInfinite(iter.size);
  3826. var newSize = this.size;
  3827. var head = this._head;
  3828. iter.__iterate(function (value) {
  3829. newSize++;
  3830. head = {
  3831. value: value,
  3832. next: head,
  3833. };
  3834. }, /* reverse */ true);
  3835. if (this.__ownerID) {
  3836. this.size = newSize;
  3837. this._head = head;
  3838. this.__hash = undefined;
  3839. this.__altered = true;
  3840. return this;
  3841. }
  3842. return makeStack(newSize, head);
  3843. };
  3844. Stack.prototype.pop = function pop () {
  3845. return this.slice(1);
  3846. };
  3847. Stack.prototype.clear = function clear () {
  3848. if (this.size === 0) {
  3849. return this;
  3850. }
  3851. if (this.__ownerID) {
  3852. this.size = 0;
  3853. this._head = undefined;
  3854. this.__hash = undefined;
  3855. this.__altered = true;
  3856. return this;
  3857. }
  3858. return emptyStack();
  3859. };
  3860. Stack.prototype.slice = function slice (begin, end) {
  3861. if (wholeSlice(begin, end, this.size)) {
  3862. return this;
  3863. }
  3864. var resolvedBegin = resolveBegin(begin, this.size);
  3865. var resolvedEnd = resolveEnd(end, this.size);
  3866. if (resolvedEnd !== this.size) {
  3867. // super.slice(begin, end);
  3868. return IndexedCollection.prototype.slice.call(this, begin, end);
  3869. }
  3870. var newSize = this.size - resolvedBegin;
  3871. var head = this._head;
  3872. while (resolvedBegin--) {
  3873. head = head.next;
  3874. }
  3875. if (this.__ownerID) {
  3876. this.size = newSize;
  3877. this._head = head;
  3878. this.__hash = undefined;
  3879. this.__altered = true;
  3880. return this;
  3881. }
  3882. return makeStack(newSize, head);
  3883. };
  3884. // @pragma Mutability
  3885. Stack.prototype.__ensureOwner = function __ensureOwner (ownerID) {
  3886. if (ownerID === this.__ownerID) {
  3887. return this;
  3888. }
  3889. if (!ownerID) {
  3890. if (this.size === 0) {
  3891. return emptyStack();
  3892. }
  3893. this.__ownerID = ownerID;
  3894. this.__altered = false;
  3895. return this;
  3896. }
  3897. return makeStack(this.size, this._head, ownerID, this.__hash);
  3898. };
  3899. // @pragma Iteration
  3900. Stack.prototype.__iterate = function __iterate (fn, reverse) {
  3901. var this$1$1 = this;
  3902. if (reverse) {
  3903. return new ArraySeq(this.toArray()).__iterate(
  3904. function (v, k) { return fn(v, k, this$1$1); },
  3905. reverse
  3906. );
  3907. }
  3908. var iterations = 0;
  3909. var node = this._head;
  3910. while (node) {
  3911. if (fn(node.value, iterations++, this) === false) {
  3912. break;
  3913. }
  3914. node = node.next;
  3915. }
  3916. return iterations;
  3917. };
  3918. Stack.prototype.__iterator = function __iterator (type, reverse) {
  3919. if (reverse) {
  3920. return new ArraySeq(this.toArray()).__iterator(type, reverse);
  3921. }
  3922. var iterations = 0;
  3923. var node = this._head;
  3924. return new Iterator(function () {
  3925. if (node) {
  3926. var value = node.value;
  3927. node = node.next;
  3928. return iteratorValue(type, iterations++, value);
  3929. }
  3930. return iteratorDone();
  3931. });
  3932. };
  3933. return Stack;
  3934. }(IndexedCollection));
  3935. Stack.isStack = isStack;
  3936. var StackPrototype = Stack.prototype;
  3937. StackPrototype[IS_STACK_SYMBOL] = true;
  3938. StackPrototype.shift = StackPrototype.pop;
  3939. StackPrototype.unshift = StackPrototype.push;
  3940. StackPrototype.unshiftAll = StackPrototype.pushAll;
  3941. StackPrototype.withMutations = withMutations;
  3942. StackPrototype.wasAltered = wasAltered;
  3943. StackPrototype.asImmutable = asImmutable;
  3944. StackPrototype['@@transducer/init'] = StackPrototype.asMutable = asMutable;
  3945. StackPrototype['@@transducer/step'] = function (result, arr) {
  3946. return result.unshift(arr);
  3947. };
  3948. StackPrototype['@@transducer/result'] = function (obj) {
  3949. return obj.asImmutable();
  3950. };
  3951. function makeStack(size, head, ownerID, hash) {
  3952. var map = Object.create(StackPrototype);
  3953. map.size = size;
  3954. map._head = head;
  3955. map.__ownerID = ownerID;
  3956. map.__hash = hash;
  3957. map.__altered = false;
  3958. return map;
  3959. }
  3960. var EMPTY_STACK;
  3961. function emptyStack() {
  3962. return EMPTY_STACK || (EMPTY_STACK = makeStack(0));
  3963. }
  3964. var IS_SET_SYMBOL = '@@__IMMUTABLE_SET__@@';
  3965. /**
  3966. * True if `maybeSet` is a Set.
  3967. *
  3968. * Also true for OrderedSets.
  3969. */
  3970. function isSet(maybeSet) {
  3971. return Boolean(maybeSet &&
  3972. // @ts-expect-error: maybeSet is typed as `{}`, need to change in 6.0 to `maybeSeq && typeof maybeSet === 'object' && MAYBE_SET_SYMBOL in maybeSet`
  3973. maybeSet[IS_SET_SYMBOL]);
  3974. }
  3975. /**
  3976. * True if `maybeOrderedSet` is an OrderedSet.
  3977. */
  3978. function isOrderedSet(maybeOrderedSet) {
  3979. return isSet(maybeOrderedSet) && isOrdered(maybeOrderedSet);
  3980. }
  3981. function deepEqual(a, b) {
  3982. if (a === b) {
  3983. return true;
  3984. }
  3985. if (!isCollection(b) ||
  3986. // @ts-expect-error size should exists on Collection
  3987. (a.size !== undefined && b.size !== undefined && a.size !== b.size) ||
  3988. // @ts-expect-error __hash exists on Collection
  3989. (a.__hash !== undefined &&
  3990. // @ts-expect-error __hash exists on Collection
  3991. b.__hash !== undefined &&
  3992. // @ts-expect-error __hash exists on Collection
  3993. a.__hash !== b.__hash) ||
  3994. isKeyed(a) !== isKeyed(b) ||
  3995. isIndexed(a) !== isIndexed(b) ||
  3996. // @ts-expect-error Range extends Collection, which implements [Symbol.iterator], so it is valid
  3997. isOrdered(a) !== isOrdered(b)) {
  3998. return false;
  3999. }
  4000. // @ts-expect-error size should exists on Collection
  4001. if (a.size === 0 && b.size === 0) {
  4002. return true;
  4003. }
  4004. var notAssociative = !isAssociative(a);
  4005. // @ts-expect-error Range extends Collection, which implements [Symbol.iterator], so it is valid
  4006. if (isOrdered(a)) {
  4007. var entries = a.entries();
  4008. // @ts-expect-error need to cast as boolean
  4009. return (b.every(function (v, k) {
  4010. var entry = entries.next().value;
  4011. return entry && is(entry[1], v) && (notAssociative || is(entry[0], k));
  4012. }) && entries.next().done);
  4013. }
  4014. var flipped = false;
  4015. if (a.size === undefined) {
  4016. // @ts-expect-error size should exists on Collection
  4017. if (b.size === undefined) {
  4018. if (typeof a.cacheResult === 'function') {
  4019. a.cacheResult();
  4020. }
  4021. }
  4022. else {
  4023. flipped = true;
  4024. var _ = a;
  4025. a = b;
  4026. b = _;
  4027. }
  4028. }
  4029. var allEqual = true;
  4030. var bSize =
  4031. // @ts-expect-error b is Range | Repeat | Collection<unknown, unknown> as it may have been flipped, and __iterate is valid
  4032. b.__iterate(function (v, k) {
  4033. if (notAssociative
  4034. ? // @ts-expect-error has exists on Collection
  4035. !a.has(v)
  4036. : flipped
  4037. ? // @ts-expect-error type of `get` does not "catch" the version with `notSetValue`
  4038. !is(v, a.get(k, NOT_SET))
  4039. : // @ts-expect-error type of `get` does not "catch" the version with `notSetValue`
  4040. !is(a.get(k, NOT_SET), v)) {
  4041. allEqual = false;
  4042. return false;
  4043. }
  4044. });
  4045. return (allEqual &&
  4046. // @ts-expect-error size should exists on Collection
  4047. a.size === bSize);
  4048. }
  4049. /**
  4050. * Contributes additional methods to a constructor
  4051. */
  4052. function mixin(ctor,
  4053. // eslint-disable-next-line @typescript-eslint/no-unsafe-function-type
  4054. methods) {
  4055. var keyCopier = function (key) {
  4056. // @ts-expect-error how to handle symbol ?
  4057. ctor.prototype[key] = methods[key];
  4058. };
  4059. Object.keys(methods).forEach(keyCopier);
  4060. // eslint-disable-next-line @typescript-eslint/no-unused-expressions -- TODO enable eslint here
  4061. Object.getOwnPropertySymbols &&
  4062. Object.getOwnPropertySymbols(methods).forEach(keyCopier);
  4063. return ctor;
  4064. }
  4065. function toJS(value) {
  4066. if (!value || typeof value !== 'object') {
  4067. return value;
  4068. }
  4069. if (!isCollection(value)) {
  4070. if (!isDataStructure(value)) {
  4071. return value;
  4072. }
  4073. value = Seq(value);
  4074. }
  4075. if (isKeyed(value)) {
  4076. var result$1 = {};
  4077. value.__iterate(function (v, k) {
  4078. result$1[k] = toJS(v);
  4079. });
  4080. return result$1;
  4081. }
  4082. var result = [];
  4083. value.__iterate(function (v) {
  4084. result.push(toJS(v));
  4085. });
  4086. return result;
  4087. }
  4088. var Set = /*@__PURE__*/(function (SetCollection) {
  4089. function Set(value) {
  4090. // eslint-disable-next-line no-constructor-return
  4091. return value === undefined || value === null
  4092. ? emptySet()
  4093. : isSet(value) && !isOrdered(value)
  4094. ? value
  4095. : emptySet().withMutations(function (set) {
  4096. var iter = SetCollection(value);
  4097. assertNotInfinite(iter.size);
  4098. iter.forEach(function (v) { return set.add(v); });
  4099. });
  4100. }
  4101. if ( SetCollection ) Set.__proto__ = SetCollection;
  4102. Set.prototype = Object.create( SetCollection && SetCollection.prototype );
  4103. Set.prototype.constructor = Set;
  4104. Set.of = function of (/*...values*/) {
  4105. return this(arguments);
  4106. };
  4107. Set.fromKeys = function fromKeys (value) {
  4108. return this(KeyedCollection(value).keySeq());
  4109. };
  4110. Set.intersect = function intersect (sets) {
  4111. sets = Collection(sets).toArray();
  4112. return sets.length
  4113. ? SetPrototype.intersect.apply(Set(sets.pop()), sets)
  4114. : emptySet();
  4115. };
  4116. Set.union = function union (sets) {
  4117. sets = Collection(sets).toArray();
  4118. return sets.length
  4119. ? SetPrototype.union.apply(Set(sets.pop()), sets)
  4120. : emptySet();
  4121. };
  4122. Set.prototype.toString = function toString () {
  4123. return this.__toString('Set {', '}');
  4124. };
  4125. // @pragma Access
  4126. Set.prototype.has = function has (value) {
  4127. return this._map.has(value);
  4128. };
  4129. // @pragma Modification
  4130. Set.prototype.add = function add (value) {
  4131. return updateSet(this, this._map.set(value, value));
  4132. };
  4133. Set.prototype.remove = function remove (value) {
  4134. return updateSet(this, this._map.remove(value));
  4135. };
  4136. Set.prototype.clear = function clear () {
  4137. return updateSet(this, this._map.clear());
  4138. };
  4139. // @pragma Composition
  4140. Set.prototype.map = function map (mapper, context) {
  4141. var this$1$1 = this;
  4142. // keep track if the set is altered by the map function
  4143. var didChanges = false;
  4144. var newMap = updateSet(
  4145. this,
  4146. this._map.mapEntries(function (ref) {
  4147. var v = ref[1];
  4148. var mapped = mapper.call(context, v, v, this$1$1);
  4149. if (mapped !== v) {
  4150. didChanges = true;
  4151. }
  4152. return [mapped, mapped];
  4153. }, context)
  4154. );
  4155. return didChanges ? newMap : this;
  4156. };
  4157. Set.prototype.union = function union () {
  4158. var iters = [], len = arguments.length;
  4159. while ( len-- ) iters[ len ] = arguments[ len ];
  4160. iters = iters.filter(function (x) { return x.size !== 0; });
  4161. if (iters.length === 0) {
  4162. return this;
  4163. }
  4164. if (this.size === 0 && !this.__ownerID && iters.length === 1) {
  4165. return this.constructor(iters[0]);
  4166. }
  4167. return this.withMutations(function (set) {
  4168. for (var ii = 0; ii < iters.length; ii++) {
  4169. if (typeof iters[ii] === 'string') {
  4170. set.add(iters[ii]);
  4171. } else {
  4172. SetCollection(iters[ii]).forEach(function (value) { return set.add(value); });
  4173. }
  4174. }
  4175. });
  4176. };
  4177. Set.prototype.intersect = function intersect () {
  4178. var iters = [], len = arguments.length;
  4179. while ( len-- ) iters[ len ] = arguments[ len ];
  4180. if (iters.length === 0) {
  4181. return this;
  4182. }
  4183. iters = iters.map(function (iter) { return SetCollection(iter); });
  4184. var toRemove = [];
  4185. this.forEach(function (value) {
  4186. if (!iters.every(function (iter) { return iter.includes(value); })) {
  4187. toRemove.push(value);
  4188. }
  4189. });
  4190. return this.withMutations(function (set) {
  4191. toRemove.forEach(function (value) {
  4192. set.remove(value);
  4193. });
  4194. });
  4195. };
  4196. Set.prototype.subtract = function subtract () {
  4197. var iters = [], len = arguments.length;
  4198. while ( len-- ) iters[ len ] = arguments[ len ];
  4199. if (iters.length === 0) {
  4200. return this;
  4201. }
  4202. iters = iters.map(function (iter) { return SetCollection(iter); });
  4203. var toRemove = [];
  4204. this.forEach(function (value) {
  4205. if (iters.some(function (iter) { return iter.includes(value); })) {
  4206. toRemove.push(value);
  4207. }
  4208. });
  4209. return this.withMutations(function (set) {
  4210. toRemove.forEach(function (value) {
  4211. set.remove(value);
  4212. });
  4213. });
  4214. };
  4215. Set.prototype.sort = function sort (comparator) {
  4216. // Late binding
  4217. return OrderedSet(sortFactory(this, comparator));
  4218. };
  4219. Set.prototype.sortBy = function sortBy (mapper, comparator) {
  4220. // Late binding
  4221. return OrderedSet(sortFactory(this, comparator, mapper));
  4222. };
  4223. Set.prototype.wasAltered = function wasAltered () {
  4224. return this._map.wasAltered();
  4225. };
  4226. Set.prototype.__iterate = function __iterate (fn, reverse) {
  4227. var this$1$1 = this;
  4228. return this._map.__iterate(function (k) { return fn(k, k, this$1$1); }, reverse);
  4229. };
  4230. Set.prototype.__iterator = function __iterator (type, reverse) {
  4231. return this._map.__iterator(type, reverse);
  4232. };
  4233. Set.prototype.__ensureOwner = function __ensureOwner (ownerID) {
  4234. if (ownerID === this.__ownerID) {
  4235. return this;
  4236. }
  4237. var newMap = this._map.__ensureOwner(ownerID);
  4238. if (!ownerID) {
  4239. if (this.size === 0) {
  4240. return this.__empty();
  4241. }
  4242. this.__ownerID = ownerID;
  4243. this._map = newMap;
  4244. return this;
  4245. }
  4246. return this.__make(newMap, ownerID);
  4247. };
  4248. return Set;
  4249. }(SetCollection));
  4250. Set.isSet = isSet;
  4251. var SetPrototype = Set.prototype;
  4252. SetPrototype[IS_SET_SYMBOL] = true;
  4253. SetPrototype[DELETE] = SetPrototype.remove;
  4254. SetPrototype.merge = SetPrototype.concat = SetPrototype.union;
  4255. SetPrototype.withMutations = withMutations;
  4256. SetPrototype.asImmutable = asImmutable;
  4257. SetPrototype['@@transducer/init'] = SetPrototype.asMutable = asMutable;
  4258. SetPrototype['@@transducer/step'] = function (result, arr) {
  4259. return result.add(arr);
  4260. };
  4261. SetPrototype['@@transducer/result'] = function (obj) {
  4262. return obj.asImmutable();
  4263. };
  4264. SetPrototype.__empty = emptySet;
  4265. SetPrototype.__make = makeSet;
  4266. function updateSet(set, newMap) {
  4267. if (set.__ownerID) {
  4268. set.size = newMap.size;
  4269. set._map = newMap;
  4270. return set;
  4271. }
  4272. return newMap === set._map
  4273. ? set
  4274. : newMap.size === 0
  4275. ? set.__empty()
  4276. : set.__make(newMap);
  4277. }
  4278. function makeSet(map, ownerID) {
  4279. var set = Object.create(SetPrototype);
  4280. set.size = map ? map.size : 0;
  4281. set._map = map;
  4282. set.__ownerID = ownerID;
  4283. return set;
  4284. }
  4285. var EMPTY_SET;
  4286. function emptySet() {
  4287. return EMPTY_SET || (EMPTY_SET = makeSet(emptyMap()));
  4288. }
  4289. /**
  4290. * Returns a lazy seq of nums from start (inclusive) to end
  4291. * (exclusive), by step, where start defaults to 0, step to 1, and end to
  4292. * infinity. When start is equal to end, returns empty list.
  4293. */
  4294. var Range = /*@__PURE__*/(function (IndexedSeq) {
  4295. function Range(start, end, step) {
  4296. if ( step === void 0 ) step = 1;
  4297. if (!(this instanceof Range)) {
  4298. // eslint-disable-next-line no-constructor-return
  4299. return new Range(start, end, step);
  4300. }
  4301. invariant(step !== 0, 'Cannot step a Range by 0');
  4302. invariant(
  4303. start !== undefined,
  4304. 'You must define a start value when using Range'
  4305. );
  4306. invariant(
  4307. end !== undefined,
  4308. 'You must define an end value when using Range'
  4309. );
  4310. step = Math.abs(step);
  4311. if (end < start) {
  4312. step = -step;
  4313. }
  4314. this._start = start;
  4315. this._end = end;
  4316. this._step = step;
  4317. this.size = Math.max(0, Math.ceil((end - start) / step - 1) + 1);
  4318. if (this.size === 0) {
  4319. if (EMPTY_RANGE) {
  4320. // eslint-disable-next-line no-constructor-return
  4321. return EMPTY_RANGE;
  4322. }
  4323. // eslint-disable-next-line @typescript-eslint/no-this-alias
  4324. EMPTY_RANGE = this;
  4325. }
  4326. }
  4327. if ( IndexedSeq ) Range.__proto__ = IndexedSeq;
  4328. Range.prototype = Object.create( IndexedSeq && IndexedSeq.prototype );
  4329. Range.prototype.constructor = Range;
  4330. Range.prototype.toString = function toString () {
  4331. return this.size === 0
  4332. ? 'Range []'
  4333. : ("Range [ " + (this._start) + "..." + (this._end) + (this._step !== 1 ? ' by ' + this._step : '') + " ]");
  4334. };
  4335. Range.prototype.get = function get (index, notSetValue) {
  4336. return this.has(index)
  4337. ? this._start + wrapIndex(this, index) * this._step
  4338. : notSetValue;
  4339. };
  4340. Range.prototype.includes = function includes (searchValue) {
  4341. var possibleIndex = (searchValue - this._start) / this._step;
  4342. return (
  4343. possibleIndex >= 0 &&
  4344. possibleIndex < this.size &&
  4345. possibleIndex === Math.floor(possibleIndex)
  4346. );
  4347. };
  4348. Range.prototype.slice = function slice (begin, end) {
  4349. if (wholeSlice(begin, end, this.size)) {
  4350. return this;
  4351. }
  4352. begin = resolveBegin(begin, this.size);
  4353. end = resolveEnd(end, this.size);
  4354. if (end <= begin) {
  4355. return new Range(0, 0);
  4356. }
  4357. return new Range(
  4358. this.get(begin, this._end),
  4359. this.get(end, this._end),
  4360. this._step
  4361. );
  4362. };
  4363. Range.prototype.indexOf = function indexOf (searchValue) {
  4364. var offsetValue = searchValue - this._start;
  4365. if (offsetValue % this._step === 0) {
  4366. var index = offsetValue / this._step;
  4367. if (index >= 0 && index < this.size) {
  4368. return index;
  4369. }
  4370. }
  4371. return -1;
  4372. };
  4373. Range.prototype.lastIndexOf = function lastIndexOf (searchValue) {
  4374. return this.indexOf(searchValue);
  4375. };
  4376. Range.prototype.__iterate = function __iterate (fn, reverse) {
  4377. var size = this.size;
  4378. var step = this._step;
  4379. var value = reverse ? this._start + (size - 1) * step : this._start;
  4380. var i = 0;
  4381. while (i !== size) {
  4382. if (fn(value, reverse ? size - ++i : i++, this) === false) {
  4383. break;
  4384. }
  4385. value += reverse ? -step : step;
  4386. }
  4387. return i;
  4388. };
  4389. Range.prototype.__iterator = function __iterator (type, reverse) {
  4390. var size = this.size;
  4391. var step = this._step;
  4392. var value = reverse ? this._start + (size - 1) * step : this._start;
  4393. var i = 0;
  4394. return new Iterator(function () {
  4395. if (i === size) {
  4396. return iteratorDone();
  4397. }
  4398. var v = value;
  4399. value += reverse ? -step : step;
  4400. return iteratorValue(type, reverse ? size - ++i : i++, v);
  4401. });
  4402. };
  4403. Range.prototype.equals = function equals (other) {
  4404. return other instanceof Range
  4405. ? this._start === other._start &&
  4406. this._end === other._end &&
  4407. this._step === other._step
  4408. : deepEqual(this, other);
  4409. };
  4410. return Range;
  4411. }(IndexedSeq));
  4412. var EMPTY_RANGE;
  4413. /**
  4414. * Returns the value at the provided key path starting at the provided
  4415. * collection, or notSetValue if the key path is not defined.
  4416. *
  4417. * A functional alternative to `collection.getIn(keypath)` which will also
  4418. * work with plain Objects and Arrays.
  4419. *
  4420. * <!-- runkit:activate -->
  4421. * ```js
  4422. * import { getIn } from 'immutable';
  4423. *
  4424. * getIn({ x: { y: { z: 123 }}}, ['x', 'y', 'z']) // 123
  4425. * getIn({ x: { y: { z: 123 }}}, ['x', 'q', 'p'], 'ifNotSet') // 'ifNotSet'
  4426. * ```
  4427. */
  4428. function getIn$1(collection, searchKeyPath, notSetValue) {
  4429. var keyPath = coerceKeyPath(searchKeyPath);
  4430. var i = 0;
  4431. while (i !== keyPath.length) {
  4432. // @ts-expect-error keyPath[i++] can not be undefined by design
  4433. collection = get(collection, keyPath[i++], NOT_SET);
  4434. if (collection === NOT_SET) {
  4435. return notSetValue;
  4436. }
  4437. }
  4438. return collection;
  4439. }
  4440. function getIn(searchKeyPath, notSetValue) {
  4441. return getIn$1(this, searchKeyPath, notSetValue);
  4442. }
  4443. /**
  4444. * Returns true if the key path is defined in the provided collection.
  4445. *
  4446. * A functional alternative to `collection.hasIn(keypath)` which will also
  4447. * work with plain Objects and Arrays.
  4448. *
  4449. * <!-- runkit:activate -->
  4450. * ```js
  4451. * import { hasIn } from 'immutable';
  4452. *
  4453. * hasIn({ x: { y: { z: 123 }}}, ['x', 'y', 'z']) // true
  4454. * hasIn({ x: { y: { z: 123 }}}, ['x', 'q', 'p']) // false
  4455. * ```
  4456. */
  4457. function hasIn$1(collection, keyPath) {
  4458. return getIn$1(collection, keyPath, NOT_SET) !== NOT_SET;
  4459. }
  4460. function hasIn(searchKeyPath) {
  4461. return hasIn$1(this, searchKeyPath);
  4462. }
  4463. function toObject() {
  4464. assertNotInfinite(this.size);
  4465. var object = {};
  4466. this.__iterate(function (v, k) {
  4467. object[k] = v;
  4468. });
  4469. return object;
  4470. }
  4471. Collection.Iterator = Iterator;
  4472. mixin(Collection, {
  4473. // ### Conversion to other types
  4474. toArray: function toArray() {
  4475. assertNotInfinite(this.size);
  4476. var array = new Array(this.size || 0);
  4477. var useTuples = isKeyed(this);
  4478. var i = 0;
  4479. this.__iterate(function (v, k) {
  4480. // Keyed collections produce an array of tuples.
  4481. array[i++] = useTuples ? [k, v] : v;
  4482. });
  4483. return array;
  4484. },
  4485. toIndexedSeq: function toIndexedSeq() {
  4486. return new ToIndexedSequence(this);
  4487. },
  4488. toJS: function toJS$1() {
  4489. return toJS(this);
  4490. },
  4491. toKeyedSeq: function toKeyedSeq() {
  4492. return new ToKeyedSequence(this, true);
  4493. },
  4494. toMap: function toMap() {
  4495. // Use Late Binding here to solve the circular dependency.
  4496. return Map(this.toKeyedSeq());
  4497. },
  4498. toObject: toObject,
  4499. toOrderedMap: function toOrderedMap() {
  4500. // Use Late Binding here to solve the circular dependency.
  4501. return OrderedMap(this.toKeyedSeq());
  4502. },
  4503. toOrderedSet: function toOrderedSet() {
  4504. // Use Late Binding here to solve the circular dependency.
  4505. return OrderedSet(isKeyed(this) ? this.valueSeq() : this);
  4506. },
  4507. toSet: function toSet() {
  4508. // Use Late Binding here to solve the circular dependency.
  4509. return Set(isKeyed(this) ? this.valueSeq() : this);
  4510. },
  4511. toSetSeq: function toSetSeq() {
  4512. return new ToSetSequence(this);
  4513. },
  4514. toSeq: function toSeq() {
  4515. return isIndexed(this)
  4516. ? this.toIndexedSeq()
  4517. : isKeyed(this)
  4518. ? this.toKeyedSeq()
  4519. : this.toSetSeq();
  4520. },
  4521. toStack: function toStack() {
  4522. // Use Late Binding here to solve the circular dependency.
  4523. return Stack(isKeyed(this) ? this.valueSeq() : this);
  4524. },
  4525. toList: function toList() {
  4526. // Use Late Binding here to solve the circular dependency.
  4527. return List(isKeyed(this) ? this.valueSeq() : this);
  4528. },
  4529. // ### Common JavaScript methods and properties
  4530. toString: function toString() {
  4531. return '[Collection]';
  4532. },
  4533. __toString: function __toString(head, tail) {
  4534. if (this.size === 0) {
  4535. return head + tail;
  4536. }
  4537. return (
  4538. head +
  4539. ' ' +
  4540. this.toSeq().map(this.__toStringMapper).join(', ') +
  4541. ' ' +
  4542. tail
  4543. );
  4544. },
  4545. // ### ES6 Collection methods (ES6 Array and Map)
  4546. concat: function concat() {
  4547. var values = [], len = arguments.length;
  4548. while ( len-- ) values[ len ] = arguments[ len ];
  4549. return reify(this, concatFactory(this, values));
  4550. },
  4551. includes: function includes(searchValue) {
  4552. return this.some(function (value) { return is(value, searchValue); });
  4553. },
  4554. entries: function entries() {
  4555. return this.__iterator(ITERATE_ENTRIES);
  4556. },
  4557. every: function every(predicate, context) {
  4558. assertNotInfinite(this.size);
  4559. var returnValue = true;
  4560. this.__iterate(function (v, k, c) {
  4561. if (!predicate.call(context, v, k, c)) {
  4562. returnValue = false;
  4563. return false;
  4564. }
  4565. });
  4566. return returnValue;
  4567. },
  4568. filter: function filter(predicate, context) {
  4569. return reify(this, filterFactory(this, predicate, context, true));
  4570. },
  4571. partition: function partition(predicate, context) {
  4572. return partitionFactory(this, predicate, context);
  4573. },
  4574. find: function find(predicate, context, notSetValue) {
  4575. var entry = this.findEntry(predicate, context);
  4576. return entry ? entry[1] : notSetValue;
  4577. },
  4578. forEach: function forEach(sideEffect, context) {
  4579. assertNotInfinite(this.size);
  4580. return this.__iterate(context ? sideEffect.bind(context) : sideEffect);
  4581. },
  4582. join: function join(separator) {
  4583. assertNotInfinite(this.size);
  4584. separator = separator !== undefined ? '' + separator : ',';
  4585. var joined = '';
  4586. var isFirst = true;
  4587. this.__iterate(function (v) {
  4588. // eslint-disable-next-line @typescript-eslint/no-unused-expressions -- TODO enable eslint here
  4589. isFirst ? (isFirst = false) : (joined += separator);
  4590. joined += v !== null && v !== undefined ? v.toString() : '';
  4591. });
  4592. return joined;
  4593. },
  4594. keys: function keys() {
  4595. return this.__iterator(ITERATE_KEYS);
  4596. },
  4597. map: function map(mapper, context) {
  4598. return reify(this, mapFactory(this, mapper, context));
  4599. },
  4600. reduce: function reduce$1(reducer, initialReduction, context) {
  4601. return reduce(
  4602. this,
  4603. reducer,
  4604. initialReduction,
  4605. context,
  4606. arguments.length < 2,
  4607. false
  4608. );
  4609. },
  4610. reduceRight: function reduceRight(reducer, initialReduction, context) {
  4611. return reduce(
  4612. this,
  4613. reducer,
  4614. initialReduction,
  4615. context,
  4616. arguments.length < 2,
  4617. true
  4618. );
  4619. },
  4620. reverse: function reverse() {
  4621. return reify(this, reverseFactory(this, true));
  4622. },
  4623. slice: function slice(begin, end) {
  4624. return reify(this, sliceFactory(this, begin, end, true));
  4625. },
  4626. some: function some(predicate, context) {
  4627. assertNotInfinite(this.size);
  4628. var returnValue = false;
  4629. this.__iterate(function (v, k, c) {
  4630. if (predicate.call(context, v, k, c)) {
  4631. returnValue = true;
  4632. return false;
  4633. }
  4634. });
  4635. return returnValue;
  4636. },
  4637. sort: function sort(comparator) {
  4638. return reify(this, sortFactory(this, comparator));
  4639. },
  4640. values: function values() {
  4641. return this.__iterator(ITERATE_VALUES);
  4642. },
  4643. // ### More sequential methods
  4644. butLast: function butLast() {
  4645. return this.slice(0, -1);
  4646. },
  4647. isEmpty: function isEmpty() {
  4648. return this.size !== undefined ? this.size === 0 : !this.some(function () { return true; });
  4649. },
  4650. count: function count(predicate, context) {
  4651. return ensureSize(
  4652. predicate ? this.toSeq().filter(predicate, context) : this
  4653. );
  4654. },
  4655. countBy: function countBy(grouper, context) {
  4656. return countByFactory(this, grouper, context);
  4657. },
  4658. equals: function equals(other) {
  4659. return deepEqual(this, other);
  4660. },
  4661. entrySeq: function entrySeq() {
  4662. // eslint-disable-next-line @typescript-eslint/no-this-alias
  4663. var collection = this;
  4664. if (collection._cache) {
  4665. // We cache as an entries array, so we can just return the cache!
  4666. return new ArraySeq(collection._cache);
  4667. }
  4668. var entriesSequence = collection.toSeq().map(entryMapper).toIndexedSeq();
  4669. entriesSequence.fromEntrySeq = function () { return collection.toSeq(); };
  4670. return entriesSequence;
  4671. },
  4672. filterNot: function filterNot(predicate, context) {
  4673. return this.filter(not(predicate), context);
  4674. },
  4675. findEntry: function findEntry(predicate, context, notSetValue) {
  4676. var found = notSetValue;
  4677. this.__iterate(function (v, k, c) {
  4678. if (predicate.call(context, v, k, c)) {
  4679. found = [k, v];
  4680. return false;
  4681. }
  4682. });
  4683. return found;
  4684. },
  4685. findKey: function findKey(predicate, context) {
  4686. var entry = this.findEntry(predicate, context);
  4687. return entry && entry[0];
  4688. },
  4689. findLast: function findLast(predicate, context, notSetValue) {
  4690. return this.toKeyedSeq().reverse().find(predicate, context, notSetValue);
  4691. },
  4692. findLastEntry: function findLastEntry(predicate, context, notSetValue) {
  4693. return this.toKeyedSeq()
  4694. .reverse()
  4695. .findEntry(predicate, context, notSetValue);
  4696. },
  4697. findLastKey: function findLastKey(predicate, context) {
  4698. return this.toKeyedSeq().reverse().findKey(predicate, context);
  4699. },
  4700. first: function first(notSetValue) {
  4701. return this.find(returnTrue, null, notSetValue);
  4702. },
  4703. flatMap: function flatMap(mapper, context) {
  4704. return reify(this, flatMapFactory(this, mapper, context));
  4705. },
  4706. flatten: function flatten(depth) {
  4707. return reify(this, flattenFactory(this, depth, true));
  4708. },
  4709. fromEntrySeq: function fromEntrySeq() {
  4710. return new FromEntriesSequence(this);
  4711. },
  4712. get: function get(searchKey, notSetValue) {
  4713. return this.find(function (_, key) { return is(key, searchKey); }, undefined, notSetValue);
  4714. },
  4715. getIn: getIn,
  4716. groupBy: function groupBy(grouper, context) {
  4717. return groupByFactory(this, grouper, context);
  4718. },
  4719. has: function has(searchKey) {
  4720. return this.get(searchKey, NOT_SET) !== NOT_SET;
  4721. },
  4722. hasIn: hasIn,
  4723. isSubset: function isSubset(iter) {
  4724. iter = typeof iter.includes === 'function' ? iter : Collection(iter);
  4725. return this.every(function (value) { return iter.includes(value); });
  4726. },
  4727. isSuperset: function isSuperset(iter) {
  4728. iter = typeof iter.isSubset === 'function' ? iter : Collection(iter);
  4729. return iter.isSubset(this);
  4730. },
  4731. keyOf: function keyOf(searchValue) {
  4732. return this.findKey(function (value) { return is(value, searchValue); });
  4733. },
  4734. keySeq: function keySeq() {
  4735. return this.toSeq().map(keyMapper).toIndexedSeq();
  4736. },
  4737. last: function last(notSetValue) {
  4738. return this.toSeq().reverse().first(notSetValue);
  4739. },
  4740. lastKeyOf: function lastKeyOf(searchValue) {
  4741. return this.toKeyedSeq().reverse().keyOf(searchValue);
  4742. },
  4743. max: function max(comparator) {
  4744. return maxFactory(this, comparator);
  4745. },
  4746. maxBy: function maxBy(mapper, comparator) {
  4747. return maxFactory(this, comparator, mapper);
  4748. },
  4749. min: function min(comparator) {
  4750. return maxFactory(
  4751. this,
  4752. comparator ? neg(comparator) : defaultNegComparator
  4753. );
  4754. },
  4755. minBy: function minBy(mapper, comparator) {
  4756. return maxFactory(
  4757. this,
  4758. comparator ? neg(comparator) : defaultNegComparator,
  4759. mapper
  4760. );
  4761. },
  4762. rest: function rest() {
  4763. return this.slice(1);
  4764. },
  4765. skip: function skip(amount) {
  4766. return amount === 0 ? this : this.slice(Math.max(0, amount));
  4767. },
  4768. skipLast: function skipLast(amount) {
  4769. return amount === 0 ? this : this.slice(0, -Math.max(0, amount));
  4770. },
  4771. skipWhile: function skipWhile(predicate, context) {
  4772. return reify(this, skipWhileFactory(this, predicate, context, true));
  4773. },
  4774. skipUntil: function skipUntil(predicate, context) {
  4775. return this.skipWhile(not(predicate), context);
  4776. },
  4777. sortBy: function sortBy(mapper, comparator) {
  4778. return reify(this, sortFactory(this, comparator, mapper));
  4779. },
  4780. take: function take(amount) {
  4781. return this.slice(0, Math.max(0, amount));
  4782. },
  4783. takeLast: function takeLast(amount) {
  4784. return this.slice(-Math.max(0, amount));
  4785. },
  4786. takeWhile: function takeWhile(predicate, context) {
  4787. return reify(this, takeWhileFactory(this, predicate, context));
  4788. },
  4789. takeUntil: function takeUntil(predicate, context) {
  4790. return this.takeWhile(not(predicate), context);
  4791. },
  4792. update: function update(fn) {
  4793. return fn(this);
  4794. },
  4795. valueSeq: function valueSeq() {
  4796. return this.toIndexedSeq();
  4797. },
  4798. // ### Hashable Object
  4799. hashCode: function hashCode() {
  4800. return this.__hash || (this.__hash = hashCollection(this));
  4801. },
  4802. // ### Internal
  4803. // abstract __iterate(fn, reverse)
  4804. // abstract __iterator(type, reverse)
  4805. });
  4806. var CollectionPrototype = Collection.prototype;
  4807. CollectionPrototype[IS_COLLECTION_SYMBOL] = true;
  4808. CollectionPrototype[ITERATOR_SYMBOL] = CollectionPrototype.values;
  4809. CollectionPrototype.toJSON = CollectionPrototype.toArray;
  4810. CollectionPrototype.__toStringMapper = quoteString;
  4811. CollectionPrototype.inspect = CollectionPrototype.toSource = function () {
  4812. return this.toString();
  4813. };
  4814. CollectionPrototype.chain = CollectionPrototype.flatMap;
  4815. CollectionPrototype.contains = CollectionPrototype.includes;
  4816. mixin(KeyedCollection, {
  4817. // ### More sequential methods
  4818. flip: function flip() {
  4819. return reify(this, flipFactory(this));
  4820. },
  4821. mapEntries: function mapEntries(mapper, context) {
  4822. var this$1$1 = this;
  4823. var iterations = 0;
  4824. return reify(
  4825. this,
  4826. this.toSeq()
  4827. .map(function (v, k) { return mapper.call(context, [k, v], iterations++, this$1$1); })
  4828. .fromEntrySeq()
  4829. );
  4830. },
  4831. mapKeys: function mapKeys(mapper, context) {
  4832. var this$1$1 = this;
  4833. return reify(
  4834. this,
  4835. this.toSeq()
  4836. .flip()
  4837. .map(function (k, v) { return mapper.call(context, k, v, this$1$1); })
  4838. .flip()
  4839. );
  4840. },
  4841. });
  4842. var KeyedCollectionPrototype = KeyedCollection.prototype;
  4843. KeyedCollectionPrototype[IS_KEYED_SYMBOL] = true;
  4844. KeyedCollectionPrototype[ITERATOR_SYMBOL] = CollectionPrototype.entries;
  4845. KeyedCollectionPrototype.toJSON = toObject;
  4846. KeyedCollectionPrototype.__toStringMapper = function (v, k) { return quoteString(k) + ': ' + quoteString(v); };
  4847. mixin(IndexedCollection, {
  4848. // ### Conversion to other types
  4849. toKeyedSeq: function toKeyedSeq() {
  4850. return new ToKeyedSequence(this, false);
  4851. },
  4852. // ### ES6 Collection methods (ES6 Array and Map)
  4853. filter: function filter(predicate, context) {
  4854. return reify(this, filterFactory(this, predicate, context, false));
  4855. },
  4856. findIndex: function findIndex(predicate, context) {
  4857. var entry = this.findEntry(predicate, context);
  4858. return entry ? entry[0] : -1;
  4859. },
  4860. indexOf: function indexOf(searchValue) {
  4861. var key = this.keyOf(searchValue);
  4862. return key === undefined ? -1 : key;
  4863. },
  4864. lastIndexOf: function lastIndexOf(searchValue) {
  4865. var key = this.lastKeyOf(searchValue);
  4866. return key === undefined ? -1 : key;
  4867. },
  4868. reverse: function reverse() {
  4869. return reify(this, reverseFactory(this, false));
  4870. },
  4871. slice: function slice(begin, end) {
  4872. return reify(this, sliceFactory(this, begin, end, false));
  4873. },
  4874. splice: function splice(index, removeNum /*, ...values*/) {
  4875. var numArgs = arguments.length;
  4876. removeNum = Math.max(removeNum || 0, 0);
  4877. if (numArgs === 0 || (numArgs === 2 && !removeNum)) {
  4878. return this;
  4879. }
  4880. // If index is negative, it should resolve relative to the size of the
  4881. // collection. However size may be expensive to compute if not cached, so
  4882. // only call count() if the number is in fact negative.
  4883. index = resolveBegin(index, index < 0 ? this.count() : this.size);
  4884. var spliced = this.slice(0, index);
  4885. return reify(
  4886. this,
  4887. numArgs === 1
  4888. ? spliced
  4889. : spliced.concat(arrCopy(arguments, 2), this.slice(index + removeNum))
  4890. );
  4891. },
  4892. // ### More collection methods
  4893. findLastIndex: function findLastIndex(predicate, context) {
  4894. var entry = this.findLastEntry(predicate, context);
  4895. return entry ? entry[0] : -1;
  4896. },
  4897. first: function first(notSetValue) {
  4898. return this.get(0, notSetValue);
  4899. },
  4900. flatten: function flatten(depth) {
  4901. return reify(this, flattenFactory(this, depth, false));
  4902. },
  4903. get: function get(index, notSetValue) {
  4904. index = wrapIndex(this, index);
  4905. return index < 0 ||
  4906. this.size === Infinity ||
  4907. (this.size !== undefined && index > this.size)
  4908. ? notSetValue
  4909. : this.find(function (_, key) { return key === index; }, undefined, notSetValue);
  4910. },
  4911. has: function has(index) {
  4912. index = wrapIndex(this, index);
  4913. return (
  4914. index >= 0 &&
  4915. (this.size !== undefined
  4916. ? this.size === Infinity || index < this.size
  4917. : this.indexOf(index) !== -1)
  4918. );
  4919. },
  4920. interpose: function interpose(separator) {
  4921. return reify(this, interposeFactory(this, separator));
  4922. },
  4923. interleave: function interleave(/*...collections*/) {
  4924. var collections = [this].concat(arrCopy(arguments));
  4925. var zipped = zipWithFactory(this.toSeq(), IndexedSeq.of, collections);
  4926. var interleaved = zipped.flatten(true);
  4927. if (zipped.size) {
  4928. interleaved.size = zipped.size * collections.length;
  4929. }
  4930. return reify(this, interleaved);
  4931. },
  4932. keySeq: function keySeq() {
  4933. return Range(0, this.size);
  4934. },
  4935. last: function last(notSetValue) {
  4936. return this.get(-1, notSetValue);
  4937. },
  4938. skipWhile: function skipWhile(predicate, context) {
  4939. return reify(this, skipWhileFactory(this, predicate, context, false));
  4940. },
  4941. zip: function zip(/*, ...collections */) {
  4942. var collections = [this].concat(arrCopy(arguments));
  4943. return reify(this, zipWithFactory(this, defaultZipper, collections));
  4944. },
  4945. zipAll: function zipAll(/*, ...collections */) {
  4946. var collections = [this].concat(arrCopy(arguments));
  4947. return reify(this, zipWithFactory(this, defaultZipper, collections, true));
  4948. },
  4949. zipWith: function zipWith(zipper /*, ...collections */) {
  4950. var collections = arrCopy(arguments);
  4951. collections[0] = this;
  4952. return reify(this, zipWithFactory(this, zipper, collections));
  4953. },
  4954. });
  4955. var IndexedCollectionPrototype = IndexedCollection.prototype;
  4956. IndexedCollectionPrototype[IS_INDEXED_SYMBOL] = true;
  4957. IndexedCollectionPrototype[IS_ORDERED_SYMBOL] = true;
  4958. mixin(SetCollection, {
  4959. // ### ES6 Collection methods (ES6 Array and Map)
  4960. get: function get(value, notSetValue) {
  4961. return this.has(value) ? value : notSetValue;
  4962. },
  4963. includes: function includes(value) {
  4964. return this.has(value);
  4965. },
  4966. // ### More sequential methods
  4967. keySeq: function keySeq() {
  4968. return this.valueSeq();
  4969. },
  4970. });
  4971. var SetCollectionPrototype = SetCollection.prototype;
  4972. SetCollectionPrototype.has = CollectionPrototype.includes;
  4973. SetCollectionPrototype.contains = SetCollectionPrototype.includes;
  4974. SetCollectionPrototype.keys = SetCollectionPrototype.values;
  4975. // Mixin subclasses
  4976. mixin(KeyedSeq, KeyedCollectionPrototype);
  4977. mixin(IndexedSeq, IndexedCollectionPrototype);
  4978. mixin(SetSeq, SetCollectionPrototype);
  4979. // #pragma Helper functions
  4980. function reduce(collection, reducer, reduction, context, useFirst, reverse) {
  4981. assertNotInfinite(collection.size);
  4982. collection.__iterate(function (v, k, c) {
  4983. if (useFirst) {
  4984. useFirst = false;
  4985. reduction = v;
  4986. } else {
  4987. reduction = reducer.call(context, reduction, v, k, c);
  4988. }
  4989. }, reverse);
  4990. return reduction;
  4991. }
  4992. function keyMapper(v, k) {
  4993. return k;
  4994. }
  4995. function entryMapper(v, k) {
  4996. return [k, v];
  4997. }
  4998. function not(predicate) {
  4999. return function () {
  5000. return !predicate.apply(this, arguments);
  5001. };
  5002. }
  5003. function neg(predicate) {
  5004. return function () {
  5005. return -predicate.apply(this, arguments);
  5006. };
  5007. }
  5008. function defaultZipper() {
  5009. return arrCopy(arguments);
  5010. }
  5011. function defaultNegComparator(a, b) {
  5012. return a < b ? 1 : a > b ? -1 : 0;
  5013. }
  5014. function hashCollection(collection) {
  5015. if (collection.size === Infinity) {
  5016. return 0;
  5017. }
  5018. var ordered = isOrdered(collection);
  5019. var keyed = isKeyed(collection);
  5020. var h = ordered ? 1 : 0;
  5021. collection.__iterate(
  5022. keyed
  5023. ? ordered
  5024. ? function (v, k) {
  5025. h = (31 * h + hashMerge(hash(v), hash(k))) | 0;
  5026. }
  5027. : function (v, k) {
  5028. h = (h + hashMerge(hash(v), hash(k))) | 0;
  5029. }
  5030. : ordered
  5031. ? function (v) {
  5032. h = (31 * h + hash(v)) | 0;
  5033. }
  5034. : function (v) {
  5035. h = (h + hash(v)) | 0;
  5036. }
  5037. );
  5038. return murmurHashOfSize(collection.size, h);
  5039. }
  5040. function murmurHashOfSize(size, h) {
  5041. h = imul(h, 0xcc9e2d51);
  5042. h = imul((h << 15) | (h >>> -15), 0x1b873593);
  5043. h = imul((h << 13) | (h >>> -13), 5);
  5044. h = ((h + 0xe6546b64) | 0) ^ size;
  5045. h = imul(h ^ (h >>> 16), 0x85ebca6b);
  5046. h = imul(h ^ (h >>> 13), 0xc2b2ae35);
  5047. h = smi(h ^ (h >>> 16));
  5048. return h;
  5049. }
  5050. function hashMerge(a, b) {
  5051. return (a ^ (b + 0x9e3779b9 + (a << 6) + (a >> 2))) | 0; // int
  5052. }
  5053. var OrderedSet = /*@__PURE__*/(function (Set) {
  5054. function OrderedSet(value) {
  5055. // eslint-disable-next-line no-constructor-return
  5056. return value === undefined || value === null
  5057. ? emptyOrderedSet()
  5058. : isOrderedSet(value)
  5059. ? value
  5060. : emptyOrderedSet().withMutations(function (set) {
  5061. var iter = SetCollection(value);
  5062. assertNotInfinite(iter.size);
  5063. iter.forEach(function (v) { return set.add(v); });
  5064. });
  5065. }
  5066. if ( Set ) OrderedSet.__proto__ = Set;
  5067. OrderedSet.prototype = Object.create( Set && Set.prototype );
  5068. OrderedSet.prototype.constructor = OrderedSet;
  5069. OrderedSet.of = function of (/*...values*/) {
  5070. return this(arguments);
  5071. };
  5072. OrderedSet.fromKeys = function fromKeys (value) {
  5073. return this(KeyedCollection(value).keySeq());
  5074. };
  5075. OrderedSet.prototype.toString = function toString () {
  5076. return this.__toString('OrderedSet {', '}');
  5077. };
  5078. return OrderedSet;
  5079. }(Set));
  5080. OrderedSet.isOrderedSet = isOrderedSet;
  5081. var OrderedSetPrototype = OrderedSet.prototype;
  5082. OrderedSetPrototype[IS_ORDERED_SYMBOL] = true;
  5083. OrderedSetPrototype.zip = IndexedCollectionPrototype.zip;
  5084. OrderedSetPrototype.zipWith = IndexedCollectionPrototype.zipWith;
  5085. OrderedSetPrototype.zipAll = IndexedCollectionPrototype.zipAll;
  5086. OrderedSetPrototype.__empty = emptyOrderedSet;
  5087. OrderedSetPrototype.__make = makeOrderedSet;
  5088. function makeOrderedSet(map, ownerID) {
  5089. var set = Object.create(OrderedSetPrototype);
  5090. set.size = map ? map.size : 0;
  5091. set._map = map;
  5092. set.__ownerID = ownerID;
  5093. return set;
  5094. }
  5095. var EMPTY_ORDERED_SET;
  5096. function emptyOrderedSet() {
  5097. return (
  5098. EMPTY_ORDERED_SET || (EMPTY_ORDERED_SET = makeOrderedSet(emptyOrderedMap()))
  5099. );
  5100. }
  5101. var PairSorting = {
  5102. LeftThenRight: -1,
  5103. RightThenLeft: 1,
  5104. };
  5105. function throwOnInvalidDefaultValues(defaultValues) {
  5106. if (isRecord(defaultValues)) {
  5107. throw new Error(
  5108. 'Can not call `Record` with an immutable Record as default values. Use a plain javascript object instead.'
  5109. );
  5110. }
  5111. if (isImmutable(defaultValues)) {
  5112. throw new Error(
  5113. 'Can not call `Record` with an immutable Collection as default values. Use a plain javascript object instead.'
  5114. );
  5115. }
  5116. if (defaultValues === null || typeof defaultValues !== 'object') {
  5117. throw new Error(
  5118. 'Can not call `Record` with a non-object as default values. Use a plain javascript object instead.'
  5119. );
  5120. }
  5121. }
  5122. var Record = function Record(defaultValues, name) {
  5123. var hasInitialized;
  5124. throwOnInvalidDefaultValues(defaultValues);
  5125. var RecordType = function Record(values) {
  5126. var this$1$1 = this;
  5127. if (values instanceof RecordType) {
  5128. return values;
  5129. }
  5130. if (!(this instanceof RecordType)) {
  5131. return new RecordType(values);
  5132. }
  5133. if (!hasInitialized) {
  5134. hasInitialized = true;
  5135. var keys = Object.keys(defaultValues);
  5136. var indices = (RecordTypePrototype._indices = {});
  5137. // Deprecated: left to attempt not to break any external code which
  5138. // relies on a ._name property existing on record instances.
  5139. // Use Record.getDescriptiveName() instead
  5140. RecordTypePrototype._name = name;
  5141. RecordTypePrototype._keys = keys;
  5142. RecordTypePrototype._defaultValues = defaultValues;
  5143. for (var i = 0; i < keys.length; i++) {
  5144. var propName = keys[i];
  5145. indices[propName] = i;
  5146. if (RecordTypePrototype[propName]) {
  5147. /* eslint-disable no-console */
  5148. // eslint-disable-next-line @typescript-eslint/no-unused-expressions -- TODO enable eslint here
  5149. typeof console === 'object' &&
  5150. console.warn &&
  5151. console.warn(
  5152. 'Cannot define ' +
  5153. recordName(this) +
  5154. ' with property "' +
  5155. propName +
  5156. '" since that property name is part of the Record API.'
  5157. );
  5158. /* eslint-enable no-console */
  5159. } else {
  5160. setProp(RecordTypePrototype, propName);
  5161. }
  5162. }
  5163. }
  5164. this.__ownerID = undefined;
  5165. this._values = List().withMutations(function (l) {
  5166. l.setSize(this$1$1._keys.length);
  5167. KeyedCollection(values).forEach(function (v, k) {
  5168. l.set(this$1$1._indices[k], v === this$1$1._defaultValues[k] ? undefined : v);
  5169. });
  5170. });
  5171. return this;
  5172. };
  5173. var RecordTypePrototype = (RecordType.prototype =
  5174. Object.create(RecordPrototype));
  5175. RecordTypePrototype.constructor = RecordType;
  5176. if (name) {
  5177. RecordType.displayName = name;
  5178. }
  5179. // eslint-disable-next-line no-constructor-return
  5180. return RecordType;
  5181. };
  5182. Record.prototype.toString = function toString () {
  5183. var str = recordName(this) + ' { ';
  5184. var keys = this._keys;
  5185. var k;
  5186. for (var i = 0, l = keys.length; i !== l; i++) {
  5187. k = keys[i];
  5188. str += (i ? ', ' : '') + k + ': ' + quoteString(this.get(k));
  5189. }
  5190. return str + ' }';
  5191. };
  5192. Record.prototype.equals = function equals (other) {
  5193. return (
  5194. this === other ||
  5195. (isRecord(other) && recordSeq(this).equals(recordSeq(other)))
  5196. );
  5197. };
  5198. Record.prototype.hashCode = function hashCode () {
  5199. return recordSeq(this).hashCode();
  5200. };
  5201. // @pragma Access
  5202. Record.prototype.has = function has (k) {
  5203. return this._indices.hasOwnProperty(k);
  5204. };
  5205. Record.prototype.get = function get (k, notSetValue) {
  5206. if (!this.has(k)) {
  5207. return notSetValue;
  5208. }
  5209. var index = this._indices[k];
  5210. var value = this._values.get(index);
  5211. return value === undefined ? this._defaultValues[k] : value;
  5212. };
  5213. // @pragma Modification
  5214. Record.prototype.set = function set (k, v) {
  5215. if (this.has(k)) {
  5216. var newValues = this._values.set(
  5217. this._indices[k],
  5218. v === this._defaultValues[k] ? undefined : v
  5219. );
  5220. if (newValues !== this._values && !this.__ownerID) {
  5221. return makeRecord(this, newValues);
  5222. }
  5223. }
  5224. return this;
  5225. };
  5226. Record.prototype.remove = function remove (k) {
  5227. return this.set(k);
  5228. };
  5229. Record.prototype.clear = function clear () {
  5230. var newValues = this._values.clear().setSize(this._keys.length);
  5231. return this.__ownerID ? this : makeRecord(this, newValues);
  5232. };
  5233. Record.prototype.wasAltered = function wasAltered () {
  5234. return this._values.wasAltered();
  5235. };
  5236. Record.prototype.toSeq = function toSeq () {
  5237. return recordSeq(this);
  5238. };
  5239. Record.prototype.toJS = function toJS$1 () {
  5240. return toJS(this);
  5241. };
  5242. Record.prototype.entries = function entries () {
  5243. return this.__iterator(ITERATE_ENTRIES);
  5244. };
  5245. Record.prototype.__iterator = function __iterator (type, reverse) {
  5246. return recordSeq(this).__iterator(type, reverse);
  5247. };
  5248. Record.prototype.__iterate = function __iterate (fn, reverse) {
  5249. return recordSeq(this).__iterate(fn, reverse);
  5250. };
  5251. Record.prototype.__ensureOwner = function __ensureOwner (ownerID) {
  5252. if (ownerID === this.__ownerID) {
  5253. return this;
  5254. }
  5255. var newValues = this._values.__ensureOwner(ownerID);
  5256. if (!ownerID) {
  5257. this.__ownerID = ownerID;
  5258. this._values = newValues;
  5259. return this;
  5260. }
  5261. return makeRecord(this, newValues, ownerID);
  5262. };
  5263. Record.isRecord = isRecord;
  5264. Record.getDescriptiveName = recordName;
  5265. var RecordPrototype = Record.prototype;
  5266. RecordPrototype[IS_RECORD_SYMBOL] = true;
  5267. RecordPrototype[DELETE] = RecordPrototype.remove;
  5268. RecordPrototype.deleteIn = RecordPrototype.removeIn = deleteIn;
  5269. RecordPrototype.getIn = getIn;
  5270. RecordPrototype.hasIn = CollectionPrototype.hasIn;
  5271. RecordPrototype.merge = merge$1;
  5272. RecordPrototype.mergeWith = mergeWith$1;
  5273. RecordPrototype.mergeIn = mergeIn;
  5274. RecordPrototype.mergeDeep = mergeDeep;
  5275. RecordPrototype.mergeDeepWith = mergeDeepWith;
  5276. RecordPrototype.mergeDeepIn = mergeDeepIn;
  5277. RecordPrototype.setIn = setIn;
  5278. RecordPrototype.update = update;
  5279. RecordPrototype.updateIn = updateIn;
  5280. RecordPrototype.withMutations = withMutations;
  5281. RecordPrototype.asMutable = asMutable;
  5282. RecordPrototype.asImmutable = asImmutable;
  5283. RecordPrototype[ITERATOR_SYMBOL] = RecordPrototype.entries;
  5284. RecordPrototype.toJSON = RecordPrototype.toObject =
  5285. CollectionPrototype.toObject;
  5286. RecordPrototype.inspect = RecordPrototype.toSource = function () {
  5287. return this.toString();
  5288. };
  5289. function makeRecord(likeRecord, values, ownerID) {
  5290. var record = Object.create(Object.getPrototypeOf(likeRecord));
  5291. record._values = values;
  5292. record.__ownerID = ownerID;
  5293. return record;
  5294. }
  5295. function recordName(record) {
  5296. return record.constructor.displayName || record.constructor.name || 'Record';
  5297. }
  5298. function recordSeq(record) {
  5299. return keyedSeqFromValue(record._keys.map(function (k) { return [k, record.get(k)]; }));
  5300. }
  5301. function setProp(prototype, name) {
  5302. try {
  5303. Object.defineProperty(prototype, name, {
  5304. get: function () {
  5305. return this.get(name);
  5306. },
  5307. set: function (value) {
  5308. invariant(this.__ownerID, 'Cannot set on an immutable record.');
  5309. this.set(name, value);
  5310. },
  5311. });
  5312. // eslint-disable-next-line @typescript-eslint/no-unused-vars -- TODO enable eslint here
  5313. } catch (error) {
  5314. // Object.defineProperty failed. Probably IE8.
  5315. }
  5316. }
  5317. /**
  5318. * Returns a lazy Seq of `value` repeated `times` times. When `times` is
  5319. * undefined, returns an infinite sequence of `value`.
  5320. */
  5321. var Repeat = /*@__PURE__*/(function (IndexedSeq) {
  5322. function Repeat(value, times) {
  5323. if (!(this instanceof Repeat)) {
  5324. // eslint-disable-next-line no-constructor-return
  5325. return new Repeat(value, times);
  5326. }
  5327. this._value = value;
  5328. this.size = times === undefined ? Infinity : Math.max(0, times);
  5329. if (this.size === 0) {
  5330. if (EMPTY_REPEAT) {
  5331. // eslint-disable-next-line no-constructor-return
  5332. return EMPTY_REPEAT;
  5333. }
  5334. // eslint-disable-next-line @typescript-eslint/no-this-alias
  5335. EMPTY_REPEAT = this;
  5336. }
  5337. }
  5338. if ( IndexedSeq ) Repeat.__proto__ = IndexedSeq;
  5339. Repeat.prototype = Object.create( IndexedSeq && IndexedSeq.prototype );
  5340. Repeat.prototype.constructor = Repeat;
  5341. Repeat.prototype.toString = function toString () {
  5342. if (this.size === 0) {
  5343. return 'Repeat []';
  5344. }
  5345. return 'Repeat [ ' + this._value + ' ' + this.size + ' times ]';
  5346. };
  5347. Repeat.prototype.get = function get (index, notSetValue) {
  5348. return this.has(index) ? this._value : notSetValue;
  5349. };
  5350. Repeat.prototype.includes = function includes (searchValue) {
  5351. return is(this._value, searchValue);
  5352. };
  5353. Repeat.prototype.slice = function slice (begin, end) {
  5354. var size = this.size;
  5355. return wholeSlice(begin, end, size)
  5356. ? this
  5357. : new Repeat(
  5358. this._value,
  5359. resolveEnd(end, size) - resolveBegin(begin, size)
  5360. );
  5361. };
  5362. Repeat.prototype.reverse = function reverse () {
  5363. return this;
  5364. };
  5365. Repeat.prototype.indexOf = function indexOf (searchValue) {
  5366. if (is(this._value, searchValue)) {
  5367. return 0;
  5368. }
  5369. return -1;
  5370. };
  5371. Repeat.prototype.lastIndexOf = function lastIndexOf (searchValue) {
  5372. if (is(this._value, searchValue)) {
  5373. return this.size;
  5374. }
  5375. return -1;
  5376. };
  5377. Repeat.prototype.__iterate = function __iterate (fn, reverse) {
  5378. var size = this.size;
  5379. var i = 0;
  5380. while (i !== size) {
  5381. if (fn(this._value, reverse ? size - ++i : i++, this) === false) {
  5382. break;
  5383. }
  5384. }
  5385. return i;
  5386. };
  5387. Repeat.prototype.__iterator = function __iterator (type, reverse) {
  5388. var this$1$1 = this;
  5389. var size = this.size;
  5390. var i = 0;
  5391. return new Iterator(function () { return i === size
  5392. ? iteratorDone()
  5393. : iteratorValue(type, reverse ? size - ++i : i++, this$1$1._value); }
  5394. );
  5395. };
  5396. Repeat.prototype.equals = function equals (other) {
  5397. return other instanceof Repeat
  5398. ? is(this._value, other._value)
  5399. : deepEqual(this, other);
  5400. };
  5401. return Repeat;
  5402. }(IndexedSeq));
  5403. var EMPTY_REPEAT;
  5404. function fromJS(value, converter) {
  5405. return fromJSWith(
  5406. [],
  5407. converter || defaultConverter,
  5408. value,
  5409. '',
  5410. converter && converter.length > 2 ? [] : undefined,
  5411. { '': value }
  5412. );
  5413. }
  5414. function fromJSWith(stack, converter, value, key, keyPath, parentValue) {
  5415. if (
  5416. typeof value !== 'string' &&
  5417. !isImmutable(value) &&
  5418. (isArrayLike(value) || hasIterator(value) || isPlainObject(value))
  5419. ) {
  5420. if (~stack.indexOf(value)) {
  5421. throw new TypeError('Cannot convert circular structure to Immutable');
  5422. }
  5423. stack.push(value);
  5424. // eslint-disable-next-line @typescript-eslint/no-unused-expressions -- TODO enable eslint here
  5425. keyPath && key !== '' && keyPath.push(key);
  5426. var converted = converter.call(
  5427. parentValue,
  5428. key,
  5429. Seq(value).map(function (v, k) { return fromJSWith(stack, converter, v, k, keyPath, value); }
  5430. ),
  5431. keyPath && keyPath.slice()
  5432. );
  5433. stack.pop();
  5434. // eslint-disable-next-line @typescript-eslint/no-unused-expressions -- TODO enable eslint here
  5435. keyPath && keyPath.pop();
  5436. return converted;
  5437. }
  5438. return value;
  5439. }
  5440. function defaultConverter(k, v) {
  5441. // Effectively the opposite of "Collection.toSeq()"
  5442. return isIndexed(v) ? v.toList() : isKeyed(v) ? v.toMap() : v.toSet();
  5443. }
  5444. var version = "5.1.1";
  5445. // Note: Iterable is deprecated
  5446. var Iterable = Collection;
  5447. export { Collection, Iterable, List, Map, OrderedMap, OrderedSet, PairSorting, Range, Record, Repeat, Seq, Set, Stack, fromJS, get, getIn$1 as getIn, has, hasIn$1 as hasIn, hash, is, isAssociative, isCollection, isImmutable, isIndexed, isKeyed, isList, isMap, isOrdered, isOrderedMap, isOrderedSet, isPlainObject, isRecord, isSeq, isSet, isStack, isValueObject, merge, mergeDeep$1 as mergeDeep, mergeDeepWith$1 as mergeDeepWith, mergeWith, remove, removeIn, set, setIn$1 as setIn, update$1 as update, updateIn$1 as updateIn, version };