immutable.es.js 183 KB

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