immutable.js 195 KB

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