immutable.js 193 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236
  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. function has(collection, key) {
  1979. return isImmutable(collection)
  1980. ? // @ts-expect-error key might be a number or symbol, which is not handled be Record key type
  1981. collection.has(key)
  1982. : // @ts-expect-error key might be anything else than PropertyKey, and will return false in that case but runtime is OK
  1983. isDataStructure(collection) && hasOwnProperty.call(collection, key);
  1984. }
  1985. function get(collection, key, notSetValue) {
  1986. return isImmutable(collection)
  1987. ? collection.get(key, notSetValue)
  1988. : !has(collection, key)
  1989. ? notSetValue
  1990. : // @ts-expect-error weird "get" here,
  1991. typeof collection.get === 'function'
  1992. ? // @ts-expect-error weird "get" here,
  1993. collection.get(key)
  1994. : // @ts-expect-error key is unknown here,
  1995. collection[key];
  1996. }
  1997. function shallowCopy(from) {
  1998. if (Array.isArray(from)) {
  1999. return arrCopy(from);
  2000. }
  2001. var to = {};
  2002. for (var key in from) {
  2003. if (hasOwnProperty.call(from, key)) {
  2004. to[key] = from[key];
  2005. }
  2006. }
  2007. return to;
  2008. }
  2009. function remove(collection, key) {
  2010. if (!isDataStructure(collection)) {
  2011. throw new TypeError('Cannot update non-data-structure value: ' + collection);
  2012. }
  2013. if (isImmutable(collection)) {
  2014. // @ts-expect-error weird "remove" here,
  2015. if (!collection.remove) {
  2016. throw new TypeError('Cannot update immutable value without .remove() method: ' + collection);
  2017. }
  2018. // @ts-expect-error weird "remove" here,
  2019. return collection.remove(key);
  2020. }
  2021. // @ts-expect-error assert that key is a string, a number or a symbol here
  2022. if (!hasOwnProperty.call(collection, key)) {
  2023. return collection;
  2024. }
  2025. var collectionCopy = shallowCopy(collection);
  2026. if (Array.isArray(collectionCopy)) {
  2027. // @ts-expect-error assert that key is a number here
  2028. collectionCopy.splice(key, 1);
  2029. }
  2030. else {
  2031. // @ts-expect-error assert that key is a string, a number or a symbol here
  2032. delete collectionCopy[key];
  2033. }
  2034. return collectionCopy;
  2035. }
  2036. function set(collection, key, value) {
  2037. if (!isDataStructure(collection)) {
  2038. throw new TypeError('Cannot update non-data-structure value: ' + collection);
  2039. }
  2040. if (isImmutable(collection)) {
  2041. // @ts-expect-error weird "set" here,
  2042. if (!collection.set) {
  2043. throw new TypeError('Cannot update immutable value without .set() method: ' + collection);
  2044. }
  2045. // @ts-expect-error weird "set" here,
  2046. return collection.set(key, value);
  2047. }
  2048. // @ts-expect-error mix of key and string here. Probably need a more fine type here
  2049. if (hasOwnProperty.call(collection, key) && value === collection[key]) {
  2050. return collection;
  2051. }
  2052. var collectionCopy = shallowCopy(collection);
  2053. // @ts-expect-error mix of key and string here. Probably need a more fine type here
  2054. collectionCopy[key] = value;
  2055. return collectionCopy;
  2056. }
  2057. function updateIn$1(collection, keyPath, notSetValue, updater) {
  2058. if (!updater) {
  2059. // handle the fact that `notSetValue` is optional here, in that case `updater` is the updater function
  2060. // @ts-expect-error updater is a function here
  2061. updater = notSetValue;
  2062. notSetValue = undefined;
  2063. }
  2064. var updatedValue = updateInDeeply(isImmutable(collection),
  2065. // @ts-expect-error type issues with Record and mixed types
  2066. collection, coerceKeyPath(keyPath), 0, notSetValue, updater);
  2067. // @ts-expect-error mixed return type
  2068. return updatedValue === NOT_SET ? notSetValue : updatedValue;
  2069. }
  2070. function updateInDeeply(inImmutable, existing, keyPath, i, notSetValue, updater) {
  2071. var wasNotSet = existing === NOT_SET;
  2072. if (i === keyPath.length) {
  2073. var existingValue = wasNotSet ? notSetValue : existing;
  2074. // @ts-expect-error mixed type with optional value
  2075. var newValue = updater(existingValue);
  2076. // @ts-expect-error mixed type
  2077. return newValue === existingValue ? existing : newValue;
  2078. }
  2079. if (!wasNotSet && !isDataStructure(existing)) {
  2080. throw new TypeError('Cannot update within non-data-structure value in path [' +
  2081. Array.from(keyPath).slice(0, i).map(quoteString) +
  2082. ']: ' +
  2083. existing);
  2084. }
  2085. var key = keyPath[i];
  2086. var nextExisting = wasNotSet ? NOT_SET : get(existing, key, NOT_SET);
  2087. var nextUpdated = updateInDeeply(nextExisting === NOT_SET ? inImmutable : isImmutable(nextExisting),
  2088. // @ts-expect-error mixed type
  2089. nextExisting, keyPath, i + 1, notSetValue, updater);
  2090. return nextUpdated === nextExisting
  2091. ? existing
  2092. : nextUpdated === NOT_SET
  2093. ? remove(existing, key)
  2094. : set(wasNotSet ? (inImmutable ? emptyMap() : {}) : existing, key, nextUpdated);
  2095. }
  2096. /**
  2097. * Returns a copy of the collection with the value at the key path set to the
  2098. * provided value.
  2099. *
  2100. * A functional alternative to `collection.setIn(keypath)` which will also
  2101. * work with plain Objects and Arrays.
  2102. */
  2103. function setIn$1(collection, keyPath, value) {
  2104. return updateIn$1(collection, keyPath, NOT_SET, function () { return value; });
  2105. }
  2106. function setIn(keyPath, v) {
  2107. return setIn$1(this, keyPath, v);
  2108. }
  2109. /**
  2110. * Returns a copy of the collection with the value at the key path removed.
  2111. *
  2112. * A functional alternative to `collection.removeIn(keypath)` which will also
  2113. * work with plain Objects and Arrays.
  2114. */
  2115. function removeIn(collection, keyPath) {
  2116. return updateIn$1(collection, keyPath, function () { return NOT_SET; });
  2117. }
  2118. function deleteIn(keyPath) {
  2119. return removeIn(this, keyPath);
  2120. }
  2121. function update$1(collection, key, notSetValue, updater) {
  2122. return updateIn$1(
  2123. // @ts-expect-error Index signature for type string is missing in type V[]
  2124. collection, [key], notSetValue, updater);
  2125. }
  2126. function update(key, notSetValue, updater) {
  2127. return arguments.length === 1
  2128. ? key(this)
  2129. : update$1(this, key, notSetValue, updater);
  2130. }
  2131. function updateIn(keyPath, notSetValue, updater) {
  2132. return updateIn$1(this, keyPath, notSetValue, updater);
  2133. }
  2134. function merge$1() {
  2135. var iters = [], len = arguments.length;
  2136. while ( len-- ) iters[ len ] = arguments[ len ];
  2137. return mergeIntoKeyedWith(this, iters);
  2138. }
  2139. function mergeWith$1(merger) {
  2140. var iters = [], len = arguments.length - 1;
  2141. while ( len-- > 0 ) iters[ len ] = arguments[ len + 1 ];
  2142. if (typeof merger !== 'function') {
  2143. throw new TypeError('Invalid merger function: ' + merger);
  2144. }
  2145. return mergeIntoKeyedWith(this, iters, merger);
  2146. }
  2147. function mergeIntoKeyedWith(collection, collections, merger) {
  2148. var iters = [];
  2149. for (var ii = 0; ii < collections.length; ii++) {
  2150. var collection$1 = KeyedCollection(collections[ii]);
  2151. if (collection$1.size !== 0) {
  2152. iters.push(collection$1);
  2153. }
  2154. }
  2155. if (iters.length === 0) {
  2156. return collection;
  2157. }
  2158. if (
  2159. collection.toSeq().size === 0 &&
  2160. !collection.__ownerID &&
  2161. iters.length === 1
  2162. ) {
  2163. return isRecord(collection)
  2164. ? collection // Record is empty and will not be updated: return the same instance
  2165. : collection.constructor(iters[0]);
  2166. }
  2167. return collection.withMutations(function (collection) {
  2168. var mergeIntoCollection = merger
  2169. ? function (value, key) {
  2170. update$1(collection, key, NOT_SET, function (oldVal) { return oldVal === NOT_SET ? value : merger(oldVal, value, key); }
  2171. );
  2172. }
  2173. : function (value, key) {
  2174. collection.set(key, value);
  2175. };
  2176. for (var ii = 0; ii < iters.length; ii++) {
  2177. iters[ii].forEach(mergeIntoCollection);
  2178. }
  2179. });
  2180. }
  2181. function merge(collection) {
  2182. var sources = [], len = arguments.length - 1;
  2183. while ( len-- > 0 ) sources[ len ] = arguments[ len + 1 ];
  2184. return mergeWithSources(collection, sources);
  2185. }
  2186. function mergeWith(merger, collection) {
  2187. var sources = [], len = arguments.length - 2;
  2188. while ( len-- > 0 ) sources[ len ] = arguments[ len + 2 ];
  2189. return mergeWithSources(collection, sources, merger);
  2190. }
  2191. function mergeDeep$1(collection) {
  2192. var sources = [], len = arguments.length - 1;
  2193. while ( len-- > 0 ) sources[ len ] = arguments[ len + 1 ];
  2194. return mergeDeepWithSources(collection, sources);
  2195. }
  2196. function mergeDeepWith$1(merger, collection) {
  2197. var sources = [], len = arguments.length - 2;
  2198. while ( len-- > 0 ) sources[ len ] = arguments[ len + 2 ];
  2199. return mergeDeepWithSources(collection, sources, merger);
  2200. }
  2201. function mergeDeepWithSources(collection, sources, merger) {
  2202. return mergeWithSources(collection, sources, deepMergerWith(merger));
  2203. }
  2204. function mergeWithSources(collection, sources, merger) {
  2205. if (!isDataStructure(collection)) {
  2206. throw new TypeError(
  2207. 'Cannot merge into non-data-structure value: ' + collection
  2208. );
  2209. }
  2210. if (isImmutable(collection)) {
  2211. return typeof merger === 'function' && collection.mergeWith
  2212. ? collection.mergeWith.apply(collection, [ merger ].concat( sources ))
  2213. : collection.merge
  2214. ? collection.merge.apply(collection, sources)
  2215. : collection.concat.apply(collection, sources);
  2216. }
  2217. var isArray = Array.isArray(collection);
  2218. var merged = collection;
  2219. var Collection = isArray ? IndexedCollection : KeyedCollection;
  2220. var mergeItem = isArray
  2221. ? function (value) {
  2222. // Copy on write
  2223. if (merged === collection) {
  2224. merged = shallowCopy(merged);
  2225. }
  2226. merged.push(value);
  2227. }
  2228. : function (value, key) {
  2229. var hasVal = hasOwnProperty.call(merged, key);
  2230. var nextVal =
  2231. hasVal && merger ? merger(merged[key], value, key) : value;
  2232. if (!hasVal || nextVal !== merged[key]) {
  2233. // Copy on write
  2234. if (merged === collection) {
  2235. merged = shallowCopy(merged);
  2236. }
  2237. merged[key] = nextVal;
  2238. }
  2239. };
  2240. for (var i = 0; i < sources.length; i++) {
  2241. Collection(sources[i]).forEach(mergeItem);
  2242. }
  2243. return merged;
  2244. }
  2245. function deepMergerWith(merger) {
  2246. function deepMerger(oldValue, newValue, key) {
  2247. return isDataStructure(oldValue) &&
  2248. isDataStructure(newValue) &&
  2249. areMergeable(oldValue, newValue)
  2250. ? mergeWithSources(oldValue, [newValue], deepMerger)
  2251. : merger
  2252. ? merger(oldValue, newValue, key)
  2253. : newValue;
  2254. }
  2255. return deepMerger;
  2256. }
  2257. /**
  2258. * It's unclear what the desired behavior is for merging two collections that
  2259. * fall into separate categories between keyed, indexed, or set-like, so we only
  2260. * consider them mergeable if they fall into the same category.
  2261. */
  2262. function areMergeable(oldDataStructure, newDataStructure) {
  2263. var oldSeq = Seq(oldDataStructure);
  2264. var newSeq = Seq(newDataStructure);
  2265. // This logic assumes that a sequence can only fall into one of the three
  2266. // categories mentioned above (since there's no `isSetLike()` method).
  2267. return (
  2268. isIndexed(oldSeq) === isIndexed(newSeq) &&
  2269. isKeyed(oldSeq) === isKeyed(newSeq)
  2270. );
  2271. }
  2272. function mergeDeep() {
  2273. var iters = [], len = arguments.length;
  2274. while ( len-- ) iters[ len ] = arguments[ len ];
  2275. return mergeDeepWithSources(this, iters);
  2276. }
  2277. function mergeDeepWith(merger) {
  2278. var iters = [], len = arguments.length - 1;
  2279. while ( len-- > 0 ) iters[ len ] = arguments[ len + 1 ];
  2280. return mergeDeepWithSources(this, iters, merger);
  2281. }
  2282. function mergeIn(keyPath) {
  2283. var iters = [], len = arguments.length - 1;
  2284. while ( len-- > 0 ) iters[ len ] = arguments[ len + 1 ];
  2285. return updateIn$1(this, keyPath, emptyMap(), function (m) { return mergeWithSources(m, iters); });
  2286. }
  2287. function mergeDeepIn(keyPath) {
  2288. var iters = [], len = arguments.length - 1;
  2289. while ( len-- > 0 ) iters[ len ] = arguments[ len + 1 ];
  2290. return updateIn$1(this, keyPath, emptyMap(), function (m) { return mergeDeepWithSources(m, iters); }
  2291. );
  2292. }
  2293. function withMutations(fn) {
  2294. var mutable = this.asMutable();
  2295. fn(mutable);
  2296. return mutable.wasAltered() ? mutable.__ensureOwner(this.__ownerID) : this;
  2297. }
  2298. function asMutable() {
  2299. return this.__ownerID ? this : this.__ensureOwner(new OwnerID());
  2300. }
  2301. function asImmutable() {
  2302. return this.__ensureOwner();
  2303. }
  2304. function wasAltered() {
  2305. return this.__altered;
  2306. }
  2307. var Map = /*@__PURE__*/(function (KeyedCollection) {
  2308. function Map(value) {
  2309. // eslint-disable-next-line no-constructor-return
  2310. return value === undefined || value === null
  2311. ? emptyMap()
  2312. : isMap(value) && !isOrdered(value)
  2313. ? value
  2314. : emptyMap().withMutations(function (map) {
  2315. var iter = KeyedCollection(value);
  2316. assertNotInfinite(iter.size);
  2317. iter.forEach(function (v, k) { return map.set(k, v); });
  2318. });
  2319. }
  2320. if ( KeyedCollection ) Map.__proto__ = KeyedCollection;
  2321. Map.prototype = Object.create( KeyedCollection && KeyedCollection.prototype );
  2322. Map.prototype.constructor = Map;
  2323. Map.prototype.toString = function toString () {
  2324. return this.__toString('Map {', '}');
  2325. };
  2326. // @pragma Access
  2327. Map.prototype.get = function get (k, notSetValue) {
  2328. return this._root
  2329. ? this._root.get(0, undefined, k, notSetValue)
  2330. : notSetValue;
  2331. };
  2332. // @pragma Modification
  2333. Map.prototype.set = function set (k, v) {
  2334. return updateMap(this, k, v);
  2335. };
  2336. Map.prototype.remove = function remove (k) {
  2337. return updateMap(this, k, NOT_SET);
  2338. };
  2339. Map.prototype.deleteAll = function deleteAll (keys) {
  2340. var collection = Collection(keys);
  2341. if (collection.size === 0) {
  2342. return this;
  2343. }
  2344. return this.withMutations(function (map) {
  2345. collection.forEach(function (key) { return map.remove(key); });
  2346. });
  2347. };
  2348. Map.prototype.clear = function clear () {
  2349. if (this.size === 0) {
  2350. return this;
  2351. }
  2352. if (this.__ownerID) {
  2353. this.size = 0;
  2354. this._root = null;
  2355. this.__hash = undefined;
  2356. this.__altered = true;
  2357. return this;
  2358. }
  2359. return emptyMap();
  2360. };
  2361. // @pragma Composition
  2362. Map.prototype.sort = function sort (comparator) {
  2363. // Late binding
  2364. return OrderedMap(sortFactory(this, comparator));
  2365. };
  2366. Map.prototype.sortBy = function sortBy (mapper, comparator) {
  2367. // Late binding
  2368. return OrderedMap(sortFactory(this, comparator, mapper));
  2369. };
  2370. Map.prototype.map = function map (mapper, context) {
  2371. var this$1$1 = this;
  2372. return this.withMutations(function (map) {
  2373. map.forEach(function (value, key) {
  2374. map.set(key, mapper.call(context, value, key, this$1$1));
  2375. });
  2376. });
  2377. };
  2378. // @pragma Mutability
  2379. Map.prototype.__iterator = function __iterator (type, reverse) {
  2380. return new MapIterator(this, type, reverse);
  2381. };
  2382. Map.prototype.__iterate = function __iterate (fn, reverse) {
  2383. var this$1$1 = this;
  2384. var iterations = 0;
  2385. // eslint-disable-next-line @typescript-eslint/no-unused-expressions -- TODO enable eslint here
  2386. this._root &&
  2387. this._root.iterate(function (entry) {
  2388. iterations++;
  2389. return fn(entry[1], entry[0], this$1$1);
  2390. }, reverse);
  2391. return iterations;
  2392. };
  2393. Map.prototype.__ensureOwner = function __ensureOwner (ownerID) {
  2394. if (ownerID === this.__ownerID) {
  2395. return this;
  2396. }
  2397. if (!ownerID) {
  2398. if (this.size === 0) {
  2399. return emptyMap();
  2400. }
  2401. this.__ownerID = ownerID;
  2402. this.__altered = false;
  2403. return this;
  2404. }
  2405. return makeMap(this.size, this._root, ownerID, this.__hash);
  2406. };
  2407. return Map;
  2408. }(KeyedCollection));
  2409. Map.isMap = isMap;
  2410. var MapPrototype = Map.prototype;
  2411. MapPrototype[IS_MAP_SYMBOL] = true;
  2412. MapPrototype[DELETE] = MapPrototype.remove;
  2413. MapPrototype.removeAll = MapPrototype.deleteAll;
  2414. MapPrototype.setIn = setIn;
  2415. MapPrototype.removeIn = MapPrototype.deleteIn = deleteIn;
  2416. MapPrototype.update = update;
  2417. MapPrototype.updateIn = updateIn;
  2418. MapPrototype.merge = MapPrototype.concat = merge$1;
  2419. MapPrototype.mergeWith = mergeWith$1;
  2420. MapPrototype.mergeDeep = mergeDeep;
  2421. MapPrototype.mergeDeepWith = mergeDeepWith;
  2422. MapPrototype.mergeIn = mergeIn;
  2423. MapPrototype.mergeDeepIn = mergeDeepIn;
  2424. MapPrototype.withMutations = withMutations;
  2425. MapPrototype.wasAltered = wasAltered;
  2426. MapPrototype.asImmutable = asImmutable;
  2427. MapPrototype['@@transducer/init'] = MapPrototype.asMutable = asMutable;
  2428. MapPrototype['@@transducer/step'] = function (result, arr) {
  2429. return result.set(arr[0], arr[1]);
  2430. };
  2431. MapPrototype['@@transducer/result'] = function (obj) {
  2432. return obj.asImmutable();
  2433. };
  2434. // #pragma Trie Nodes
  2435. var ArrayMapNode = function ArrayMapNode(ownerID, entries) {
  2436. this.ownerID = ownerID;
  2437. this.entries = entries;
  2438. };
  2439. ArrayMapNode.prototype.get = function get (shift, keyHash, key, notSetValue) {
  2440. var entries = this.entries;
  2441. for (var ii = 0, len = entries.length; ii < len; ii++) {
  2442. if (is(key, entries[ii][0])) {
  2443. return entries[ii][1];
  2444. }
  2445. }
  2446. return notSetValue;
  2447. };
  2448. ArrayMapNode.prototype.update = function update (ownerID, shift, keyHash, key, value, didChangeSize, didAlter) {
  2449. var removed = value === NOT_SET;
  2450. var entries = this.entries;
  2451. var idx = 0;
  2452. var len = entries.length;
  2453. for (; idx < len; idx++) {
  2454. if (is(key, entries[idx][0])) {
  2455. break;
  2456. }
  2457. }
  2458. var exists = idx < len;
  2459. if (exists ? entries[idx][1] === value : removed) {
  2460. return this;
  2461. }
  2462. SetRef(didAlter);
  2463. // eslint-disable-next-line @typescript-eslint/no-unused-expressions -- TODO enable eslint here
  2464. (removed || !exists) && SetRef(didChangeSize);
  2465. if (removed && entries.length === 1) {
  2466. return; // undefined
  2467. }
  2468. if (!exists && !removed && entries.length >= MAX_ARRAY_MAP_SIZE) {
  2469. return createNodes(ownerID, entries, key, value);
  2470. }
  2471. var isEditable = ownerID && ownerID === this.ownerID;
  2472. var newEntries = isEditable ? entries : arrCopy(entries);
  2473. if (exists) {
  2474. if (removed) {
  2475. // eslint-disable-next-line @typescript-eslint/no-unused-expressions -- TODO enable eslint here
  2476. idx === len - 1
  2477. ? newEntries.pop()
  2478. : (newEntries[idx] = newEntries.pop());
  2479. } else {
  2480. newEntries[idx] = [key, value];
  2481. }
  2482. } else {
  2483. newEntries.push([key, value]);
  2484. }
  2485. if (isEditable) {
  2486. this.entries = newEntries;
  2487. return this;
  2488. }
  2489. return new ArrayMapNode(ownerID, newEntries);
  2490. };
  2491. var BitmapIndexedNode = function BitmapIndexedNode(ownerID, bitmap, nodes) {
  2492. this.ownerID = ownerID;
  2493. this.bitmap = bitmap;
  2494. this.nodes = nodes;
  2495. };
  2496. BitmapIndexedNode.prototype.get = function get (shift, keyHash, key, notSetValue) {
  2497. if (keyHash === undefined) {
  2498. keyHash = hash(key);
  2499. }
  2500. var bit = 1 << ((shift === 0 ? keyHash : keyHash >>> shift) & MASK);
  2501. var bitmap = this.bitmap;
  2502. return (bitmap & bit) === 0
  2503. ? notSetValue
  2504. : this.nodes[popCount(bitmap & (bit - 1))].get(
  2505. shift + SHIFT,
  2506. keyHash,
  2507. key,
  2508. notSetValue
  2509. );
  2510. };
  2511. BitmapIndexedNode.prototype.update = function update (ownerID, shift, keyHash, key, value, didChangeSize, didAlter) {
  2512. if (keyHash === undefined) {
  2513. keyHash = hash(key);
  2514. }
  2515. var keyHashFrag = (shift === 0 ? keyHash : keyHash >>> shift) & MASK;
  2516. var bit = 1 << keyHashFrag;
  2517. var bitmap = this.bitmap;
  2518. var exists = (bitmap & bit) !== 0;
  2519. if (!exists && value === NOT_SET) {
  2520. return this;
  2521. }
  2522. var idx = popCount(bitmap & (bit - 1));
  2523. var nodes = this.nodes;
  2524. var node = exists ? nodes[idx] : undefined;
  2525. var newNode = updateNode(
  2526. node,
  2527. ownerID,
  2528. shift + SHIFT,
  2529. keyHash,
  2530. key,
  2531. value,
  2532. didChangeSize,
  2533. didAlter
  2534. );
  2535. if (newNode === node) {
  2536. return this;
  2537. }
  2538. if (!exists && newNode && nodes.length >= MAX_BITMAP_INDEXED_SIZE) {
  2539. return expandNodes(ownerID, nodes, bitmap, keyHashFrag, newNode);
  2540. }
  2541. if (
  2542. exists &&
  2543. !newNode &&
  2544. nodes.length === 2 &&
  2545. isLeafNode(nodes[idx ^ 1])
  2546. ) {
  2547. return nodes[idx ^ 1];
  2548. }
  2549. if (exists && newNode && nodes.length === 1 && isLeafNode(newNode)) {
  2550. return newNode;
  2551. }
  2552. var isEditable = ownerID && ownerID === this.ownerID;
  2553. var newBitmap = exists ? (newNode ? bitmap : bitmap ^ bit) : bitmap | bit;
  2554. var newNodes = exists
  2555. ? newNode
  2556. ? setAt(nodes, idx, newNode, isEditable)
  2557. : spliceOut(nodes, idx, isEditable)
  2558. : spliceIn(nodes, idx, newNode, isEditable);
  2559. if (isEditable) {
  2560. this.bitmap = newBitmap;
  2561. this.nodes = newNodes;
  2562. return this;
  2563. }
  2564. return new BitmapIndexedNode(ownerID, newBitmap, newNodes);
  2565. };
  2566. var HashArrayMapNode = function HashArrayMapNode(ownerID, count, nodes) {
  2567. this.ownerID = ownerID;
  2568. this.count = count;
  2569. this.nodes = nodes;
  2570. };
  2571. HashArrayMapNode.prototype.get = function get (shift, keyHash, key, notSetValue) {
  2572. if (keyHash === undefined) {
  2573. keyHash = hash(key);
  2574. }
  2575. var idx = (shift === 0 ? keyHash : keyHash >>> shift) & MASK;
  2576. var node = this.nodes[idx];
  2577. return node
  2578. ? node.get(shift + SHIFT, keyHash, key, notSetValue)
  2579. : notSetValue;
  2580. };
  2581. HashArrayMapNode.prototype.update = function update (ownerID, shift, keyHash, key, value, didChangeSize, didAlter) {
  2582. if (keyHash === undefined) {
  2583. keyHash = hash(key);
  2584. }
  2585. var idx = (shift === 0 ? keyHash : keyHash >>> shift) & MASK;
  2586. var removed = value === NOT_SET;
  2587. var nodes = this.nodes;
  2588. var node = nodes[idx];
  2589. if (removed && !node) {
  2590. return this;
  2591. }
  2592. var newNode = updateNode(
  2593. node,
  2594. ownerID,
  2595. shift + SHIFT,
  2596. keyHash,
  2597. key,
  2598. value,
  2599. didChangeSize,
  2600. didAlter
  2601. );
  2602. if (newNode === node) {
  2603. return this;
  2604. }
  2605. var newCount = this.count;
  2606. if (!node) {
  2607. newCount++;
  2608. } else if (!newNode) {
  2609. newCount--;
  2610. if (newCount < MIN_HASH_ARRAY_MAP_SIZE) {
  2611. return packNodes(ownerID, nodes, newCount, idx);
  2612. }
  2613. }
  2614. var isEditable = ownerID && ownerID === this.ownerID;
  2615. var newNodes = setAt(nodes, idx, newNode, isEditable);
  2616. if (isEditable) {
  2617. this.count = newCount;
  2618. this.nodes = newNodes;
  2619. return this;
  2620. }
  2621. return new HashArrayMapNode(ownerID, newCount, newNodes);
  2622. };
  2623. var HashCollisionNode = function HashCollisionNode(ownerID, keyHash, entries) {
  2624. this.ownerID = ownerID;
  2625. this.keyHash = keyHash;
  2626. this.entries = entries;
  2627. };
  2628. HashCollisionNode.prototype.get = function get (shift, keyHash, key, notSetValue) {
  2629. var entries = this.entries;
  2630. for (var ii = 0, len = entries.length; ii < len; ii++) {
  2631. if (is(key, entries[ii][0])) {
  2632. return entries[ii][1];
  2633. }
  2634. }
  2635. return notSetValue;
  2636. };
  2637. HashCollisionNode.prototype.update = function update (ownerID, shift, keyHash, key, value, didChangeSize, didAlter) {
  2638. if (keyHash === undefined) {
  2639. keyHash = hash(key);
  2640. }
  2641. var removed = value === NOT_SET;
  2642. if (keyHash !== this.keyHash) {
  2643. if (removed) {
  2644. return this;
  2645. }
  2646. SetRef(didAlter);
  2647. SetRef(didChangeSize);
  2648. return mergeIntoNode(this, ownerID, shift, keyHash, [key, value]);
  2649. }
  2650. var entries = this.entries;
  2651. var idx = 0;
  2652. var len = entries.length;
  2653. for (; idx < len; idx++) {
  2654. if (is(key, entries[idx][0])) {
  2655. break;
  2656. }
  2657. }
  2658. var exists = idx < len;
  2659. if (exists ? entries[idx][1] === value : removed) {
  2660. return this;
  2661. }
  2662. SetRef(didAlter);
  2663. // eslint-disable-next-line @typescript-eslint/no-unused-expressions -- TODO enable eslint here
  2664. (removed || !exists) && SetRef(didChangeSize);
  2665. if (removed && len === 2) {
  2666. return new ValueNode(ownerID, this.keyHash, entries[idx ^ 1]);
  2667. }
  2668. var isEditable = ownerID && ownerID === this.ownerID;
  2669. var newEntries = isEditable ? entries : arrCopy(entries);
  2670. if (exists) {
  2671. if (removed) {
  2672. // eslint-disable-next-line @typescript-eslint/no-unused-expressions -- TODO enable eslint here
  2673. idx === len - 1
  2674. ? newEntries.pop()
  2675. : (newEntries[idx] = newEntries.pop());
  2676. } else {
  2677. newEntries[idx] = [key, value];
  2678. }
  2679. } else {
  2680. newEntries.push([key, value]);
  2681. }
  2682. if (isEditable) {
  2683. this.entries = newEntries;
  2684. return this;
  2685. }
  2686. return new HashCollisionNode(ownerID, this.keyHash, newEntries);
  2687. };
  2688. var ValueNode = function ValueNode(ownerID, keyHash, entry) {
  2689. this.ownerID = ownerID;
  2690. this.keyHash = keyHash;
  2691. this.entry = entry;
  2692. };
  2693. ValueNode.prototype.get = function get (shift, keyHash, key, notSetValue) {
  2694. return is(key, this.entry[0]) ? this.entry[1] : notSetValue;
  2695. };
  2696. ValueNode.prototype.update = function update (ownerID, shift, keyHash, key, value, didChangeSize, didAlter) {
  2697. var removed = value === NOT_SET;
  2698. var keyMatch = is(key, this.entry[0]);
  2699. if (keyMatch ? value === this.entry[1] : removed) {
  2700. return this;
  2701. }
  2702. SetRef(didAlter);
  2703. if (removed) {
  2704. SetRef(didChangeSize);
  2705. return; // undefined
  2706. }
  2707. if (keyMatch) {
  2708. if (ownerID && ownerID === this.ownerID) {
  2709. this.entry[1] = value;
  2710. return this;
  2711. }
  2712. return new ValueNode(ownerID, this.keyHash, [key, value]);
  2713. }
  2714. SetRef(didChangeSize);
  2715. return mergeIntoNode(this, ownerID, shift, hash(key), [key, value]);
  2716. };
  2717. // #pragma Iterators
  2718. ArrayMapNode.prototype.iterate = HashCollisionNode.prototype.iterate =
  2719. function (fn, reverse) {
  2720. var entries = this.entries;
  2721. for (var ii = 0, maxIndex = entries.length - 1; ii <= maxIndex; ii++) {
  2722. if (fn(entries[reverse ? maxIndex - ii : ii]) === false) {
  2723. return false;
  2724. }
  2725. }
  2726. };
  2727. BitmapIndexedNode.prototype.iterate = HashArrayMapNode.prototype.iterate =
  2728. function (fn, reverse) {
  2729. var nodes = this.nodes;
  2730. for (var ii = 0, maxIndex = nodes.length - 1; ii <= maxIndex; ii++) {
  2731. var node = nodes[reverse ? maxIndex - ii : ii];
  2732. if (node && node.iterate(fn, reverse) === false) {
  2733. return false;
  2734. }
  2735. }
  2736. };
  2737. // eslint-disable-next-line @typescript-eslint/no-unused-vars
  2738. ValueNode.prototype.iterate = function (fn, reverse) {
  2739. return fn(this.entry);
  2740. };
  2741. var MapIterator = /*@__PURE__*/(function (Iterator) {
  2742. function MapIterator(map, type, reverse) {
  2743. this._type = type;
  2744. this._reverse = reverse;
  2745. this._stack = map._root && mapIteratorFrame(map._root);
  2746. }
  2747. if ( Iterator ) MapIterator.__proto__ = Iterator;
  2748. MapIterator.prototype = Object.create( Iterator && Iterator.prototype );
  2749. MapIterator.prototype.constructor = MapIterator;
  2750. MapIterator.prototype.next = function next () {
  2751. var type = this._type;
  2752. var stack = this._stack;
  2753. while (stack) {
  2754. var node = stack.node;
  2755. var index = stack.index++;
  2756. var maxIndex = (void 0);
  2757. if (node.entry) {
  2758. if (index === 0) {
  2759. return mapIteratorValue(type, node.entry);
  2760. }
  2761. } else if (node.entries) {
  2762. maxIndex = node.entries.length - 1;
  2763. if (index <= maxIndex) {
  2764. return mapIteratorValue(
  2765. type,
  2766. node.entries[this._reverse ? maxIndex - index : index]
  2767. );
  2768. }
  2769. } else {
  2770. maxIndex = node.nodes.length - 1;
  2771. if (index <= maxIndex) {
  2772. var subNode = node.nodes[this._reverse ? maxIndex - index : index];
  2773. if (subNode) {
  2774. if (subNode.entry) {
  2775. return mapIteratorValue(type, subNode.entry);
  2776. }
  2777. stack = this._stack = mapIteratorFrame(subNode, stack);
  2778. }
  2779. continue;
  2780. }
  2781. }
  2782. stack = this._stack = this._stack.__prev;
  2783. }
  2784. return iteratorDone();
  2785. };
  2786. return MapIterator;
  2787. }(Iterator));
  2788. function mapIteratorValue(type, entry) {
  2789. return iteratorValue(type, entry[0], entry[1]);
  2790. }
  2791. function mapIteratorFrame(node, prev) {
  2792. return {
  2793. node: node,
  2794. index: 0,
  2795. __prev: prev,
  2796. };
  2797. }
  2798. function makeMap(size, root, ownerID, hash) {
  2799. var map = Object.create(MapPrototype);
  2800. map.size = size;
  2801. map._root = root;
  2802. map.__ownerID = ownerID;
  2803. map.__hash = hash;
  2804. map.__altered = false;
  2805. return map;
  2806. }
  2807. var EMPTY_MAP;
  2808. function emptyMap() {
  2809. return EMPTY_MAP || (EMPTY_MAP = makeMap(0));
  2810. }
  2811. function updateMap(map, k, v) {
  2812. var newRoot;
  2813. var newSize;
  2814. if (!map._root) {
  2815. if (v === NOT_SET) {
  2816. return map;
  2817. }
  2818. newSize = 1;
  2819. newRoot = new ArrayMapNode(map.__ownerID, [[k, v]]);
  2820. } else {
  2821. var didChangeSize = MakeRef();
  2822. var didAlter = MakeRef();
  2823. newRoot = updateNode(
  2824. map._root,
  2825. map.__ownerID,
  2826. 0,
  2827. undefined,
  2828. k,
  2829. v,
  2830. didChangeSize,
  2831. didAlter
  2832. );
  2833. if (!didAlter.value) {
  2834. return map;
  2835. }
  2836. newSize = map.size + (didChangeSize.value ? (v === NOT_SET ? -1 : 1) : 0);
  2837. }
  2838. if (map.__ownerID) {
  2839. map.size = newSize;
  2840. map._root = newRoot;
  2841. map.__hash = undefined;
  2842. map.__altered = true;
  2843. return map;
  2844. }
  2845. return newRoot ? makeMap(newSize, newRoot) : emptyMap();
  2846. }
  2847. function updateNode(
  2848. node,
  2849. ownerID,
  2850. shift,
  2851. keyHash,
  2852. key,
  2853. value,
  2854. didChangeSize,
  2855. didAlter
  2856. ) {
  2857. if (!node) {
  2858. if (value === NOT_SET) {
  2859. return node;
  2860. }
  2861. SetRef(didAlter);
  2862. SetRef(didChangeSize);
  2863. return new ValueNode(ownerID, keyHash, [key, value]);
  2864. }
  2865. return node.update(
  2866. ownerID,
  2867. shift,
  2868. keyHash,
  2869. key,
  2870. value,
  2871. didChangeSize,
  2872. didAlter
  2873. );
  2874. }
  2875. function isLeafNode(node) {
  2876. return (
  2877. node.constructor === ValueNode || node.constructor === HashCollisionNode
  2878. );
  2879. }
  2880. function mergeIntoNode(node, ownerID, shift, keyHash, entry) {
  2881. if (node.keyHash === keyHash) {
  2882. return new HashCollisionNode(ownerID, keyHash, [node.entry, entry]);
  2883. }
  2884. var idx1 = (shift === 0 ? node.keyHash : node.keyHash >>> shift) & MASK;
  2885. var idx2 = (shift === 0 ? keyHash : keyHash >>> shift) & MASK;
  2886. var newNode;
  2887. var nodes =
  2888. idx1 === idx2
  2889. ? [mergeIntoNode(node, ownerID, shift + SHIFT, keyHash, entry)]
  2890. : ((newNode = new ValueNode(ownerID, keyHash, entry)),
  2891. idx1 < idx2 ? [node, newNode] : [newNode, node]);
  2892. return new BitmapIndexedNode(ownerID, (1 << idx1) | (1 << idx2), nodes);
  2893. }
  2894. function createNodes(ownerID, entries, key, value) {
  2895. if (!ownerID) {
  2896. ownerID = new OwnerID();
  2897. }
  2898. var node = new ValueNode(ownerID, hash(key), [key, value]);
  2899. for (var ii = 0; ii < entries.length; ii++) {
  2900. var entry = entries[ii];
  2901. node = node.update(ownerID, 0, undefined, entry[0], entry[1]);
  2902. }
  2903. return node;
  2904. }
  2905. function packNodes(ownerID, nodes, count, excluding) {
  2906. var bitmap = 0;
  2907. var packedII = 0;
  2908. var packedNodes = new Array(count);
  2909. for (var ii = 0, bit = 1, len = nodes.length; ii < len; ii++, bit <<= 1) {
  2910. var node = nodes[ii];
  2911. if (node !== undefined && ii !== excluding) {
  2912. bitmap |= bit;
  2913. packedNodes[packedII++] = node;
  2914. }
  2915. }
  2916. return new BitmapIndexedNode(ownerID, bitmap, packedNodes);
  2917. }
  2918. function expandNodes(ownerID, nodes, bitmap, including, node) {
  2919. var count = 0;
  2920. var expandedNodes = new Array(SIZE);
  2921. for (var ii = 0; bitmap !== 0; ii++, bitmap >>>= 1) {
  2922. expandedNodes[ii] = bitmap & 1 ? nodes[count++] : undefined;
  2923. }
  2924. expandedNodes[including] = node;
  2925. return new HashArrayMapNode(ownerID, count + 1, expandedNodes);
  2926. }
  2927. function popCount(x) {
  2928. x -= (x >> 1) & 0x55555555;
  2929. x = (x & 0x33333333) + ((x >> 2) & 0x33333333);
  2930. x = (x + (x >> 4)) & 0x0f0f0f0f;
  2931. x += x >> 8;
  2932. x += x >> 16;
  2933. return x & 0x7f;
  2934. }
  2935. function setAt(array, idx, val, canEdit) {
  2936. var newArray = canEdit ? array : arrCopy(array);
  2937. newArray[idx] = val;
  2938. return newArray;
  2939. }
  2940. function spliceIn(array, idx, val, canEdit) {
  2941. var newLen = array.length + 1;
  2942. if (canEdit && idx + 1 === newLen) {
  2943. array[idx] = val;
  2944. return array;
  2945. }
  2946. var newArray = new Array(newLen);
  2947. var after = 0;
  2948. for (var ii = 0; ii < newLen; ii++) {
  2949. if (ii === idx) {
  2950. newArray[ii] = val;
  2951. after = -1;
  2952. } else {
  2953. newArray[ii] = array[ii + after];
  2954. }
  2955. }
  2956. return newArray;
  2957. }
  2958. function spliceOut(array, idx, canEdit) {
  2959. var newLen = array.length - 1;
  2960. if (canEdit && idx === newLen) {
  2961. array.pop();
  2962. return array;
  2963. }
  2964. var newArray = new Array(newLen);
  2965. var after = 0;
  2966. for (var ii = 0; ii < newLen; ii++) {
  2967. if (ii === idx) {
  2968. after = 1;
  2969. }
  2970. newArray[ii] = array[ii + after];
  2971. }
  2972. return newArray;
  2973. }
  2974. var MAX_ARRAY_MAP_SIZE = SIZE / 4;
  2975. var MAX_BITMAP_INDEXED_SIZE = SIZE / 2;
  2976. var MIN_HASH_ARRAY_MAP_SIZE = SIZE / 4;
  2977. var IS_LIST_SYMBOL = '@@__IMMUTABLE_LIST__@@';
  2978. /**
  2979. * True if `maybeList` is a List.
  2980. */
  2981. function isList(maybeList) {
  2982. return Boolean(maybeList &&
  2983. // @ts-expect-error: maybeList is typed as `{}`, need to change in 6.0 to `maybeList && typeof maybeList === 'object' && IS_LIST_SYMBOL in maybeList`
  2984. maybeList[IS_LIST_SYMBOL]);
  2985. }
  2986. var List = /*@__PURE__*/(function (IndexedCollection) {
  2987. function List(value) {
  2988. var empty = emptyList();
  2989. if (value === undefined || value === null) {
  2990. // eslint-disable-next-line no-constructor-return
  2991. return empty;
  2992. }
  2993. if (isList(value)) {
  2994. // eslint-disable-next-line no-constructor-return
  2995. return value;
  2996. }
  2997. var iter = IndexedCollection(value);
  2998. var size = iter.size;
  2999. if (size === 0) {
  3000. // eslint-disable-next-line no-constructor-return
  3001. return empty;
  3002. }
  3003. assertNotInfinite(size);
  3004. if (size > 0 && size < SIZE) {
  3005. // eslint-disable-next-line no-constructor-return
  3006. return makeList(0, size, SHIFT, null, new VNode(iter.toArray()));
  3007. }
  3008. // eslint-disable-next-line no-constructor-return
  3009. return empty.withMutations(function (list) {
  3010. list.setSize(size);
  3011. iter.forEach(function (v, i) { return list.set(i, v); });
  3012. });
  3013. }
  3014. if ( IndexedCollection ) List.__proto__ = IndexedCollection;
  3015. List.prototype = Object.create( IndexedCollection && IndexedCollection.prototype );
  3016. List.prototype.constructor = List;
  3017. List.of = function of (/*...values*/) {
  3018. return this(arguments);
  3019. };
  3020. List.prototype.toString = function toString () {
  3021. return this.__toString('List [', ']');
  3022. };
  3023. // @pragma Access
  3024. List.prototype.get = function get (index, notSetValue) {
  3025. index = wrapIndex(this, index);
  3026. if (index >= 0 && index < this.size) {
  3027. index += this._origin;
  3028. var node = listNodeFor(this, index);
  3029. return node && node.array[index & MASK];
  3030. }
  3031. return notSetValue;
  3032. };
  3033. // @pragma Modification
  3034. List.prototype.set = function set (index, value) {
  3035. return updateList(this, index, value);
  3036. };
  3037. List.prototype.remove = function remove (index) {
  3038. return !this.has(index)
  3039. ? this
  3040. : index === 0
  3041. ? this.shift()
  3042. : index === this.size - 1
  3043. ? this.pop()
  3044. : this.splice(index, 1);
  3045. };
  3046. List.prototype.insert = function insert (index, value) {
  3047. return this.splice(index, 0, value);
  3048. };
  3049. List.prototype.clear = function clear () {
  3050. if (this.size === 0) {
  3051. return this;
  3052. }
  3053. if (this.__ownerID) {
  3054. this.size = this._origin = this._capacity = 0;
  3055. this._level = SHIFT;
  3056. this._root = this._tail = this.__hash = undefined;
  3057. this.__altered = true;
  3058. return this;
  3059. }
  3060. return emptyList();
  3061. };
  3062. List.prototype.push = function push (/*...values*/) {
  3063. var values = arguments;
  3064. var oldSize = this.size;
  3065. return this.withMutations(function (list) {
  3066. setListBounds(list, 0, oldSize + values.length);
  3067. for (var ii = 0; ii < values.length; ii++) {
  3068. list.set(oldSize + ii, values[ii]);
  3069. }
  3070. });
  3071. };
  3072. List.prototype.pop = function pop () {
  3073. return setListBounds(this, 0, -1);
  3074. };
  3075. List.prototype.unshift = function unshift (/*...values*/) {
  3076. var values = arguments;
  3077. return this.withMutations(function (list) {
  3078. setListBounds(list, -values.length);
  3079. for (var ii = 0; ii < values.length; ii++) {
  3080. list.set(ii, values[ii]);
  3081. }
  3082. });
  3083. };
  3084. List.prototype.shift = function shift () {
  3085. return setListBounds(this, 1);
  3086. };
  3087. List.prototype.shuffle = function shuffle (random) {
  3088. if ( random === void 0 ) random = Math.random;
  3089. return this.withMutations(function (mutable) {
  3090. // implementation of the Fisher-Yates shuffle: https://en.wikipedia.org/wiki/Fisher%E2%80%93Yates_shuffle
  3091. var current = mutable.size;
  3092. var destination;
  3093. var tmp;
  3094. while (current) {
  3095. destination = Math.floor(random() * current--);
  3096. tmp = mutable.get(destination);
  3097. mutable.set(destination, mutable.get(current));
  3098. mutable.set(current, tmp);
  3099. }
  3100. });
  3101. };
  3102. // @pragma Composition
  3103. List.prototype.concat = function concat (/*...collections*/) {
  3104. var arguments$1 = arguments;
  3105. var seqs = [];
  3106. for (var i = 0; i < arguments.length; i++) {
  3107. var argument = arguments$1[i];
  3108. var seq = IndexedCollection(
  3109. typeof argument !== 'string' && hasIterator(argument)
  3110. ? argument
  3111. : [argument]
  3112. );
  3113. if (seq.size !== 0) {
  3114. seqs.push(seq);
  3115. }
  3116. }
  3117. if (seqs.length === 0) {
  3118. return this;
  3119. }
  3120. if (this.size === 0 && !this.__ownerID && seqs.length === 1) {
  3121. return this.constructor(seqs[0]);
  3122. }
  3123. return this.withMutations(function (list) {
  3124. seqs.forEach(function (seq) { return seq.forEach(function (value) { return list.push(value); }); });
  3125. });
  3126. };
  3127. List.prototype.setSize = function setSize (size) {
  3128. return setListBounds(this, 0, size);
  3129. };
  3130. List.prototype.map = function map (mapper, context) {
  3131. var this$1$1 = this;
  3132. return this.withMutations(function (list) {
  3133. for (var i = 0; i < this$1$1.size; i++) {
  3134. list.set(i, mapper.call(context, list.get(i), i, this$1$1));
  3135. }
  3136. });
  3137. };
  3138. // @pragma Iteration
  3139. List.prototype.slice = function slice (begin, end) {
  3140. var size = this.size;
  3141. if (wholeSlice(begin, end, size)) {
  3142. return this;
  3143. }
  3144. return setListBounds(
  3145. this,
  3146. resolveBegin(begin, size),
  3147. resolveEnd(end, size)
  3148. );
  3149. };
  3150. List.prototype.__iterator = function __iterator (type, reverse) {
  3151. var index = reverse ? this.size : 0;
  3152. var values = iterateList(this, reverse);
  3153. return new Iterator(function () {
  3154. var value = values();
  3155. return value === DONE
  3156. ? iteratorDone()
  3157. : iteratorValue(type, reverse ? --index : index++, value);
  3158. });
  3159. };
  3160. List.prototype.__iterate = function __iterate (fn, reverse) {
  3161. var index = reverse ? this.size : 0;
  3162. var values = iterateList(this, reverse);
  3163. var value;
  3164. while ((value = values()) !== DONE) {
  3165. if (fn(value, reverse ? --index : index++, this) === false) {
  3166. break;
  3167. }
  3168. }
  3169. return index;
  3170. };
  3171. List.prototype.__ensureOwner = function __ensureOwner (ownerID) {
  3172. if (ownerID === this.__ownerID) {
  3173. return this;
  3174. }
  3175. if (!ownerID) {
  3176. if (this.size === 0) {
  3177. return emptyList();
  3178. }
  3179. this.__ownerID = ownerID;
  3180. this.__altered = false;
  3181. return this;
  3182. }
  3183. return makeList(
  3184. this._origin,
  3185. this._capacity,
  3186. this._level,
  3187. this._root,
  3188. this._tail,
  3189. ownerID,
  3190. this.__hash
  3191. );
  3192. };
  3193. return List;
  3194. }(IndexedCollection));
  3195. List.isList = isList;
  3196. var ListPrototype = List.prototype;
  3197. ListPrototype[IS_LIST_SYMBOL] = true;
  3198. ListPrototype[DELETE] = ListPrototype.remove;
  3199. ListPrototype.merge = ListPrototype.concat;
  3200. ListPrototype.setIn = setIn;
  3201. ListPrototype.deleteIn = ListPrototype.removeIn = deleteIn;
  3202. ListPrototype.update = update;
  3203. ListPrototype.updateIn = updateIn;
  3204. ListPrototype.mergeIn = mergeIn;
  3205. ListPrototype.mergeDeepIn = mergeDeepIn;
  3206. ListPrototype.withMutations = withMutations;
  3207. ListPrototype.wasAltered = wasAltered;
  3208. ListPrototype.asImmutable = asImmutable;
  3209. ListPrototype['@@transducer/init'] = ListPrototype.asMutable = asMutable;
  3210. ListPrototype['@@transducer/step'] = function (result, arr) {
  3211. return result.push(arr);
  3212. };
  3213. ListPrototype['@@transducer/result'] = function (obj) {
  3214. return obj.asImmutable();
  3215. };
  3216. var VNode = function VNode(array, ownerID) {
  3217. this.array = array;
  3218. this.ownerID = ownerID;
  3219. };
  3220. // TODO: seems like these methods are very similar
  3221. VNode.prototype.removeBefore = function removeBefore (ownerID, level, index) {
  3222. if (
  3223. (index & ((1 << (level + SHIFT)) - 1)) === 0 ||
  3224. this.array.length === 0
  3225. ) {
  3226. return this;
  3227. }
  3228. var originIndex = (index >>> level) & MASK;
  3229. if (originIndex >= this.array.length) {
  3230. return new VNode([], ownerID);
  3231. }
  3232. var removingFirst = originIndex === 0;
  3233. var newChild;
  3234. if (level > 0) {
  3235. var oldChild = this.array[originIndex];
  3236. newChild =
  3237. oldChild && oldChild.removeBefore(ownerID, level - SHIFT, index);
  3238. if (newChild === oldChild && removingFirst) {
  3239. return this;
  3240. }
  3241. }
  3242. if (removingFirst && !newChild) {
  3243. return this;
  3244. }
  3245. var editable = editableVNode(this, ownerID);
  3246. if (!removingFirst) {
  3247. for (var ii = 0; ii < originIndex; ii++) {
  3248. editable.array[ii] = undefined;
  3249. }
  3250. }
  3251. if (newChild) {
  3252. editable.array[originIndex] = newChild;
  3253. }
  3254. return editable;
  3255. };
  3256. VNode.prototype.removeAfter = function removeAfter (ownerID, level, index) {
  3257. if (
  3258. index === (level ? 1 << (level + SHIFT) : SIZE) ||
  3259. this.array.length === 0
  3260. ) {
  3261. return this;
  3262. }
  3263. var sizeIndex = ((index - 1) >>> level) & MASK;
  3264. if (sizeIndex >= this.array.length) {
  3265. return this;
  3266. }
  3267. var newChild;
  3268. if (level > 0) {
  3269. var oldChild = this.array[sizeIndex];
  3270. newChild =
  3271. oldChild && oldChild.removeAfter(ownerID, level - SHIFT, index);
  3272. if (newChild === oldChild && sizeIndex === this.array.length - 1) {
  3273. return this;
  3274. }
  3275. }
  3276. var editable = editableVNode(this, ownerID);
  3277. editable.array.splice(sizeIndex + 1);
  3278. if (newChild) {
  3279. editable.array[sizeIndex] = newChild;
  3280. }
  3281. return editable;
  3282. };
  3283. var DONE = {};
  3284. function iterateList(list, reverse) {
  3285. var left = list._origin;
  3286. var right = list._capacity;
  3287. var tailPos = getTailOffset(right);
  3288. var tail = list._tail;
  3289. return iterateNodeOrLeaf(list._root, list._level, 0);
  3290. function iterateNodeOrLeaf(node, level, offset) {
  3291. return level === 0
  3292. ? iterateLeaf(node, offset)
  3293. : iterateNode(node, level, offset);
  3294. }
  3295. function iterateLeaf(node, offset) {
  3296. var array = offset === tailPos ? tail && tail.array : node && node.array;
  3297. var from = offset > left ? 0 : left - offset;
  3298. var to = right - offset;
  3299. if (to > SIZE) {
  3300. to = SIZE;
  3301. }
  3302. return function () {
  3303. if (from === to) {
  3304. return DONE;
  3305. }
  3306. var idx = reverse ? --to : from++;
  3307. return array && array[idx];
  3308. };
  3309. }
  3310. function iterateNode(node, level, offset) {
  3311. var values;
  3312. var array = node && node.array;
  3313. var from = offset > left ? 0 : (left - offset) >> level;
  3314. var to = ((right - offset) >> level) + 1;
  3315. if (to > SIZE) {
  3316. to = SIZE;
  3317. }
  3318. return function () {
  3319. while (true) {
  3320. if (values) {
  3321. var value = values();
  3322. if (value !== DONE) {
  3323. return value;
  3324. }
  3325. values = null;
  3326. }
  3327. if (from === to) {
  3328. return DONE;
  3329. }
  3330. var idx = reverse ? --to : from++;
  3331. values = iterateNodeOrLeaf(
  3332. array && array[idx],
  3333. level - SHIFT,
  3334. offset + (idx << level)
  3335. );
  3336. }
  3337. };
  3338. }
  3339. }
  3340. function makeList(origin, capacity, level, root, tail, ownerID, hash) {
  3341. var list = Object.create(ListPrototype);
  3342. list.size = capacity - origin;
  3343. list._origin = origin;
  3344. list._capacity = capacity;
  3345. list._level = level;
  3346. list._root = root;
  3347. list._tail = tail;
  3348. list.__ownerID = ownerID;
  3349. list.__hash = hash;
  3350. list.__altered = false;
  3351. return list;
  3352. }
  3353. function emptyList() {
  3354. return makeList(0, 0, SHIFT);
  3355. }
  3356. function updateList(list, index, value) {
  3357. index = wrapIndex(list, index);
  3358. if (index !== index) {
  3359. return list;
  3360. }
  3361. if (index >= list.size || index < 0) {
  3362. return list.withMutations(function (list) {
  3363. // eslint-disable-next-line @typescript-eslint/no-unused-expressions -- TODO enable eslint here
  3364. index < 0
  3365. ? setListBounds(list, index).set(0, value)
  3366. : setListBounds(list, 0, index + 1).set(index, value);
  3367. });
  3368. }
  3369. index += list._origin;
  3370. var newTail = list._tail;
  3371. var newRoot = list._root;
  3372. var didAlter = MakeRef();
  3373. if (index >= getTailOffset(list._capacity)) {
  3374. newTail = updateVNode(newTail, list.__ownerID, 0, index, value, didAlter);
  3375. } else {
  3376. newRoot = updateVNode(
  3377. newRoot,
  3378. list.__ownerID,
  3379. list._level,
  3380. index,
  3381. value,
  3382. didAlter
  3383. );
  3384. }
  3385. if (!didAlter.value) {
  3386. return list;
  3387. }
  3388. if (list.__ownerID) {
  3389. list._root = newRoot;
  3390. list._tail = newTail;
  3391. list.__hash = undefined;
  3392. list.__altered = true;
  3393. return list;
  3394. }
  3395. return makeList(list._origin, list._capacity, list._level, newRoot, newTail);
  3396. }
  3397. function updateVNode(node, ownerID, level, index, value, didAlter) {
  3398. var idx = (index >>> level) & MASK;
  3399. var nodeHas = node && idx < node.array.length;
  3400. if (!nodeHas && value === undefined) {
  3401. return node;
  3402. }
  3403. var newNode;
  3404. if (level > 0) {
  3405. var lowerNode = node && node.array[idx];
  3406. var newLowerNode = updateVNode(
  3407. lowerNode,
  3408. ownerID,
  3409. level - SHIFT,
  3410. index,
  3411. value,
  3412. didAlter
  3413. );
  3414. if (newLowerNode === lowerNode) {
  3415. return node;
  3416. }
  3417. newNode = editableVNode(node, ownerID);
  3418. newNode.array[idx] = newLowerNode;
  3419. return newNode;
  3420. }
  3421. if (nodeHas && node.array[idx] === value) {
  3422. return node;
  3423. }
  3424. if (didAlter) {
  3425. SetRef(didAlter);
  3426. }
  3427. newNode = editableVNode(node, ownerID);
  3428. if (value === undefined && idx === newNode.array.length - 1) {
  3429. newNode.array.pop();
  3430. } else {
  3431. newNode.array[idx] = value;
  3432. }
  3433. return newNode;
  3434. }
  3435. function editableVNode(node, ownerID) {
  3436. if (ownerID && node && ownerID === node.ownerID) {
  3437. return node;
  3438. }
  3439. return new VNode(node ? node.array.slice() : [], ownerID);
  3440. }
  3441. function listNodeFor(list, rawIndex) {
  3442. if (rawIndex >= getTailOffset(list._capacity)) {
  3443. return list._tail;
  3444. }
  3445. if (rawIndex < 1 << (list._level + SHIFT)) {
  3446. var node = list._root;
  3447. var level = list._level;
  3448. while (node && level > 0) {
  3449. node = node.array[(rawIndex >>> level) & MASK];
  3450. level -= SHIFT;
  3451. }
  3452. return node;
  3453. }
  3454. }
  3455. function setListBounds(list, begin, end) {
  3456. // Sanitize begin & end using this shorthand for ToInt32(argument)
  3457. // http://www.ecma-international.org/ecma-262/6.0/#sec-toint32
  3458. if (begin !== undefined) {
  3459. begin |= 0;
  3460. }
  3461. if (end !== undefined) {
  3462. end |= 0;
  3463. }
  3464. var owner = list.__ownerID || new OwnerID();
  3465. var oldOrigin = list._origin;
  3466. var oldCapacity = list._capacity;
  3467. var newOrigin = oldOrigin + begin;
  3468. var newCapacity =
  3469. end === undefined
  3470. ? oldCapacity
  3471. : end < 0
  3472. ? oldCapacity + end
  3473. : oldOrigin + end;
  3474. if (newOrigin === oldOrigin && newCapacity === oldCapacity) {
  3475. return list;
  3476. }
  3477. // If it's going to end after it starts, it's empty.
  3478. if (newOrigin >= newCapacity) {
  3479. return list.clear();
  3480. }
  3481. var newLevel = list._level;
  3482. var newRoot = list._root;
  3483. // New origin might need creating a higher root.
  3484. var offsetShift = 0;
  3485. while (newOrigin + offsetShift < 0) {
  3486. newRoot = new VNode(
  3487. newRoot && newRoot.array.length ? [undefined, newRoot] : [],
  3488. owner
  3489. );
  3490. newLevel += SHIFT;
  3491. offsetShift += 1 << newLevel;
  3492. }
  3493. if (offsetShift) {
  3494. newOrigin += offsetShift;
  3495. oldOrigin += offsetShift;
  3496. newCapacity += offsetShift;
  3497. oldCapacity += offsetShift;
  3498. }
  3499. var oldTailOffset = getTailOffset(oldCapacity);
  3500. var newTailOffset = getTailOffset(newCapacity);
  3501. // New size might need creating a higher root.
  3502. while (newTailOffset >= 1 << (newLevel + SHIFT)) {
  3503. newRoot = new VNode(
  3504. newRoot && newRoot.array.length ? [newRoot] : [],
  3505. owner
  3506. );
  3507. newLevel += SHIFT;
  3508. }
  3509. // Locate or create the new tail.
  3510. var oldTail = list._tail;
  3511. var newTail =
  3512. newTailOffset < oldTailOffset
  3513. ? listNodeFor(list, newCapacity - 1)
  3514. : newTailOffset > oldTailOffset
  3515. ? new VNode([], owner)
  3516. : oldTail;
  3517. // Merge Tail into tree.
  3518. if (
  3519. oldTail &&
  3520. newTailOffset > oldTailOffset &&
  3521. newOrigin < oldCapacity &&
  3522. oldTail.array.length
  3523. ) {
  3524. newRoot = editableVNode(newRoot, owner);
  3525. var node = newRoot;
  3526. for (var level = newLevel; level > SHIFT; level -= SHIFT) {
  3527. var idx = (oldTailOffset >>> level) & MASK;
  3528. node = node.array[idx] = editableVNode(node.array[idx], owner);
  3529. }
  3530. node.array[(oldTailOffset >>> SHIFT) & MASK] = oldTail;
  3531. }
  3532. // If the size has been reduced, there's a chance the tail needs to be trimmed.
  3533. if (newCapacity < oldCapacity) {
  3534. newTail = newTail && newTail.removeAfter(owner, 0, newCapacity);
  3535. }
  3536. // If the new origin is within the tail, then we do not need a root.
  3537. if (newOrigin >= newTailOffset) {
  3538. newOrigin -= newTailOffset;
  3539. newCapacity -= newTailOffset;
  3540. newLevel = SHIFT;
  3541. newRoot = null;
  3542. newTail = newTail && newTail.removeBefore(owner, 0, newOrigin);
  3543. // Otherwise, if the root has been trimmed, garbage collect.
  3544. } else if (newOrigin > oldOrigin || newTailOffset < oldTailOffset) {
  3545. offsetShift = 0;
  3546. // Identify the new top root node of the subtree of the old root.
  3547. while (newRoot) {
  3548. var beginIndex = (newOrigin >>> newLevel) & MASK;
  3549. if ((beginIndex !== newTailOffset >>> newLevel) & MASK) {
  3550. break;
  3551. }
  3552. if (beginIndex) {
  3553. offsetShift += (1 << newLevel) * beginIndex;
  3554. }
  3555. newLevel -= SHIFT;
  3556. newRoot = newRoot.array[beginIndex];
  3557. }
  3558. // Trim the new sides of the new root.
  3559. if (newRoot && newOrigin > oldOrigin) {
  3560. newRoot = newRoot.removeBefore(owner, newLevel, newOrigin - offsetShift);
  3561. }
  3562. if (newRoot && newTailOffset < oldTailOffset) {
  3563. newRoot = newRoot.removeAfter(
  3564. owner,
  3565. newLevel,
  3566. newTailOffset - offsetShift
  3567. );
  3568. }
  3569. if (offsetShift) {
  3570. newOrigin -= offsetShift;
  3571. newCapacity -= offsetShift;
  3572. }
  3573. }
  3574. if (list.__ownerID) {
  3575. list.size = newCapacity - newOrigin;
  3576. list._origin = newOrigin;
  3577. list._capacity = newCapacity;
  3578. list._level = newLevel;
  3579. list._root = newRoot;
  3580. list._tail = newTail;
  3581. list.__hash = undefined;
  3582. list.__altered = true;
  3583. return list;
  3584. }
  3585. return makeList(newOrigin, newCapacity, newLevel, newRoot, newTail);
  3586. }
  3587. function getTailOffset(size) {
  3588. return size < SIZE ? 0 : ((size - 1) >>> SHIFT) << SHIFT;
  3589. }
  3590. var OrderedMap = /*@__PURE__*/(function (Map) {
  3591. function OrderedMap(value) {
  3592. // eslint-disable-next-line no-constructor-return
  3593. return value === undefined || value === null
  3594. ? emptyOrderedMap()
  3595. : isOrderedMap(value)
  3596. ? value
  3597. : emptyOrderedMap().withMutations(function (map) {
  3598. var iter = KeyedCollection(value);
  3599. assertNotInfinite(iter.size);
  3600. iter.forEach(function (v, k) { return map.set(k, v); });
  3601. });
  3602. }
  3603. if ( Map ) OrderedMap.__proto__ = Map;
  3604. OrderedMap.prototype = Object.create( Map && Map.prototype );
  3605. OrderedMap.prototype.constructor = OrderedMap;
  3606. OrderedMap.of = function of (/*...values*/) {
  3607. return this(arguments);
  3608. };
  3609. OrderedMap.prototype.toString = function toString () {
  3610. return this.__toString('OrderedMap {', '}');
  3611. };
  3612. // @pragma Access
  3613. OrderedMap.prototype.get = function get (k, notSetValue) {
  3614. var index = this._map.get(k);
  3615. return index !== undefined ? this._list.get(index)[1] : notSetValue;
  3616. };
  3617. // @pragma Modification
  3618. OrderedMap.prototype.clear = function clear () {
  3619. if (this.size === 0) {
  3620. return this;
  3621. }
  3622. if (this.__ownerID) {
  3623. this.size = 0;
  3624. this._map.clear();
  3625. this._list.clear();
  3626. this.__altered = true;
  3627. return this;
  3628. }
  3629. return emptyOrderedMap();
  3630. };
  3631. OrderedMap.prototype.set = function set (k, v) {
  3632. return updateOrderedMap(this, k, v);
  3633. };
  3634. OrderedMap.prototype.remove = function remove (k) {
  3635. return updateOrderedMap(this, k, NOT_SET);
  3636. };
  3637. OrderedMap.prototype.__iterate = function __iterate (fn, reverse) {
  3638. var this$1$1 = this;
  3639. return this._list.__iterate(
  3640. function (entry) { return entry && fn(entry[1], entry[0], this$1$1); },
  3641. reverse
  3642. );
  3643. };
  3644. OrderedMap.prototype.__iterator = function __iterator (type, reverse) {
  3645. return this._list.fromEntrySeq().__iterator(type, reverse);
  3646. };
  3647. OrderedMap.prototype.__ensureOwner = function __ensureOwner (ownerID) {
  3648. if (ownerID === this.__ownerID) {
  3649. return this;
  3650. }
  3651. var newMap = this._map.__ensureOwner(ownerID);
  3652. var newList = this._list.__ensureOwner(ownerID);
  3653. if (!ownerID) {
  3654. if (this.size === 0) {
  3655. return emptyOrderedMap();
  3656. }
  3657. this.__ownerID = ownerID;
  3658. this.__altered = false;
  3659. this._map = newMap;
  3660. this._list = newList;
  3661. return this;
  3662. }
  3663. return makeOrderedMap(newMap, newList, ownerID, this.__hash);
  3664. };
  3665. return OrderedMap;
  3666. }(Map));
  3667. OrderedMap.isOrderedMap = isOrderedMap;
  3668. OrderedMap.prototype[IS_ORDERED_SYMBOL] = true;
  3669. OrderedMap.prototype[DELETE] = OrderedMap.prototype.remove;
  3670. function makeOrderedMap(map, list, ownerID, hash) {
  3671. var omap = Object.create(OrderedMap.prototype);
  3672. omap.size = map ? map.size : 0;
  3673. omap._map = map;
  3674. omap._list = list;
  3675. omap.__ownerID = ownerID;
  3676. omap.__hash = hash;
  3677. omap.__altered = false;
  3678. return omap;
  3679. }
  3680. var EMPTY_ORDERED_MAP;
  3681. function emptyOrderedMap() {
  3682. return (
  3683. EMPTY_ORDERED_MAP ||
  3684. (EMPTY_ORDERED_MAP = makeOrderedMap(emptyMap(), emptyList()))
  3685. );
  3686. }
  3687. function updateOrderedMap(omap, k, v) {
  3688. var map = omap._map;
  3689. var list = omap._list;
  3690. var i = map.get(k);
  3691. var has = i !== undefined;
  3692. var newMap;
  3693. var newList;
  3694. if (v === NOT_SET) {
  3695. // removed
  3696. if (!has) {
  3697. return omap;
  3698. }
  3699. if (list.size >= SIZE && list.size >= map.size * 2) {
  3700. newList = list.filter(function (entry, idx) { return entry !== undefined && i !== idx; });
  3701. newMap = newList
  3702. .toKeyedSeq()
  3703. .map(function (entry) { return entry[0]; })
  3704. .flip()
  3705. .toMap();
  3706. if (omap.__ownerID) {
  3707. newMap.__ownerID = newList.__ownerID = omap.__ownerID;
  3708. }
  3709. } else {
  3710. newMap = map.remove(k);
  3711. newList = i === list.size - 1 ? list.pop() : list.set(i, undefined);
  3712. }
  3713. } else if (has) {
  3714. if (v === list.get(i)[1]) {
  3715. return omap;
  3716. }
  3717. newMap = map;
  3718. newList = list.set(i, [k, v]);
  3719. } else {
  3720. newMap = map.set(k, list.size);
  3721. newList = list.set(list.size, [k, v]);
  3722. }
  3723. if (omap.__ownerID) {
  3724. omap.size = newMap.size;
  3725. omap._map = newMap;
  3726. omap._list = newList;
  3727. omap.__hash = undefined;
  3728. omap.__altered = true;
  3729. return omap;
  3730. }
  3731. return makeOrderedMap(newMap, newList);
  3732. }
  3733. var IS_STACK_SYMBOL = '@@__IMMUTABLE_STACK__@@';
  3734. /**
  3735. * True if `maybeStack` is a Stack.
  3736. */
  3737. function isStack(maybeStack) {
  3738. return Boolean(maybeStack &&
  3739. // @ts-expect-error: maybeStack is typed as `{}`, need to change in 6.0 to `maybeStack && typeof maybeStack === 'object' && MAYBE_STACK_SYMBOL in maybeStack`
  3740. maybeStack[IS_STACK_SYMBOL]);
  3741. }
  3742. var Stack = /*@__PURE__*/(function (IndexedCollection) {
  3743. function Stack(value) {
  3744. // eslint-disable-next-line no-constructor-return
  3745. return value === undefined || value === null
  3746. ? emptyStack()
  3747. : isStack(value)
  3748. ? value
  3749. : emptyStack().pushAll(value);
  3750. }
  3751. if ( IndexedCollection ) Stack.__proto__ = IndexedCollection;
  3752. Stack.prototype = Object.create( IndexedCollection && IndexedCollection.prototype );
  3753. Stack.prototype.constructor = Stack;
  3754. Stack.of = function of (/*...values*/) {
  3755. return this(arguments);
  3756. };
  3757. Stack.prototype.toString = function toString () {
  3758. return this.__toString('Stack [', ']');
  3759. };
  3760. // @pragma Access
  3761. Stack.prototype.get = function get (index, notSetValue) {
  3762. var head = this._head;
  3763. index = wrapIndex(this, index);
  3764. while (head && index--) {
  3765. head = head.next;
  3766. }
  3767. return head ? head.value : notSetValue;
  3768. };
  3769. Stack.prototype.peek = function peek () {
  3770. return this._head && this._head.value;
  3771. };
  3772. // @pragma Modification
  3773. Stack.prototype.push = function push (/*...values*/) {
  3774. var arguments$1 = arguments;
  3775. if (arguments.length === 0) {
  3776. return this;
  3777. }
  3778. var newSize = this.size + arguments.length;
  3779. var head = this._head;
  3780. for (var ii = arguments.length - 1; ii >= 0; ii--) {
  3781. head = {
  3782. value: arguments$1[ii],
  3783. next: head,
  3784. };
  3785. }
  3786. if (this.__ownerID) {
  3787. this.size = newSize;
  3788. this._head = head;
  3789. this.__hash = undefined;
  3790. this.__altered = true;
  3791. return this;
  3792. }
  3793. return makeStack(newSize, head);
  3794. };
  3795. Stack.prototype.pushAll = function pushAll (iter) {
  3796. iter = IndexedCollection(iter);
  3797. if (iter.size === 0) {
  3798. return this;
  3799. }
  3800. if (this.size === 0 && isStack(iter)) {
  3801. return iter;
  3802. }
  3803. assertNotInfinite(iter.size);
  3804. var newSize = this.size;
  3805. var head = this._head;
  3806. iter.__iterate(function (value) {
  3807. newSize++;
  3808. head = {
  3809. value: value,
  3810. next: head,
  3811. };
  3812. }, /* reverse */ true);
  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.pop = function pop () {
  3823. return this.slice(1);
  3824. };
  3825. Stack.prototype.clear = function clear () {
  3826. if (this.size === 0) {
  3827. return this;
  3828. }
  3829. if (this.__ownerID) {
  3830. this.size = 0;
  3831. this._head = undefined;
  3832. this.__hash = undefined;
  3833. this.__altered = true;
  3834. return this;
  3835. }
  3836. return emptyStack();
  3837. };
  3838. Stack.prototype.slice = function slice (begin, end) {
  3839. if (wholeSlice(begin, end, this.size)) {
  3840. return this;
  3841. }
  3842. var resolvedBegin = resolveBegin(begin, this.size);
  3843. var resolvedEnd = resolveEnd(end, this.size);
  3844. if (resolvedEnd !== this.size) {
  3845. // super.slice(begin, end);
  3846. return IndexedCollection.prototype.slice.call(this, begin, end);
  3847. }
  3848. var newSize = this.size - resolvedBegin;
  3849. var head = this._head;
  3850. while (resolvedBegin--) {
  3851. head = head.next;
  3852. }
  3853. if (this.__ownerID) {
  3854. this.size = newSize;
  3855. this._head = head;
  3856. this.__hash = undefined;
  3857. this.__altered = true;
  3858. return this;
  3859. }
  3860. return makeStack(newSize, head);
  3861. };
  3862. // @pragma Mutability
  3863. Stack.prototype.__ensureOwner = function __ensureOwner (ownerID) {
  3864. if (ownerID === this.__ownerID) {
  3865. return this;
  3866. }
  3867. if (!ownerID) {
  3868. if (this.size === 0) {
  3869. return emptyStack();
  3870. }
  3871. this.__ownerID = ownerID;
  3872. this.__altered = false;
  3873. return this;
  3874. }
  3875. return makeStack(this.size, this._head, ownerID, this.__hash);
  3876. };
  3877. // @pragma Iteration
  3878. Stack.prototype.__iterate = function __iterate (fn, reverse) {
  3879. var this$1$1 = this;
  3880. if (reverse) {
  3881. return new ArraySeq(this.toArray()).__iterate(
  3882. function (v, k) { return fn(v, k, this$1$1); },
  3883. reverse
  3884. );
  3885. }
  3886. var iterations = 0;
  3887. var node = this._head;
  3888. while (node) {
  3889. if (fn(node.value, iterations++, this) === false) {
  3890. break;
  3891. }
  3892. node = node.next;
  3893. }
  3894. return iterations;
  3895. };
  3896. Stack.prototype.__iterator = function __iterator (type, reverse) {
  3897. if (reverse) {
  3898. return new ArraySeq(this.toArray()).__iterator(type, reverse);
  3899. }
  3900. var iterations = 0;
  3901. var node = this._head;
  3902. return new Iterator(function () {
  3903. if (node) {
  3904. var value = node.value;
  3905. node = node.next;
  3906. return iteratorValue(type, iterations++, value);
  3907. }
  3908. return iteratorDone();
  3909. });
  3910. };
  3911. return Stack;
  3912. }(IndexedCollection));
  3913. Stack.isStack = isStack;
  3914. var StackPrototype = Stack.prototype;
  3915. StackPrototype[IS_STACK_SYMBOL] = true;
  3916. StackPrototype.shift = StackPrototype.pop;
  3917. StackPrototype.unshift = StackPrototype.push;
  3918. StackPrototype.unshiftAll = StackPrototype.pushAll;
  3919. StackPrototype.withMutations = withMutations;
  3920. StackPrototype.wasAltered = wasAltered;
  3921. StackPrototype.asImmutable = asImmutable;
  3922. StackPrototype['@@transducer/init'] = StackPrototype.asMutable = asMutable;
  3923. StackPrototype['@@transducer/step'] = function (result, arr) {
  3924. return result.unshift(arr);
  3925. };
  3926. StackPrototype['@@transducer/result'] = function (obj) {
  3927. return obj.asImmutable();
  3928. };
  3929. function makeStack(size, head, ownerID, hash) {
  3930. var map = Object.create(StackPrototype);
  3931. map.size = size;
  3932. map._head = head;
  3933. map.__ownerID = ownerID;
  3934. map.__hash = hash;
  3935. map.__altered = false;
  3936. return map;
  3937. }
  3938. var EMPTY_STACK;
  3939. function emptyStack() {
  3940. return EMPTY_STACK || (EMPTY_STACK = makeStack(0));
  3941. }
  3942. var IS_SET_SYMBOL = '@@__IMMUTABLE_SET__@@';
  3943. /**
  3944. * True if `maybeSet` is a Set.
  3945. *
  3946. * Also true for OrderedSets.
  3947. */
  3948. function isSet(maybeSet) {
  3949. return Boolean(maybeSet &&
  3950. // @ts-expect-error: maybeSet is typed as `{}`, need to change in 6.0 to `maybeSeq && typeof maybeSet === 'object' && MAYBE_SET_SYMBOL in maybeSet`
  3951. maybeSet[IS_SET_SYMBOL]);
  3952. }
  3953. /**
  3954. * True if `maybeOrderedSet` is an OrderedSet.
  3955. */
  3956. function isOrderedSet(maybeOrderedSet) {
  3957. return isSet(maybeOrderedSet) && isOrdered(maybeOrderedSet);
  3958. }
  3959. function deepEqual(a, b) {
  3960. if (a === b) {
  3961. return true;
  3962. }
  3963. if (!isCollection(b) ||
  3964. // @ts-expect-error size should exists on Collection
  3965. (a.size !== undefined && b.size !== undefined && a.size !== b.size) ||
  3966. // @ts-expect-error __hash exists on Collection
  3967. (a.__hash !== undefined &&
  3968. // @ts-expect-error __hash exists on Collection
  3969. b.__hash !== undefined &&
  3970. // @ts-expect-error __hash exists on Collection
  3971. a.__hash !== b.__hash) ||
  3972. isKeyed(a) !== isKeyed(b) ||
  3973. isIndexed(a) !== isIndexed(b) ||
  3974. // @ts-expect-error Range extends Collection, which implements [Symbol.iterator], so it is valid
  3975. isOrdered(a) !== isOrdered(b)) {
  3976. return false;
  3977. }
  3978. // @ts-expect-error size should exists on Collection
  3979. if (a.size === 0 && b.size === 0) {
  3980. return true;
  3981. }
  3982. var notAssociative = !isAssociative(a);
  3983. // @ts-expect-error Range extends Collection, which implements [Symbol.iterator], so it is valid
  3984. if (isOrdered(a)) {
  3985. var entries = a.entries();
  3986. // @ts-expect-error need to cast as boolean
  3987. return (b.every(function (v, k) {
  3988. var entry = entries.next().value;
  3989. return entry && is(entry[1], v) && (notAssociative || is(entry[0], k));
  3990. }) && entries.next().done);
  3991. }
  3992. var flipped = false;
  3993. if (a.size === undefined) {
  3994. // @ts-expect-error size should exists on Collection
  3995. if (b.size === undefined) {
  3996. if (typeof a.cacheResult === 'function') {
  3997. a.cacheResult();
  3998. }
  3999. }
  4000. else {
  4001. flipped = true;
  4002. var _ = a;
  4003. a = b;
  4004. b = _;
  4005. }
  4006. }
  4007. var allEqual = true;
  4008. var bSize =
  4009. // @ts-expect-error b is Range | Repeat | Collection<unknown, unknown> as it may have been flipped, and __iterate is valid
  4010. b.__iterate(function (v, k) {
  4011. if (notAssociative
  4012. ? // @ts-expect-error has exists on Collection
  4013. !a.has(v)
  4014. : flipped
  4015. ? // @ts-expect-error type of `get` does not "catch" the version with `notSetValue`
  4016. !is(v, a.get(k, NOT_SET))
  4017. : // @ts-expect-error type of `get` does not "catch" the version with `notSetValue`
  4018. !is(a.get(k, NOT_SET), v)) {
  4019. allEqual = false;
  4020. return false;
  4021. }
  4022. });
  4023. return (allEqual &&
  4024. // @ts-expect-error size should exists on Collection
  4025. a.size === bSize);
  4026. }
  4027. /**
  4028. * Contributes additional methods to a constructor
  4029. */
  4030. function mixin(ctor,
  4031. // eslint-disable-next-line @typescript-eslint/no-unsafe-function-type
  4032. methods) {
  4033. var keyCopier = function (key) {
  4034. // @ts-expect-error how to handle symbol ?
  4035. ctor.prototype[key] = methods[key];
  4036. };
  4037. Object.keys(methods).forEach(keyCopier);
  4038. // eslint-disable-next-line @typescript-eslint/no-unused-expressions -- TODO enable eslint here
  4039. Object.getOwnPropertySymbols &&
  4040. Object.getOwnPropertySymbols(methods).forEach(keyCopier);
  4041. return ctor;
  4042. }
  4043. function toJS(value) {
  4044. if (!value || typeof value !== 'object') {
  4045. return value;
  4046. }
  4047. if (!isCollection(value)) {
  4048. if (!isDataStructure(value)) {
  4049. return value;
  4050. }
  4051. value = Seq(value);
  4052. }
  4053. if (isKeyed(value)) {
  4054. var result$1 = {};
  4055. value.__iterate(function (v, k) {
  4056. result$1[k] = toJS(v);
  4057. });
  4058. return result$1;
  4059. }
  4060. var result = [];
  4061. value.__iterate(function (v) {
  4062. result.push(toJS(v));
  4063. });
  4064. return result;
  4065. }
  4066. var Set = /*@__PURE__*/(function (SetCollection) {
  4067. function Set(value) {
  4068. // eslint-disable-next-line no-constructor-return
  4069. return value === undefined || value === null
  4070. ? emptySet()
  4071. : isSet(value) && !isOrdered(value)
  4072. ? value
  4073. : emptySet().withMutations(function (set) {
  4074. var iter = SetCollection(value);
  4075. assertNotInfinite(iter.size);
  4076. iter.forEach(function (v) { return set.add(v); });
  4077. });
  4078. }
  4079. if ( SetCollection ) Set.__proto__ = SetCollection;
  4080. Set.prototype = Object.create( SetCollection && SetCollection.prototype );
  4081. Set.prototype.constructor = Set;
  4082. Set.of = function of (/*...values*/) {
  4083. return this(arguments);
  4084. };
  4085. Set.fromKeys = function fromKeys (value) {
  4086. return this(KeyedCollection(value).keySeq());
  4087. };
  4088. Set.intersect = function intersect (sets) {
  4089. sets = Collection(sets).toArray();
  4090. return sets.length
  4091. ? SetPrototype.intersect.apply(Set(sets.pop()), sets)
  4092. : emptySet();
  4093. };
  4094. Set.union = function union (sets) {
  4095. sets = Collection(sets).toArray();
  4096. return sets.length
  4097. ? SetPrototype.union.apply(Set(sets.pop()), sets)
  4098. : emptySet();
  4099. };
  4100. Set.prototype.toString = function toString () {
  4101. return this.__toString('Set {', '}');
  4102. };
  4103. // @pragma Access
  4104. Set.prototype.has = function has (value) {
  4105. return this._map.has(value);
  4106. };
  4107. // @pragma Modification
  4108. Set.prototype.add = function add (value) {
  4109. return updateSet(this, this._map.set(value, value));
  4110. };
  4111. Set.prototype.remove = function remove (value) {
  4112. return updateSet(this, this._map.remove(value));
  4113. };
  4114. Set.prototype.clear = function clear () {
  4115. return updateSet(this, this._map.clear());
  4116. };
  4117. // @pragma Composition
  4118. Set.prototype.map = function map (mapper, context) {
  4119. var this$1$1 = this;
  4120. // keep track if the set is altered by the map function
  4121. var didChanges = false;
  4122. var newMap = updateSet(
  4123. this,
  4124. this._map.mapEntries(function (ref) {
  4125. var v = ref[1];
  4126. var mapped = mapper.call(context, v, v, this$1$1);
  4127. if (mapped !== v) {
  4128. didChanges = true;
  4129. }
  4130. return [mapped, mapped];
  4131. }, context)
  4132. );
  4133. return didChanges ? newMap : this;
  4134. };
  4135. Set.prototype.union = function union () {
  4136. var iters = [], len = arguments.length;
  4137. while ( len-- ) iters[ len ] = arguments[ len ];
  4138. iters = iters.filter(function (x) { return x.size !== 0; });
  4139. if (iters.length === 0) {
  4140. return this;
  4141. }
  4142. if (this.size === 0 && !this.__ownerID && iters.length === 1) {
  4143. return this.constructor(iters[0]);
  4144. }
  4145. return this.withMutations(function (set) {
  4146. for (var ii = 0; ii < iters.length; ii++) {
  4147. if (typeof iters[ii] === 'string') {
  4148. set.add(iters[ii]);
  4149. } else {
  4150. SetCollection(iters[ii]).forEach(function (value) { return set.add(value); });
  4151. }
  4152. }
  4153. });
  4154. };
  4155. Set.prototype.intersect = function intersect () {
  4156. var iters = [], len = arguments.length;
  4157. while ( len-- ) iters[ len ] = arguments[ len ];
  4158. if (iters.length === 0) {
  4159. return this;
  4160. }
  4161. iters = iters.map(function (iter) { return SetCollection(iter); });
  4162. var toRemove = [];
  4163. this.forEach(function (value) {
  4164. if (!iters.every(function (iter) { return iter.includes(value); })) {
  4165. toRemove.push(value);
  4166. }
  4167. });
  4168. return this.withMutations(function (set) {
  4169. toRemove.forEach(function (value) {
  4170. set.remove(value);
  4171. });
  4172. });
  4173. };
  4174. Set.prototype.subtract = function subtract () {
  4175. var iters = [], len = arguments.length;
  4176. while ( len-- ) iters[ len ] = arguments[ len ];
  4177. if (iters.length === 0) {
  4178. return this;
  4179. }
  4180. iters = iters.map(function (iter) { return SetCollection(iter); });
  4181. var toRemove = [];
  4182. this.forEach(function (value) {
  4183. if (iters.some(function (iter) { return iter.includes(value); })) {
  4184. toRemove.push(value);
  4185. }
  4186. });
  4187. return this.withMutations(function (set) {
  4188. toRemove.forEach(function (value) {
  4189. set.remove(value);
  4190. });
  4191. });
  4192. };
  4193. Set.prototype.sort = function sort (comparator) {
  4194. // Late binding
  4195. return OrderedSet(sortFactory(this, comparator));
  4196. };
  4197. Set.prototype.sortBy = function sortBy (mapper, comparator) {
  4198. // Late binding
  4199. return OrderedSet(sortFactory(this, comparator, mapper));
  4200. };
  4201. Set.prototype.wasAltered = function wasAltered () {
  4202. return this._map.wasAltered();
  4203. };
  4204. Set.prototype.__iterate = function __iterate (fn, reverse) {
  4205. var this$1$1 = this;
  4206. return this._map.__iterate(function (k) { return fn(k, k, this$1$1); }, reverse);
  4207. };
  4208. Set.prototype.__iterator = function __iterator (type, reverse) {
  4209. return this._map.__iterator(type, reverse);
  4210. };
  4211. Set.prototype.__ensureOwner = function __ensureOwner (ownerID) {
  4212. if (ownerID === this.__ownerID) {
  4213. return this;
  4214. }
  4215. var newMap = this._map.__ensureOwner(ownerID);
  4216. if (!ownerID) {
  4217. if (this.size === 0) {
  4218. return this.__empty();
  4219. }
  4220. this.__ownerID = ownerID;
  4221. this._map = newMap;
  4222. return this;
  4223. }
  4224. return this.__make(newMap, ownerID);
  4225. };
  4226. return Set;
  4227. }(SetCollection));
  4228. Set.isSet = isSet;
  4229. var SetPrototype = Set.prototype;
  4230. SetPrototype[IS_SET_SYMBOL] = true;
  4231. SetPrototype[DELETE] = SetPrototype.remove;
  4232. SetPrototype.merge = SetPrototype.concat = SetPrototype.union;
  4233. SetPrototype.withMutations = withMutations;
  4234. SetPrototype.asImmutable = asImmutable;
  4235. SetPrototype['@@transducer/init'] = SetPrototype.asMutable = asMutable;
  4236. SetPrototype['@@transducer/step'] = function (result, arr) {
  4237. return result.add(arr);
  4238. };
  4239. SetPrototype['@@transducer/result'] = function (obj) {
  4240. return obj.asImmutable();
  4241. };
  4242. SetPrototype.__empty = emptySet;
  4243. SetPrototype.__make = makeSet;
  4244. function updateSet(set, newMap) {
  4245. if (set.__ownerID) {
  4246. set.size = newMap.size;
  4247. set._map = newMap;
  4248. return set;
  4249. }
  4250. return newMap === set._map
  4251. ? set
  4252. : newMap.size === 0
  4253. ? set.__empty()
  4254. : set.__make(newMap);
  4255. }
  4256. function makeSet(map, ownerID) {
  4257. var set = Object.create(SetPrototype);
  4258. set.size = map ? map.size : 0;
  4259. set._map = map;
  4260. set.__ownerID = ownerID;
  4261. return set;
  4262. }
  4263. var EMPTY_SET;
  4264. function emptySet() {
  4265. return EMPTY_SET || (EMPTY_SET = makeSet(emptyMap()));
  4266. }
  4267. /**
  4268. * Returns a lazy seq of nums from start (inclusive) to end
  4269. * (exclusive), by step, where start defaults to 0, step to 1, and end to
  4270. * infinity. When start is equal to end, returns empty list.
  4271. */
  4272. var Range = /*@__PURE__*/(function (IndexedSeq) {
  4273. function Range(start, end, step) {
  4274. if ( step === void 0 ) step = 1;
  4275. if (!(this instanceof Range)) {
  4276. // eslint-disable-next-line no-constructor-return
  4277. return new Range(start, end, step);
  4278. }
  4279. invariant(step !== 0, 'Cannot step a Range by 0');
  4280. invariant(
  4281. start !== undefined,
  4282. 'You must define a start value when using Range'
  4283. );
  4284. invariant(
  4285. end !== undefined,
  4286. 'You must define an end value when using Range'
  4287. );
  4288. step = Math.abs(step);
  4289. if (end < start) {
  4290. step = -step;
  4291. }
  4292. this._start = start;
  4293. this._end = end;
  4294. this._step = step;
  4295. this.size = Math.max(0, Math.ceil((end - start) / step - 1) + 1);
  4296. if (this.size === 0) {
  4297. if (EMPTY_RANGE) {
  4298. // eslint-disable-next-line no-constructor-return
  4299. return EMPTY_RANGE;
  4300. }
  4301. // eslint-disable-next-line @typescript-eslint/no-this-alias
  4302. EMPTY_RANGE = this;
  4303. }
  4304. }
  4305. if ( IndexedSeq ) Range.__proto__ = IndexedSeq;
  4306. Range.prototype = Object.create( IndexedSeq && IndexedSeq.prototype );
  4307. Range.prototype.constructor = Range;
  4308. Range.prototype.toString = function toString () {
  4309. return this.size === 0
  4310. ? 'Range []'
  4311. : ("Range [ " + (this._start) + "..." + (this._end) + (this._step !== 1 ? ' by ' + this._step : '') + " ]");
  4312. };
  4313. Range.prototype.get = function get (index, notSetValue) {
  4314. return this.has(index)
  4315. ? this._start + wrapIndex(this, index) * this._step
  4316. : notSetValue;
  4317. };
  4318. Range.prototype.includes = function includes (searchValue) {
  4319. var possibleIndex = (searchValue - this._start) / this._step;
  4320. return (
  4321. possibleIndex >= 0 &&
  4322. possibleIndex < this.size &&
  4323. possibleIndex === Math.floor(possibleIndex)
  4324. );
  4325. };
  4326. Range.prototype.slice = function slice (begin, end) {
  4327. if (wholeSlice(begin, end, this.size)) {
  4328. return this;
  4329. }
  4330. begin = resolveBegin(begin, this.size);
  4331. end = resolveEnd(end, this.size);
  4332. if (end <= begin) {
  4333. return new Range(0, 0);
  4334. }
  4335. return new Range(
  4336. this.get(begin, this._end),
  4337. this.get(end, this._end),
  4338. this._step
  4339. );
  4340. };
  4341. Range.prototype.indexOf = function indexOf (searchValue) {
  4342. var offsetValue = searchValue - this._start;
  4343. if (offsetValue % this._step === 0) {
  4344. var index = offsetValue / this._step;
  4345. if (index >= 0 && index < this.size) {
  4346. return index;
  4347. }
  4348. }
  4349. return -1;
  4350. };
  4351. Range.prototype.lastIndexOf = function lastIndexOf (searchValue) {
  4352. return this.indexOf(searchValue);
  4353. };
  4354. Range.prototype.__iterate = function __iterate (fn, reverse) {
  4355. var size = this.size;
  4356. var step = this._step;
  4357. var value = reverse ? this._start + (size - 1) * step : this._start;
  4358. var i = 0;
  4359. while (i !== size) {
  4360. if (fn(value, reverse ? size - ++i : i++, this) === false) {
  4361. break;
  4362. }
  4363. value += reverse ? -step : step;
  4364. }
  4365. return i;
  4366. };
  4367. Range.prototype.__iterator = function __iterator (type, reverse) {
  4368. var size = this.size;
  4369. var step = this._step;
  4370. var value = reverse ? this._start + (size - 1) * step : this._start;
  4371. var i = 0;
  4372. return new Iterator(function () {
  4373. if (i === size) {
  4374. return iteratorDone();
  4375. }
  4376. var v = value;
  4377. value += reverse ? -step : step;
  4378. return iteratorValue(type, reverse ? size - ++i : i++, v);
  4379. });
  4380. };
  4381. Range.prototype.equals = function equals (other) {
  4382. return other instanceof Range
  4383. ? this._start === other._start &&
  4384. this._end === other._end &&
  4385. this._step === other._step
  4386. : deepEqual(this, other);
  4387. };
  4388. return Range;
  4389. }(IndexedSeq));
  4390. var EMPTY_RANGE;
  4391. /**
  4392. * Returns the value at the provided key path starting at the provided
  4393. * collection, or notSetValue if the key path is not defined.
  4394. *
  4395. * A functional alternative to `collection.getIn(keypath)` which will also
  4396. * work with plain Objects and Arrays.
  4397. */
  4398. function getIn$1(collection, searchKeyPath, notSetValue) {
  4399. var keyPath = coerceKeyPath(searchKeyPath);
  4400. var i = 0;
  4401. while (i !== keyPath.length) {
  4402. // @ts-expect-error keyPath[i++] can not be undefined by design
  4403. collection = get(collection, keyPath[i++], NOT_SET);
  4404. if (collection === NOT_SET) {
  4405. return notSetValue;
  4406. }
  4407. }
  4408. return collection;
  4409. }
  4410. function getIn(searchKeyPath, notSetValue) {
  4411. return getIn$1(this, searchKeyPath, notSetValue);
  4412. }
  4413. /**
  4414. * Returns true if the key path is defined in the provided collection.
  4415. *
  4416. * A functional alternative to `collection.hasIn(keypath)` which will also
  4417. * work with plain Objects and Arrays.
  4418. */
  4419. function hasIn$1(collection, keyPath) {
  4420. return getIn$1(collection, keyPath, NOT_SET) !== NOT_SET;
  4421. }
  4422. function hasIn(searchKeyPath) {
  4423. return hasIn$1(this, searchKeyPath);
  4424. }
  4425. function toObject() {
  4426. assertNotInfinite(this.size);
  4427. var object = {};
  4428. this.__iterate(function (v, k) {
  4429. object[k] = v;
  4430. });
  4431. return object;
  4432. }
  4433. Collection.Iterator = Iterator;
  4434. mixin(Collection, {
  4435. // ### Conversion to other types
  4436. toArray: function toArray() {
  4437. assertNotInfinite(this.size);
  4438. var array = new Array(this.size || 0);
  4439. var useTuples = isKeyed(this);
  4440. var i = 0;
  4441. this.__iterate(function (v, k) {
  4442. // Keyed collections produce an array of tuples.
  4443. array[i++] = useTuples ? [k, v] : v;
  4444. });
  4445. return array;
  4446. },
  4447. toIndexedSeq: function toIndexedSeq() {
  4448. return new ToIndexedSequence(this);
  4449. },
  4450. toJS: function toJS$1() {
  4451. return toJS(this);
  4452. },
  4453. toKeyedSeq: function toKeyedSeq() {
  4454. return new ToKeyedSequence(this, true);
  4455. },
  4456. toMap: function toMap() {
  4457. // Use Late Binding here to solve the circular dependency.
  4458. return Map(this.toKeyedSeq());
  4459. },
  4460. toObject: toObject,
  4461. toOrderedMap: function toOrderedMap() {
  4462. // Use Late Binding here to solve the circular dependency.
  4463. return OrderedMap(this.toKeyedSeq());
  4464. },
  4465. toOrderedSet: function toOrderedSet() {
  4466. // Use Late Binding here to solve the circular dependency.
  4467. return OrderedSet(isKeyed(this) ? this.valueSeq() : this);
  4468. },
  4469. toSet: function toSet() {
  4470. // Use Late Binding here to solve the circular dependency.
  4471. return Set(isKeyed(this) ? this.valueSeq() : this);
  4472. },
  4473. toSetSeq: function toSetSeq() {
  4474. return new ToSetSequence(this);
  4475. },
  4476. toSeq: function toSeq() {
  4477. return isIndexed(this)
  4478. ? this.toIndexedSeq()
  4479. : isKeyed(this)
  4480. ? this.toKeyedSeq()
  4481. : this.toSetSeq();
  4482. },
  4483. toStack: function toStack() {
  4484. // Use Late Binding here to solve the circular dependency.
  4485. return Stack(isKeyed(this) ? this.valueSeq() : this);
  4486. },
  4487. toList: function toList() {
  4488. // Use Late Binding here to solve the circular dependency.
  4489. return List(isKeyed(this) ? this.valueSeq() : this);
  4490. },
  4491. // ### Common JavaScript methods and properties
  4492. toString: function toString() {
  4493. return '[Collection]';
  4494. },
  4495. __toString: function __toString(head, tail) {
  4496. if (this.size === 0) {
  4497. return head + tail;
  4498. }
  4499. return (
  4500. head +
  4501. ' ' +
  4502. this.toSeq().map(this.__toStringMapper).join(', ') +
  4503. ' ' +
  4504. tail
  4505. );
  4506. },
  4507. // ### ES6 Collection methods (ES6 Array and Map)
  4508. concat: function concat() {
  4509. var values = [], len = arguments.length;
  4510. while ( len-- ) values[ len ] = arguments[ len ];
  4511. return reify(this, concatFactory(this, values));
  4512. },
  4513. includes: function includes(searchValue) {
  4514. return this.some(function (value) { return is(value, searchValue); });
  4515. },
  4516. entries: function entries() {
  4517. return this.__iterator(ITERATE_ENTRIES);
  4518. },
  4519. every: function every(predicate, context) {
  4520. assertNotInfinite(this.size);
  4521. var returnValue = true;
  4522. this.__iterate(function (v, k, c) {
  4523. if (!predicate.call(context, v, k, c)) {
  4524. returnValue = false;
  4525. return false;
  4526. }
  4527. });
  4528. return returnValue;
  4529. },
  4530. filter: function filter(predicate, context) {
  4531. return reify(this, filterFactory(this, predicate, context, true));
  4532. },
  4533. partition: function partition(predicate, context) {
  4534. return partitionFactory(this, predicate, context);
  4535. },
  4536. find: function find(predicate, context, notSetValue) {
  4537. var entry = this.findEntry(predicate, context);
  4538. return entry ? entry[1] : notSetValue;
  4539. },
  4540. forEach: function forEach(sideEffect, context) {
  4541. assertNotInfinite(this.size);
  4542. return this.__iterate(context ? sideEffect.bind(context) : sideEffect);
  4543. },
  4544. join: function join(separator) {
  4545. assertNotInfinite(this.size);
  4546. separator = separator !== undefined ? '' + separator : ',';
  4547. var joined = '';
  4548. var isFirst = true;
  4549. this.__iterate(function (v) {
  4550. // eslint-disable-next-line @typescript-eslint/no-unused-expressions -- TODO enable eslint here
  4551. isFirst ? (isFirst = false) : (joined += separator);
  4552. joined += v !== null && v !== undefined ? v.toString() : '';
  4553. });
  4554. return joined;
  4555. },
  4556. keys: function keys() {
  4557. return this.__iterator(ITERATE_KEYS);
  4558. },
  4559. map: function map(mapper, context) {
  4560. return reify(this, mapFactory(this, mapper, context));
  4561. },
  4562. reduce: function reduce$1(reducer, initialReduction, context) {
  4563. return reduce(
  4564. this,
  4565. reducer,
  4566. initialReduction,
  4567. context,
  4568. arguments.length < 2,
  4569. false
  4570. );
  4571. },
  4572. reduceRight: function reduceRight(reducer, initialReduction, context) {
  4573. return reduce(
  4574. this,
  4575. reducer,
  4576. initialReduction,
  4577. context,
  4578. arguments.length < 2,
  4579. true
  4580. );
  4581. },
  4582. reverse: function reverse() {
  4583. return reify(this, reverseFactory(this, true));
  4584. },
  4585. slice: function slice(begin, end) {
  4586. return reify(this, sliceFactory(this, begin, end, true));
  4587. },
  4588. some: function some(predicate, context) {
  4589. assertNotInfinite(this.size);
  4590. var returnValue = false;
  4591. this.__iterate(function (v, k, c) {
  4592. if (predicate.call(context, v, k, c)) {
  4593. returnValue = true;
  4594. return false;
  4595. }
  4596. });
  4597. return returnValue;
  4598. },
  4599. sort: function sort(comparator) {
  4600. return reify(this, sortFactory(this, comparator));
  4601. },
  4602. values: function values() {
  4603. return this.__iterator(ITERATE_VALUES);
  4604. },
  4605. // ### More sequential methods
  4606. butLast: function butLast() {
  4607. return this.slice(0, -1);
  4608. },
  4609. isEmpty: function isEmpty() {
  4610. return this.size !== undefined ? this.size === 0 : !this.some(function () { return true; });
  4611. },
  4612. count: function count(predicate, context) {
  4613. return ensureSize(
  4614. predicate ? this.toSeq().filter(predicate, context) : this
  4615. );
  4616. },
  4617. countBy: function countBy(grouper, context) {
  4618. return countByFactory(this, grouper, context);
  4619. },
  4620. equals: function equals(other) {
  4621. return deepEqual(this, other);
  4622. },
  4623. entrySeq: function entrySeq() {
  4624. // eslint-disable-next-line @typescript-eslint/no-this-alias
  4625. var collection = this;
  4626. if (collection._cache) {
  4627. // We cache as an entries array, so we can just return the cache!
  4628. return new ArraySeq(collection._cache);
  4629. }
  4630. var entriesSequence = collection.toSeq().map(entryMapper).toIndexedSeq();
  4631. entriesSequence.fromEntrySeq = function () { return collection.toSeq(); };
  4632. return entriesSequence;
  4633. },
  4634. filterNot: function filterNot(predicate, context) {
  4635. return this.filter(not(predicate), context);
  4636. },
  4637. findEntry: function findEntry(predicate, context, notSetValue) {
  4638. var found = notSetValue;
  4639. this.__iterate(function (v, k, c) {
  4640. if (predicate.call(context, v, k, c)) {
  4641. found = [k, v];
  4642. return false;
  4643. }
  4644. });
  4645. return found;
  4646. },
  4647. findKey: function findKey(predicate, context) {
  4648. var entry = this.findEntry(predicate, context);
  4649. return entry && entry[0];
  4650. },
  4651. findLast: function findLast(predicate, context, notSetValue) {
  4652. return this.toKeyedSeq().reverse().find(predicate, context, notSetValue);
  4653. },
  4654. findLastEntry: function findLastEntry(predicate, context, notSetValue) {
  4655. return this.toKeyedSeq()
  4656. .reverse()
  4657. .findEntry(predicate, context, notSetValue);
  4658. },
  4659. findLastKey: function findLastKey(predicate, context) {
  4660. return this.toKeyedSeq().reverse().findKey(predicate, context);
  4661. },
  4662. first: function first(notSetValue) {
  4663. return this.find(returnTrue, null, notSetValue);
  4664. },
  4665. flatMap: function flatMap(mapper, context) {
  4666. return reify(this, flatMapFactory(this, mapper, context));
  4667. },
  4668. flatten: function flatten(depth) {
  4669. return reify(this, flattenFactory(this, depth, true));
  4670. },
  4671. fromEntrySeq: function fromEntrySeq() {
  4672. return new FromEntriesSequence(this);
  4673. },
  4674. get: function get(searchKey, notSetValue) {
  4675. return this.find(function (_, key) { return is(key, searchKey); }, undefined, notSetValue);
  4676. },
  4677. getIn: getIn,
  4678. groupBy: function groupBy(grouper, context) {
  4679. return groupByFactory(this, grouper, context);
  4680. },
  4681. has: function has(searchKey) {
  4682. return this.get(searchKey, NOT_SET) !== NOT_SET;
  4683. },
  4684. hasIn: hasIn,
  4685. isSubset: function isSubset(iter) {
  4686. iter = typeof iter.includes === 'function' ? iter : Collection(iter);
  4687. return this.every(function (value) { return iter.includes(value); });
  4688. },
  4689. isSuperset: function isSuperset(iter) {
  4690. iter = typeof iter.isSubset === 'function' ? iter : Collection(iter);
  4691. return iter.isSubset(this);
  4692. },
  4693. keyOf: function keyOf(searchValue) {
  4694. return this.findKey(function (value) { return is(value, searchValue); });
  4695. },
  4696. keySeq: function keySeq() {
  4697. return this.toSeq().map(keyMapper).toIndexedSeq();
  4698. },
  4699. last: function last(notSetValue) {
  4700. return this.toSeq().reverse().first(notSetValue);
  4701. },
  4702. lastKeyOf: function lastKeyOf(searchValue) {
  4703. return this.toKeyedSeq().reverse().keyOf(searchValue);
  4704. },
  4705. max: function max(comparator) {
  4706. return maxFactory(this, comparator);
  4707. },
  4708. maxBy: function maxBy(mapper, comparator) {
  4709. return maxFactory(this, comparator, mapper);
  4710. },
  4711. min: function min(comparator) {
  4712. return maxFactory(
  4713. this,
  4714. comparator ? neg(comparator) : defaultNegComparator
  4715. );
  4716. },
  4717. minBy: function minBy(mapper, comparator) {
  4718. return maxFactory(
  4719. this,
  4720. comparator ? neg(comparator) : defaultNegComparator,
  4721. mapper
  4722. );
  4723. },
  4724. rest: function rest() {
  4725. return this.slice(1);
  4726. },
  4727. skip: function skip(amount) {
  4728. return amount === 0 ? this : this.slice(Math.max(0, amount));
  4729. },
  4730. skipLast: function skipLast(amount) {
  4731. return amount === 0 ? this : this.slice(0, -Math.max(0, amount));
  4732. },
  4733. skipWhile: function skipWhile(predicate, context) {
  4734. return reify(this, skipWhileFactory(this, predicate, context, true));
  4735. },
  4736. skipUntil: function skipUntil(predicate, context) {
  4737. return this.skipWhile(not(predicate), context);
  4738. },
  4739. sortBy: function sortBy(mapper, comparator) {
  4740. return reify(this, sortFactory(this, comparator, mapper));
  4741. },
  4742. take: function take(amount) {
  4743. return this.slice(0, Math.max(0, amount));
  4744. },
  4745. takeLast: function takeLast(amount) {
  4746. return this.slice(-Math.max(0, amount));
  4747. },
  4748. takeWhile: function takeWhile(predicate, context) {
  4749. return reify(this, takeWhileFactory(this, predicate, context));
  4750. },
  4751. takeUntil: function takeUntil(predicate, context) {
  4752. return this.takeWhile(not(predicate), context);
  4753. },
  4754. update: function update(fn) {
  4755. return fn(this);
  4756. },
  4757. valueSeq: function valueSeq() {
  4758. return this.toIndexedSeq();
  4759. },
  4760. // ### Hashable Object
  4761. hashCode: function hashCode() {
  4762. return this.__hash || (this.__hash = hashCollection(this));
  4763. },
  4764. // ### Internal
  4765. // abstract __iterate(fn, reverse)
  4766. // abstract __iterator(type, reverse)
  4767. });
  4768. var CollectionPrototype = Collection.prototype;
  4769. CollectionPrototype[IS_COLLECTION_SYMBOL] = true;
  4770. CollectionPrototype[ITERATOR_SYMBOL] = CollectionPrototype.values;
  4771. CollectionPrototype.toJSON = CollectionPrototype.toArray;
  4772. CollectionPrototype.__toStringMapper = quoteString;
  4773. CollectionPrototype.inspect = CollectionPrototype.toSource = function () {
  4774. return this.toString();
  4775. };
  4776. CollectionPrototype.chain = CollectionPrototype.flatMap;
  4777. CollectionPrototype.contains = CollectionPrototype.includes;
  4778. mixin(KeyedCollection, {
  4779. // ### More sequential methods
  4780. flip: function flip() {
  4781. return reify(this, flipFactory(this));
  4782. },
  4783. mapEntries: function mapEntries(mapper, context) {
  4784. var this$1$1 = this;
  4785. var iterations = 0;
  4786. return reify(
  4787. this,
  4788. this.toSeq()
  4789. .map(function (v, k) { return mapper.call(context, [k, v], iterations++, this$1$1); })
  4790. .fromEntrySeq()
  4791. );
  4792. },
  4793. mapKeys: function mapKeys(mapper, context) {
  4794. var this$1$1 = this;
  4795. return reify(
  4796. this,
  4797. this.toSeq()
  4798. .flip()
  4799. .map(function (k, v) { return mapper.call(context, k, v, this$1$1); })
  4800. .flip()
  4801. );
  4802. },
  4803. });
  4804. var KeyedCollectionPrototype = KeyedCollection.prototype;
  4805. KeyedCollectionPrototype[IS_KEYED_SYMBOL] = true;
  4806. KeyedCollectionPrototype[ITERATOR_SYMBOL] = CollectionPrototype.entries;
  4807. KeyedCollectionPrototype.toJSON = toObject;
  4808. KeyedCollectionPrototype.__toStringMapper = function (v, k) { return quoteString(k) + ': ' + quoteString(v); };
  4809. mixin(IndexedCollection, {
  4810. // ### Conversion to other types
  4811. toKeyedSeq: function toKeyedSeq() {
  4812. return new ToKeyedSequence(this, false);
  4813. },
  4814. // ### ES6 Collection methods (ES6 Array and Map)
  4815. filter: function filter(predicate, context) {
  4816. return reify(this, filterFactory(this, predicate, context, false));
  4817. },
  4818. findIndex: function findIndex(predicate, context) {
  4819. var entry = this.findEntry(predicate, context);
  4820. return entry ? entry[0] : -1;
  4821. },
  4822. indexOf: function indexOf(searchValue) {
  4823. var key = this.keyOf(searchValue);
  4824. return key === undefined ? -1 : key;
  4825. },
  4826. lastIndexOf: function lastIndexOf(searchValue) {
  4827. var key = this.lastKeyOf(searchValue);
  4828. return key === undefined ? -1 : key;
  4829. },
  4830. reverse: function reverse() {
  4831. return reify(this, reverseFactory(this, false));
  4832. },
  4833. slice: function slice(begin, end) {
  4834. return reify(this, sliceFactory(this, begin, end, false));
  4835. },
  4836. splice: function splice(index, removeNum /*, ...values*/) {
  4837. var numArgs = arguments.length;
  4838. removeNum = Math.max(removeNum || 0, 0);
  4839. if (numArgs === 0 || (numArgs === 2 && !removeNum)) {
  4840. return this;
  4841. }
  4842. // If index is negative, it should resolve relative to the size of the
  4843. // collection. However size may be expensive to compute if not cached, so
  4844. // only call count() if the number is in fact negative.
  4845. index = resolveBegin(index, index < 0 ? this.count() : this.size);
  4846. var spliced = this.slice(0, index);
  4847. return reify(
  4848. this,
  4849. numArgs === 1
  4850. ? spliced
  4851. : spliced.concat(arrCopy(arguments, 2), this.slice(index + removeNum))
  4852. );
  4853. },
  4854. // ### More collection methods
  4855. findLastIndex: function findLastIndex(predicate, context) {
  4856. var entry = this.findLastEntry(predicate, context);
  4857. return entry ? entry[0] : -1;
  4858. },
  4859. first: function first(notSetValue) {
  4860. return this.get(0, notSetValue);
  4861. },
  4862. flatten: function flatten(depth) {
  4863. return reify(this, flattenFactory(this, depth, false));
  4864. },
  4865. get: function get(index, notSetValue) {
  4866. index = wrapIndex(this, index);
  4867. return index < 0 ||
  4868. this.size === Infinity ||
  4869. (this.size !== undefined && index > this.size)
  4870. ? notSetValue
  4871. : this.find(function (_, key) { return key === index; }, undefined, notSetValue);
  4872. },
  4873. has: function has(index) {
  4874. index = wrapIndex(this, index);
  4875. return (
  4876. index >= 0 &&
  4877. (this.size !== undefined
  4878. ? this.size === Infinity || index < this.size
  4879. : this.indexOf(index) !== -1)
  4880. );
  4881. },
  4882. interpose: function interpose(separator) {
  4883. return reify(this, interposeFactory(this, separator));
  4884. },
  4885. interleave: function interleave(/*...collections*/) {
  4886. var collections = [this].concat(arrCopy(arguments));
  4887. var zipped = zipWithFactory(this.toSeq(), IndexedSeq.of, collections);
  4888. var interleaved = zipped.flatten(true);
  4889. if (zipped.size) {
  4890. interleaved.size = zipped.size * collections.length;
  4891. }
  4892. return reify(this, interleaved);
  4893. },
  4894. keySeq: function keySeq() {
  4895. return Range(0, this.size);
  4896. },
  4897. last: function last(notSetValue) {
  4898. return this.get(-1, notSetValue);
  4899. },
  4900. skipWhile: function skipWhile(predicate, context) {
  4901. return reify(this, skipWhileFactory(this, predicate, context, false));
  4902. },
  4903. zip: function zip(/*, ...collections */) {
  4904. var collections = [this].concat(arrCopy(arguments));
  4905. return reify(this, zipWithFactory(this, defaultZipper, collections));
  4906. },
  4907. zipAll: function zipAll(/*, ...collections */) {
  4908. var collections = [this].concat(arrCopy(arguments));
  4909. return reify(this, zipWithFactory(this, defaultZipper, collections, true));
  4910. },
  4911. zipWith: function zipWith(zipper /*, ...collections */) {
  4912. var collections = arrCopy(arguments);
  4913. collections[0] = this;
  4914. return reify(this, zipWithFactory(this, zipper, collections));
  4915. },
  4916. });
  4917. var IndexedCollectionPrototype = IndexedCollection.prototype;
  4918. IndexedCollectionPrototype[IS_INDEXED_SYMBOL] = true;
  4919. IndexedCollectionPrototype[IS_ORDERED_SYMBOL] = true;
  4920. mixin(SetCollection, {
  4921. // ### ES6 Collection methods (ES6 Array and Map)
  4922. get: function get(value, notSetValue) {
  4923. return this.has(value) ? value : notSetValue;
  4924. },
  4925. includes: function includes(value) {
  4926. return this.has(value);
  4927. },
  4928. // ### More sequential methods
  4929. keySeq: function keySeq() {
  4930. return this.valueSeq();
  4931. },
  4932. });
  4933. var SetCollectionPrototype = SetCollection.prototype;
  4934. SetCollectionPrototype.has = CollectionPrototype.includes;
  4935. SetCollectionPrototype.contains = SetCollectionPrototype.includes;
  4936. SetCollectionPrototype.keys = SetCollectionPrototype.values;
  4937. // Mixin subclasses
  4938. mixin(KeyedSeq, KeyedCollectionPrototype);
  4939. mixin(IndexedSeq, IndexedCollectionPrototype);
  4940. mixin(SetSeq, SetCollectionPrototype);
  4941. // #pragma Helper functions
  4942. function reduce(collection, reducer, reduction, context, useFirst, reverse) {
  4943. assertNotInfinite(collection.size);
  4944. collection.__iterate(function (v, k, c) {
  4945. if (useFirst) {
  4946. useFirst = false;
  4947. reduction = v;
  4948. } else {
  4949. reduction = reducer.call(context, reduction, v, k, c);
  4950. }
  4951. }, reverse);
  4952. return reduction;
  4953. }
  4954. function keyMapper(v, k) {
  4955. return k;
  4956. }
  4957. function entryMapper(v, k) {
  4958. return [k, v];
  4959. }
  4960. function not(predicate) {
  4961. return function () {
  4962. return !predicate.apply(this, arguments);
  4963. };
  4964. }
  4965. function neg(predicate) {
  4966. return function () {
  4967. return -predicate.apply(this, arguments);
  4968. };
  4969. }
  4970. function defaultZipper() {
  4971. return arrCopy(arguments);
  4972. }
  4973. function defaultNegComparator(a, b) {
  4974. return a < b ? 1 : a > b ? -1 : 0;
  4975. }
  4976. function hashCollection(collection) {
  4977. if (collection.size === Infinity) {
  4978. return 0;
  4979. }
  4980. var ordered = isOrdered(collection);
  4981. var keyed = isKeyed(collection);
  4982. var h = ordered ? 1 : 0;
  4983. collection.__iterate(
  4984. keyed
  4985. ? ordered
  4986. ? function (v, k) {
  4987. h = (31 * h + hashMerge(hash(v), hash(k))) | 0;
  4988. }
  4989. : function (v, k) {
  4990. h = (h + hashMerge(hash(v), hash(k))) | 0;
  4991. }
  4992. : ordered
  4993. ? function (v) {
  4994. h = (31 * h + hash(v)) | 0;
  4995. }
  4996. : function (v) {
  4997. h = (h + hash(v)) | 0;
  4998. }
  4999. );
  5000. return murmurHashOfSize(collection.size, h);
  5001. }
  5002. function murmurHashOfSize(size, h) {
  5003. h = imul(h, 0xcc9e2d51);
  5004. h = imul((h << 15) | (h >>> -15), 0x1b873593);
  5005. h = imul((h << 13) | (h >>> -13), 5);
  5006. h = ((h + 0xe6546b64) | 0) ^ size;
  5007. h = imul(h ^ (h >>> 16), 0x85ebca6b);
  5008. h = imul(h ^ (h >>> 13), 0xc2b2ae35);
  5009. h = smi(h ^ (h >>> 16));
  5010. return h;
  5011. }
  5012. function hashMerge(a, b) {
  5013. return (a ^ (b + 0x9e3779b9 + (a << 6) + (a >> 2))) | 0; // int
  5014. }
  5015. var OrderedSet = /*@__PURE__*/(function (Set) {
  5016. function OrderedSet(value) {
  5017. // eslint-disable-next-line no-constructor-return
  5018. return value === undefined || value === null
  5019. ? emptyOrderedSet()
  5020. : isOrderedSet(value)
  5021. ? value
  5022. : emptyOrderedSet().withMutations(function (set) {
  5023. var iter = SetCollection(value);
  5024. assertNotInfinite(iter.size);
  5025. iter.forEach(function (v) { return set.add(v); });
  5026. });
  5027. }
  5028. if ( Set ) OrderedSet.__proto__ = Set;
  5029. OrderedSet.prototype = Object.create( Set && Set.prototype );
  5030. OrderedSet.prototype.constructor = OrderedSet;
  5031. OrderedSet.of = function of (/*...values*/) {
  5032. return this(arguments);
  5033. };
  5034. OrderedSet.fromKeys = function fromKeys (value) {
  5035. return this(KeyedCollection(value).keySeq());
  5036. };
  5037. OrderedSet.prototype.toString = function toString () {
  5038. return this.__toString('OrderedSet {', '}');
  5039. };
  5040. return OrderedSet;
  5041. }(Set));
  5042. OrderedSet.isOrderedSet = isOrderedSet;
  5043. var OrderedSetPrototype = OrderedSet.prototype;
  5044. OrderedSetPrototype[IS_ORDERED_SYMBOL] = true;
  5045. OrderedSetPrototype.zip = IndexedCollectionPrototype.zip;
  5046. OrderedSetPrototype.zipWith = IndexedCollectionPrototype.zipWith;
  5047. OrderedSetPrototype.zipAll = IndexedCollectionPrototype.zipAll;
  5048. OrderedSetPrototype.__empty = emptyOrderedSet;
  5049. OrderedSetPrototype.__make = makeOrderedSet;
  5050. function makeOrderedSet(map, ownerID) {
  5051. var set = Object.create(OrderedSetPrototype);
  5052. set.size = map ? map.size : 0;
  5053. set._map = map;
  5054. set.__ownerID = ownerID;
  5055. return set;
  5056. }
  5057. var EMPTY_ORDERED_SET;
  5058. function emptyOrderedSet() {
  5059. return (
  5060. EMPTY_ORDERED_SET || (EMPTY_ORDERED_SET = makeOrderedSet(emptyOrderedMap()))
  5061. );
  5062. }
  5063. var PairSorting = {
  5064. LeftThenRight: -1,
  5065. RightThenLeft: 1,
  5066. };
  5067. function throwOnInvalidDefaultValues(defaultValues) {
  5068. if (isRecord(defaultValues)) {
  5069. throw new Error(
  5070. 'Can not call `Record` with an immutable Record as default values. Use a plain javascript object instead.'
  5071. );
  5072. }
  5073. if (isImmutable(defaultValues)) {
  5074. throw new Error(
  5075. 'Can not call `Record` with an immutable Collection as default values. Use a plain javascript object instead.'
  5076. );
  5077. }
  5078. if (defaultValues === null || typeof defaultValues !== 'object') {
  5079. throw new Error(
  5080. 'Can not call `Record` with a non-object as default values. Use a plain javascript object instead.'
  5081. );
  5082. }
  5083. }
  5084. var Record = function Record(defaultValues, name) {
  5085. var hasInitialized;
  5086. throwOnInvalidDefaultValues(defaultValues);
  5087. var RecordType = function Record(values) {
  5088. var this$1$1 = this;
  5089. if (values instanceof RecordType) {
  5090. return values;
  5091. }
  5092. if (!(this instanceof RecordType)) {
  5093. return new RecordType(values);
  5094. }
  5095. if (!hasInitialized) {
  5096. hasInitialized = true;
  5097. var keys = Object.keys(defaultValues);
  5098. var indices = (RecordTypePrototype._indices = {});
  5099. // Deprecated: left to attempt not to break any external code which
  5100. // relies on a ._name property existing on record instances.
  5101. // Use Record.getDescriptiveName() instead
  5102. RecordTypePrototype._name = name;
  5103. RecordTypePrototype._keys = keys;
  5104. RecordTypePrototype._defaultValues = defaultValues;
  5105. for (var i = 0; i < keys.length; i++) {
  5106. var propName = keys[i];
  5107. indices[propName] = i;
  5108. if (RecordTypePrototype[propName]) {
  5109. /* eslint-disable no-console */
  5110. // eslint-disable-next-line @typescript-eslint/no-unused-expressions -- TODO enable eslint here
  5111. typeof console === 'object' &&
  5112. console.warn &&
  5113. console.warn(
  5114. 'Cannot define ' +
  5115. recordName(this) +
  5116. ' with property "' +
  5117. propName +
  5118. '" since that property name is part of the Record API.'
  5119. );
  5120. /* eslint-enable no-console */
  5121. } else {
  5122. setProp(RecordTypePrototype, propName);
  5123. }
  5124. }
  5125. }
  5126. this.__ownerID = undefined;
  5127. this._values = List().withMutations(function (l) {
  5128. l.setSize(this$1$1._keys.length);
  5129. KeyedCollection(values).forEach(function (v, k) {
  5130. l.set(this$1$1._indices[k], v === this$1$1._defaultValues[k] ? undefined : v);
  5131. });
  5132. });
  5133. return this;
  5134. };
  5135. var RecordTypePrototype = (RecordType.prototype =
  5136. Object.create(RecordPrototype));
  5137. RecordTypePrototype.constructor = RecordType;
  5138. if (name) {
  5139. RecordType.displayName = name;
  5140. }
  5141. // eslint-disable-next-line no-constructor-return
  5142. return RecordType;
  5143. };
  5144. Record.prototype.toString = function toString () {
  5145. var str = recordName(this) + ' { ';
  5146. var keys = this._keys;
  5147. var k;
  5148. for (var i = 0, l = keys.length; i !== l; i++) {
  5149. k = keys[i];
  5150. str += (i ? ', ' : '') + k + ': ' + quoteString(this.get(k));
  5151. }
  5152. return str + ' }';
  5153. };
  5154. Record.prototype.equals = function equals (other) {
  5155. return (
  5156. this === other ||
  5157. (isRecord(other) && recordSeq(this).equals(recordSeq(other)))
  5158. );
  5159. };
  5160. Record.prototype.hashCode = function hashCode () {
  5161. return recordSeq(this).hashCode();
  5162. };
  5163. // @pragma Access
  5164. Record.prototype.has = function has (k) {
  5165. return this._indices.hasOwnProperty(k);
  5166. };
  5167. Record.prototype.get = function get (k, notSetValue) {
  5168. if (!this.has(k)) {
  5169. return notSetValue;
  5170. }
  5171. var index = this._indices[k];
  5172. var value = this._values.get(index);
  5173. return value === undefined ? this._defaultValues[k] : value;
  5174. };
  5175. // @pragma Modification
  5176. Record.prototype.set = function set (k, v) {
  5177. if (this.has(k)) {
  5178. var newValues = this._values.set(
  5179. this._indices[k],
  5180. v === this._defaultValues[k] ? undefined : v
  5181. );
  5182. if (newValues !== this._values && !this.__ownerID) {
  5183. return makeRecord(this, newValues);
  5184. }
  5185. }
  5186. return this;
  5187. };
  5188. Record.prototype.remove = function remove (k) {
  5189. return this.set(k);
  5190. };
  5191. Record.prototype.clear = function clear () {
  5192. var newValues = this._values.clear().setSize(this._keys.length);
  5193. return this.__ownerID ? this : makeRecord(this, newValues);
  5194. };
  5195. Record.prototype.wasAltered = function wasAltered () {
  5196. return this._values.wasAltered();
  5197. };
  5198. Record.prototype.toSeq = function toSeq () {
  5199. return recordSeq(this);
  5200. };
  5201. Record.prototype.toJS = function toJS$1 () {
  5202. return toJS(this);
  5203. };
  5204. Record.prototype.entries = function entries () {
  5205. return this.__iterator(ITERATE_ENTRIES);
  5206. };
  5207. Record.prototype.__iterator = function __iterator (type, reverse) {
  5208. return recordSeq(this).__iterator(type, reverse);
  5209. };
  5210. Record.prototype.__iterate = function __iterate (fn, reverse) {
  5211. return recordSeq(this).__iterate(fn, reverse);
  5212. };
  5213. Record.prototype.__ensureOwner = function __ensureOwner (ownerID) {
  5214. if (ownerID === this.__ownerID) {
  5215. return this;
  5216. }
  5217. var newValues = this._values.__ensureOwner(ownerID);
  5218. if (!ownerID) {
  5219. this.__ownerID = ownerID;
  5220. this._values = newValues;
  5221. return this;
  5222. }
  5223. return makeRecord(this, newValues, ownerID);
  5224. };
  5225. Record.isRecord = isRecord;
  5226. Record.getDescriptiveName = recordName;
  5227. var RecordPrototype = Record.prototype;
  5228. RecordPrototype[IS_RECORD_SYMBOL] = true;
  5229. RecordPrototype[DELETE] = RecordPrototype.remove;
  5230. RecordPrototype.deleteIn = RecordPrototype.removeIn = deleteIn;
  5231. RecordPrototype.getIn = getIn;
  5232. RecordPrototype.hasIn = CollectionPrototype.hasIn;
  5233. RecordPrototype.merge = merge$1;
  5234. RecordPrototype.mergeWith = mergeWith$1;
  5235. RecordPrototype.mergeIn = mergeIn;
  5236. RecordPrototype.mergeDeep = mergeDeep;
  5237. RecordPrototype.mergeDeepWith = mergeDeepWith;
  5238. RecordPrototype.mergeDeepIn = mergeDeepIn;
  5239. RecordPrototype.setIn = setIn;
  5240. RecordPrototype.update = update;
  5241. RecordPrototype.updateIn = updateIn;
  5242. RecordPrototype.withMutations = withMutations;
  5243. RecordPrototype.asMutable = asMutable;
  5244. RecordPrototype.asImmutable = asImmutable;
  5245. RecordPrototype[ITERATOR_SYMBOL] = RecordPrototype.entries;
  5246. RecordPrototype.toJSON = RecordPrototype.toObject =
  5247. CollectionPrototype.toObject;
  5248. RecordPrototype.inspect = RecordPrototype.toSource = function () {
  5249. return this.toString();
  5250. };
  5251. function makeRecord(likeRecord, values, ownerID) {
  5252. var record = Object.create(Object.getPrototypeOf(likeRecord));
  5253. record._values = values;
  5254. record.__ownerID = ownerID;
  5255. return record;
  5256. }
  5257. function recordName(record) {
  5258. return record.constructor.displayName || record.constructor.name || 'Record';
  5259. }
  5260. function recordSeq(record) {
  5261. return keyedSeqFromValue(record._keys.map(function (k) { return [k, record.get(k)]; }));
  5262. }
  5263. function setProp(prototype, name) {
  5264. try {
  5265. Object.defineProperty(prototype, name, {
  5266. get: function () {
  5267. return this.get(name);
  5268. },
  5269. set: function (value) {
  5270. invariant(this.__ownerID, 'Cannot set on an immutable record.');
  5271. this.set(name, value);
  5272. },
  5273. });
  5274. // eslint-disable-next-line @typescript-eslint/no-unused-vars -- TODO enable eslint here
  5275. } catch (error) {
  5276. // Object.defineProperty failed. Probably IE8.
  5277. }
  5278. }
  5279. /**
  5280. * Returns a lazy Seq of `value` repeated `times` times. When `times` is
  5281. * undefined, returns an infinite sequence of `value`.
  5282. */
  5283. var Repeat = /*@__PURE__*/(function (IndexedSeq) {
  5284. function Repeat(value, times) {
  5285. if (!(this instanceof Repeat)) {
  5286. // eslint-disable-next-line no-constructor-return
  5287. return new Repeat(value, times);
  5288. }
  5289. this._value = value;
  5290. this.size = times === undefined ? Infinity : Math.max(0, times);
  5291. if (this.size === 0) {
  5292. if (EMPTY_REPEAT) {
  5293. // eslint-disable-next-line no-constructor-return
  5294. return EMPTY_REPEAT;
  5295. }
  5296. // eslint-disable-next-line @typescript-eslint/no-this-alias
  5297. EMPTY_REPEAT = this;
  5298. }
  5299. }
  5300. if ( IndexedSeq ) Repeat.__proto__ = IndexedSeq;
  5301. Repeat.prototype = Object.create( IndexedSeq && IndexedSeq.prototype );
  5302. Repeat.prototype.constructor = Repeat;
  5303. Repeat.prototype.toString = function toString () {
  5304. if (this.size === 0) {
  5305. return 'Repeat []';
  5306. }
  5307. return 'Repeat [ ' + this._value + ' ' + this.size + ' times ]';
  5308. };
  5309. Repeat.prototype.get = function get (index, notSetValue) {
  5310. return this.has(index) ? this._value : notSetValue;
  5311. };
  5312. Repeat.prototype.includes = function includes (searchValue) {
  5313. return is(this._value, searchValue);
  5314. };
  5315. Repeat.prototype.slice = function slice (begin, end) {
  5316. var size = this.size;
  5317. return wholeSlice(begin, end, size)
  5318. ? this
  5319. : new Repeat(
  5320. this._value,
  5321. resolveEnd(end, size) - resolveBegin(begin, size)
  5322. );
  5323. };
  5324. Repeat.prototype.reverse = function reverse () {
  5325. return this;
  5326. };
  5327. Repeat.prototype.indexOf = function indexOf (searchValue) {
  5328. if (is(this._value, searchValue)) {
  5329. return 0;
  5330. }
  5331. return -1;
  5332. };
  5333. Repeat.prototype.lastIndexOf = function lastIndexOf (searchValue) {
  5334. if (is(this._value, searchValue)) {
  5335. return this.size;
  5336. }
  5337. return -1;
  5338. };
  5339. Repeat.prototype.__iterate = function __iterate (fn, reverse) {
  5340. var size = this.size;
  5341. var i = 0;
  5342. while (i !== size) {
  5343. if (fn(this._value, reverse ? size - ++i : i++, this) === false) {
  5344. break;
  5345. }
  5346. }
  5347. return i;
  5348. };
  5349. Repeat.prototype.__iterator = function __iterator (type, reverse) {
  5350. var this$1$1 = this;
  5351. var size = this.size;
  5352. var i = 0;
  5353. return new Iterator(function () { return i === size
  5354. ? iteratorDone()
  5355. : iteratorValue(type, reverse ? size - ++i : i++, this$1$1._value); }
  5356. );
  5357. };
  5358. Repeat.prototype.equals = function equals (other) {
  5359. return other instanceof Repeat
  5360. ? is(this._value, other._value)
  5361. : deepEqual(this, other);
  5362. };
  5363. return Repeat;
  5364. }(IndexedSeq));
  5365. var EMPTY_REPEAT;
  5366. function fromJS(value, converter) {
  5367. return fromJSWith(
  5368. [],
  5369. converter || defaultConverter,
  5370. value,
  5371. '',
  5372. converter && converter.length > 2 ? [] : undefined,
  5373. { '': value }
  5374. );
  5375. }
  5376. function fromJSWith(stack, converter, value, key, keyPath, parentValue) {
  5377. if (
  5378. typeof value !== 'string' &&
  5379. !isImmutable(value) &&
  5380. (isArrayLike(value) || hasIterator(value) || isPlainObject(value))
  5381. ) {
  5382. if (~stack.indexOf(value)) {
  5383. throw new TypeError('Cannot convert circular structure to Immutable');
  5384. }
  5385. stack.push(value);
  5386. // eslint-disable-next-line @typescript-eslint/no-unused-expressions -- TODO enable eslint here
  5387. keyPath && key !== '' && keyPath.push(key);
  5388. var converted = converter.call(
  5389. parentValue,
  5390. key,
  5391. Seq(value).map(function (v, k) { return fromJSWith(stack, converter, v, k, keyPath, value); }
  5392. ),
  5393. keyPath && keyPath.slice()
  5394. );
  5395. stack.pop();
  5396. // eslint-disable-next-line @typescript-eslint/no-unused-expressions -- TODO enable eslint here
  5397. keyPath && keyPath.pop();
  5398. return converted;
  5399. }
  5400. return value;
  5401. }
  5402. function defaultConverter(k, v) {
  5403. // Effectively the opposite of "Collection.toSeq()"
  5404. return isIndexed(v) ? v.toList() : isKeyed(v) ? v.toMap() : v.toSet();
  5405. }
  5406. var version = "5.1.3";
  5407. // Note: Iterable is deprecated
  5408. var Iterable = Collection;
  5409. exports.Collection = Collection;
  5410. exports.Iterable = Iterable;
  5411. exports.List = List;
  5412. exports.Map = Map;
  5413. exports.OrderedMap = OrderedMap;
  5414. exports.OrderedSet = OrderedSet;
  5415. exports.PairSorting = PairSorting;
  5416. exports.Range = Range;
  5417. exports.Record = Record;
  5418. exports.Repeat = Repeat;
  5419. exports.Seq = Seq;
  5420. exports.Set = Set;
  5421. exports.Stack = Stack;
  5422. exports.fromJS = fromJS;
  5423. exports.get = get;
  5424. exports.getIn = getIn$1;
  5425. exports.has = has;
  5426. exports.hasIn = hasIn$1;
  5427. exports.hash = hash;
  5428. exports.is = is;
  5429. exports.isAssociative = isAssociative;
  5430. exports.isCollection = isCollection;
  5431. exports.isImmutable = isImmutable;
  5432. exports.isIndexed = isIndexed;
  5433. exports.isKeyed = isKeyed;
  5434. exports.isList = isList;
  5435. exports.isMap = isMap;
  5436. exports.isOrdered = isOrdered;
  5437. exports.isOrderedMap = isOrderedMap;
  5438. exports.isOrderedSet = isOrderedSet;
  5439. exports.isPlainObject = isPlainObject;
  5440. exports.isRecord = isRecord;
  5441. exports.isSeq = isSeq;
  5442. exports.isSet = isSet;
  5443. exports.isStack = isStack;
  5444. exports.isValueObject = isValueObject;
  5445. exports.merge = merge;
  5446. exports.mergeDeep = mergeDeep$1;
  5447. exports.mergeDeepWith = mergeDeepWith$1;
  5448. exports.mergeWith = mergeWith;
  5449. exports.remove = remove;
  5450. exports.removeIn = removeIn;
  5451. exports.set = set;
  5452. exports.setIn = setIn$1;
  5453. exports.update = update$1;
  5454. exports.updateIn = updateIn$1;
  5455. exports.version = version;
  5456. }));