immutable.js 206 KB

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