immutable.js 195 KB

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