immutable.es.js 172 KB

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