immutable.es.js 171 KB

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