immutable.d.ts 191 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106
  1. /** @ignore we should disable this rules, but let's activate it to enable eslint first */
  2. /* eslint-disable @typescript-eslint/no-explicit-any, @typescript-eslint/ban-types */
  3. /**
  4. * Immutable data encourages pure functions (data-in, data-out) and lends itself
  5. * to much simpler application development and enabling techniques from
  6. * functional programming such as lazy evaluation.
  7. *
  8. * While designed to bring these powerful functional concepts to JavaScript, it
  9. * presents an Object-Oriented API familiar to Javascript engineers and closely
  10. * mirroring that of Array, Map, and Set. It is easy and efficient to convert to
  11. * and from plain Javascript types.
  12. *
  13. * ## How to read these docs
  14. *
  15. * In order to better explain what kinds of values the Immutable.js API expects
  16. * and produces, this documentation is presented in a statically typed dialect of
  17. * JavaScript (like [Flow][] or [TypeScript][]). You *don't need* to use these
  18. * type checking tools in order to use Immutable.js, however becoming familiar
  19. * with their syntax will help you get a deeper understanding of this API.
  20. *
  21. * **A few examples and how to read them.**
  22. *
  23. * All methods describe the kinds of data they accept and the kinds of data
  24. * they return. For example a function which accepts two numbers and returns
  25. * a number would look like this:
  26. *
  27. * ```js
  28. * sum(first: number, second: number): number
  29. * ```
  30. *
  31. * Sometimes, methods can accept different kinds of data or return different
  32. * kinds of data, and this is described with a *type variable*, which is
  33. * typically in all-caps. For example, a function which always returns the same
  34. * kind of data it was provided would look like this:
  35. *
  36. * ```js
  37. * identity<T>(value: T): T
  38. * ```
  39. *
  40. * Type variables are defined with classes and referred to in methods. For
  41. * example, a class that holds onto a value for you might look like this:
  42. *
  43. * ```js
  44. * class Box<T> {
  45. * constructor(value: T)
  46. * getValue(): T
  47. * }
  48. * ```
  49. *
  50. * In order to manipulate Immutable data, methods that we're used to affecting
  51. * a Collection instead return a new Collection of the same type. The type
  52. * `this` refers to the same kind of class. For example, a List which returns
  53. * new Lists when you `push` a value onto it might look like:
  54. *
  55. * ```js
  56. * class List<T> {
  57. * push(value: T): this
  58. * }
  59. * ```
  60. *
  61. * Many methods in Immutable.js accept values which implement the JavaScript
  62. * [Iterable][] protocol, and might appear like `Iterable<string>` for something
  63. * which represents sequence of strings. Typically in JavaScript we use plain
  64. * Arrays (`[]`) when an Iterable is expected, but also all of the Immutable.js
  65. * collections are iterable themselves!
  66. *
  67. * For example, to get a value deep within a structure of data, we might use
  68. * `getIn` which expects an `Iterable` path:
  69. *
  70. * ```
  71. * getIn(path: Iterable<string | number>): unknown
  72. * ```
  73. *
  74. * To use this method, we could pass an array: `data.getIn([ "key", 2 ])`.
  75. *
  76. *
  77. * Note: All examples are presented in the modern [ES2015][] version of
  78. * JavaScript. Use tools like Babel to support older browsers.
  79. *
  80. * For example:
  81. *
  82. * ```js
  83. * // ES2015
  84. * const mappedFoo = foo.map(x => x * x);
  85. * // ES5
  86. * var mappedFoo = foo.map(function (x) { return x * x; });
  87. * ```
  88. *
  89. * [ES2015]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/New_in_JavaScript/ECMAScript_6_support_in_Mozilla
  90. * [TypeScript]: https://www.typescriptlang.org/
  91. * [Flow]: https://flowtype.org/
  92. * [Iterable]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Iteration_protocols
  93. */
  94. declare namespace Immutable {
  95. /** @ignore */
  96. type OnlyObject<T> = Extract<T, object>;
  97. /** @ignore */
  98. type ContainObject<T> = OnlyObject<T> extends object
  99. ? OnlyObject<T> extends never
  100. ? false
  101. : true
  102. : false;
  103. /**
  104. * @ignore
  105. *
  106. * Used to convert deeply all immutable types to a plain TS type.
  107. * Using `unknown` on object instead of recursive call as we have a circular reference issue
  108. */
  109. export type DeepCopy<T> = T extends Record<infer R>
  110. ? // convert Record to DeepCopy plain JS object
  111. {
  112. [key in keyof R]: ContainObject<R[key]> extends true ? unknown : R[key];
  113. }
  114. : T extends MapOf<infer R>
  115. ? // convert MapOf to DeepCopy plain JS object
  116. {
  117. [key in keyof R]: ContainObject<R[key]> extends true ? unknown : R[key];
  118. }
  119. : T extends Collection.Keyed<infer KeyedKey, infer V>
  120. ? // convert KeyedCollection to DeepCopy plain JS object
  121. {
  122. [key in KeyedKey extends string | number | symbol
  123. ? KeyedKey
  124. : string]: V extends object ? unknown : V;
  125. }
  126. : // convert IndexedCollection or Immutable.Set to DeepCopy plain JS array
  127. // eslint-disable-next-line @typescript-eslint/no-unused-vars
  128. T extends Collection<infer _, infer V>
  129. ? Array<DeepCopy<V>>
  130. : T extends string | number // Iterable scalar types : should be kept as is
  131. ? T
  132. : T extends Iterable<infer V> // Iterable are converted to plain JS array
  133. ? Array<DeepCopy<V>>
  134. : T extends object // plain JS object are converted deeply
  135. ? {
  136. [ObjectKey in keyof T]: ContainObject<T[ObjectKey]> extends true
  137. ? unknown
  138. : T[ObjectKey];
  139. }
  140. : // other case : should be kept as is
  141. T;
  142. /**
  143. * Describes which item in a pair should be placed first when sorting
  144. *
  145. * @ignore
  146. */
  147. export enum PairSorting {
  148. LeftThenRight = -1,
  149. RightThenLeft = +1,
  150. }
  151. /**
  152. * Function comparing two items of the same type. It can return:
  153. *
  154. * * a PairSorting value, to indicate whether the left-hand item or the right-hand item should be placed before the other
  155. *
  156. * * the traditional numeric return value - especially -1, 0, or 1
  157. *
  158. * @ignore
  159. */
  160. export type Comparator<T> = (left: T, right: T) => PairSorting | number;
  161. /**
  162. * Lists are ordered indexed dense collections, much like a JavaScript
  163. * Array.
  164. *
  165. * Lists are immutable and fully persistent with O(log32 N) gets and sets,
  166. * and O(1) push and pop.
  167. *
  168. * Lists implement Deque, with efficient addition and removal from both the
  169. * end (`push`, `pop`) and beginning (`unshift`, `shift`).
  170. *
  171. * Unlike a JavaScript Array, there is no distinction between an
  172. * "unset" index and an index set to `undefined`. `List#forEach` visits all
  173. * indices from 0 to size, regardless of whether they were explicitly defined.
  174. */
  175. namespace List {
  176. /**
  177. * True if the provided value is a List
  178. *
  179. * <!-- runkit:activate -->
  180. * ```js
  181. * const { List } = require('immutable');
  182. * List.isList([]); // false
  183. * List.isList(List()); // true
  184. * ```
  185. */
  186. function isList(maybeList: unknown): maybeList is List<unknown>;
  187. /**
  188. * Creates a new List containing `values`.
  189. *
  190. * <!-- runkit:activate -->
  191. * ```js
  192. * const { List } = require('immutable');
  193. * List.of(1, 2, 3, 4)
  194. * // List [ 1, 2, 3, 4 ]
  195. * ```
  196. *
  197. * Note: Values are not altered or converted in any way.
  198. *
  199. * <!-- runkit:activate -->
  200. * ```js
  201. * const { List } = require('immutable');
  202. * List.of({x:1}, 2, [3], 4)
  203. * // List [ { x: 1 }, 2, [ 3 ], 4 ]
  204. * ```
  205. */
  206. function of<T>(...values: Array<T>): List<T>;
  207. }
  208. /**
  209. * Create a new immutable List containing the values of the provided
  210. * collection-like.
  211. *
  212. * Note: `List` is a factory function and not a class, and does not use the
  213. * `new` keyword during construction.
  214. *
  215. * <!-- runkit:activate -->
  216. * ```js
  217. * const { List, Set } = require('immutable')
  218. *
  219. * const emptyList = List()
  220. * // List []
  221. *
  222. * const plainArray = [ 1, 2, 3, 4 ]
  223. * const listFromPlainArray = List(plainArray)
  224. * // List [ 1, 2, 3, 4 ]
  225. *
  226. * const plainSet = Set([ 1, 2, 3, 4 ])
  227. * const listFromPlainSet = List(plainSet)
  228. * // List [ 1, 2, 3, 4 ]
  229. *
  230. * const arrayIterator = plainArray[Symbol.iterator]()
  231. * const listFromCollectionArray = List(arrayIterator)
  232. * // List [ 1, 2, 3, 4 ]
  233. *
  234. * listFromPlainArray.equals(listFromCollectionArray) // true
  235. * listFromPlainSet.equals(listFromCollectionArray) // true
  236. * listFromPlainSet.equals(listFromPlainArray) // true
  237. * ```
  238. */
  239. function List<T>(collection?: Iterable<T> | ArrayLike<T>): List<T>;
  240. interface List<T> extends Collection.Indexed<T> {
  241. /**
  242. * The number of items in this List.
  243. */
  244. readonly size: number;
  245. // Persistent changes
  246. /**
  247. * Returns a new List which includes `value` at `index`. If `index` already
  248. * exists in this List, it will be replaced.
  249. *
  250. * `index` may be a negative number, which indexes back from the end of the
  251. * List. `v.set(-1, "value")` sets the last item in the List.
  252. *
  253. * If `index` larger than `size`, the returned List's `size` will be large
  254. * enough to include the `index`.
  255. *
  256. * <!-- runkit:activate
  257. * { "preamble": "const { List } = require('immutable');" }
  258. * -->
  259. * ```js
  260. * const originalList = List([ 0 ]);
  261. * // List [ 0 ]
  262. * originalList.set(1, 1);
  263. * // List [ 0, 1 ]
  264. * originalList.set(0, 'overwritten');
  265. * // List [ "overwritten" ]
  266. * originalList.set(2, 2);
  267. * // List [ 0, undefined, 2 ]
  268. *
  269. * List().set(50000, 'value').size;
  270. * // 50001
  271. * ```
  272. *
  273. * Note: `set` can be used in `withMutations`.
  274. */
  275. set(index: number, value: T): List<T>;
  276. /**
  277. * Returns a new List which excludes this `index` and with a size 1 less
  278. * than this List. Values at indices above `index` are shifted down by 1 to
  279. * fill the position.
  280. *
  281. * This is synonymous with `list.splice(index, 1)`.
  282. *
  283. * `index` may be a negative number, which indexes back from the end of the
  284. * List. `v.delete(-1)` deletes the last item in the List.
  285. *
  286. * Note: `delete` cannot be safely used in IE8
  287. *
  288. * <!-- runkit:activate
  289. * { "preamble": "const { List } = require('immutable');" }
  290. * -->
  291. * ```js
  292. * List([ 0, 1, 2, 3, 4 ]).delete(0);
  293. * // List [ 1, 2, 3, 4 ]
  294. * ```
  295. *
  296. * Since `delete()` re-indexes values, it produces a complete copy, which
  297. * has `O(N)` complexity.
  298. *
  299. * Note: `delete` *cannot* be used in `withMutations`.
  300. *
  301. * @alias remove
  302. */
  303. delete(index: number): List<T>;
  304. remove(index: number): List<T>;
  305. /**
  306. * Returns a new List with `value` at `index` with a size 1 more than this
  307. * List. Values at indices above `index` are shifted over by 1.
  308. *
  309. * This is synonymous with `list.splice(index, 0, value)`.
  310. *
  311. * <!-- runkit:activate
  312. * { "preamble": "const { List } = require('immutable');" }
  313. * -->
  314. * ```js
  315. * List([ 0, 1, 2, 3, 4 ]).insert(6, 5)
  316. * // List [ 0, 1, 2, 3, 4, 5 ]
  317. * ```
  318. *
  319. * Since `insert()` re-indexes values, it produces a complete copy, which
  320. * has `O(N)` complexity.
  321. *
  322. * Note: `insert` *cannot* be used in `withMutations`.
  323. */
  324. insert(index: number, value: T): List<T>;
  325. /**
  326. * Returns a new List with 0 size and no values in constant time.
  327. *
  328. * <!-- runkit:activate
  329. * { "preamble": "const { List } = require('immutable');" }
  330. * -->
  331. * ```js
  332. * List([ 1, 2, 3, 4 ]).clear()
  333. * // List []
  334. * ```
  335. *
  336. * Note: `clear` can be used in `withMutations`.
  337. */
  338. clear(): List<T>;
  339. /**
  340. * Returns a new List with the provided `values` appended, starting at this
  341. * List's `size`.
  342. *
  343. * <!-- runkit:activate
  344. * { "preamble": "const { List } = require('immutable');" }
  345. * -->
  346. * ```js
  347. * List([ 1, 2, 3, 4 ]).push(5)
  348. * // List [ 1, 2, 3, 4, 5 ]
  349. * ```
  350. *
  351. * Note: `push` can be used in `withMutations`.
  352. */
  353. push(...values: Array<T>): List<T>;
  354. /**
  355. * Returns a new List with a size ones less than this List, excluding
  356. * the last index in this List.
  357. *
  358. * Note: this differs from `Array#pop` because it returns a new
  359. * List rather than the removed value. Use `last()` to get the last value
  360. * in this List.
  361. *
  362. * ```js
  363. * List([ 1, 2, 3, 4 ]).pop()
  364. * // List[ 1, 2, 3 ]
  365. * ```
  366. *
  367. * Note: `pop` can be used in `withMutations`.
  368. */
  369. pop(): List<T>;
  370. /**
  371. * Returns a new List with the provided `values` prepended, shifting other
  372. * values ahead to higher indices.
  373. *
  374. * <!-- runkit:activate
  375. * { "preamble": "const { List } = require('immutable');" }
  376. * -->
  377. * ```js
  378. * List([ 2, 3, 4]).unshift(1);
  379. * // List [ 1, 2, 3, 4 ]
  380. * ```
  381. *
  382. * Note: `unshift` can be used in `withMutations`.
  383. */
  384. unshift(...values: Array<T>): List<T>;
  385. /**
  386. * Returns a new List with a size ones less than this List, excluding
  387. * the first index in this List, shifting all other values to a lower index.
  388. *
  389. * Note: this differs from `Array#shift` because it returns a new
  390. * List rather than the removed value. Use `first()` to get the first
  391. * value in this List.
  392. *
  393. * <!-- runkit:activate
  394. * { "preamble": "const { List } = require('immutable');" }
  395. * -->
  396. * ```js
  397. * List([ 0, 1, 2, 3, 4 ]).shift();
  398. * // List [ 1, 2, 3, 4 ]
  399. * ```
  400. *
  401. * Note: `shift` can be used in `withMutations`.
  402. */
  403. shift(): List<T>;
  404. /**
  405. * Returns a new List with an updated value at `index` with the return
  406. * value of calling `updater` with the existing value, or `notSetValue` if
  407. * `index` was not set. If called with a single argument, `updater` is
  408. * called with the List itself.
  409. *
  410. * `index` may be a negative number, which indexes back from the end of the
  411. * List. `v.update(-1)` updates the last item in the List.
  412. *
  413. * <!-- runkit:activate
  414. * { "preamble": "const { List } = require('immutable');" }
  415. * -->
  416. * ```js
  417. * const list = List([ 'a', 'b', 'c' ])
  418. * const result = list.update(2, val => val.toUpperCase())
  419. * // List [ "a", "b", "C" ]
  420. * ```
  421. *
  422. * This can be very useful as a way to "chain" a normal function into a
  423. * sequence of methods. RxJS calls this "let" and lodash calls it "thru".
  424. *
  425. * For example, to sum a List after mapping and filtering:
  426. *
  427. * <!-- runkit:activate
  428. * { "preamble": "const { List } = require('immutable');" }
  429. * -->
  430. * ```js
  431. * function sum(collection) {
  432. * return collection.reduce((sum, x) => sum + x, 0)
  433. * }
  434. *
  435. * List([ 1, 2, 3 ])
  436. * .map(x => x + 1)
  437. * .filter(x => x % 2 === 0)
  438. * .update(sum)
  439. * // 6
  440. * ```
  441. *
  442. * Note: `update(index)` can be used in `withMutations`.
  443. *
  444. * @see `Map#update`
  445. */
  446. update(index: number, notSetValue: T, updater: (value: T) => T): this;
  447. update(
  448. index: number,
  449. updater: (value: T | undefined) => T | undefined
  450. ): this;
  451. update<R>(updater: (value: this) => R): R;
  452. /**
  453. * Returns a new List with size `size`. If `size` is less than this
  454. * List's size, the new List will exclude values at the higher indices.
  455. * If `size` is greater than this List's size, the new List will have
  456. * undefined values for the newly available indices.
  457. *
  458. * When building a new List and the final size is known up front, `setSize`
  459. * used in conjunction with `withMutations` may result in the more
  460. * performant construction.
  461. */
  462. setSize(size: number): List<T>;
  463. // Deep persistent changes
  464. /**
  465. * Returns a new List having set `value` at this `keyPath`. If any keys in
  466. * `keyPath` do not exist, a new immutable Map will be created at that key.
  467. *
  468. * Index numbers are used as keys to determine the path to follow in
  469. * the List.
  470. *
  471. * <!-- runkit:activate -->
  472. * ```js
  473. * const { List } = require('immutable')
  474. * const list = List([ 0, 1, 2, List([ 3, 4 ])])
  475. * list.setIn([3, 0], 999);
  476. * // List [ 0, 1, 2, List [ 999, 4 ] ]
  477. * ```
  478. *
  479. * Plain JavaScript Object or Arrays may be nested within an Immutable.js
  480. * Collection, and setIn() can update those values as well, treating them
  481. * immutably by creating new copies of those values with the changes applied.
  482. *
  483. * <!-- runkit:activate -->
  484. * ```js
  485. * const { List } = require('immutable')
  486. * const list = List([ 0, 1, 2, { plain: 'object' }])
  487. * list.setIn([3, 'plain'], 'value');
  488. * // List([ 0, 1, 2, { plain: 'value' }])
  489. * ```
  490. *
  491. * Note: `setIn` can be used in `withMutations`.
  492. */
  493. setIn(keyPath: Iterable<unknown>, value: unknown): this;
  494. /**
  495. * Returns a new List having removed the value at this `keyPath`. If any
  496. * keys in `keyPath` do not exist, no change will occur.
  497. *
  498. * <!-- runkit:activate -->
  499. * ```js
  500. * const { List } = require('immutable')
  501. * const list = List([ 0, 1, 2, List([ 3, 4 ])])
  502. * list.deleteIn([3, 0]);
  503. * // List [ 0, 1, 2, List [ 4 ] ]
  504. * ```
  505. *
  506. * Plain JavaScript Object or Arrays may be nested within an Immutable.js
  507. * Collection, and removeIn() can update those values as well, treating them
  508. * immutably by creating new copies of those values with the changes applied.
  509. *
  510. * <!-- runkit:activate -->
  511. * ```js
  512. * const { List } = require('immutable')
  513. * const list = List([ 0, 1, 2, { plain: 'object' }])
  514. * list.removeIn([3, 'plain']);
  515. * // List([ 0, 1, 2, {}])
  516. * ```
  517. *
  518. * Note: `deleteIn` *cannot* be safely used in `withMutations`.
  519. *
  520. * @alias removeIn
  521. */
  522. deleteIn(keyPath: Iterable<unknown>): this;
  523. removeIn(keyPath: Iterable<unknown>): this;
  524. /**
  525. * Note: `updateIn` can be used in `withMutations`.
  526. *
  527. * @see `Map#updateIn`
  528. */
  529. updateIn(
  530. keyPath: Iterable<unknown>,
  531. notSetValue: unknown,
  532. updater: (value: unknown) => unknown
  533. ): this;
  534. updateIn(
  535. keyPath: Iterable<unknown>,
  536. updater: (value: unknown) => unknown
  537. ): this;
  538. /**
  539. * Note: `mergeIn` can be used in `withMutations`.
  540. *
  541. * @see `Map#mergeIn`
  542. */
  543. mergeIn(keyPath: Iterable<unknown>, ...collections: Array<unknown>): this;
  544. /**
  545. * Note: `mergeDeepIn` can be used in `withMutations`.
  546. *
  547. * @see `Map#mergeDeepIn`
  548. */
  549. mergeDeepIn(
  550. keyPath: Iterable<unknown>,
  551. ...collections: Array<unknown>
  552. ): this;
  553. // Transient changes
  554. /**
  555. * Note: Not all methods can be safely used on a mutable collection or within
  556. * `withMutations`! Check the documentation for each method to see if it
  557. * allows being used in `withMutations`.
  558. *
  559. * @see `Map#withMutations`
  560. */
  561. withMutations(mutator: (mutable: this) => unknown): this;
  562. /**
  563. * An alternative API for withMutations()
  564. *
  565. * Note: Not all methods can be safely used on a mutable collection or within
  566. * `withMutations`! Check the documentation for each method to see if it
  567. * allows being used in `withMutations`.
  568. *
  569. * @see `Map#asMutable`
  570. */
  571. asMutable(): this;
  572. /**
  573. * @see `Map#wasAltered`
  574. */
  575. wasAltered(): boolean;
  576. /**
  577. * @see `Map#asImmutable`
  578. */
  579. asImmutable(): this;
  580. // Sequence algorithms
  581. /**
  582. * Returns a new List with other values or collections concatenated to this one.
  583. *
  584. * Note: `concat` can be used in `withMutations`.
  585. *
  586. * @alias merge
  587. */
  588. concat<C>(...valuesOrCollections: Array<Iterable<C> | C>): List<T | C>;
  589. merge<C>(...collections: Array<Iterable<C>>): List<T | C>;
  590. /**
  591. * Returns a new List with values passed through a
  592. * `mapper` function.
  593. *
  594. * <!-- runkit:activate
  595. * { "preamble": "const { List } = require('immutable');" }
  596. * -->
  597. * ```js
  598. * List([ 1, 2 ]).map(x => 10 * x)
  599. * // List [ 10, 20 ]
  600. * ```
  601. */
  602. map<M>(
  603. mapper: (value: T, key: number, iter: this) => M,
  604. context?: unknown
  605. ): List<M>;
  606. /**
  607. * Flat-maps the List, returning a new List.
  608. *
  609. * Similar to `list.map(...).flatten(true)`.
  610. */
  611. flatMap<M>(
  612. mapper: (value: T, key: number, iter: this) => Iterable<M>,
  613. context?: unknown
  614. ): List<M>;
  615. /**
  616. * Returns a new List with only the values for which the `predicate`
  617. * function returns true.
  618. *
  619. * Note: `filter()` always returns a new instance, even if it results in
  620. * not filtering out any values.
  621. */
  622. filter<F extends T>(
  623. predicate: (value: T, index: number, iter: this) => value is F,
  624. context?: unknown
  625. ): List<F>;
  626. filter(
  627. predicate: (value: T, index: number, iter: this) => unknown,
  628. context?: unknown
  629. ): this;
  630. /**
  631. * Returns a new List with the values for which the `predicate`
  632. * function returns false and another for which is returns true.
  633. */
  634. partition<F extends T, C>(
  635. predicate: (this: C, value: T, index: number, iter: this) => value is F,
  636. context?: C
  637. ): [List<T>, List<F>];
  638. partition<C>(
  639. predicate: (this: C, value: T, index: number, iter: this) => unknown,
  640. context?: C
  641. ): [this, this];
  642. /**
  643. * Returns a List "zipped" with the provided collection.
  644. *
  645. * Like `zipWith`, but using the default `zipper`: creating an `Array`.
  646. *
  647. * <!-- runkit:activate
  648. * { "preamble": "const { List } = require('immutable');" }
  649. * -->
  650. * ```js
  651. * const a = List([ 1, 2, 3 ]);
  652. * const b = List([ 4, 5, 6 ]);
  653. * const c = a.zip(b); // List [ [ 1, 4 ], [ 2, 5 ], [ 3, 6 ] ]
  654. * ```
  655. */
  656. zip<U>(other: Collection<unknown, U>): List<[T, U]>;
  657. zip<U, V>(
  658. other: Collection<unknown, U>,
  659. other2: Collection<unknown, V>
  660. ): List<[T, U, V]>;
  661. zip(...collections: Array<Collection<unknown, unknown>>): List<unknown>;
  662. /**
  663. * Returns a List "zipped" with the provided collections.
  664. *
  665. * Unlike `zip`, `zipAll` continues zipping until the longest collection is
  666. * exhausted. Missing values from shorter collections are filled with `undefined`.
  667. *
  668. * <!-- runkit:activate
  669. * { "preamble": "const { List } = require('immutable');" }
  670. * -->
  671. * ```js
  672. * const a = List([ 1, 2 ]);
  673. * const b = List([ 3, 4, 5 ]);
  674. * const c = a.zipAll(b); // List [ [ 1, 3 ], [ 2, 4 ], [ undefined, 5 ] ]
  675. * ```
  676. *
  677. * Note: Since zipAll will return a collection as large as the largest
  678. * input, some results may contain undefined values. TypeScript cannot
  679. * account for these without cases (as of v2.5).
  680. */
  681. zipAll<U>(other: Collection<unknown, U>): List<[T, U]>;
  682. zipAll<U, V>(
  683. other: Collection<unknown, U>,
  684. other2: Collection<unknown, V>
  685. ): List<[T, U, V]>;
  686. zipAll(...collections: Array<Collection<unknown, unknown>>): List<unknown>;
  687. /**
  688. * Returns a List "zipped" with the provided collections by using a
  689. * custom `zipper` function.
  690. *
  691. * <!-- runkit:activate
  692. * { "preamble": "const { List } = require('immutable');" }
  693. * -->
  694. * ```js
  695. * const a = List([ 1, 2, 3 ]);
  696. * const b = List([ 4, 5, 6 ]);
  697. * const c = a.zipWith((a, b) => a + b, b);
  698. * // List [ 5, 7, 9 ]
  699. * ```
  700. */
  701. zipWith<U, Z>(
  702. zipper: (value: T, otherValue: U) => Z,
  703. otherCollection: Collection<unknown, U>
  704. ): List<Z>;
  705. zipWith<U, V, Z>(
  706. zipper: (value: T, otherValue: U, thirdValue: V) => Z,
  707. otherCollection: Collection<unknown, U>,
  708. thirdCollection: Collection<unknown, V>
  709. ): List<Z>;
  710. zipWith<Z>(
  711. zipper: (...values: Array<unknown>) => Z,
  712. ...collections: Array<Collection<unknown, unknown>>
  713. ): List<Z>;
  714. }
  715. /**
  716. * Immutable Map is an unordered Collection.Keyed of (key, value) pairs with
  717. * `O(log32 N)` gets and `O(log32 N)` persistent sets.
  718. *
  719. * Iteration order of a Map is undefined, however is stable. Multiple
  720. * iterations of the same Map will iterate in the same order.
  721. *
  722. * Map's keys can be of any type, and use `Immutable.is` to determine key
  723. * equality. This allows the use of any value (including NaN) as a key.
  724. *
  725. * Because `Immutable.is` returns equality based on value semantics, and
  726. * Immutable collections are treated as values, any Immutable collection may
  727. * be used as a key.
  728. *
  729. * <!-- runkit:activate -->
  730. * ```js
  731. * const { Map, List } = require('immutable');
  732. * Map().set(List([ 1 ]), 'listofone').get(List([ 1 ]));
  733. * // 'listofone'
  734. * ```
  735. *
  736. * Any JavaScript object may be used as a key, however strict identity is used
  737. * to evaluate key equality. Two similar looking objects will represent two
  738. * different keys.
  739. *
  740. * Implemented by a hash-array mapped trie.
  741. */
  742. namespace Map {
  743. /**
  744. * True if the provided value is a Map
  745. *
  746. * <!-- runkit:activate -->
  747. * ```js
  748. * const { Map } = require('immutable')
  749. * Map.isMap({}) // false
  750. * Map.isMap(Map()) // true
  751. * ```
  752. */
  753. function isMap(maybeMap: unknown): maybeMap is Map<unknown, unknown>;
  754. }
  755. /**
  756. * Creates a new Immutable Map.
  757. *
  758. * Created with the same key value pairs as the provided Collection.Keyed or
  759. * JavaScript Object or expects a Collection of [K, V] tuple entries.
  760. *
  761. * Note: `Map` is a factory function and not a class, and does not use the
  762. * `new` keyword during construction.
  763. *
  764. * <!-- runkit:activate -->
  765. * ```js
  766. * const { Map } = require('immutable')
  767. * Map({ key: "value" })
  768. * Map([ [ "key", "value" ] ])
  769. * ```
  770. *
  771. * Keep in mind, when using JS objects to construct Immutable Maps, that
  772. * JavaScript Object properties are always strings, even if written in a
  773. * quote-less shorthand, while Immutable Maps accept keys of any type.
  774. *
  775. * <!-- runkit:activate
  776. * { "preamble": "const { Map } = require('immutable');" }
  777. * -->
  778. * ```js
  779. * let obj = { 1: "one" }
  780. * Object.keys(obj) // [ "1" ]
  781. * assert.equal(obj["1"], obj[1]) // "one" === "one"
  782. *
  783. * let map = Map(obj)
  784. * assert.notEqual(map.get("1"), map.get(1)) // "one" !== undefined
  785. * ```
  786. *
  787. * Property access for JavaScript Objects first converts the key to a string,
  788. * but since Immutable Map keys can be of any type the argument to `get()` is
  789. * not altered.
  790. */
  791. function Map<K, V>(collection?: Iterable<[K, V]>): Map<K, V>;
  792. function Map<R extends { [key in string | number | symbol]: unknown }>(
  793. obj: R
  794. ): MapOf<R>;
  795. function Map<V>(obj: { [key: string]: V }): Map<string, V>;
  796. function Map<K extends string | symbol, V>(obj: { [P in K]?: V }): Map<K, V>;
  797. /**
  798. * Represent a Map constructed by an object
  799. *
  800. * @ignore
  801. */
  802. interface MapOf<R extends { [key in string | number | symbol]: unknown }>
  803. extends Map<keyof R, R[keyof R]> {
  804. /**
  805. * Returns the value associated with the provided key, or notSetValue if
  806. * the Collection does not contain this key.
  807. *
  808. * Note: it is possible a key may be associated with an `undefined` value,
  809. * so if `notSetValue` is not provided and this method returns `undefined`,
  810. * that does not guarantee the key was not found.
  811. */
  812. get<K extends keyof R>(key: K, notSetValue?: unknown): R[K];
  813. get<NSV>(key: any, notSetValue: NSV): NSV;
  814. // TODO `<const P extends ...>` can be used after dropping support for TypeScript 4.x
  815. // reference: https://www.typescriptlang.org/docs/handbook/release-notes/typescript-5-0.html#const-type-parameters
  816. // after this change, `as const` assertions can be remove from the type tests
  817. getIn<P extends ReadonlyArray<string | number | symbol>>(
  818. searchKeyPath: [...P],
  819. notSetValue?: unknown
  820. ): RetrievePath<R, P>;
  821. set<K extends keyof R>(key: K, value: R[K]): this;
  822. update(updater: (value: this) => this): this;
  823. update<K extends keyof R>(key: K, updater: (value: R[K]) => R[K]): this;
  824. update<K extends keyof R, NSV extends R[K]>(
  825. key: K,
  826. notSetValue: NSV,
  827. updater: (value: R[K]) => R[K]
  828. ): this;
  829. // Possible best type is MapOf<Omit<R, K>> but Omit seems to broke other function calls
  830. // and generate recursion error with other methods (update, merge, etc.) until those functions are defined in MapOf
  831. delete<K extends keyof R>(
  832. key: K
  833. ): Extract<R[K], undefined> extends never ? never : this;
  834. remove<K extends keyof R>(
  835. key: K
  836. ): Extract<R[K], undefined> extends never ? never : this;
  837. toJS(): { [K in keyof R]: DeepCopy<R[K]> };
  838. toJSON(): { [K in keyof R]: R[K] };
  839. }
  840. // Loosely based off of this work.
  841. // https://github.com/immutable-js/immutable-js/issues/1462#issuecomment-584123268
  842. /** @ignore */
  843. type GetMapType<S> = S extends MapOf<infer T> ? T : S;
  844. /** @ignore */
  845. type Head<T extends ReadonlyArray<any>> = T extends [
  846. infer H,
  847. ...Array<unknown>
  848. ]
  849. ? H
  850. : never;
  851. /** @ignore */
  852. type Tail<T extends ReadonlyArray<any>> = T extends [unknown, ...infer I]
  853. ? I
  854. : Array<never>;
  855. /** @ignore */
  856. type RetrievePathReducer<
  857. T,
  858. C,
  859. L extends ReadonlyArray<any>
  860. > = C extends keyof GetMapType<T>
  861. ? L extends []
  862. ? GetMapType<T>[C]
  863. : RetrievePathReducer<GetMapType<T>[C], Head<L>, Tail<L>>
  864. : never;
  865. /** @ignore */
  866. type RetrievePath<
  867. R,
  868. P extends ReadonlyArray<string | number | symbol>
  869. > = P extends [] ? P : RetrievePathReducer<R, Head<P>, Tail<P>>;
  870. interface Map<K, V> extends Collection.Keyed<K, V> {
  871. /**
  872. * The number of entries in this Map.
  873. */
  874. readonly size: number;
  875. // Persistent changes
  876. /**
  877. * Returns a new Map also containing the new key, value pair. If an equivalent
  878. * key already exists in this Map, it will be replaced.
  879. *
  880. * <!-- runkit:activate -->
  881. * ```js
  882. * const { Map } = require('immutable')
  883. * const originalMap = Map()
  884. * const newerMap = originalMap.set('key', 'value')
  885. * const newestMap = newerMap.set('key', 'newer value')
  886. *
  887. * originalMap
  888. * // Map {}
  889. * newerMap
  890. * // Map { "key": "value" }
  891. * newestMap
  892. * // Map { "key": "newer value" }
  893. * ```
  894. *
  895. * Note: `set` can be used in `withMutations`.
  896. */
  897. set(key: K, value: V): this;
  898. /**
  899. * Returns a new Map which excludes this `key`.
  900. *
  901. * Note: `delete` cannot be safely used in IE8, but is provided to mirror
  902. * the ES6 collection API.
  903. *
  904. * <!-- runkit:activate -->
  905. * ```js
  906. * const { Map } = require('immutable')
  907. * const originalMap = Map({
  908. * key: 'value',
  909. * otherKey: 'other value'
  910. * })
  911. * // Map { "key": "value", "otherKey": "other value" }
  912. * originalMap.delete('otherKey')
  913. * // Map { "key": "value" }
  914. * ```
  915. *
  916. * Note: `delete` can be used in `withMutations`.
  917. *
  918. * @alias remove
  919. */
  920. delete(key: K): this;
  921. remove(key: K): this;
  922. /**
  923. * Returns a new Map which excludes the provided `keys`.
  924. *
  925. * <!-- runkit:activate -->
  926. * ```js
  927. * const { Map } = require('immutable')
  928. * const names = Map({ a: "Aaron", b: "Barry", c: "Connor" })
  929. * names.deleteAll([ 'a', 'c' ])
  930. * // Map { "b": "Barry" }
  931. * ```
  932. *
  933. * Note: `deleteAll` can be used in `withMutations`.
  934. *
  935. * @alias removeAll
  936. */
  937. deleteAll(keys: Iterable<K>): this;
  938. removeAll(keys: Iterable<K>): this;
  939. /**
  940. * Returns a new Map containing no keys or values.
  941. *
  942. * <!-- runkit:activate -->
  943. * ```js
  944. * const { Map } = require('immutable')
  945. * Map({ key: 'value' }).clear()
  946. * // Map {}
  947. * ```
  948. *
  949. * Note: `clear` can be used in `withMutations`.
  950. */
  951. clear(): this;
  952. /**
  953. * Returns a new Map having updated the value at this `key` with the return
  954. * value of calling `updater` with the existing value.
  955. *
  956. * Similar to: `map.set(key, updater(map.get(key)))`.
  957. *
  958. * <!-- runkit:activate -->
  959. * ```js
  960. * const { Map } = require('immutable')
  961. * const aMap = Map({ key: 'value' })
  962. * const newMap = aMap.update('key', value => value + value)
  963. * // Map { "key": "valuevalue" }
  964. * ```
  965. *
  966. * This is most commonly used to call methods on collections within a
  967. * structure of data. For example, in order to `.push()` onto a nested `List`,
  968. * `update` and `push` can be used together:
  969. *
  970. * <!-- runkit:activate
  971. * { "preamble": "const { Map, List } = require('immutable');" }
  972. * -->
  973. * ```js
  974. * const aMap = Map({ nestedList: List([ 1, 2, 3 ]) })
  975. * const newMap = aMap.update('nestedList', list => list.push(4))
  976. * // Map { "nestedList": List [ 1, 2, 3, 4 ] }
  977. * ```
  978. *
  979. * When a `notSetValue` is provided, it is provided to the `updater`
  980. * function when the value at the key does not exist in the Map.
  981. *
  982. * <!-- runkit:activate
  983. * { "preamble": "const { Map } = require('immutable');" }
  984. * -->
  985. * ```js
  986. * const aMap = Map({ key: 'value' })
  987. * const newMap = aMap.update('noKey', 'no value', value => value + value)
  988. * // Map { "key": "value", "noKey": "no valueno value" }
  989. * ```
  990. *
  991. * However, if the `updater` function returns the same value it was called
  992. * with, then no change will occur. This is still true if `notSetValue`
  993. * is provided.
  994. *
  995. * <!-- runkit:activate
  996. * { "preamble": "const { Map } = require('immutable');" }
  997. * -->
  998. * ```js
  999. * const aMap = Map({ apples: 10 })
  1000. * const newMap = aMap.update('oranges', 0, val => val)
  1001. * // Map { "apples": 10 }
  1002. * assert.strictEqual(newMap, map);
  1003. * ```
  1004. *
  1005. * For code using ES2015 or later, using `notSetValue` is discourged in
  1006. * favor of function parameter default values. This helps to avoid any
  1007. * potential confusion with identify functions as described above.
  1008. *
  1009. * The previous example behaves differently when written with default values:
  1010. *
  1011. * <!-- runkit:activate
  1012. * { "preamble": "const { Map } = require('immutable');" }
  1013. * -->
  1014. * ```js
  1015. * const aMap = Map({ apples: 10 })
  1016. * const newMap = aMap.update('oranges', (val = 0) => val)
  1017. * // Map { "apples": 10, "oranges": 0 }
  1018. * ```
  1019. *
  1020. * If no key is provided, then the `updater` function return value is
  1021. * returned as well.
  1022. *
  1023. * <!-- runkit:activate
  1024. * { "preamble": "const { Map } = require('immutable');" }
  1025. * -->
  1026. * ```js
  1027. * const aMap = Map({ key: 'value' })
  1028. * const result = aMap.update(aMap => aMap.get('key'))
  1029. * // "value"
  1030. * ```
  1031. *
  1032. * This can be very useful as a way to "chain" a normal function into a
  1033. * sequence of methods. RxJS calls this "let" and lodash calls it "thru".
  1034. *
  1035. * For example, to sum the values in a Map
  1036. *
  1037. * <!-- runkit:activate
  1038. * { "preamble": "const { Map } = require('immutable');" }
  1039. * -->
  1040. * ```js
  1041. * function sum(collection) {
  1042. * return collection.reduce((sum, x) => sum + x, 0)
  1043. * }
  1044. *
  1045. * Map({ x: 1, y: 2, z: 3 })
  1046. * .map(x => x + 1)
  1047. * .filter(x => x % 2 === 0)
  1048. * .update(sum)
  1049. * // 6
  1050. * ```
  1051. *
  1052. * Note: `update(key)` can be used in `withMutations`.
  1053. */
  1054. update(key: K, notSetValue: V, updater: (value: V) => V): this;
  1055. update(key: K, updater: (value: V | undefined) => V | undefined): this;
  1056. update<R>(updater: (value: this) => R): R;
  1057. /**
  1058. * Returns a new Map resulting from merging the provided Collections
  1059. * (or JS objects) into this Map. In other words, this takes each entry of
  1060. * each collection and sets it on this Map.
  1061. *
  1062. * Note: Values provided to `merge` are shallowly converted before being
  1063. * merged. No nested values are altered.
  1064. *
  1065. * <!-- runkit:activate -->
  1066. * ```js
  1067. * const { Map } = require('immutable')
  1068. * const one = Map({ a: 10, b: 20, c: 30 })
  1069. * const two = Map({ b: 40, a: 50, d: 60 })
  1070. * one.merge(two) // Map { "a": 50, "b": 40, "c": 30, "d": 60 }
  1071. * two.merge(one) // Map { "b": 20, "a": 10, "d": 60, "c": 30 }
  1072. * ```
  1073. *
  1074. * Note: `merge` can be used in `withMutations`.
  1075. *
  1076. * @alias concat
  1077. */
  1078. merge<KC, VC>(
  1079. ...collections: Array<Iterable<[KC, VC]>>
  1080. ): Map<K | KC, Exclude<V, VC> | VC>;
  1081. merge<C>(
  1082. ...collections: Array<{ [key: string]: C }>
  1083. ): Map<K | string, Exclude<V, C> | C>;
  1084. concat<KC, VC>(
  1085. ...collections: Array<Iterable<[KC, VC]>>
  1086. ): Map<K | KC, Exclude<V, VC> | VC>;
  1087. concat<C>(
  1088. ...collections: Array<{ [key: string]: C }>
  1089. ): Map<K | string, Exclude<V, C> | C>;
  1090. /**
  1091. * Like `merge()`, `mergeWith()` returns a new Map resulting from merging
  1092. * the provided Collections (or JS objects) into this Map, but uses the
  1093. * `merger` function for dealing with conflicts.
  1094. *
  1095. * <!-- runkit:activate -->
  1096. * ```js
  1097. * const { Map } = require('immutable')
  1098. * const one = Map({ a: 10, b: 20, c: 30 })
  1099. * const two = Map({ b: 40, a: 50, d: 60 })
  1100. * one.mergeWith((oldVal, newVal) => oldVal / newVal, two)
  1101. * // { "a": 0.2, "b": 0.5, "c": 30, "d": 60 }
  1102. * two.mergeWith((oldVal, newVal) => oldVal / newVal, one)
  1103. * // { "b": 2, "a": 5, "d": 60, "c": 30 }
  1104. * ```
  1105. *
  1106. * Note: `mergeWith` can be used in `withMutations`.
  1107. */
  1108. mergeWith<KC, VC, VCC>(
  1109. merger: (oldVal: V, newVal: VC, key: K) => VCC,
  1110. ...collections: Array<Iterable<[KC, VC]>>
  1111. ): Map<K | KC, V | VC | VCC>;
  1112. mergeWith<C, CC>(
  1113. merger: (oldVal: V, newVal: C, key: string) => CC,
  1114. ...collections: Array<{ [key: string]: C }>
  1115. ): Map<K | string, V | C | CC>;
  1116. /**
  1117. * Like `merge()`, but when two compatible collections are encountered with
  1118. * the same key, it merges them as well, recursing deeply through the nested
  1119. * data. Two collections are considered to be compatible (and thus will be
  1120. * merged together) if they both fall into one of three categories: keyed
  1121. * (e.g., `Map`s, `Record`s, and objects), indexed (e.g., `List`s and
  1122. * arrays), or set-like (e.g., `Set`s). If they fall into separate
  1123. * categories, `mergeDeep` will replace the existing collection with the
  1124. * collection being merged in. This behavior can be customized by using
  1125. * `mergeDeepWith()`.
  1126. *
  1127. * Note: Indexed and set-like collections are merged using
  1128. * `concat()`/`union()` and therefore do not recurse.
  1129. *
  1130. * <!-- runkit:activate -->
  1131. * ```js
  1132. * const { Map } = require('immutable')
  1133. * const one = Map({ a: Map({ x: 10, y: 10 }), b: Map({ x: 20, y: 50 }) })
  1134. * const two = Map({ a: Map({ x: 2 }), b: Map({ y: 5 }), c: Map({ z: 3 }) })
  1135. * one.mergeDeep(two)
  1136. * // Map {
  1137. * // "a": Map { "x": 2, "y": 10 },
  1138. * // "b": Map { "x": 20, "y": 5 },
  1139. * // "c": Map { "z": 3 }
  1140. * // }
  1141. * ```
  1142. *
  1143. * Note: `mergeDeep` can be used in `withMutations`.
  1144. */
  1145. mergeDeep<KC, VC>(
  1146. ...collections: Array<Iterable<[KC, VC]>>
  1147. ): Map<K | KC, V | VC>;
  1148. mergeDeep<C>(
  1149. ...collections: Array<{ [key: string]: C }>
  1150. ): Map<K | string, V | C>;
  1151. /**
  1152. * Like `mergeDeep()`, but when two non-collections or incompatible
  1153. * collections are encountered at the same key, it uses the `merger`
  1154. * function to determine the resulting value. Collections are considered
  1155. * incompatible if they fall into separate categories between keyed,
  1156. * indexed, and set-like.
  1157. *
  1158. * <!-- runkit:activate -->
  1159. * ```js
  1160. * const { Map } = require('immutable')
  1161. * const one = Map({ a: Map({ x: 10, y: 10 }), b: Map({ x: 20, y: 50 }) })
  1162. * const two = Map({ a: Map({ x: 2 }), b: Map({ y: 5 }), c: Map({ z: 3 }) })
  1163. * one.mergeDeepWith((oldVal, newVal) => oldVal / newVal, two)
  1164. * // Map {
  1165. * // "a": Map { "x": 5, "y": 10 },
  1166. * // "b": Map { "x": 20, "y": 10 },
  1167. * // "c": Map { "z": 3 }
  1168. * // }
  1169. * ```
  1170. *
  1171. * Note: `mergeDeepWith` can be used in `withMutations`.
  1172. */
  1173. mergeDeepWith(
  1174. merger: (oldVal: unknown, newVal: unknown, key: unknown) => unknown,
  1175. ...collections: Array<Iterable<[K, V]> | { [key: string]: V }>
  1176. ): this;
  1177. // Deep persistent changes
  1178. /**
  1179. * Returns a new Map having set `value` at this `keyPath`. If any keys in
  1180. * `keyPath` do not exist, a new immutable Map will be created at that key.
  1181. *
  1182. * <!-- runkit:activate -->
  1183. * ```js
  1184. * const { Map } = require('immutable')
  1185. * const originalMap = Map({
  1186. * subObject: Map({
  1187. * subKey: 'subvalue',
  1188. * subSubObject: Map({
  1189. * subSubKey: 'subSubValue'
  1190. * })
  1191. * })
  1192. * })
  1193. *
  1194. * const newMap = originalMap.setIn(['subObject', 'subKey'], 'ha ha!')
  1195. * // Map {
  1196. * // "subObject": Map {
  1197. * // "subKey": "ha ha!",
  1198. * // "subSubObject": Map { "subSubKey": "subSubValue" }
  1199. * // }
  1200. * // }
  1201. *
  1202. * const newerMap = originalMap.setIn(
  1203. * ['subObject', 'subSubObject', 'subSubKey'],
  1204. * 'ha ha ha!'
  1205. * )
  1206. * // Map {
  1207. * // "subObject": Map {
  1208. * // "subKey": "subvalue",
  1209. * // "subSubObject": Map { "subSubKey": "ha ha ha!" }
  1210. * // }
  1211. * // }
  1212. * ```
  1213. *
  1214. * Plain JavaScript Object or Arrays may be nested within an Immutable.js
  1215. * Collection, and setIn() can update those values as well, treating them
  1216. * immutably by creating new copies of those values with the changes applied.
  1217. *
  1218. * <!-- runkit:activate -->
  1219. * ```js
  1220. * const { Map } = require('immutable')
  1221. * const originalMap = Map({
  1222. * subObject: {
  1223. * subKey: 'subvalue',
  1224. * subSubObject: {
  1225. * subSubKey: 'subSubValue'
  1226. * }
  1227. * }
  1228. * })
  1229. *
  1230. * originalMap.setIn(['subObject', 'subKey'], 'ha ha!')
  1231. * // Map {
  1232. * // "subObject": {
  1233. * // subKey: "ha ha!",
  1234. * // subSubObject: { subSubKey: "subSubValue" }
  1235. * // }
  1236. * // }
  1237. * ```
  1238. *
  1239. * If any key in the path exists but cannot be updated (such as a primitive
  1240. * like number or a custom Object like Date), an error will be thrown.
  1241. *
  1242. * Note: `setIn` can be used in `withMutations`.
  1243. */
  1244. setIn(keyPath: Iterable<unknown>, value: unknown): this;
  1245. /**
  1246. * Returns a new Map having removed the value at this `keyPath`. If any keys
  1247. * in `keyPath` do not exist, no change will occur.
  1248. *
  1249. * Note: `deleteIn` can be used in `withMutations`.
  1250. *
  1251. * @alias removeIn
  1252. */
  1253. deleteIn(keyPath: Iterable<unknown>): this;
  1254. removeIn(keyPath: Iterable<unknown>): this;
  1255. /**
  1256. * Returns a new Map having applied the `updater` to the entry found at the
  1257. * keyPath.
  1258. *
  1259. * This is most commonly used to call methods on collections nested within a
  1260. * structure of data. For example, in order to `.push()` onto a nested `List`,
  1261. * `updateIn` and `push` can be used together:
  1262. *
  1263. * <!-- runkit:activate -->
  1264. * ```js
  1265. * const { Map, List } = require('immutable')
  1266. * const map = Map({ inMap: Map({ inList: List([ 1, 2, 3 ]) }) })
  1267. * const newMap = map.updateIn(['inMap', 'inList'], list => list.push(4))
  1268. * // Map { "inMap": Map { "inList": List [ 1, 2, 3, 4 ] } }
  1269. * ```
  1270. *
  1271. * If any keys in `keyPath` do not exist, new Immutable `Map`s will
  1272. * be created at those keys. If the `keyPath` does not already contain a
  1273. * value, the `updater` function will be called with `notSetValue`, if
  1274. * provided, otherwise `undefined`.
  1275. *
  1276. * <!-- runkit:activate
  1277. * { "preamble": "const { Map } = require('immutable')" }
  1278. * -->
  1279. * ```js
  1280. * const map = Map({ a: Map({ b: Map({ c: 10 }) }) })
  1281. * const newMap = map.updateIn(['a', 'b', 'c'], val => val * 2)
  1282. * // Map { "a": Map { "b": Map { "c": 20 } } }
  1283. * ```
  1284. *
  1285. * If the `updater` function returns the same value it was called with, then
  1286. * no change will occur. This is still true if `notSetValue` is provided.
  1287. *
  1288. * <!-- runkit:activate
  1289. * { "preamble": "const { Map } = require('immutable')" }
  1290. * -->
  1291. * ```js
  1292. * const map = Map({ a: Map({ b: Map({ c: 10 }) }) })
  1293. * const newMap = map.updateIn(['a', 'b', 'x'], 100, val => val)
  1294. * // Map { "a": Map { "b": Map { "c": 10 } } }
  1295. * assert.strictEqual(newMap, aMap)
  1296. * ```
  1297. *
  1298. * For code using ES2015 or later, using `notSetValue` is discourged in
  1299. * favor of function parameter default values. This helps to avoid any
  1300. * potential confusion with identify functions as described above.
  1301. *
  1302. * The previous example behaves differently when written with default values:
  1303. *
  1304. * <!-- runkit:activate
  1305. * { "preamble": "const { Map } = require('immutable')" }
  1306. * -->
  1307. * ```js
  1308. * const map = Map({ a: Map({ b: Map({ c: 10 }) }) })
  1309. * const newMap = map.updateIn(['a', 'b', 'x'], (val = 100) => val)
  1310. * // Map { "a": Map { "b": Map { "c": 10, "x": 100 } } }
  1311. * ```
  1312. *
  1313. * Plain JavaScript Object or Arrays may be nested within an Immutable.js
  1314. * Collection, and updateIn() can update those values as well, treating them
  1315. * immutably by creating new copies of those values with the changes applied.
  1316. *
  1317. * <!-- runkit:activate
  1318. * { "preamble": "const { Map } = require('immutable')" }
  1319. * -->
  1320. * ```js
  1321. * const map = Map({ a: { b: { c: 10 } } })
  1322. * const newMap = map.updateIn(['a', 'b', 'c'], val => val * 2)
  1323. * // Map { "a": { b: { c: 20 } } }
  1324. * ```
  1325. *
  1326. * If any key in the path exists but cannot be updated (such as a primitive
  1327. * like number or a custom Object like Date), an error will be thrown.
  1328. *
  1329. * Note: `updateIn` can be used in `withMutations`.
  1330. */
  1331. updateIn(
  1332. keyPath: Iterable<unknown>,
  1333. notSetValue: unknown,
  1334. updater: (value: unknown) => unknown
  1335. ): this;
  1336. updateIn(
  1337. keyPath: Iterable<unknown>,
  1338. updater: (value: unknown) => unknown
  1339. ): this;
  1340. /**
  1341. * A combination of `updateIn` and `merge`, returning a new Map, but
  1342. * performing the merge at a point arrived at by following the keyPath.
  1343. * In other words, these two lines are equivalent:
  1344. *
  1345. * ```js
  1346. * map.updateIn(['a', 'b', 'c'], abc => abc.merge(y))
  1347. * map.mergeIn(['a', 'b', 'c'], y)
  1348. * ```
  1349. *
  1350. * Note: `mergeIn` can be used in `withMutations`.
  1351. */
  1352. mergeIn(keyPath: Iterable<unknown>, ...collections: Array<unknown>): this;
  1353. /**
  1354. * A combination of `updateIn` and `mergeDeep`, returning a new Map, but
  1355. * performing the deep merge at a point arrived at by following the keyPath.
  1356. * In other words, these two lines are equivalent:
  1357. *
  1358. * ```js
  1359. * map.updateIn(['a', 'b', 'c'], abc => abc.mergeDeep(y))
  1360. * map.mergeDeepIn(['a', 'b', 'c'], y)
  1361. * ```
  1362. *
  1363. * Note: `mergeDeepIn` can be used in `withMutations`.
  1364. */
  1365. mergeDeepIn(
  1366. keyPath: Iterable<unknown>,
  1367. ...collections: Array<unknown>
  1368. ): this;
  1369. // Transient changes
  1370. /**
  1371. * Every time you call one of the above functions, a new immutable Map is
  1372. * created. If a pure function calls a number of these to produce a final
  1373. * return value, then a penalty on performance and memory has been paid by
  1374. * creating all of the intermediate immutable Maps.
  1375. *
  1376. * If you need to apply a series of mutations to produce a new immutable
  1377. * Map, `withMutations()` creates a temporary mutable copy of the Map which
  1378. * can apply mutations in a highly performant manner. In fact, this is
  1379. * exactly how complex mutations like `merge` are done.
  1380. *
  1381. * As an example, this results in the creation of 2, not 4, new Maps:
  1382. *
  1383. * <!-- runkit:activate -->
  1384. * ```js
  1385. * const { Map } = require('immutable')
  1386. * const map1 = Map()
  1387. * const map2 = map1.withMutations(map => {
  1388. * map.set('a', 1).set('b', 2).set('c', 3)
  1389. * })
  1390. * assert.equal(map1.size, 0)
  1391. * assert.equal(map2.size, 3)
  1392. * ```
  1393. *
  1394. * Note: Not all methods can be used on a mutable collection or within
  1395. * `withMutations`! Read the documentation for each method to see if it
  1396. * is safe to use in `withMutations`.
  1397. */
  1398. withMutations(mutator: (mutable: this) => unknown): this;
  1399. /**
  1400. * Another way to avoid creation of intermediate Immutable maps is to create
  1401. * a mutable copy of this collection. Mutable copies *always* return `this`,
  1402. * and thus shouldn't be used for equality. Your function should never return
  1403. * a mutable copy of a collection, only use it internally to create a new
  1404. * collection.
  1405. *
  1406. * If possible, use `withMutations` to work with temporary mutable copies as
  1407. * it provides an easier to use API and considers many common optimizations.
  1408. *
  1409. * Note: if the collection is already mutable, `asMutable` returns itself.
  1410. *
  1411. * Note: Not all methods can be used on a mutable collection or within
  1412. * `withMutations`! Read the documentation for each method to see if it
  1413. * is safe to use in `withMutations`.
  1414. *
  1415. * @see `Map#asImmutable`
  1416. */
  1417. asMutable(): this;
  1418. /**
  1419. * Returns true if this is a mutable copy (see `asMutable()`) and mutative
  1420. * alterations have been applied.
  1421. *
  1422. * @see `Map#asMutable`
  1423. */
  1424. wasAltered(): boolean;
  1425. /**
  1426. * The yin to `asMutable`'s yang. Because it applies to mutable collections,
  1427. * this operation is *mutable* and may return itself (though may not
  1428. * return itself, i.e. if the result is an empty collection). Once
  1429. * performed, the original mutable copy must no longer be mutated since it
  1430. * may be the immutable result.
  1431. *
  1432. * If possible, use `withMutations` to work with temporary mutable copies as
  1433. * it provides an easier to use API and considers many common optimizations.
  1434. *
  1435. * @see `Map#asMutable`
  1436. */
  1437. asImmutable(): this;
  1438. // Sequence algorithms
  1439. /**
  1440. * Returns a new Map with values passed through a
  1441. * `mapper` function.
  1442. *
  1443. * Map({ a: 1, b: 2 }).map(x => 10 * x)
  1444. * // Map { a: 10, b: 20 }
  1445. */
  1446. map<M>(
  1447. mapper: (value: V, key: K, iter: this) => M,
  1448. context?: unknown
  1449. ): Map<K, M>;
  1450. /**
  1451. * @see Collection.Keyed.mapKeys
  1452. */
  1453. mapKeys<M>(
  1454. mapper: (key: K, value: V, iter: this) => M,
  1455. context?: unknown
  1456. ): Map<M, V>;
  1457. /**
  1458. * @see Collection.Keyed.mapEntries
  1459. */
  1460. mapEntries<KM, VM>(
  1461. mapper: (
  1462. entry: [K, V],
  1463. index: number,
  1464. iter: this
  1465. ) => [KM, VM] | undefined,
  1466. context?: unknown
  1467. ): Map<KM, VM>;
  1468. /**
  1469. * Flat-maps the Map, returning a new Map.
  1470. *
  1471. * Similar to `data.map(...).flatten(true)`.
  1472. */
  1473. flatMap<KM, VM>(
  1474. mapper: (value: V, key: K, iter: this) => Iterable<[KM, VM]>,
  1475. context?: unknown
  1476. ): Map<KM, VM>;
  1477. /**
  1478. * Returns a new Map with only the entries for which the `predicate`
  1479. * function returns true.
  1480. *
  1481. * Note: `filter()` always returns a new instance, even if it results in
  1482. * not filtering out any values.
  1483. */
  1484. filter<F extends V>(
  1485. predicate: (value: V, key: K, iter: this) => value is F,
  1486. context?: unknown
  1487. ): Map<K, F>;
  1488. filter(
  1489. predicate: (value: V, key: K, iter: this) => unknown,
  1490. context?: unknown
  1491. ): this;
  1492. /**
  1493. * Returns a new Map with the values for which the `predicate`
  1494. * function returns false and another for which is returns true.
  1495. */
  1496. partition<F extends V, C>(
  1497. predicate: (this: C, value: V, key: K, iter: this) => value is F,
  1498. context?: C
  1499. ): [Map<K, V>, Map<K, F>];
  1500. partition<C>(
  1501. predicate: (this: C, value: V, key: K, iter: this) => unknown,
  1502. context?: C
  1503. ): [this, this];
  1504. /**
  1505. * @see Collection.Keyed.flip
  1506. */
  1507. flip(): Map<V, K>;
  1508. /**
  1509. * Returns an OrderedMap of the same type which includes the same entries,
  1510. * stably sorted by using a `comparator`.
  1511. *
  1512. * If a `comparator` is not provided, a default comparator uses `<` and `>`.
  1513. *
  1514. * `comparator(valueA, valueB)`:
  1515. *
  1516. * * Returns `0` if the elements should not be swapped.
  1517. * * Returns `-1` (or any negative number) if `valueA` comes before `valueB`
  1518. * * Returns `1` (or any positive number) if `valueA` comes after `valueB`
  1519. * * Alternatively, can return a value of the `PairSorting` enum type
  1520. * * Is pure, i.e. it must always return the same value for the same pair
  1521. * of values.
  1522. *
  1523. * <!-- runkit:activate -->
  1524. * ```js
  1525. * const { Map } = require('immutable')
  1526. * Map({ "c": 3, "a": 1, "b": 2 }).sort((a, b) => {
  1527. * if (a < b) { return -1; }
  1528. * if (a > b) { return 1; }
  1529. * if (a === b) { return 0; }
  1530. * });
  1531. * // OrderedMap { "a": 1, "b": 2, "c": 3 }
  1532. * ```
  1533. *
  1534. * Note: `sort()` Always returns a new instance, even if the original was
  1535. * already sorted.
  1536. *
  1537. * Note: This is always an eager operation.
  1538. */
  1539. sort(comparator?: Comparator<V>): this & OrderedMap<K, V>;
  1540. /**
  1541. * Like `sort`, but also accepts a `comparatorValueMapper` which allows for
  1542. * sorting by more sophisticated means:
  1543. *
  1544. * <!-- runkit:activate -->
  1545. * ```js
  1546. * const { Map } = require('immutable')
  1547. * const beattles = Map({
  1548. * John: { name: "Lennon" },
  1549. * Paul: { name: "McCartney" },
  1550. * George: { name: "Harrison" },
  1551. * Ringo: { name: "Starr" },
  1552. * });
  1553. * beattles.sortBy(member => member.name);
  1554. * ```
  1555. *
  1556. * Note: `sortBy()` Always returns a new instance, even if the original was
  1557. * already sorted.
  1558. *
  1559. * Note: This is always an eager operation.
  1560. */
  1561. sortBy<C>(
  1562. comparatorValueMapper: (value: V, key: K, iter: this) => C,
  1563. comparator?: (valueA: C, valueB: C) => number
  1564. ): this & OrderedMap<K, V>;
  1565. }
  1566. /**
  1567. * A type of Map that has the additional guarantee that the iteration order of
  1568. * entries will be the order in which they were set().
  1569. *
  1570. * The iteration behavior of OrderedMap is the same as native ES6 Map and
  1571. * JavaScript Object.
  1572. *
  1573. * Note that `OrderedMap` are more expensive than non-ordered `Map` and may
  1574. * consume more memory. `OrderedMap#set` is amortized O(log32 N), but not
  1575. * stable.
  1576. */
  1577. namespace OrderedMap {
  1578. /**
  1579. * True if the provided value is an OrderedMap.
  1580. */
  1581. function isOrderedMap(
  1582. maybeOrderedMap: unknown
  1583. ): maybeOrderedMap is OrderedMap<unknown, unknown>;
  1584. }
  1585. /**
  1586. * Creates a new Immutable OrderedMap.
  1587. *
  1588. * Created with the same key value pairs as the provided Collection.Keyed or
  1589. * JavaScript Object or expects a Collection of [K, V] tuple entries.
  1590. *
  1591. * The iteration order of key-value pairs provided to this constructor will
  1592. * be preserved in the OrderedMap.
  1593. *
  1594. * let newOrderedMap = OrderedMap({key: "value"})
  1595. * let newOrderedMap = OrderedMap([["key", "value"]])
  1596. *
  1597. * Note: `OrderedMap` is a factory function and not a class, and does not use
  1598. * the `new` keyword during construction.
  1599. */
  1600. function OrderedMap<K, V>(collection?: Iterable<[K, V]>): OrderedMap<K, V>;
  1601. function OrderedMap<V>(obj: { [key: string]: V }): OrderedMap<string, V>;
  1602. interface OrderedMap<K, V> extends Map<K, V> {
  1603. /**
  1604. * The number of entries in this OrderedMap.
  1605. */
  1606. readonly size: number;
  1607. /**
  1608. * Returns a new OrderedMap also containing the new key, value pair. If an
  1609. * equivalent key already exists in this OrderedMap, it will be replaced
  1610. * while maintaining the existing order.
  1611. *
  1612. * <!-- runkit:activate -->
  1613. * ```js
  1614. * const { OrderedMap } = require('immutable')
  1615. * const originalMap = OrderedMap({a:1, b:1, c:1})
  1616. * const updatedMap = originalMap.set('b', 2)
  1617. *
  1618. * originalMap
  1619. * // OrderedMap {a: 1, b: 1, c: 1}
  1620. * updatedMap
  1621. * // OrderedMap {a: 1, b: 2, c: 1}
  1622. * ```
  1623. *
  1624. * Note: `set` can be used in `withMutations`.
  1625. */
  1626. set(key: K, value: V): this;
  1627. /**
  1628. * Returns a new OrderedMap resulting from merging the provided Collections
  1629. * (or JS objects) into this OrderedMap. In other words, this takes each
  1630. * entry of each collection and sets it on this OrderedMap.
  1631. *
  1632. * Note: Values provided to `merge` are shallowly converted before being
  1633. * merged. No nested values are altered.
  1634. *
  1635. * <!-- runkit:activate -->
  1636. * ```js
  1637. * const { OrderedMap } = require('immutable')
  1638. * const one = OrderedMap({ a: 10, b: 20, c: 30 })
  1639. * const two = OrderedMap({ b: 40, a: 50, d: 60 })
  1640. * one.merge(two) // OrderedMap { "a": 50, "b": 40, "c": 30, "d": 60 }
  1641. * two.merge(one) // OrderedMap { "b": 20, "a": 10, "d": 60, "c": 30 }
  1642. * ```
  1643. *
  1644. * Note: `merge` can be used in `withMutations`.
  1645. *
  1646. * @alias concat
  1647. */
  1648. merge<KC, VC>(
  1649. ...collections: Array<Iterable<[KC, VC]>>
  1650. ): OrderedMap<K | KC, Exclude<V, VC> | VC>;
  1651. merge<C>(
  1652. ...collections: Array<{ [key: string]: C }>
  1653. ): OrderedMap<K | string, Exclude<V, C> | C>;
  1654. concat<KC, VC>(
  1655. ...collections: Array<Iterable<[KC, VC]>>
  1656. ): OrderedMap<K | KC, Exclude<V, VC> | VC>;
  1657. concat<C>(
  1658. ...collections: Array<{ [key: string]: C }>
  1659. ): OrderedMap<K | string, Exclude<V, C> | C>;
  1660. mergeWith<KC, VC, VCC>(
  1661. merger: (oldVal: V, newVal: VC, key: K) => VCC,
  1662. ...collections: Array<Iterable<[KC, VC]>>
  1663. ): OrderedMap<K | KC, V | VC | VCC>;
  1664. mergeWith<C, CC>(
  1665. merger: (oldVal: V, newVal: C, key: string) => CC,
  1666. ...collections: Array<{ [key: string]: C }>
  1667. ): OrderedMap<K | string, V | C | CC>;
  1668. mergeDeep<KC, VC>(
  1669. ...collections: Array<Iterable<[KC, VC]>>
  1670. ): OrderedMap<K | KC, V | VC>;
  1671. mergeDeep<C>(
  1672. ...collections: Array<{ [key: string]: C }>
  1673. ): OrderedMap<K | string, V | C>;
  1674. // Sequence algorithms
  1675. /**
  1676. * Returns a new OrderedMap with values passed through a
  1677. * `mapper` function.
  1678. *
  1679. * OrderedMap({ a: 1, b: 2 }).map(x => 10 * x)
  1680. * // OrderedMap { "a": 10, "b": 20 }
  1681. *
  1682. * Note: `map()` always returns a new instance, even if it produced the same
  1683. * value at every step.
  1684. */
  1685. map<M>(
  1686. mapper: (value: V, key: K, iter: this) => M,
  1687. context?: unknown
  1688. ): OrderedMap<K, M>;
  1689. /**
  1690. * @see Collection.Keyed.mapKeys
  1691. */
  1692. mapKeys<M>(
  1693. mapper: (key: K, value: V, iter: this) => M,
  1694. context?: unknown
  1695. ): OrderedMap<M, V>;
  1696. /**
  1697. * @see Collection.Keyed.mapEntries
  1698. */
  1699. mapEntries<KM, VM>(
  1700. mapper: (
  1701. entry: [K, V],
  1702. index: number,
  1703. iter: this
  1704. ) => [KM, VM] | undefined,
  1705. context?: unknown
  1706. ): OrderedMap<KM, VM>;
  1707. /**
  1708. * Flat-maps the OrderedMap, returning a new OrderedMap.
  1709. *
  1710. * Similar to `data.map(...).flatten(true)`.
  1711. */
  1712. flatMap<KM, VM>(
  1713. mapper: (value: V, key: K, iter: this) => Iterable<[KM, VM]>,
  1714. context?: unknown
  1715. ): OrderedMap<KM, VM>;
  1716. /**
  1717. * Returns a new OrderedMap with only the entries for which the `predicate`
  1718. * function returns true.
  1719. *
  1720. * Note: `filter()` always returns a new instance, even if it results in
  1721. * not filtering out any values.
  1722. */
  1723. filter<F extends V>(
  1724. predicate: (value: V, key: K, iter: this) => value is F,
  1725. context?: unknown
  1726. ): OrderedMap<K, F>;
  1727. filter(
  1728. predicate: (value: V, key: K, iter: this) => unknown,
  1729. context?: unknown
  1730. ): this;
  1731. /**
  1732. * Returns a new OrderedMap with the values for which the `predicate`
  1733. * function returns false and another for which is returns true.
  1734. */
  1735. partition<F extends V, C>(
  1736. predicate: (this: C, value: V, key: K, iter: this) => value is F,
  1737. context?: C
  1738. ): [OrderedMap<K, V>, OrderedMap<K, F>];
  1739. partition<C>(
  1740. predicate: (this: C, value: V, key: K, iter: this) => unknown,
  1741. context?: C
  1742. ): [this, this];
  1743. /**
  1744. * @see Collection.Keyed.flip
  1745. */
  1746. flip(): OrderedMap<V, K>;
  1747. }
  1748. /**
  1749. * A Collection of unique values with `O(log32 N)` adds and has.
  1750. *
  1751. * When iterating a Set, the entries will be (value, value) pairs. Iteration
  1752. * order of a Set is undefined, however is stable. Multiple iterations of the
  1753. * same Set will iterate in the same order.
  1754. *
  1755. * Set values, like Map keys, may be of any type. Equality is determined using
  1756. * `Immutable.is`, enabling Sets to uniquely include other Immutable
  1757. * collections, custom value types, and NaN.
  1758. */
  1759. namespace Set {
  1760. /**
  1761. * True if the provided value is a Set
  1762. */
  1763. function isSet(maybeSet: unknown): maybeSet is Set<unknown>;
  1764. /**
  1765. * Creates a new Set containing `values`.
  1766. */
  1767. function of<T>(...values: Array<T>): Set<T>;
  1768. /**
  1769. * `Set.fromKeys()` creates a new immutable Set containing the keys from
  1770. * this Collection or JavaScript Object.
  1771. */
  1772. function fromKeys<T>(iter: Collection.Keyed<T, unknown>): Set<T>;
  1773. function fromKeys<T>(iter: Collection<T, unknown>): Set<T>;
  1774. function fromKeys(obj: { [key: string]: unknown }): Set<string>;
  1775. /**
  1776. * `Set.intersect()` creates a new immutable Set that is the intersection of
  1777. * a collection of other sets.
  1778. *
  1779. * ```js
  1780. * const { Set } = require('immutable')
  1781. * const intersected = Set.intersect([
  1782. * Set([ 'a', 'b', 'c' ])
  1783. * Set([ 'c', 'a', 't' ])
  1784. * ])
  1785. * // Set [ "a", "c" ]
  1786. * ```
  1787. */
  1788. function intersect<T>(sets: Iterable<Iterable<T>>): Set<T>;
  1789. /**
  1790. * `Set.union()` creates a new immutable Set that is the union of a
  1791. * collection of other sets.
  1792. *
  1793. * ```js
  1794. * const { Set } = require('immutable')
  1795. * const unioned = Set.union([
  1796. * Set([ 'a', 'b', 'c' ])
  1797. * Set([ 'c', 'a', 't' ])
  1798. * ])
  1799. * // Set [ "a", "b", "c", "t" ]
  1800. * ```
  1801. */
  1802. function union<T>(sets: Iterable<Iterable<T>>): Set<T>;
  1803. }
  1804. /**
  1805. * Create a new immutable Set containing the values of the provided
  1806. * collection-like.
  1807. *
  1808. * Note: `Set` is a factory function and not a class, and does not use the
  1809. * `new` keyword during construction.
  1810. */
  1811. function Set<T>(collection?: Iterable<T> | ArrayLike<T>): Set<T>;
  1812. interface Set<T> extends Collection.Set<T> {
  1813. /**
  1814. * The number of items in this Set.
  1815. */
  1816. readonly size: number;
  1817. // Persistent changes
  1818. /**
  1819. * Returns a new Set which also includes this value.
  1820. *
  1821. * Note: `add` can be used in `withMutations`.
  1822. */
  1823. add(value: T): this;
  1824. /**
  1825. * Returns a new Set which excludes this value.
  1826. *
  1827. * Note: `delete` can be used in `withMutations`.
  1828. *
  1829. * Note: `delete` **cannot** be safely used in IE8, use `remove` if
  1830. * supporting old browsers.
  1831. *
  1832. * @alias remove
  1833. */
  1834. delete(value: T): this;
  1835. remove(value: T): this;
  1836. /**
  1837. * Returns a new Set containing no values.
  1838. *
  1839. * Note: `clear` can be used in `withMutations`.
  1840. */
  1841. clear(): this;
  1842. /**
  1843. * Returns a Set including any value from `collections` that does not already
  1844. * exist in this Set.
  1845. *
  1846. * Note: `union` can be used in `withMutations`.
  1847. * @alias merge
  1848. * @alias concat
  1849. */
  1850. union<C>(...collections: Array<Iterable<C>>): Set<T | C>;
  1851. merge<C>(...collections: Array<Iterable<C>>): Set<T | C>;
  1852. concat<C>(...collections: Array<Iterable<C>>): Set<T | C>;
  1853. /**
  1854. * Returns a Set which has removed any values not also contained
  1855. * within `collections`.
  1856. *
  1857. * Note: `intersect` can be used in `withMutations`.
  1858. */
  1859. intersect(...collections: Array<Iterable<T>>): this;
  1860. /**
  1861. * Returns a Set excluding any values contained within `collections`.
  1862. *
  1863. * <!-- runkit:activate -->
  1864. * ```js
  1865. * const { OrderedSet } = require('immutable')
  1866. * OrderedSet([ 1, 2, 3 ]).subtract([1, 3])
  1867. * // OrderedSet [2]
  1868. * ```
  1869. *
  1870. * Note: `subtract` can be used in `withMutations`.
  1871. */
  1872. subtract(...collections: Array<Iterable<T>>): this;
  1873. // Transient changes
  1874. /**
  1875. * Note: Not all methods can be used on a mutable collection or within
  1876. * `withMutations`! Check the documentation for each method to see if it
  1877. * mentions being safe to use in `withMutations`.
  1878. *
  1879. * @see `Map#withMutations`
  1880. */
  1881. withMutations(mutator: (mutable: this) => unknown): this;
  1882. /**
  1883. * Note: Not all methods can be used on a mutable collection or within
  1884. * `withMutations`! Check the documentation for each method to see if it
  1885. * mentions being safe to use in `withMutations`.
  1886. *
  1887. * @see `Map#asMutable`
  1888. */
  1889. asMutable(): this;
  1890. /**
  1891. * @see `Map#wasAltered`
  1892. */
  1893. wasAltered(): boolean;
  1894. /**
  1895. * @see `Map#asImmutable`
  1896. */
  1897. asImmutable(): this;
  1898. // Sequence algorithms
  1899. /**
  1900. * Returns a new Set with values passed through a
  1901. * `mapper` function.
  1902. *
  1903. * Set([1,2]).map(x => 10 * x)
  1904. * // Set [10,20]
  1905. */
  1906. map<M>(
  1907. mapper: (value: T, key: T, iter: this) => M,
  1908. context?: unknown
  1909. ): Set<M>;
  1910. /**
  1911. * Flat-maps the Set, returning a new Set.
  1912. *
  1913. * Similar to `set.map(...).flatten(true)`.
  1914. */
  1915. flatMap<M>(
  1916. mapper: (value: T, key: T, iter: this) => Iterable<M>,
  1917. context?: unknown
  1918. ): Set<M>;
  1919. /**
  1920. * Returns a new Set with only the values for which the `predicate`
  1921. * function returns true.
  1922. *
  1923. * Note: `filter()` always returns a new instance, even if it results in
  1924. * not filtering out any values.
  1925. */
  1926. filter<F extends T>(
  1927. predicate: (value: T, key: T, iter: this) => value is F,
  1928. context?: unknown
  1929. ): Set<F>;
  1930. filter(
  1931. predicate: (value: T, key: T, iter: this) => unknown,
  1932. context?: unknown
  1933. ): this;
  1934. /**
  1935. * Returns a new Set with the values for which the `predicate` function
  1936. * returns false and another for which is returns true.
  1937. */
  1938. partition<F extends T, C>(
  1939. predicate: (this: C, value: T, key: T, iter: this) => value is F,
  1940. context?: C
  1941. ): [Set<T>, Set<F>];
  1942. partition<C>(
  1943. predicate: (this: C, value: T, key: T, iter: this) => unknown,
  1944. context?: C
  1945. ): [this, this];
  1946. /**
  1947. * Returns an OrderedSet of the same type which includes the same entries,
  1948. * stably sorted by using a `comparator`.
  1949. *
  1950. * If a `comparator` is not provided, a default comparator uses `<` and `>`.
  1951. *
  1952. * `comparator(valueA, valueB)`:
  1953. *
  1954. * * Returns `0` if the elements should not be swapped.
  1955. * * Returns `-1` (or any negative number) if `valueA` comes before `valueB`
  1956. * * Returns `1` (or any positive number) if `valueA` comes after `valueB`
  1957. * * Alternatively, can return a value of the `PairSorting` enum type
  1958. * * Is pure, i.e. it must always return the same value for the same pair
  1959. * of values.
  1960. *
  1961. * <!-- runkit:activate -->
  1962. * ```js
  1963. * const { Set } = require('immutable')
  1964. * Set(['b', 'a', 'c']).sort((a, b) => {
  1965. * if (a < b) { return -1; }
  1966. * if (a > b) { return 1; }
  1967. * if (a === b) { return 0; }
  1968. * });
  1969. * // OrderedSet { "a":, "b", "c" }
  1970. * ```
  1971. *
  1972. * Note: `sort()` Always returns a new instance, even if the original was
  1973. * already sorted.
  1974. *
  1975. * Note: This is always an eager operation.
  1976. */
  1977. sort(comparator?: Comparator<T>): this & OrderedSet<T>;
  1978. /**
  1979. * Like `sort`, but also accepts a `comparatorValueMapper` which allows for
  1980. * sorting by more sophisticated means:
  1981. *
  1982. * <!-- runkit:activate -->
  1983. * ```js
  1984. * const { Set } = require('immutable')
  1985. * const beattles = Set([
  1986. * { name: "Lennon" },
  1987. * { name: "McCartney" },
  1988. * { name: "Harrison" },
  1989. * { name: "Starr" },
  1990. * ]);
  1991. * beattles.sortBy(member => member.name);
  1992. * ```
  1993. *
  1994. * Note: `sortBy()` Always returns a new instance, even if the original was
  1995. * already sorted.
  1996. *
  1997. * Note: This is always an eager operation.
  1998. */
  1999. sortBy<C>(
  2000. comparatorValueMapper: (value: T, key: T, iter: this) => C,
  2001. comparator?: (valueA: C, valueB: C) => number
  2002. ): this & OrderedSet<T>;
  2003. }
  2004. /**
  2005. * A type of Set that has the additional guarantee that the iteration order of
  2006. * values will be the order in which they were `add`ed.
  2007. *
  2008. * The iteration behavior of OrderedSet is the same as native ES6 Set.
  2009. *
  2010. * Note that `OrderedSet` are more expensive than non-ordered `Set` and may
  2011. * consume more memory. `OrderedSet#add` is amortized O(log32 N), but not
  2012. * stable.
  2013. */
  2014. namespace OrderedSet {
  2015. /**
  2016. * True if the provided value is an OrderedSet.
  2017. */
  2018. function isOrderedSet(
  2019. maybeOrderedSet: unknown
  2020. ): maybeOrderedSet is OrderedSet<unknown>;
  2021. /**
  2022. * Creates a new OrderedSet containing `values`.
  2023. */
  2024. function of<T>(...values: Array<T>): OrderedSet<T>;
  2025. /**
  2026. * `OrderedSet.fromKeys()` creates a new immutable OrderedSet containing
  2027. * the keys from this Collection or JavaScript Object.
  2028. */
  2029. function fromKeys<T>(iter: Collection.Keyed<T, unknown>): OrderedSet<T>;
  2030. function fromKeys<T>(iter: Collection<T, unknown>): OrderedSet<T>;
  2031. function fromKeys(obj: { [key: string]: unknown }): OrderedSet<string>;
  2032. }
  2033. /**
  2034. * Create a new immutable OrderedSet containing the values of the provided
  2035. * collection-like.
  2036. *
  2037. * Note: `OrderedSet` is a factory function and not a class, and does not use
  2038. * the `new` keyword during construction.
  2039. */
  2040. function OrderedSet<T>(
  2041. collection?: Iterable<T> | ArrayLike<T>
  2042. ): OrderedSet<T>;
  2043. interface OrderedSet<T> extends Set<T> {
  2044. /**
  2045. * The number of items in this OrderedSet.
  2046. */
  2047. readonly size: number;
  2048. /**
  2049. * Returns an OrderedSet including any value from `collections` that does
  2050. * not already exist in this OrderedSet.
  2051. *
  2052. * Note: `union` can be used in `withMutations`.
  2053. * @alias merge
  2054. * @alias concat
  2055. */
  2056. union<C>(...collections: Array<Iterable<C>>): OrderedSet<T | C>;
  2057. merge<C>(...collections: Array<Iterable<C>>): OrderedSet<T | C>;
  2058. concat<C>(...collections: Array<Iterable<C>>): OrderedSet<T | C>;
  2059. // Sequence algorithms
  2060. /**
  2061. * Returns a new Set with values passed through a
  2062. * `mapper` function.
  2063. *
  2064. * OrderedSet([ 1, 2 ]).map(x => 10 * x)
  2065. * // OrderedSet [10, 20]
  2066. */
  2067. map<M>(
  2068. mapper: (value: T, key: T, iter: this) => M,
  2069. context?: unknown
  2070. ): OrderedSet<M>;
  2071. /**
  2072. * Flat-maps the OrderedSet, returning a new OrderedSet.
  2073. *
  2074. * Similar to `set.map(...).flatten(true)`.
  2075. */
  2076. flatMap<M>(
  2077. mapper: (value: T, key: T, iter: this) => Iterable<M>,
  2078. context?: unknown
  2079. ): OrderedSet<M>;
  2080. /**
  2081. * Returns a new OrderedSet with only the values for which the `predicate`
  2082. * function returns true.
  2083. *
  2084. * Note: `filter()` always returns a new instance, even if it results in
  2085. * not filtering out any values.
  2086. */
  2087. filter<F extends T>(
  2088. predicate: (value: T, key: T, iter: this) => value is F,
  2089. context?: unknown
  2090. ): OrderedSet<F>;
  2091. filter(
  2092. predicate: (value: T, key: T, iter: this) => unknown,
  2093. context?: unknown
  2094. ): this;
  2095. /**
  2096. * Returns a new OrderedSet with the values for which the `predicate`
  2097. * function returns false and another for which is returns true.
  2098. */
  2099. partition<F extends T, C>(
  2100. predicate: (this: C, value: T, key: T, iter: this) => value is F,
  2101. context?: C
  2102. ): [OrderedSet<T>, OrderedSet<F>];
  2103. partition<C>(
  2104. predicate: (this: C, value: T, key: T, iter: this) => unknown,
  2105. context?: C
  2106. ): [this, this];
  2107. /**
  2108. * Returns an OrderedSet of the same type "zipped" with the provided
  2109. * collections.
  2110. *
  2111. * Like `zipWith`, but using the default `zipper`: creating an `Array`.
  2112. *
  2113. * ```js
  2114. * const a = OrderedSet([ 1, 2, 3 ])
  2115. * const b = OrderedSet([ 4, 5, 6 ])
  2116. * const c = a.zip(b)
  2117. * // OrderedSet [ [ 1, 4 ], [ 2, 5 ], [ 3, 6 ] ]
  2118. * ```
  2119. */
  2120. zip<U>(other: Collection<unknown, U>): OrderedSet<[T, U]>;
  2121. zip<U, V>(
  2122. other1: Collection<unknown, U>,
  2123. other2: Collection<unknown, V>
  2124. ): OrderedSet<[T, U, V]>;
  2125. zip(
  2126. ...collections: Array<Collection<unknown, unknown>>
  2127. ): OrderedSet<unknown>;
  2128. /**
  2129. * Returns a OrderedSet of the same type "zipped" with the provided
  2130. * collections.
  2131. *
  2132. * Unlike `zip`, `zipAll` continues zipping until the longest collection is
  2133. * exhausted. Missing values from shorter collections are filled with `undefined`.
  2134. *
  2135. * ```js
  2136. * const a = OrderedSet([ 1, 2 ]);
  2137. * const b = OrderedSet([ 3, 4, 5 ]);
  2138. * const c = a.zipAll(b); // OrderedSet [ [ 1, 3 ], [ 2, 4 ], [ undefined, 5 ] ]
  2139. * ```
  2140. *
  2141. * Note: Since zipAll will return a collection as large as the largest
  2142. * input, some results may contain undefined values. TypeScript cannot
  2143. * account for these without cases (as of v2.5).
  2144. */
  2145. zipAll<U>(other: Collection<unknown, U>): OrderedSet<[T, U]>;
  2146. zipAll<U, V>(
  2147. other1: Collection<unknown, U>,
  2148. other2: Collection<unknown, V>
  2149. ): OrderedSet<[T, U, V]>;
  2150. zipAll(
  2151. ...collections: Array<Collection<unknown, unknown>>
  2152. ): OrderedSet<unknown>;
  2153. /**
  2154. * Returns an OrderedSet of the same type "zipped" with the provided
  2155. * collections by using a custom `zipper` function.
  2156. *
  2157. * @see Seq.Indexed.zipWith
  2158. */
  2159. zipWith<U, Z>(
  2160. zipper: (value: T, otherValue: U) => Z,
  2161. otherCollection: Collection<unknown, U>
  2162. ): OrderedSet<Z>;
  2163. zipWith<U, V, Z>(
  2164. zipper: (value: T, otherValue: U, thirdValue: V) => Z,
  2165. otherCollection: Collection<unknown, U>,
  2166. thirdCollection: Collection<unknown, V>
  2167. ): OrderedSet<Z>;
  2168. zipWith<Z>(
  2169. zipper: (...values: Array<unknown>) => Z,
  2170. ...collections: Array<Collection<unknown, unknown>>
  2171. ): OrderedSet<Z>;
  2172. }
  2173. /**
  2174. * Stacks are indexed collections which support very efficient O(1) addition
  2175. * and removal from the front using `unshift(v)` and `shift()`.
  2176. *
  2177. * For familiarity, Stack also provides `push(v)`, `pop()`, and `peek()`, but
  2178. * be aware that they also operate on the front of the list, unlike List or
  2179. * a JavaScript Array.
  2180. *
  2181. * Note: `reverse()` or any inherent reverse traversal (`reduceRight`,
  2182. * `lastIndexOf`, etc.) is not efficient with a Stack.
  2183. *
  2184. * Stack is implemented with a Single-Linked List.
  2185. */
  2186. namespace Stack {
  2187. /**
  2188. * True if the provided value is a Stack
  2189. */
  2190. function isStack(maybeStack: unknown): maybeStack is Stack<unknown>;
  2191. /**
  2192. * Creates a new Stack containing `values`.
  2193. */
  2194. function of<T>(...values: Array<T>): Stack<T>;
  2195. }
  2196. /**
  2197. * Create a new immutable Stack containing the values of the provided
  2198. * collection-like.
  2199. *
  2200. * The iteration order of the provided collection is preserved in the
  2201. * resulting `Stack`.
  2202. *
  2203. * Note: `Stack` is a factory function and not a class, and does not use the
  2204. * `new` keyword during construction.
  2205. */
  2206. function Stack<T>(collection?: Iterable<T> | ArrayLike<T>): Stack<T>;
  2207. interface Stack<T> extends Collection.Indexed<T> {
  2208. /**
  2209. * The number of items in this Stack.
  2210. */
  2211. readonly size: number;
  2212. // Reading values
  2213. /**
  2214. * Alias for `Stack.first()`.
  2215. */
  2216. peek(): T | undefined;
  2217. // Persistent changes
  2218. /**
  2219. * Returns a new Stack with 0 size and no values.
  2220. *
  2221. * Note: `clear` can be used in `withMutations`.
  2222. */
  2223. clear(): Stack<T>;
  2224. /**
  2225. * Returns a new Stack with the provided `values` prepended, shifting other
  2226. * values ahead to higher indices.
  2227. *
  2228. * This is very efficient for Stack.
  2229. *
  2230. * Note: `unshift` can be used in `withMutations`.
  2231. */
  2232. unshift(...values: Array<T>): Stack<T>;
  2233. /**
  2234. * Like `Stack#unshift`, but accepts a collection rather than varargs.
  2235. *
  2236. * Note: `unshiftAll` can be used in `withMutations`.
  2237. */
  2238. unshiftAll(iter: Iterable<T>): Stack<T>;
  2239. /**
  2240. * Returns a new Stack with a size ones less than this Stack, excluding
  2241. * the first item in this Stack, shifting all other values to a lower index.
  2242. *
  2243. * Note: this differs from `Array#shift` because it returns a new
  2244. * Stack rather than the removed value. Use `first()` or `peek()` to get the
  2245. * first value in this Stack.
  2246. *
  2247. * Note: `shift` can be used in `withMutations`.
  2248. */
  2249. shift(): Stack<T>;
  2250. /**
  2251. * Alias for `Stack#unshift` and is not equivalent to `List#push`.
  2252. */
  2253. push(...values: Array<T>): Stack<T>;
  2254. /**
  2255. * Alias for `Stack#unshiftAll`.
  2256. */
  2257. pushAll(iter: Iterable<T>): Stack<T>;
  2258. /**
  2259. * Alias for `Stack#shift` and is not equivalent to `List#pop`.
  2260. */
  2261. pop(): Stack<T>;
  2262. // Transient changes
  2263. /**
  2264. * Note: Not all methods can be used on a mutable collection or within
  2265. * `withMutations`! Check the documentation for each method to see if it
  2266. * mentions being safe to use in `withMutations`.
  2267. *
  2268. * @see `Map#withMutations`
  2269. */
  2270. withMutations(mutator: (mutable: this) => unknown): this;
  2271. /**
  2272. * Note: Not all methods can be used on a mutable collection or within
  2273. * `withMutations`! Check the documentation for each method to see if it
  2274. * mentions being safe to use in `withMutations`.
  2275. *
  2276. * @see `Map#asMutable`
  2277. */
  2278. asMutable(): this;
  2279. /**
  2280. * @see `Map#wasAltered`
  2281. */
  2282. wasAltered(): boolean;
  2283. /**
  2284. * @see `Map#asImmutable`
  2285. */
  2286. asImmutable(): this;
  2287. // Sequence algorithms
  2288. /**
  2289. * Returns a new Stack with other collections concatenated to this one.
  2290. */
  2291. concat<C>(...valuesOrCollections: Array<Iterable<C> | C>): Stack<T | C>;
  2292. /**
  2293. * Returns a new Stack with values passed through a
  2294. * `mapper` function.
  2295. *
  2296. * Stack([ 1, 2 ]).map(x => 10 * x)
  2297. * // Stack [ 10, 20 ]
  2298. *
  2299. * Note: `map()` always returns a new instance, even if it produced the same
  2300. * value at every step.
  2301. */
  2302. map<M>(
  2303. mapper: (value: T, key: number, iter: this) => M,
  2304. context?: unknown
  2305. ): Stack<M>;
  2306. /**
  2307. * Flat-maps the Stack, returning a new Stack.
  2308. *
  2309. * Similar to `stack.map(...).flatten(true)`.
  2310. */
  2311. flatMap<M>(
  2312. mapper: (value: T, key: number, iter: this) => Iterable<M>,
  2313. context?: unknown
  2314. ): Stack<M>;
  2315. /**
  2316. * Returns a new Set with only the values for which the `predicate`
  2317. * function returns true.
  2318. *
  2319. * Note: `filter()` always returns a new instance, even if it results in
  2320. * not filtering out any values.
  2321. */
  2322. filter<F extends T>(
  2323. predicate: (value: T, index: number, iter: this) => value is F,
  2324. context?: unknown
  2325. ): Set<F>;
  2326. filter(
  2327. predicate: (value: T, index: number, iter: this) => unknown,
  2328. context?: unknown
  2329. ): this;
  2330. /**
  2331. * Returns a Stack "zipped" with the provided collections.
  2332. *
  2333. * Like `zipWith`, but using the default `zipper`: creating an `Array`.
  2334. *
  2335. * ```js
  2336. * const a = Stack([ 1, 2, 3 ]);
  2337. * const b = Stack([ 4, 5, 6 ]);
  2338. * const c = a.zip(b); // Stack [ [ 1, 4 ], [ 2, 5 ], [ 3, 6 ] ]
  2339. * ```
  2340. */
  2341. zip<U>(other: Collection<unknown, U>): Stack<[T, U]>;
  2342. zip<U, V>(
  2343. other: Collection<unknown, U>,
  2344. other2: Collection<unknown, V>
  2345. ): Stack<[T, U, V]>;
  2346. zip(...collections: Array<Collection<unknown, unknown>>): Stack<unknown>;
  2347. /**
  2348. * Returns a Stack "zipped" with the provided collections.
  2349. *
  2350. * Unlike `zip`, `zipAll` continues zipping until the longest collection is
  2351. * exhausted. Missing values from shorter collections are filled with `undefined`.
  2352. *
  2353. * ```js
  2354. * const a = Stack([ 1, 2 ]);
  2355. * const b = Stack([ 3, 4, 5 ]);
  2356. * const c = a.zipAll(b); // Stack [ [ 1, 3 ], [ 2, 4 ], [ undefined, 5 ] ]
  2357. * ```
  2358. *
  2359. * Note: Since zipAll will return a collection as large as the largest
  2360. * input, some results may contain undefined values. TypeScript cannot
  2361. * account for these without cases (as of v2.5).
  2362. */
  2363. zipAll<U>(other: Collection<unknown, U>): Stack<[T, U]>;
  2364. zipAll<U, V>(
  2365. other: Collection<unknown, U>,
  2366. other2: Collection<unknown, V>
  2367. ): Stack<[T, U, V]>;
  2368. zipAll(...collections: Array<Collection<unknown, unknown>>): Stack<unknown>;
  2369. /**
  2370. * Returns a Stack "zipped" with the provided collections by using a
  2371. * custom `zipper` function.
  2372. *
  2373. * ```js
  2374. * const a = Stack([ 1, 2, 3 ]);
  2375. * const b = Stack([ 4, 5, 6 ]);
  2376. * const c = a.zipWith((a, b) => a + b, b);
  2377. * // Stack [ 5, 7, 9 ]
  2378. * ```
  2379. */
  2380. zipWith<U, Z>(
  2381. zipper: (value: T, otherValue: U) => Z,
  2382. otherCollection: Collection<unknown, U>
  2383. ): Stack<Z>;
  2384. zipWith<U, V, Z>(
  2385. zipper: (value: T, otherValue: U, thirdValue: V) => Z,
  2386. otherCollection: Collection<unknown, U>,
  2387. thirdCollection: Collection<unknown, V>
  2388. ): Stack<Z>;
  2389. zipWith<Z>(
  2390. zipper: (...values: Array<unknown>) => Z,
  2391. ...collections: Array<Collection<unknown, unknown>>
  2392. ): Stack<Z>;
  2393. }
  2394. /**
  2395. * Returns a Seq.Indexed of numbers from `start` (inclusive) to `end`
  2396. * (exclusive), by `step`, where `start` defaults to 0, `step` to 1, and `end` to
  2397. * infinity. When `start` is equal to `end`, returns empty range.
  2398. *
  2399. * Note: `Range` is a factory function and not a class, and does not use the
  2400. * `new` keyword during construction.
  2401. *
  2402. * ```js
  2403. * const { Range } = require('immutable')
  2404. * Range() // [ 0, 1, 2, 3, ... ]
  2405. * Range(10) // [ 10, 11, 12, 13, ... ]
  2406. * Range(10, 15) // [ 10, 11, 12, 13, 14 ]
  2407. * Range(10, 30, 5) // [ 10, 15, 20, 25 ]
  2408. * Range(30, 10, 5) // [ 30, 25, 20, 15 ]
  2409. * Range(30, 30, 5) // []
  2410. * ```
  2411. */
  2412. function Range(
  2413. start: number,
  2414. end: number,
  2415. step?: number
  2416. ): Seq.Indexed<number>;
  2417. /**
  2418. * Returns a Seq.Indexed of `value` repeated `times` times. When `times` is
  2419. * not defined, returns an infinite `Seq` of `value`.
  2420. *
  2421. * Note: `Repeat` is a factory function and not a class, and does not use the
  2422. * `new` keyword during construction.
  2423. *
  2424. * ```js
  2425. * const { Repeat } = require('immutable')
  2426. * Repeat('foo') // [ 'foo', 'foo', 'foo', ... ]
  2427. * Repeat('bar', 4) // [ 'bar', 'bar', 'bar', 'bar' ]
  2428. * ```
  2429. */
  2430. function Repeat<T>(value: T, times?: number): Seq.Indexed<T>;
  2431. /**
  2432. * A record is similar to a JS object, but enforces a specific set of allowed
  2433. * string keys, and has default values.
  2434. *
  2435. * The `Record()` function produces new Record Factories, which when called
  2436. * create Record instances.
  2437. *
  2438. * ```js
  2439. * const { Record } = require('immutable')
  2440. * const ABRecord = Record({ a: 1, b: 2 })
  2441. * const myRecord = ABRecord({ b: 3 })
  2442. * ```
  2443. *
  2444. * Records always have a value for the keys they define. `remove`ing a key
  2445. * from a record simply resets it to the default value for that key.
  2446. *
  2447. * ```js
  2448. * myRecord.get('a') // 1
  2449. * myRecord.get('b') // 3
  2450. * const myRecordWithoutB = myRecord.remove('b')
  2451. * myRecordWithoutB.get('b') // 2
  2452. * ```
  2453. *
  2454. * Values provided to the constructor not found in the Record type will
  2455. * be ignored. For example, in this case, ABRecord is provided a key "x" even
  2456. * though only "a" and "b" have been defined. The value for "x" will be
  2457. * ignored for this record.
  2458. *
  2459. * ```js
  2460. * const myRecord = ABRecord({ b: 3, x: 10 })
  2461. * myRecord.get('x') // undefined
  2462. * ```
  2463. *
  2464. * Because Records have a known set of string keys, property get access works
  2465. * as expected, however property sets will throw an Error.
  2466. *
  2467. * Note: IE8 does not support property access. Only use `get()` when
  2468. * supporting IE8.
  2469. *
  2470. * ```js
  2471. * myRecord.b // 3
  2472. * myRecord.b = 5 // throws Error
  2473. * ```
  2474. *
  2475. * Record Types can be extended as well, allowing for custom methods on your
  2476. * Record. This is not a common pattern in functional environments, but is in
  2477. * many JS programs.
  2478. *
  2479. * However Record Types are more restricted than typical JavaScript classes.
  2480. * They do not use a class constructor, which also means they cannot use
  2481. * class properties (since those are technically part of a constructor).
  2482. *
  2483. * While Record Types can be syntactically created with the JavaScript `class`
  2484. * form, the resulting Record function is actually a factory function, not a
  2485. * class constructor. Even though Record Types are not classes, JavaScript
  2486. * currently requires the use of `new` when creating new Record instances if
  2487. * they are defined as a `class`.
  2488. *
  2489. * ```
  2490. * class ABRecord extends Record({ a: 1, b: 2 }) {
  2491. * getAB() {
  2492. * return this.a + this.b;
  2493. * }
  2494. * }
  2495. *
  2496. * var myRecord = new ABRecord({b: 3})
  2497. * myRecord.getAB() // 4
  2498. * ```
  2499. *
  2500. *
  2501. * **Flow Typing Records:**
  2502. *
  2503. * Immutable.js exports two Flow types designed to make it easier to use
  2504. * Records with flow typed code, `RecordOf<TProps>` and `RecordFactory<TProps>`.
  2505. *
  2506. * When defining a new kind of Record factory function, use a flow type that
  2507. * describes the values the record contains along with `RecordFactory<TProps>`.
  2508. * To type instances of the Record (which the factory function returns),
  2509. * use `RecordOf<TProps>`.
  2510. *
  2511. * Typically, new Record definitions will export both the Record factory
  2512. * function as well as the Record instance type for use in other code.
  2513. *
  2514. * ```js
  2515. * import type { RecordFactory, RecordOf } from 'immutable';
  2516. *
  2517. * // Use RecordFactory<TProps> for defining new Record factory functions.
  2518. * type Point3DProps = { x: number, y: number, z: number };
  2519. * const defaultValues: Point3DProps = { x: 0, y: 0, z: 0 };
  2520. * const makePoint3D: RecordFactory<Point3DProps> = Record(defaultValues);
  2521. * export makePoint3D;
  2522. *
  2523. * // Use RecordOf<T> for defining new instances of that Record.
  2524. * export type Point3D = RecordOf<Point3DProps>;
  2525. * const some3DPoint: Point3D = makePoint3D({ x: 10, y: 20, z: 30 });
  2526. * ```
  2527. *
  2528. * **Flow Typing Record Subclasses:**
  2529. *
  2530. * Records can be subclassed as a means to add additional methods to Record
  2531. * instances. This is generally discouraged in favor of a more functional API,
  2532. * since Subclasses have some minor overhead. However the ability to create
  2533. * a rich API on Record types can be quite valuable.
  2534. *
  2535. * When using Flow to type Subclasses, do not use `RecordFactory<TProps>`,
  2536. * instead apply the props type when subclassing:
  2537. *
  2538. * ```js
  2539. * type PersonProps = {name: string, age: number};
  2540. * const defaultValues: PersonProps = {name: 'Aristotle', age: 2400};
  2541. * const PersonRecord = Record(defaultValues);
  2542. * class Person extends PersonRecord<PersonProps> {
  2543. * getName(): string {
  2544. * return this.get('name')
  2545. * }
  2546. *
  2547. * setName(name: string): this {
  2548. * return this.set('name', name);
  2549. * }
  2550. * }
  2551. * ```
  2552. *
  2553. * **Choosing Records vs plain JavaScript objects**
  2554. *
  2555. * Records offer a persistently immutable alternative to plain JavaScript
  2556. * objects, however they're not required to be used within Immutable.js
  2557. * collections. In fact, the deep-access and deep-updating functions
  2558. * like `getIn()` and `setIn()` work with plain JavaScript Objects as well.
  2559. *
  2560. * Deciding to use Records or Objects in your application should be informed
  2561. * by the tradeoffs and relative benefits of each:
  2562. *
  2563. * - *Runtime immutability*: plain JS objects may be carefully treated as
  2564. * immutable, however Record instances will *throw* if attempted to be
  2565. * mutated directly. Records provide this additional guarantee, however at
  2566. * some marginal runtime cost. While JS objects are mutable by nature, the
  2567. * use of type-checking tools like [Flow](https://medium.com/@gcanti/immutability-with-flow-faa050a1aef4)
  2568. * can help gain confidence in code written to favor immutability.
  2569. *
  2570. * - *Value equality*: Records use value equality when compared with `is()`
  2571. * or `record.equals()`. That is, two Records with the same keys and values
  2572. * are equal. Plain objects use *reference equality*. Two objects with the
  2573. * same keys and values are not equal since they are different objects.
  2574. * This is important to consider when using objects as keys in a `Map` or
  2575. * values in a `Set`, which use equality when retrieving values.
  2576. *
  2577. * - *API methods*: Records have a full featured API, with methods like
  2578. * `.getIn()`, and `.equals()`. These can make working with these values
  2579. * easier, but comes at the cost of not allowing keys with those names.
  2580. *
  2581. * - *Default values*: Records provide default values for every key, which
  2582. * can be useful when constructing Records with often unchanging values.
  2583. * However default values can make using Flow and TypeScript more laborious.
  2584. *
  2585. * - *Serialization*: Records use a custom internal representation to
  2586. * efficiently store and update their values. Converting to and from this
  2587. * form isn't free. If converting Records to plain objects is common,
  2588. * consider sticking with plain objects to begin with.
  2589. */
  2590. namespace Record {
  2591. /**
  2592. * True if `maybeRecord` is an instance of a Record.
  2593. */
  2594. function isRecord(maybeRecord: unknown): maybeRecord is Record<{}>;
  2595. /**
  2596. * Records allow passing a second parameter to supply a descriptive name
  2597. * that appears when converting a Record to a string or in any error
  2598. * messages. A descriptive name for any record can be accessed by using this
  2599. * method. If one was not provided, the string "Record" is returned.
  2600. *
  2601. * ```js
  2602. * const { Record } = require('immutable')
  2603. * const Person = Record({
  2604. * name: null
  2605. * }, 'Person')
  2606. *
  2607. * var me = Person({ name: 'My Name' })
  2608. * me.toString() // "Person { "name": "My Name" }"
  2609. * Record.getDescriptiveName(me) // "Person"
  2610. * ```
  2611. */
  2612. function getDescriptiveName(record: Record<any>): string;
  2613. /**
  2614. * A Record.Factory is created by the `Record()` function. Record instances
  2615. * are created by passing it some of the accepted values for that Record
  2616. * type:
  2617. *
  2618. * <!-- runkit:activate
  2619. * { "preamble": "const { Record } = require('immutable')" }
  2620. * -->
  2621. * ```js
  2622. * // makePerson is a Record Factory function
  2623. * const makePerson = Record({ name: null, favoriteColor: 'unknown' });
  2624. *
  2625. * // alan is a Record instance
  2626. * const alan = makePerson({ name: 'Alan' });
  2627. * ```
  2628. *
  2629. * Note that Record Factories return `Record<TProps> & Readonly<TProps>`,
  2630. * this allows use of both the Record instance API, and direct property
  2631. * access on the resulting instances:
  2632. *
  2633. * <!-- runkit:activate
  2634. * { "preamble": "const { Record } = require('immutable');const makePerson = Record({ name: null, favoriteColor: 'unknown' });const alan = makePerson({ name: 'Alan' });" }
  2635. * -->
  2636. * ```js
  2637. * // Use the Record API
  2638. * console.log('Record API: ' + alan.get('name'))
  2639. *
  2640. * // Or direct property access (Readonly)
  2641. * console.log('property access: ' + alan.name)
  2642. * ```
  2643. *
  2644. * **Flow Typing Records:**
  2645. *
  2646. * Use the `RecordFactory<TProps>` Flow type to get high quality type checking of
  2647. * Records:
  2648. *
  2649. * ```js
  2650. * import type { RecordFactory, RecordOf } from 'immutable';
  2651. *
  2652. * // Use RecordFactory<TProps> for defining new Record factory functions.
  2653. * type PersonProps = { name: ?string, favoriteColor: string };
  2654. * const makePerson: RecordFactory<PersonProps> = Record({ name: null, favoriteColor: 'unknown' });
  2655. *
  2656. * // Use RecordOf<T> for defining new instances of that Record.
  2657. * type Person = RecordOf<PersonProps>;
  2658. * const alan: Person = makePerson({ name: 'Alan' });
  2659. * ```
  2660. */
  2661. namespace Factory {}
  2662. interface Factory<TProps extends object> {
  2663. (values?: Partial<TProps> | Iterable<[string, unknown]>): Record<TProps> &
  2664. Readonly<TProps>;
  2665. new (
  2666. values?: Partial<TProps> | Iterable<[string, unknown]>
  2667. ): Record<TProps> & Readonly<TProps>;
  2668. /**
  2669. * The name provided to `Record(values, name)` can be accessed with
  2670. * `displayName`.
  2671. */
  2672. displayName: string;
  2673. }
  2674. function Factory<TProps extends object>(
  2675. values?: Partial<TProps> | Iterable<[string, unknown]>
  2676. ): Record<TProps> & Readonly<TProps>;
  2677. }
  2678. /**
  2679. * Unlike other types in Immutable.js, the `Record()` function creates a new
  2680. * Record Factory, which is a function that creates Record instances.
  2681. *
  2682. * See above for examples of using `Record()`.
  2683. *
  2684. * Note: `Record` is a factory function and not a class, and does not use the
  2685. * `new` keyword during construction.
  2686. */
  2687. function Record<TProps extends object>(
  2688. defaultValues: TProps,
  2689. name?: string
  2690. ): Record.Factory<TProps>;
  2691. interface Record<TProps extends object> {
  2692. // Reading values
  2693. has(key: string): key is keyof TProps & string;
  2694. /**
  2695. * Returns the value associated with the provided key, which may be the
  2696. * default value defined when creating the Record factory function.
  2697. *
  2698. * If the requested key is not defined by this Record type, then
  2699. * notSetValue will be returned if provided. Note that this scenario would
  2700. * produce an error when using Flow or TypeScript.
  2701. */
  2702. get<K extends keyof TProps>(key: K, notSetValue?: unknown): TProps[K];
  2703. get<T>(key: string, notSetValue: T): T;
  2704. // Reading deep values
  2705. hasIn(keyPath: Iterable<unknown>): boolean;
  2706. getIn(keyPath: Iterable<unknown>): unknown;
  2707. // Value equality
  2708. equals(other: unknown): boolean;
  2709. hashCode(): number;
  2710. // Persistent changes
  2711. set<K extends keyof TProps>(key: K, value: TProps[K]): this;
  2712. update<K extends keyof TProps>(
  2713. key: K,
  2714. updater: (value: TProps[K]) => TProps[K]
  2715. ): this;
  2716. merge(
  2717. ...collections: Array<Partial<TProps> | Iterable<[string, unknown]>>
  2718. ): this;
  2719. mergeDeep(
  2720. ...collections: Array<Partial<TProps> | Iterable<[string, unknown]>>
  2721. ): this;
  2722. mergeWith(
  2723. merger: (oldVal: unknown, newVal: unknown, key: keyof TProps) => unknown,
  2724. ...collections: Array<Partial<TProps> | Iterable<[string, unknown]>>
  2725. ): this;
  2726. mergeDeepWith(
  2727. merger: (oldVal: unknown, newVal: unknown, key: unknown) => unknown,
  2728. ...collections: Array<Partial<TProps> | Iterable<[string, unknown]>>
  2729. ): this;
  2730. /**
  2731. * Returns a new instance of this Record type with the value for the
  2732. * specific key set to its default value.
  2733. *
  2734. * @alias remove
  2735. */
  2736. delete<K extends keyof TProps>(key: K): this;
  2737. remove<K extends keyof TProps>(key: K): this;
  2738. /**
  2739. * Returns a new instance of this Record type with all values set
  2740. * to their default values.
  2741. */
  2742. clear(): this;
  2743. // Deep persistent changes
  2744. setIn(keyPath: Iterable<unknown>, value: unknown): this;
  2745. updateIn(
  2746. keyPath: Iterable<unknown>,
  2747. updater: (value: unknown) => unknown
  2748. ): this;
  2749. mergeIn(keyPath: Iterable<unknown>, ...collections: Array<unknown>): this;
  2750. mergeDeepIn(
  2751. keyPath: Iterable<unknown>,
  2752. ...collections: Array<unknown>
  2753. ): this;
  2754. /**
  2755. * @alias removeIn
  2756. */
  2757. deleteIn(keyPath: Iterable<unknown>): this;
  2758. removeIn(keyPath: Iterable<unknown>): this;
  2759. // Conversion to JavaScript types
  2760. /**
  2761. * Deeply converts this Record to equivalent native JavaScript Object.
  2762. *
  2763. * Note: This method may not be overridden. Objects with custom
  2764. * serialization to plain JS may override toJSON() instead.
  2765. */
  2766. toJS(): DeepCopy<TProps>;
  2767. /**
  2768. * Shallowly converts this Record to equivalent native JavaScript Object.
  2769. */
  2770. toJSON(): TProps;
  2771. /**
  2772. * Shallowly converts this Record to equivalent JavaScript Object.
  2773. */
  2774. toObject(): TProps;
  2775. // Transient changes
  2776. /**
  2777. * Note: Not all methods can be used on a mutable collection or within
  2778. * `withMutations`! Only `set` may be used mutatively.
  2779. *
  2780. * @see `Map#withMutations`
  2781. */
  2782. withMutations(mutator: (mutable: this) => unknown): this;
  2783. /**
  2784. * @see `Map#asMutable`
  2785. */
  2786. asMutable(): this;
  2787. /**
  2788. * @see `Map#wasAltered`
  2789. */
  2790. wasAltered(): boolean;
  2791. /**
  2792. * @see `Map#asImmutable`
  2793. */
  2794. asImmutable(): this;
  2795. // Sequence algorithms
  2796. toSeq(): Seq.Keyed<keyof TProps, TProps[keyof TProps]>;
  2797. [Symbol.iterator](): IterableIterator<[keyof TProps, TProps[keyof TProps]]>;
  2798. }
  2799. /**
  2800. * RecordOf<T> is used in TypeScript to define interfaces expecting an
  2801. * instance of record with type T.
  2802. *
  2803. * This is equivalent to an instance of a record created by a Record Factory.
  2804. */
  2805. type RecordOf<TProps extends object> = Record<TProps> & Readonly<TProps>;
  2806. /**
  2807. * `Seq` describes a lazy operation, allowing them to efficiently chain
  2808. * use of all the higher-order collection methods (such as `map` and `filter`)
  2809. * by not creating intermediate collections.
  2810. *
  2811. * **Seq is immutable** — Once a Seq is created, it cannot be
  2812. * changed, appended to, rearranged or otherwise modified. Instead, any
  2813. * mutative method called on a `Seq` will return a new `Seq`.
  2814. *
  2815. * **Seq is lazy** — `Seq` does as little work as necessary to respond to any
  2816. * method call. Values are often created during iteration, including implicit
  2817. * iteration when reducing or converting to a concrete data structure such as
  2818. * a `List` or JavaScript `Array`.
  2819. *
  2820. * For example, the following performs no work, because the resulting
  2821. * `Seq`'s values are never iterated:
  2822. *
  2823. * ```js
  2824. * const { Seq } = require('immutable')
  2825. * const oddSquares = Seq([ 1, 2, 3, 4, 5, 6, 7, 8 ])
  2826. * .filter(x => x % 2 !== 0)
  2827. * .map(x => x * x)
  2828. * ```
  2829. *
  2830. * Once the `Seq` is used, it performs only the work necessary. In this
  2831. * example, no intermediate arrays are ever created, filter is called three
  2832. * times, and map is only called once:
  2833. *
  2834. * ```js
  2835. * oddSquares.get(1); // 9
  2836. * ```
  2837. *
  2838. * Any collection can be converted to a lazy Seq with `Seq()`.
  2839. *
  2840. * <!-- runkit:activate -->
  2841. * ```js
  2842. * const { Map } = require('immutable')
  2843. * const map = Map({ a: 1, b: 2, c: 3 })
  2844. * const lazySeq = Seq(map)
  2845. * ```
  2846. *
  2847. * `Seq` allows for the efficient chaining of operations, allowing for the
  2848. * expression of logic that can otherwise be very tedious:
  2849. *
  2850. * ```js
  2851. * lazySeq
  2852. * .flip()
  2853. * .map(key => key.toUpperCase())
  2854. * .flip()
  2855. * // Seq { A: 1, B: 1, C: 1 }
  2856. * ```
  2857. *
  2858. * As well as expressing logic that would otherwise seem memory or time
  2859. * limited, for example `Range` is a special kind of Lazy sequence.
  2860. *
  2861. * <!-- runkit:activate -->
  2862. * ```js
  2863. * const { Range } = require('immutable')
  2864. * Range(1, Infinity)
  2865. * .skip(1000)
  2866. * .map(n => -n)
  2867. * .filter(n => n % 2 === 0)
  2868. * .take(2)
  2869. * .reduce((r, n) => r * n, 1)
  2870. * // 1006008
  2871. * ```
  2872. *
  2873. * Seq is often used to provide a rich collection API to JavaScript Object.
  2874. *
  2875. * ```js
  2876. * Seq({ x: 0, y: 1, z: 2 }).map(v => v * 2).toObject();
  2877. * // { x: 0, y: 2, z: 4 }
  2878. * ```
  2879. */
  2880. namespace Seq {
  2881. /**
  2882. * True if `maybeSeq` is a Seq, it is not backed by a concrete
  2883. * structure such as Map, List, or Set.
  2884. */
  2885. function isSeq(
  2886. maybeSeq: unknown
  2887. ): maybeSeq is
  2888. | Seq.Indexed<unknown>
  2889. | Seq.Keyed<unknown, unknown>
  2890. | Seq.Set<unknown>;
  2891. /**
  2892. * `Seq` which represents key-value pairs.
  2893. */
  2894. namespace Keyed {}
  2895. /**
  2896. * Always returns a Seq.Keyed, if input is not keyed, expects an
  2897. * collection of [K, V] tuples.
  2898. *
  2899. * Note: `Seq.Keyed` is a conversion function and not a class, and does not
  2900. * use the `new` keyword during construction.
  2901. */
  2902. function Keyed<K, V>(collection?: Iterable<[K, V]>): Seq.Keyed<K, V>;
  2903. function Keyed<V>(obj: { [key: string]: V }): Seq.Keyed<string, V>;
  2904. interface Keyed<K, V> extends Seq<K, V>, Collection.Keyed<K, V> {
  2905. /**
  2906. * Deeply converts this Keyed Seq to equivalent native JavaScript Object.
  2907. *
  2908. * Converts keys to Strings.
  2909. */
  2910. toJS(): { [key in string | number | symbol]: DeepCopy<V> };
  2911. /**
  2912. * Shallowly converts this Keyed Seq to equivalent native JavaScript Object.
  2913. *
  2914. * Converts keys to Strings.
  2915. */
  2916. toJSON(): { [key in string | number | symbol]: V };
  2917. /**
  2918. * Shallowly converts this collection to an Array.
  2919. */
  2920. toArray(): Array<[K, V]>;
  2921. /**
  2922. * Returns itself
  2923. */
  2924. toSeq(): this;
  2925. /**
  2926. * Returns a new Seq with other collections concatenated to this one.
  2927. *
  2928. * All entries will be present in the resulting Seq, even if they
  2929. * have the same key.
  2930. */
  2931. concat<KC, VC>(
  2932. ...collections: Array<Iterable<[KC, VC]>>
  2933. ): Seq.Keyed<K | KC, V | VC>;
  2934. concat<C>(
  2935. ...collections: Array<{ [key: string]: C }>
  2936. ): Seq.Keyed<K | string, V | C>;
  2937. /**
  2938. * Returns a new Seq.Keyed with values passed through a
  2939. * `mapper` function.
  2940. *
  2941. * ```js
  2942. * const { Seq } = require('immutable')
  2943. * Seq.Keyed({ a: 1, b: 2 }).map(x => 10 * x)
  2944. * // Seq { "a": 10, "b": 20 }
  2945. * ```
  2946. *
  2947. * Note: `map()` always returns a new instance, even if it produced the
  2948. * same value at every step.
  2949. */
  2950. map<M>(
  2951. mapper: (value: V, key: K, iter: this) => M,
  2952. context?: unknown
  2953. ): Seq.Keyed<K, M>;
  2954. /**
  2955. * @see Collection.Keyed.mapKeys
  2956. */
  2957. mapKeys<M>(
  2958. mapper: (key: K, value: V, iter: this) => M,
  2959. context?: unknown
  2960. ): Seq.Keyed<M, V>;
  2961. /**
  2962. * @see Collection.Keyed.mapEntries
  2963. */
  2964. mapEntries<KM, VM>(
  2965. mapper: (
  2966. entry: [K, V],
  2967. index: number,
  2968. iter: this
  2969. ) => [KM, VM] | undefined,
  2970. context?: unknown
  2971. ): Seq.Keyed<KM, VM>;
  2972. /**
  2973. * Flat-maps the Seq, returning a Seq of the same type.
  2974. *
  2975. * Similar to `seq.map(...).flatten(true)`.
  2976. */
  2977. flatMap<KM, VM>(
  2978. mapper: (value: V, key: K, iter: this) => Iterable<[KM, VM]>,
  2979. context?: unknown
  2980. ): Seq.Keyed<KM, VM>;
  2981. /**
  2982. * Returns a new Seq with only the entries for which the `predicate`
  2983. * function returns true.
  2984. *
  2985. * Note: `filter()` always returns a new instance, even if it results in
  2986. * not filtering out any values.
  2987. */
  2988. filter<F extends V>(
  2989. predicate: (value: V, key: K, iter: this) => value is F,
  2990. context?: unknown
  2991. ): Seq.Keyed<K, F>;
  2992. filter(
  2993. predicate: (value: V, key: K, iter: this) => unknown,
  2994. context?: unknown
  2995. ): this;
  2996. /**
  2997. * Returns a new keyed Seq with the values for which the `predicate`
  2998. * function returns false and another for which is returns true.
  2999. */
  3000. partition<F extends V, C>(
  3001. predicate: (this: C, value: V, key: K, iter: this) => value is F,
  3002. context?: C
  3003. ): [Seq.Keyed<K, V>, Seq.Keyed<K, F>];
  3004. partition<C>(
  3005. predicate: (this: C, value: V, key: K, iter: this) => unknown,
  3006. context?: C
  3007. ): [this, this];
  3008. /**
  3009. * @see Collection.Keyed.flip
  3010. */
  3011. flip(): Seq.Keyed<V, K>;
  3012. [Symbol.iterator](): IterableIterator<[K, V]>;
  3013. }
  3014. /**
  3015. * `Seq` which represents an ordered indexed list of values.
  3016. */
  3017. namespace Indexed {
  3018. /**
  3019. * Provides an Seq.Indexed of the values provided.
  3020. */
  3021. function of<T>(...values: Array<T>): Seq.Indexed<T>;
  3022. }
  3023. /**
  3024. * Always returns Seq.Indexed, discarding associated keys and
  3025. * supplying incrementing indices.
  3026. *
  3027. * Note: `Seq.Indexed` is a conversion function and not a class, and does
  3028. * not use the `new` keyword during construction.
  3029. */
  3030. function Indexed<T>(
  3031. collection?: Iterable<T> | ArrayLike<T>
  3032. ): Seq.Indexed<T>;
  3033. interface Indexed<T> extends Seq<number, T>, Collection.Indexed<T> {
  3034. /**
  3035. * Deeply converts this Indexed Seq to equivalent native JavaScript Array.
  3036. */
  3037. toJS(): Array<DeepCopy<T>>;
  3038. /**
  3039. * Shallowly converts this Indexed Seq to equivalent native JavaScript Array.
  3040. */
  3041. toJSON(): Array<T>;
  3042. /**
  3043. * Shallowly converts this collection to an Array.
  3044. */
  3045. toArray(): Array<T>;
  3046. /**
  3047. * Returns itself
  3048. */
  3049. toSeq(): this;
  3050. /**
  3051. * Returns a new Seq with other collections concatenated to this one.
  3052. */
  3053. concat<C>(
  3054. ...valuesOrCollections: Array<Iterable<C> | C>
  3055. ): Seq.Indexed<T | C>;
  3056. /**
  3057. * Returns a new Seq.Indexed with values passed through a
  3058. * `mapper` function.
  3059. *
  3060. * ```js
  3061. * const { Seq } = require('immutable')
  3062. * Seq.Indexed([ 1, 2 ]).map(x => 10 * x)
  3063. * // Seq [ 10, 20 ]
  3064. * ```
  3065. *
  3066. * Note: `map()` always returns a new instance, even if it produced the
  3067. * same value at every step.
  3068. */
  3069. map<M>(
  3070. mapper: (value: T, key: number, iter: this) => M,
  3071. context?: unknown
  3072. ): Seq.Indexed<M>;
  3073. /**
  3074. * Flat-maps the Seq, returning a a Seq of the same type.
  3075. *
  3076. * Similar to `seq.map(...).flatten(true)`.
  3077. */
  3078. flatMap<M>(
  3079. mapper: (value: T, key: number, iter: this) => Iterable<M>,
  3080. context?: unknown
  3081. ): Seq.Indexed<M>;
  3082. /**
  3083. * Returns a new Seq with only the values for which the `predicate`
  3084. * function returns true.
  3085. *
  3086. * Note: `filter()` always returns a new instance, even if it results in
  3087. * not filtering out any values.
  3088. */
  3089. filter<F extends T>(
  3090. predicate: (value: T, index: number, iter: this) => value is F,
  3091. context?: unknown
  3092. ): Seq.Indexed<F>;
  3093. filter(
  3094. predicate: (value: T, index: number, iter: this) => unknown,
  3095. context?: unknown
  3096. ): this;
  3097. /**
  3098. * Returns a new indexed Seq with the values for which the `predicate`
  3099. * function returns false and another for which is returns true.
  3100. */
  3101. partition<F extends T, C>(
  3102. predicate: (this: C, value: T, index: number, iter: this) => value is F,
  3103. context?: C
  3104. ): [Seq.Indexed<T>, Seq.Indexed<F>];
  3105. partition<C>(
  3106. predicate: (this: C, value: T, index: number, iter: this) => unknown,
  3107. context?: C
  3108. ): [this, this];
  3109. /**
  3110. * Returns a Seq "zipped" with the provided collections.
  3111. *
  3112. * Like `zipWith`, but using the default `zipper`: creating an `Array`.
  3113. *
  3114. * ```js
  3115. * const a = Seq([ 1, 2, 3 ]);
  3116. * const b = Seq([ 4, 5, 6 ]);
  3117. * const c = a.zip(b); // Seq [ [ 1, 4 ], [ 2, 5 ], [ 3, 6 ] ]
  3118. * ```
  3119. */
  3120. zip<U>(other: Collection<unknown, U>): Seq.Indexed<[T, U]>;
  3121. zip<U, V>(
  3122. other: Collection<unknown, U>,
  3123. other2: Collection<unknown, V>
  3124. ): Seq.Indexed<[T, U, V]>;
  3125. zip(
  3126. ...collections: Array<Collection<unknown, unknown>>
  3127. ): Seq.Indexed<unknown>;
  3128. /**
  3129. * Returns a Seq "zipped" with the provided collections.
  3130. *
  3131. * Unlike `zip`, `zipAll` continues zipping until the longest collection is
  3132. * exhausted. Missing values from shorter collections are filled with `undefined`.
  3133. *
  3134. * ```js
  3135. * const a = Seq([ 1, 2 ]);
  3136. * const b = Seq([ 3, 4, 5 ]);
  3137. * const c = a.zipAll(b); // Seq [ [ 1, 3 ], [ 2, 4 ], [ undefined, 5 ] ]
  3138. * ```
  3139. */
  3140. zipAll<U>(other: Collection<unknown, U>): Seq.Indexed<[T, U]>;
  3141. zipAll<U, V>(
  3142. other: Collection<unknown, U>,
  3143. other2: Collection<unknown, V>
  3144. ): Seq.Indexed<[T, U, V]>;
  3145. zipAll(
  3146. ...collections: Array<Collection<unknown, unknown>>
  3147. ): Seq.Indexed<unknown>;
  3148. /**
  3149. * Returns a Seq "zipped" with the provided collections by using a
  3150. * custom `zipper` function.
  3151. *
  3152. * ```js
  3153. * const a = Seq([ 1, 2, 3 ]);
  3154. * const b = Seq([ 4, 5, 6 ]);
  3155. * const c = a.zipWith((a, b) => a + b, b);
  3156. * // Seq [ 5, 7, 9 ]
  3157. * ```
  3158. */
  3159. zipWith<U, Z>(
  3160. zipper: (value: T, otherValue: U) => Z,
  3161. otherCollection: Collection<unknown, U>
  3162. ): Seq.Indexed<Z>;
  3163. zipWith<U, V, Z>(
  3164. zipper: (value: T, otherValue: U, thirdValue: V) => Z,
  3165. otherCollection: Collection<unknown, U>,
  3166. thirdCollection: Collection<unknown, V>
  3167. ): Seq.Indexed<Z>;
  3168. zipWith<Z>(
  3169. zipper: (...values: Array<unknown>) => Z,
  3170. ...collections: Array<Collection<unknown, unknown>>
  3171. ): Seq.Indexed<Z>;
  3172. [Symbol.iterator](): IterableIterator<T>;
  3173. }
  3174. /**
  3175. * `Seq` which represents a set of values.
  3176. *
  3177. * Because `Seq` are often lazy, `Seq.Set` does not provide the same guarantee
  3178. * of value uniqueness as the concrete `Set`.
  3179. */
  3180. namespace Set {
  3181. /**
  3182. * Returns a Seq.Set of the provided values
  3183. */
  3184. function of<T>(...values: Array<T>): Seq.Set<T>;
  3185. }
  3186. /**
  3187. * Always returns a Seq.Set, discarding associated indices or keys.
  3188. *
  3189. * Note: `Seq.Set` is a conversion function and not a class, and does not
  3190. * use the `new` keyword during construction.
  3191. */
  3192. function Set<T>(collection?: Iterable<T> | ArrayLike<T>): Seq.Set<T>;
  3193. interface Set<T> extends Seq<T, T>, Collection.Set<T> {
  3194. /**
  3195. * Deeply converts this Set Seq to equivalent native JavaScript Array.
  3196. */
  3197. toJS(): Array<DeepCopy<T>>;
  3198. /**
  3199. * Shallowly converts this Set Seq to equivalent native JavaScript Array.
  3200. */
  3201. toJSON(): Array<T>;
  3202. /**
  3203. * Shallowly converts this collection to an Array.
  3204. */
  3205. toArray(): Array<T>;
  3206. /**
  3207. * Returns itself
  3208. */
  3209. toSeq(): this;
  3210. /**
  3211. * Returns a new Seq with other collections concatenated to this one.
  3212. *
  3213. * All entries will be present in the resulting Seq, even if they
  3214. * are duplicates.
  3215. */
  3216. concat<U>(...collections: Array<Iterable<U>>): Seq.Set<T | U>;
  3217. /**
  3218. * Returns a new Seq.Set with values passed through a
  3219. * `mapper` function.
  3220. *
  3221. * ```js
  3222. * Seq.Set([ 1, 2 ]).map(x => 10 * x)
  3223. * // Seq { 10, 20 }
  3224. * ```
  3225. *
  3226. * Note: `map()` always returns a new instance, even if it produced the
  3227. * same value at every step.
  3228. */
  3229. map<M>(
  3230. mapper: (value: T, key: T, iter: this) => M,
  3231. context?: unknown
  3232. ): Seq.Set<M>;
  3233. /**
  3234. * Flat-maps the Seq, returning a Seq of the same type.
  3235. *
  3236. * Similar to `seq.map(...).flatten(true)`.
  3237. */
  3238. flatMap<M>(
  3239. mapper: (value: T, key: T, iter: this) => Iterable<M>,
  3240. context?: unknown
  3241. ): Seq.Set<M>;
  3242. /**
  3243. * Returns a new Seq with only the values for which the `predicate`
  3244. * function returns true.
  3245. *
  3246. * Note: `filter()` always returns a new instance, even if it results in
  3247. * not filtering out any values.
  3248. */
  3249. filter<F extends T>(
  3250. predicate: (value: T, key: T, iter: this) => value is F,
  3251. context?: unknown
  3252. ): Seq.Set<F>;
  3253. filter(
  3254. predicate: (value: T, key: T, iter: this) => unknown,
  3255. context?: unknown
  3256. ): this;
  3257. /**
  3258. * Returns a new set Seq with the values for which the `predicate`
  3259. * function returns false and another for which is returns true.
  3260. */
  3261. partition<F extends T, C>(
  3262. predicate: (this: C, value: T, key: T, iter: this) => value is F,
  3263. context?: C
  3264. ): [Seq.Set<T>, Seq.Set<F>];
  3265. partition<C>(
  3266. predicate: (this: C, value: T, key: T, iter: this) => unknown,
  3267. context?: C
  3268. ): [this, this];
  3269. [Symbol.iterator](): IterableIterator<T>;
  3270. }
  3271. }
  3272. /**
  3273. * Creates a Seq.
  3274. *
  3275. * Returns a particular kind of `Seq` based on the input.
  3276. *
  3277. * * If a `Seq`, that same `Seq`.
  3278. * * If an `Collection`, a `Seq` of the same kind (Keyed, Indexed, or Set).
  3279. * * If an Array-like, an `Seq.Indexed`.
  3280. * * If an Iterable Object, an `Seq.Indexed`.
  3281. * * If an Object, a `Seq.Keyed`.
  3282. *
  3283. * Note: An Iterator itself will be treated as an object, becoming a `Seq.Keyed`,
  3284. * which is usually not what you want. You should turn your Iterator Object into
  3285. * an iterable object by defining a Symbol.iterator (or @@iterator) method which
  3286. * returns `this`.
  3287. *
  3288. * Note: `Seq` is a conversion function and not a class, and does not use the
  3289. * `new` keyword during construction.
  3290. */
  3291. function Seq<S extends Seq<unknown, unknown>>(seq: S): S;
  3292. function Seq<K, V>(collection: Collection.Keyed<K, V>): Seq.Keyed<K, V>;
  3293. function Seq<T>(collection: Collection.Set<T>): Seq.Set<T>;
  3294. function Seq<T>(
  3295. collection: Collection.Indexed<T> | Iterable<T> | ArrayLike<T>
  3296. ): Seq.Indexed<T>;
  3297. function Seq<V>(obj: { [key: string]: V }): Seq.Keyed<string, V>;
  3298. function Seq<K = unknown, V = unknown>(): Seq<K, V>;
  3299. interface Seq<K, V> extends Collection<K, V> {
  3300. /**
  3301. * Some Seqs can describe their size lazily. When this is the case,
  3302. * size will be an integer. Otherwise it will be undefined.
  3303. *
  3304. * For example, Seqs returned from `map()` or `reverse()`
  3305. * preserve the size of the original `Seq` while `filter()` does not.
  3306. *
  3307. * Note: `Range`, `Repeat` and `Seq`s made from `Array`s and `Object`s will
  3308. * always have a size.
  3309. */
  3310. readonly size: number | undefined;
  3311. // Force evaluation
  3312. /**
  3313. * Because Sequences are lazy and designed to be chained together, they do
  3314. * not cache their results. For example, this map function is called a total
  3315. * of 6 times, as each `join` iterates the Seq of three values.
  3316. *
  3317. * var squares = Seq([ 1, 2, 3 ]).map(x => x * x)
  3318. * squares.join() + squares.join()
  3319. *
  3320. * If you know a `Seq` will be used multiple times, it may be more
  3321. * efficient to first cache it in memory. Here, the map function is called
  3322. * only 3 times.
  3323. *
  3324. * var squares = Seq([ 1, 2, 3 ]).map(x => x * x).cacheResult()
  3325. * squares.join() + squares.join()
  3326. *
  3327. * Use this method judiciously, as it must fully evaluate a Seq which can be
  3328. * a burden on memory and possibly performance.
  3329. *
  3330. * Note: after calling `cacheResult`, a Seq will always have a `size`.
  3331. */
  3332. cacheResult(): this;
  3333. // Sequence algorithms
  3334. /**
  3335. * Returns a new Seq with values passed through a
  3336. * `mapper` function.
  3337. *
  3338. * ```js
  3339. * const { Seq } = require('immutable')
  3340. * Seq([ 1, 2 ]).map(x => 10 * x)
  3341. * // Seq [ 10, 20 ]
  3342. * ```
  3343. *
  3344. * Note: `map()` always returns a new instance, even if it produced the same
  3345. * value at every step.
  3346. */
  3347. map<M>(
  3348. mapper: (value: V, key: K, iter: this) => M,
  3349. context?: unknown
  3350. ): Seq<K, M>;
  3351. /**
  3352. * Returns a new Seq with values passed through a
  3353. * `mapper` function.
  3354. *
  3355. * ```js
  3356. * const { Seq } = require('immutable')
  3357. * Seq([ 1, 2 ]).map(x => 10 * x)
  3358. * // Seq [ 10, 20 ]
  3359. * ```
  3360. *
  3361. * Note: `map()` always returns a new instance, even if it produced the same
  3362. * value at every step.
  3363. * Note: used only for sets.
  3364. */
  3365. map<M>(
  3366. mapper: (value: V, key: K, iter: this) => M,
  3367. context?: unknown
  3368. ): Seq<M, M>;
  3369. /**
  3370. * Flat-maps the Seq, returning a Seq of the same type.
  3371. *
  3372. * Similar to `seq.map(...).flatten(true)`.
  3373. */
  3374. flatMap<M>(
  3375. mapper: (value: V, key: K, iter: this) => Iterable<M>,
  3376. context?: unknown
  3377. ): Seq<K, M>;
  3378. /**
  3379. * Flat-maps the Seq, returning a Seq of the same type.
  3380. *
  3381. * Similar to `seq.map(...).flatten(true)`.
  3382. * Note: Used only for sets.
  3383. */
  3384. flatMap<M>(
  3385. mapper: (value: V, key: K, iter: this) => Iterable<M>,
  3386. context?: unknown
  3387. ): Seq<M, M>;
  3388. /**
  3389. * Returns a new Seq with only the values for which the `predicate`
  3390. * function returns true.
  3391. *
  3392. * Note: `filter()` always returns a new instance, even if it results in
  3393. * not filtering out any values.
  3394. */
  3395. filter<F extends V>(
  3396. predicate: (value: V, key: K, iter: this) => value is F,
  3397. context?: unknown
  3398. ): Seq<K, F>;
  3399. filter(
  3400. predicate: (value: V, key: K, iter: this) => unknown,
  3401. context?: unknown
  3402. ): this;
  3403. /**
  3404. * Returns a new Seq with the values for which the `predicate` function
  3405. * returns false and another for which is returns true.
  3406. */
  3407. partition<F extends V, C>(
  3408. predicate: (this: C, value: V, key: K, iter: this) => value is F,
  3409. context?: C
  3410. ): [Seq<K, V>, Seq<K, F>];
  3411. partition<C>(
  3412. predicate: (this: C, value: V, key: K, iter: this) => unknown,
  3413. context?: C
  3414. ): [this, this];
  3415. }
  3416. /**
  3417. * The `Collection` is a set of (key, value) entries which can be iterated, and
  3418. * is the base class for all collections in `immutable`, allowing them to
  3419. * make use of all the Collection methods (such as `map` and `filter`).
  3420. *
  3421. * Note: A collection is always iterated in the same order, however that order
  3422. * may not always be well defined, as is the case for the `Map` and `Set`.
  3423. *
  3424. * Collection is the abstract base class for concrete data structures. It
  3425. * cannot be constructed directly.
  3426. *
  3427. * Implementations should extend one of the subclasses, `Collection.Keyed`,
  3428. * `Collection.Indexed`, or `Collection.Set`.
  3429. */
  3430. namespace Collection {
  3431. /**
  3432. * Keyed Collections have discrete keys tied to each value.
  3433. *
  3434. * When iterating `Collection.Keyed`, each iteration will yield a `[K, V]`
  3435. * tuple, in other words, `Collection#entries` is the default iterator for
  3436. * Keyed Collections.
  3437. */
  3438. namespace Keyed {}
  3439. /**
  3440. * Creates a Collection.Keyed
  3441. *
  3442. * Similar to `Collection()`, however it expects collection-likes of [K, V]
  3443. * tuples if not constructed from a Collection.Keyed or JS Object.
  3444. *
  3445. * Note: `Collection.Keyed` is a conversion function and not a class, and
  3446. * does not use the `new` keyword during construction.
  3447. */
  3448. function Keyed<K, V>(collection?: Iterable<[K, V]>): Collection.Keyed<K, V>;
  3449. function Keyed<V>(obj: { [key: string]: V }): Collection.Keyed<string, V>;
  3450. interface Keyed<K, V> extends Collection<K, V> {
  3451. /**
  3452. * Deeply converts this Keyed collection to equivalent native JavaScript Object.
  3453. *
  3454. * Converts keys to Strings.
  3455. */
  3456. toJS(): { [key in string | number | symbol]: DeepCopy<V> };
  3457. /**
  3458. * Shallowly converts this Keyed collection to equivalent native JavaScript Object.
  3459. *
  3460. * Converts keys to Strings.
  3461. */
  3462. toJSON(): { [key in string | number | symbol]: V };
  3463. /**
  3464. * Shallowly converts this collection to an Array.
  3465. */
  3466. toArray(): Array<[K, V]>;
  3467. /**
  3468. * Returns Seq.Keyed.
  3469. * @override
  3470. */
  3471. toSeq(): Seq.Keyed<K, V>;
  3472. // Sequence functions
  3473. /**
  3474. * Returns a new Collection.Keyed of the same type where the keys and values
  3475. * have been flipped.
  3476. *
  3477. * <!-- runkit:activate -->
  3478. * ```js
  3479. * const { Map } = require('immutable')
  3480. * Map({ a: 'z', b: 'y' }).flip()
  3481. * // Map { "z": "a", "y": "b" }
  3482. * ```
  3483. */
  3484. flip(): Collection.Keyed<V, K>;
  3485. /**
  3486. * Returns a new Collection with other collections concatenated to this one.
  3487. */
  3488. concat<KC, VC>(
  3489. ...collections: Array<Iterable<[KC, VC]>>
  3490. ): Collection.Keyed<K | KC, V | VC>;
  3491. concat<C>(
  3492. ...collections: Array<{ [key: string]: C }>
  3493. ): Collection.Keyed<K | string, V | C>;
  3494. /**
  3495. * Returns a new Collection.Keyed with values passed through a
  3496. * `mapper` function.
  3497. *
  3498. * ```js
  3499. * const { Collection } = require('immutable')
  3500. * Collection.Keyed({ a: 1, b: 2 }).map(x => 10 * x)
  3501. * // Seq { "a": 10, "b": 20 }
  3502. * ```
  3503. *
  3504. * Note: `map()` always returns a new instance, even if it produced the
  3505. * same value at every step.
  3506. */
  3507. map<M>(
  3508. mapper: (value: V, key: K, iter: this) => M,
  3509. context?: unknown
  3510. ): Collection.Keyed<K, M>;
  3511. /**
  3512. * Returns a new Collection.Keyed of the same type with keys passed through
  3513. * a `mapper` function.
  3514. *
  3515. * <!-- runkit:activate -->
  3516. * ```js
  3517. * const { Map } = require('immutable')
  3518. * Map({ a: 1, b: 2 }).mapKeys(x => x.toUpperCase())
  3519. * // Map { "A": 1, "B": 2 }
  3520. * ```
  3521. *
  3522. * Note: `mapKeys()` always returns a new instance, even if it produced
  3523. * the same key at every step.
  3524. */
  3525. mapKeys<M>(
  3526. mapper: (key: K, value: V, iter: this) => M,
  3527. context?: unknown
  3528. ): Collection.Keyed<M, V>;
  3529. /**
  3530. * Returns a new Collection.Keyed of the same type with entries
  3531. * ([key, value] tuples) passed through a `mapper` function.
  3532. *
  3533. * <!-- runkit:activate -->
  3534. * ```js
  3535. * const { Map } = require('immutable')
  3536. * Map({ a: 1, b: 2 })
  3537. * .mapEntries(([ k, v ]) => [ k.toUpperCase(), v * 2 ])
  3538. * // Map { "A": 2, "B": 4 }
  3539. * ```
  3540. *
  3541. * Note: `mapEntries()` always returns a new instance, even if it produced
  3542. * the same entry at every step.
  3543. *
  3544. * If the mapper function returns `undefined`, then the entry will be filtered
  3545. */
  3546. mapEntries<KM, VM>(
  3547. mapper: (
  3548. entry: [K, V],
  3549. index: number,
  3550. iter: this
  3551. ) => [KM, VM] | undefined,
  3552. context?: unknown
  3553. ): Collection.Keyed<KM, VM>;
  3554. /**
  3555. * Flat-maps the Collection, returning a Collection of the same type.
  3556. *
  3557. * Similar to `collection.map(...).flatten(true)`.
  3558. */
  3559. flatMap<KM, VM>(
  3560. mapper: (value: V, key: K, iter: this) => Iterable<[KM, VM]>,
  3561. context?: unknown
  3562. ): Collection.Keyed<KM, VM>;
  3563. /**
  3564. * Returns a new Collection with only the values for which the `predicate`
  3565. * function returns true.
  3566. *
  3567. * Note: `filter()` always returns a new instance, even if it results in
  3568. * not filtering out any values.
  3569. */
  3570. filter<F extends V>(
  3571. predicate: (value: V, key: K, iter: this) => value is F,
  3572. context?: unknown
  3573. ): Collection.Keyed<K, F>;
  3574. filter(
  3575. predicate: (value: V, key: K, iter: this) => unknown,
  3576. context?: unknown
  3577. ): this;
  3578. /**
  3579. * Returns a new keyed Collection with the values for which the
  3580. * `predicate` function returns false and another for which is returns
  3581. * true.
  3582. */
  3583. partition<F extends V, C>(
  3584. predicate: (this: C, value: V, key: K, iter: this) => value is F,
  3585. context?: C
  3586. ): [Collection.Keyed<K, V>, Collection.Keyed<K, F>];
  3587. partition<C>(
  3588. predicate: (this: C, value: V, key: K, iter: this) => unknown,
  3589. context?: C
  3590. ): [this, this];
  3591. [Symbol.iterator](): IterableIterator<[K, V]>;
  3592. }
  3593. /**
  3594. * Indexed Collections have incrementing numeric keys. They exhibit
  3595. * slightly different behavior than `Collection.Keyed` for some methods in order
  3596. * to better mirror the behavior of JavaScript's `Array`, and add methods
  3597. * which do not make sense on non-indexed Collections such as `indexOf`.
  3598. *
  3599. * Unlike JavaScript arrays, `Collection.Indexed`s are always dense. "Unset"
  3600. * indices and `undefined` indices are indistinguishable, and all indices from
  3601. * 0 to `size` are visited when iterated.
  3602. *
  3603. * All Collection.Indexed methods return re-indexed Collections. In other words,
  3604. * indices always start at 0 and increment until size. If you wish to
  3605. * preserve indices, using them as keys, convert to a Collection.Keyed by
  3606. * calling `toKeyedSeq`.
  3607. */
  3608. namespace Indexed {}
  3609. /**
  3610. * Creates a new Collection.Indexed.
  3611. *
  3612. * Note: `Collection.Indexed` is a conversion function and not a class, and
  3613. * does not use the `new` keyword during construction.
  3614. */
  3615. function Indexed<T>(
  3616. collection?: Iterable<T> | ArrayLike<T>
  3617. ): Collection.Indexed<T>;
  3618. interface Indexed<T> extends Collection<number, T> {
  3619. /**
  3620. * Deeply converts this Indexed collection to equivalent native JavaScript Array.
  3621. */
  3622. toJS(): Array<DeepCopy<T>>;
  3623. /**
  3624. * Shallowly converts this Indexed collection to equivalent native JavaScript Array.
  3625. */
  3626. toJSON(): Array<T>;
  3627. /**
  3628. * Shallowly converts this collection to an Array.
  3629. */
  3630. toArray(): Array<T>;
  3631. // Reading values
  3632. /**
  3633. * Returns the value associated with the provided index, or notSetValue if
  3634. * the index is beyond the bounds of the Collection.
  3635. *
  3636. * `index` may be a negative number, which indexes back from the end of the
  3637. * Collection. `s.get(-1)` gets the last item in the Collection.
  3638. */
  3639. get<NSV>(index: number, notSetValue: NSV): T | NSV;
  3640. get(index: number): T | undefined;
  3641. // Conversion to Seq
  3642. /**
  3643. * Returns Seq.Indexed.
  3644. * @override
  3645. */
  3646. toSeq(): Seq.Indexed<T>;
  3647. /**
  3648. * If this is a collection of [key, value] entry tuples, it will return a
  3649. * Seq.Keyed of those entries.
  3650. */
  3651. fromEntrySeq(): Seq.Keyed<unknown, unknown>;
  3652. // Combination
  3653. /**
  3654. * Returns a Collection of the same type with `separator` between each item
  3655. * in this Collection.
  3656. */
  3657. interpose(separator: T): this;
  3658. /**
  3659. * Returns a Collection of the same type with the provided `collections`
  3660. * interleaved into this collection.
  3661. *
  3662. * The resulting Collection includes the first item from each, then the
  3663. * second from each, etc.
  3664. *
  3665. * <!-- runkit:activate
  3666. * { "preamble": "require('immutable')"}
  3667. * -->
  3668. * ```js
  3669. * const { List } = require('immutable')
  3670. * List([ 1, 2, 3 ]).interleave(List([ 'A', 'B', 'C' ]))
  3671. * // List [ 1, "A", 2, "B", 3, "C" ]
  3672. * ```
  3673. *
  3674. * The shortest Collection stops interleave.
  3675. *
  3676. * <!-- runkit:activate
  3677. * { "preamble": "const { List } = require('immutable')" }
  3678. * -->
  3679. * ```js
  3680. * List([ 1, 2, 3 ]).interleave(
  3681. * List([ 'A', 'B' ]),
  3682. * List([ 'X', 'Y', 'Z' ])
  3683. * )
  3684. * // List [ 1, "A", "X", 2, "B", "Y" ]
  3685. * ```
  3686. *
  3687. * Since `interleave()` re-indexes values, it produces a complete copy,
  3688. * which has `O(N)` complexity.
  3689. *
  3690. * Note: `interleave` *cannot* be used in `withMutations`.
  3691. */
  3692. interleave(...collections: Array<Collection<unknown, T>>): this;
  3693. /**
  3694. * Splice returns a new indexed Collection by replacing a region of this
  3695. * Collection with new values. If values are not provided, it only skips the
  3696. * region to be removed.
  3697. *
  3698. * `index` may be a negative number, which indexes back from the end of the
  3699. * Collection. `s.splice(-2)` splices after the second to last item.
  3700. *
  3701. * <!-- runkit:activate -->
  3702. * ```js
  3703. * const { List } = require('immutable')
  3704. * List([ 'a', 'b', 'c', 'd' ]).splice(1, 2, 'q', 'r', 's')
  3705. * // List [ "a", "q", "r", "s", "d" ]
  3706. * ```
  3707. *
  3708. * Since `splice()` re-indexes values, it produces a complete copy, which
  3709. * has `O(N)` complexity.
  3710. *
  3711. * Note: `splice` *cannot* be used in `withMutations`.
  3712. */
  3713. splice(index: number, removeNum: number, ...values: Array<T>): this;
  3714. /**
  3715. * Returns a Collection of the same type "zipped" with the provided
  3716. * collections.
  3717. *
  3718. * Like `zipWith`, but using the default `zipper`: creating an `Array`.
  3719. *
  3720. *
  3721. * <!-- runkit:activate
  3722. * { "preamble": "const { List } = require('immutable')" }
  3723. * -->
  3724. * ```js
  3725. * const a = List([ 1, 2, 3 ]);
  3726. * const b = List([ 4, 5, 6 ]);
  3727. * const c = a.zip(b); // List [ [ 1, 4 ], [ 2, 5 ], [ 3, 6 ] ]
  3728. * ```
  3729. */
  3730. zip<U>(other: Collection<unknown, U>): Collection.Indexed<[T, U]>;
  3731. zip<U, V>(
  3732. other: Collection<unknown, U>,
  3733. other2: Collection<unknown, V>
  3734. ): Collection.Indexed<[T, U, V]>;
  3735. zip(
  3736. ...collections: Array<Collection<unknown, unknown>>
  3737. ): Collection.Indexed<unknown>;
  3738. /**
  3739. * Returns a Collection "zipped" with the provided collections.
  3740. *
  3741. * Unlike `zip`, `zipAll` continues zipping until the longest collection is
  3742. * exhausted. Missing values from shorter collections are filled with `undefined`.
  3743. *
  3744. * ```js
  3745. * const a = List([ 1, 2 ]);
  3746. * const b = List([ 3, 4, 5 ]);
  3747. * const c = a.zipAll(b); // List [ [ 1, 3 ], [ 2, 4 ], [ undefined, 5 ] ]
  3748. * ```
  3749. */
  3750. zipAll<U>(other: Collection<unknown, U>): Collection.Indexed<[T, U]>;
  3751. zipAll<U, V>(
  3752. other: Collection<unknown, U>,
  3753. other2: Collection<unknown, V>
  3754. ): Collection.Indexed<[T, U, V]>;
  3755. zipAll(
  3756. ...collections: Array<Collection<unknown, unknown>>
  3757. ): Collection.Indexed<unknown>;
  3758. /**
  3759. * Returns a Collection of the same type "zipped" with the provided
  3760. * collections by using a custom `zipper` function.
  3761. *
  3762. * <!-- runkit:activate
  3763. * { "preamble": "const { List } = require('immutable')" }
  3764. * -->
  3765. * ```js
  3766. * const a = List([ 1, 2, 3 ]);
  3767. * const b = List([ 4, 5, 6 ]);
  3768. * const c = a.zipWith((a, b) => a + b, b);
  3769. * // List [ 5, 7, 9 ]
  3770. * ```
  3771. */
  3772. zipWith<U, Z>(
  3773. zipper: (value: T, otherValue: U) => Z,
  3774. otherCollection: Collection<unknown, U>
  3775. ): Collection.Indexed<Z>;
  3776. zipWith<U, V, Z>(
  3777. zipper: (value: T, otherValue: U, thirdValue: V) => Z,
  3778. otherCollection: Collection<unknown, U>,
  3779. thirdCollection: Collection<unknown, V>
  3780. ): Collection.Indexed<Z>;
  3781. zipWith<Z>(
  3782. zipper: (...values: Array<unknown>) => Z,
  3783. ...collections: Array<Collection<unknown, unknown>>
  3784. ): Collection.Indexed<Z>;
  3785. // Search for value
  3786. /**
  3787. * Returns the first index at which a given value can be found in the
  3788. * Collection, or -1 if it is not present.
  3789. */
  3790. indexOf(searchValue: T): number;
  3791. /**
  3792. * Returns the last index at which a given value can be found in the
  3793. * Collection, or -1 if it is not present.
  3794. */
  3795. lastIndexOf(searchValue: T): number;
  3796. /**
  3797. * Returns the first index in the Collection where a value satisfies the
  3798. * provided predicate function. Otherwise -1 is returned.
  3799. */
  3800. findIndex(
  3801. predicate: (value: T, index: number, iter: this) => boolean,
  3802. context?: unknown
  3803. ): number;
  3804. /**
  3805. * Returns the last index in the Collection where a value satisfies the
  3806. * provided predicate function. Otherwise -1 is returned.
  3807. */
  3808. findLastIndex(
  3809. predicate: (value: T, index: number, iter: this) => boolean,
  3810. context?: unknown
  3811. ): number;
  3812. // Sequence algorithms
  3813. /**
  3814. * Returns a new Collection with other collections concatenated to this one.
  3815. */
  3816. concat<C>(
  3817. ...valuesOrCollections: Array<Iterable<C> | C>
  3818. ): Collection.Indexed<T | C>;
  3819. /**
  3820. * Returns a new Collection.Indexed with values passed through a
  3821. * `mapper` function.
  3822. *
  3823. * ```js
  3824. * const { Collection } = require('immutable')
  3825. * Collection.Indexed([1,2]).map(x => 10 * x)
  3826. * // Seq [ 1, 2 ]
  3827. * ```
  3828. *
  3829. * Note: `map()` always returns a new instance, even if it produced the
  3830. * same value at every step.
  3831. */
  3832. map<M>(
  3833. mapper: (value: T, key: number, iter: this) => M,
  3834. context?: unknown
  3835. ): Collection.Indexed<M>;
  3836. /**
  3837. * Flat-maps the Collection, returning a Collection of the same type.
  3838. *
  3839. * Similar to `collection.map(...).flatten(true)`.
  3840. */
  3841. flatMap<M>(
  3842. mapper: (value: T, key: number, iter: this) => Iterable<M>,
  3843. context?: unknown
  3844. ): Collection.Indexed<M>;
  3845. /**
  3846. * Returns a new Collection with only the values for which the `predicate`
  3847. * function returns true.
  3848. *
  3849. * Note: `filter()` always returns a new instance, even if it results in
  3850. * not filtering out any values.
  3851. */
  3852. filter<F extends T>(
  3853. predicate: (value: T, index: number, iter: this) => value is F,
  3854. context?: unknown
  3855. ): Collection.Indexed<F>;
  3856. filter(
  3857. predicate: (value: T, index: number, iter: this) => unknown,
  3858. context?: unknown
  3859. ): this;
  3860. /**
  3861. * Returns a new indexed Collection with the values for which the
  3862. * `predicate` function returns false and another for which is returns
  3863. * true.
  3864. */
  3865. partition<F extends T, C>(
  3866. predicate: (this: C, value: T, index: number, iter: this) => value is F,
  3867. context?: C
  3868. ): [Collection.Indexed<T>, Collection.Indexed<F>];
  3869. partition<C>(
  3870. predicate: (this: C, value: T, index: number, iter: this) => unknown,
  3871. context?: C
  3872. ): [this, this];
  3873. [Symbol.iterator](): IterableIterator<T>;
  3874. }
  3875. /**
  3876. * Set Collections only represent values. They have no associated keys or
  3877. * indices. Duplicate values are possible in the lazy `Seq.Set`s, however
  3878. * the concrete `Set` Collection does not allow duplicate values.
  3879. *
  3880. * Collection methods on Collection.Set such as `map` and `forEach` will provide
  3881. * the value as both the first and second arguments to the provided function.
  3882. *
  3883. * ```js
  3884. * const { Collection } = require('immutable')
  3885. * const seq = Collection.Set([ 'A', 'B', 'C' ])
  3886. * // Seq { "A", "B", "C" }
  3887. * seq.forEach((v, k) =>
  3888. * assert.equal(v, k)
  3889. * )
  3890. * ```
  3891. */
  3892. namespace Set {}
  3893. /**
  3894. * Similar to `Collection()`, but always returns a Collection.Set.
  3895. *
  3896. * Note: `Collection.Set` is a factory function and not a class, and does
  3897. * not use the `new` keyword during construction.
  3898. */
  3899. function Set<T>(collection?: Iterable<T> | ArrayLike<T>): Collection.Set<T>;
  3900. interface Set<T> extends Collection<T, T> {
  3901. /**
  3902. * Deeply converts this Set collection to equivalent native JavaScript Array.
  3903. */
  3904. toJS(): Array<DeepCopy<T>>;
  3905. /**
  3906. * Shallowly converts this Set collection to equivalent native JavaScript Array.
  3907. */
  3908. toJSON(): Array<T>;
  3909. /**
  3910. * Shallowly converts this collection to an Array.
  3911. */
  3912. toArray(): Array<T>;
  3913. /**
  3914. * Returns Seq.Set.
  3915. * @override
  3916. */
  3917. toSeq(): Seq.Set<T>;
  3918. // Sequence algorithms
  3919. /**
  3920. * Returns a new Collection with other collections concatenated to this one.
  3921. */
  3922. concat<U>(...collections: Array<Iterable<U>>): Collection.Set<T | U>;
  3923. /**
  3924. * Returns a new Collection.Set with values passed through a
  3925. * `mapper` function.
  3926. *
  3927. * ```
  3928. * Collection.Set([ 1, 2 ]).map(x => 10 * x)
  3929. * // Seq { 1, 2 }
  3930. * ```
  3931. *
  3932. * Note: `map()` always returns a new instance, even if it produced the
  3933. * same value at every step.
  3934. */
  3935. map<M>(
  3936. mapper: (value: T, key: T, iter: this) => M,
  3937. context?: unknown
  3938. ): Collection.Set<M>;
  3939. /**
  3940. * Flat-maps the Collection, returning a Collection of the same type.
  3941. *
  3942. * Similar to `collection.map(...).flatten(true)`.
  3943. */
  3944. flatMap<M>(
  3945. mapper: (value: T, key: T, iter: this) => Iterable<M>,
  3946. context?: unknown
  3947. ): Collection.Set<M>;
  3948. /**
  3949. * Returns a new Collection with only the values for which the `predicate`
  3950. * function returns true.
  3951. *
  3952. * Note: `filter()` always returns a new instance, even if it results in
  3953. * not filtering out any values.
  3954. */
  3955. filter<F extends T>(
  3956. predicate: (value: T, key: T, iter: this) => value is F,
  3957. context?: unknown
  3958. ): Collection.Set<F>;
  3959. filter(
  3960. predicate: (value: T, key: T, iter: this) => unknown,
  3961. context?: unknown
  3962. ): this;
  3963. /**
  3964. * Returns a new set Collection with the values for which the
  3965. * `predicate` function returns false and another for which is returns
  3966. * true.
  3967. */
  3968. partition<F extends T, C>(
  3969. predicate: (this: C, value: T, key: T, iter: this) => value is F,
  3970. context?: C
  3971. ): [Collection.Set<T>, Collection.Set<F>];
  3972. partition<C>(
  3973. predicate: (this: C, value: T, key: T, iter: this) => unknown,
  3974. context?: C
  3975. ): [this, this];
  3976. [Symbol.iterator](): IterableIterator<T>;
  3977. }
  3978. }
  3979. /**
  3980. * Creates a Collection.
  3981. *
  3982. * The type of Collection created is based on the input.
  3983. *
  3984. * * If an `Collection`, that same `Collection`.
  3985. * * If an Array-like, an `Collection.Indexed`.
  3986. * * If an Object with an Iterator defined, an `Collection.Indexed`.
  3987. * * If an Object, an `Collection.Keyed`.
  3988. *
  3989. * This methods forces the conversion of Objects and Strings to Collections.
  3990. * If you want to ensure that a Collection of one item is returned, use
  3991. * `Seq.of`.
  3992. *
  3993. * Note: An Iterator itself will be treated as an object, becoming a `Seq.Keyed`,
  3994. * which is usually not what you want. You should turn your Iterator Object into
  3995. * an iterable object by defining a Symbol.iterator (or @@iterator) method which
  3996. * returns `this`.
  3997. *
  3998. * Note: `Collection` is a conversion function and not a class, and does not
  3999. * use the `new` keyword during construction.
  4000. */
  4001. function Collection<I extends Collection<unknown, unknown>>(collection: I): I;
  4002. function Collection<T>(
  4003. collection: Iterable<T> | ArrayLike<T>
  4004. ): Collection.Indexed<T>;
  4005. function Collection<V>(obj: {
  4006. [key: string]: V;
  4007. }): Collection.Keyed<string, V>;
  4008. function Collection<K = unknown, V = unknown>(): Collection<K, V>;
  4009. interface Collection<K, V> extends ValueObject {
  4010. // Value equality
  4011. /**
  4012. * True if this and the other Collection have value equality, as defined
  4013. * by `Immutable.is()`.
  4014. *
  4015. * Note: This is equivalent to `Immutable.is(this, other)`, but provided to
  4016. * allow for chained expressions.
  4017. */
  4018. equals(other: unknown): boolean;
  4019. /**
  4020. * Computes and returns the hashed identity for this Collection.
  4021. *
  4022. * The `hashCode` of a Collection is used to determine potential equality,
  4023. * and is used when adding this to a `Set` or as a key in a `Map`, enabling
  4024. * lookup via a different instance.
  4025. *
  4026. * <!-- runkit:activate
  4027. * { "preamble": "const { Set, List } = require('immutable')" }
  4028. * -->
  4029. * ```js
  4030. * const a = List([ 1, 2, 3 ]);
  4031. * const b = List([ 1, 2, 3 ]);
  4032. * assert.notStrictEqual(a, b); // different instances
  4033. * const set = Set([ a ]);
  4034. * assert.equal(set.has(b), true);
  4035. * ```
  4036. *
  4037. * If two values have the same `hashCode`, they are [not guaranteed
  4038. * to be equal][Hash Collision]. If two values have different `hashCode`s,
  4039. * they must not be equal.
  4040. *
  4041. * [Hash Collision]: https://en.wikipedia.org/wiki/Collision_(computer_science)
  4042. */
  4043. hashCode(): number;
  4044. // Reading values
  4045. /**
  4046. * Returns the value associated with the provided key, or notSetValue if
  4047. * the Collection does not contain this key.
  4048. *
  4049. * Note: it is possible a key may be associated with an `undefined` value,
  4050. * so if `notSetValue` is not provided and this method returns `undefined`,
  4051. * that does not guarantee the key was not found.
  4052. */
  4053. get<NSV>(key: K, notSetValue: NSV): V | NSV;
  4054. get(key: K): V | undefined;
  4055. /**
  4056. * True if a key exists within this `Collection`, using `Immutable.is`
  4057. * to determine equality
  4058. */
  4059. has(key: K): boolean;
  4060. /**
  4061. * True if a value exists within this `Collection`, using `Immutable.is`
  4062. * to determine equality
  4063. * @alias contains
  4064. */
  4065. includes(value: V): boolean;
  4066. contains(value: V): boolean;
  4067. /**
  4068. * In case the `Collection` is not empty returns the first element of the
  4069. * `Collection`.
  4070. * In case the `Collection` is empty returns the optional default
  4071. * value if provided, if no default value is provided returns undefined.
  4072. */
  4073. first<NSV>(notSetValue: NSV): V | NSV;
  4074. first(): V | undefined;
  4075. /**
  4076. * In case the `Collection` is not empty returns the last element of the
  4077. * `Collection`.
  4078. * In case the `Collection` is empty returns the optional default
  4079. * value if provided, if no default value is provided returns undefined.
  4080. */
  4081. last<NSV>(notSetValue: NSV): V | NSV;
  4082. last(): V | undefined;
  4083. // Reading deep values
  4084. /**
  4085. * Returns the value found by following a path of keys or indices through
  4086. * nested Collections.
  4087. *
  4088. * <!-- runkit:activate -->
  4089. * ```js
  4090. * const { Map, List } = require('immutable')
  4091. * const deepData = Map({ x: List([ Map({ y: 123 }) ]) });
  4092. * deepData.getIn(['x', 0, 'y']) // 123
  4093. * ```
  4094. *
  4095. * Plain JavaScript Object or Arrays may be nested within an Immutable.js
  4096. * Collection, and getIn() can access those values as well:
  4097. *
  4098. * <!-- runkit:activate -->
  4099. * ```js
  4100. * const { Map, List } = require('immutable')
  4101. * const deepData = Map({ x: [ { y: 123 } ] });
  4102. * deepData.getIn(['x', 0, 'y']) // 123
  4103. * ```
  4104. */
  4105. getIn(searchKeyPath: Iterable<unknown>, notSetValue?: unknown): unknown;
  4106. /**
  4107. * True if the result of following a path of keys or indices through nested
  4108. * Collections results in a set value.
  4109. */
  4110. hasIn(searchKeyPath: Iterable<unknown>): boolean;
  4111. // Persistent changes
  4112. /**
  4113. * This can be very useful as a way to "chain" a normal function into a
  4114. * sequence of methods. RxJS calls this "let" and lodash calls it "thru".
  4115. *
  4116. * For example, to sum a Seq after mapping and filtering:
  4117. *
  4118. * <!-- runkit:activate -->
  4119. * ```js
  4120. * const { Seq } = require('immutable')
  4121. *
  4122. * function sum(collection) {
  4123. * return collection.reduce((sum, x) => sum + x, 0)
  4124. * }
  4125. *
  4126. * Seq([ 1, 2, 3 ])
  4127. * .map(x => x + 1)
  4128. * .filter(x => x % 2 === 0)
  4129. * .update(sum)
  4130. * // 6
  4131. * ```
  4132. */
  4133. update<R>(updater: (value: this) => R): R;
  4134. // Conversion to JavaScript types
  4135. /**
  4136. * Deeply converts this Collection to equivalent native JavaScript Array or Object.
  4137. *
  4138. * `Collection.Indexed`, and `Collection.Set` become `Array`, while
  4139. * `Collection.Keyed` become `Object`, converting keys to Strings.
  4140. */
  4141. toJS():
  4142. | Array<DeepCopy<V>>
  4143. | { [key in string | number | symbol]: DeepCopy<V> };
  4144. /**
  4145. * Shallowly converts this Collection to equivalent native JavaScript Array or Object.
  4146. *
  4147. * `Collection.Indexed`, and `Collection.Set` become `Array`, while
  4148. * `Collection.Keyed` become `Object`, converting keys to Strings.
  4149. */
  4150. toJSON(): Array<V> | { [key in string | number | symbol]: V };
  4151. /**
  4152. * Shallowly converts this collection to an Array.
  4153. *
  4154. * `Collection.Indexed`, and `Collection.Set` produce an Array of values.
  4155. * `Collection.Keyed` produce an Array of [key, value] tuples.
  4156. */
  4157. toArray(): Array<V> | Array<[K, V]>;
  4158. /**
  4159. * Shallowly converts this Collection to an Object.
  4160. *
  4161. * Converts keys to Strings.
  4162. */
  4163. toObject(): { [key: string]: V };
  4164. // Conversion to Collections
  4165. /**
  4166. * Converts this Collection to a Map, Throws if keys are not hashable.
  4167. *
  4168. * Note: This is equivalent to `Map(this.toKeyedSeq())`, but provided
  4169. * for convenience and to allow for chained expressions.
  4170. */
  4171. toMap(): Map<K, V>;
  4172. /**
  4173. * Converts this Collection to a Map, maintaining the order of iteration.
  4174. *
  4175. * Note: This is equivalent to `OrderedMap(this.toKeyedSeq())`, but
  4176. * provided for convenience and to allow for chained expressions.
  4177. */
  4178. toOrderedMap(): OrderedMap<K, V>;
  4179. /**
  4180. * Converts this Collection to a Set, discarding keys. Throws if values
  4181. * are not hashable.
  4182. *
  4183. * Note: This is equivalent to `Set(this)`, but provided to allow for
  4184. * chained expressions.
  4185. */
  4186. toSet(): Set<V>;
  4187. /**
  4188. * Converts this Collection to a Set, maintaining the order of iteration and
  4189. * discarding keys.
  4190. *
  4191. * Note: This is equivalent to `OrderedSet(this.valueSeq())`, but provided
  4192. * for convenience and to allow for chained expressions.
  4193. */
  4194. toOrderedSet(): OrderedSet<V>;
  4195. /**
  4196. * Converts this Collection to a List, discarding keys.
  4197. *
  4198. * This is similar to `List(collection)`, but provided to allow for chained
  4199. * expressions. However, when called on `Map` or other keyed collections,
  4200. * `collection.toList()` discards the keys and creates a list of only the
  4201. * values, whereas `List(collection)` creates a list of entry tuples.
  4202. *
  4203. * <!-- runkit:activate -->
  4204. * ```js
  4205. * const { Map, List } = require('immutable')
  4206. * var myMap = Map({ a: 'Apple', b: 'Banana' })
  4207. * List(myMap) // List [ [ "a", "Apple" ], [ "b", "Banana" ] ]
  4208. * myMap.toList() // List [ "Apple", "Banana" ]
  4209. * ```
  4210. */
  4211. toList(): List<V>;
  4212. /**
  4213. * Converts this Collection to a Stack, discarding keys. Throws if values
  4214. * are not hashable.
  4215. *
  4216. * Note: This is equivalent to `Stack(this)`, but provided to allow for
  4217. * chained expressions.
  4218. */
  4219. toStack(): Stack<V>;
  4220. // Conversion to Seq
  4221. /**
  4222. * Converts this Collection to a Seq of the same kind (indexed,
  4223. * keyed, or set).
  4224. */
  4225. toSeq(): Seq<K, V>;
  4226. /**
  4227. * Returns a Seq.Keyed from this Collection where indices are treated as keys.
  4228. *
  4229. * This is useful if you want to operate on an
  4230. * Collection.Indexed and preserve the [index, value] pairs.
  4231. *
  4232. * The returned Seq will have identical iteration order as
  4233. * this Collection.
  4234. *
  4235. * <!-- runkit:activate -->
  4236. * ```js
  4237. * const { Seq } = require('immutable')
  4238. * const indexedSeq = Seq([ 'A', 'B', 'C' ])
  4239. * // Seq [ "A", "B", "C" ]
  4240. * indexedSeq.filter(v => v === 'B')
  4241. * // Seq [ "B" ]
  4242. * const keyedSeq = indexedSeq.toKeyedSeq()
  4243. * // Seq { 0: "A", 1: "B", 2: "C" }
  4244. * keyedSeq.filter(v => v === 'B')
  4245. * // Seq { 1: "B" }
  4246. * ```
  4247. */
  4248. toKeyedSeq(): Seq.Keyed<K, V>;
  4249. /**
  4250. * Returns an Seq.Indexed of the values of this Collection, discarding keys.
  4251. */
  4252. toIndexedSeq(): Seq.Indexed<V>;
  4253. /**
  4254. * Returns a Seq.Set of the values of this Collection, discarding keys.
  4255. */
  4256. toSetSeq(): Seq.Set<V>;
  4257. // Iterators
  4258. /**
  4259. * An iterator of this `Collection`'s keys.
  4260. *
  4261. * Note: this will return an ES6 iterator which does not support
  4262. * Immutable.js sequence algorithms. Use `keySeq` instead, if this is
  4263. * what you want.
  4264. */
  4265. keys(): IterableIterator<K>;
  4266. /**
  4267. * An iterator of this `Collection`'s values.
  4268. *
  4269. * Note: this will return an ES6 iterator which does not support
  4270. * Immutable.js sequence algorithms. Use `valueSeq` instead, if this is
  4271. * what you want.
  4272. */
  4273. values(): IterableIterator<V>;
  4274. /**
  4275. * An iterator of this `Collection`'s entries as `[ key, value ]` tuples.
  4276. *
  4277. * Note: this will return an ES6 iterator which does not support
  4278. * Immutable.js sequence algorithms. Use `entrySeq` instead, if this is
  4279. * what you want.
  4280. */
  4281. entries(): IterableIterator<[K, V]>;
  4282. [Symbol.iterator](): IterableIterator<unknown>;
  4283. // Collections (Seq)
  4284. /**
  4285. * Returns a new Seq.Indexed of the keys of this Collection,
  4286. * discarding values.
  4287. */
  4288. keySeq(): Seq.Indexed<K>;
  4289. /**
  4290. * Returns an Seq.Indexed of the values of this Collection, discarding keys.
  4291. */
  4292. valueSeq(): Seq.Indexed<V>;
  4293. /**
  4294. * Returns a new Seq.Indexed of [key, value] tuples.
  4295. */
  4296. entrySeq(): Seq.Indexed<[K, V]>;
  4297. // Sequence algorithms
  4298. /**
  4299. * Returns a new Collection of the same type with values passed through a
  4300. * `mapper` function.
  4301. *
  4302. * <!-- runkit:activate -->
  4303. * ```js
  4304. * const { Collection } = require('immutable')
  4305. * Collection({ a: 1, b: 2 }).map(x => 10 * x)
  4306. * // Seq { "a": 10, "b": 20 }
  4307. * ```
  4308. *
  4309. * Note: `map()` always returns a new instance, even if it produced the same
  4310. * value at every step.
  4311. */
  4312. map<M>(
  4313. mapper: (value: V, key: K, iter: this) => M,
  4314. context?: unknown
  4315. ): Collection<K, M>;
  4316. /**
  4317. * Note: used only for sets, which return Collection<M, M> but are otherwise
  4318. * identical to normal `map()`.
  4319. *
  4320. * @ignore
  4321. */
  4322. map(...args: Array<never>): unknown;
  4323. /**
  4324. * Returns a new Collection of the same type with only the entries for which
  4325. * the `predicate` function returns true.
  4326. *
  4327. * <!-- runkit:activate -->
  4328. * ```js
  4329. * const { Map } = require('immutable')
  4330. * Map({ a: 1, b: 2, c: 3, d: 4}).filter(x => x % 2 === 0)
  4331. * // Map { "b": 2, "d": 4 }
  4332. * ```
  4333. *
  4334. * Note: `filter()` always returns a new instance, even if it results in
  4335. * not filtering out any values.
  4336. */
  4337. filter<F extends V>(
  4338. predicate: (value: V, key: K, iter: this) => value is F,
  4339. context?: unknown
  4340. ): Collection<K, F>;
  4341. filter(
  4342. predicate: (value: V, key: K, iter: this) => unknown,
  4343. context?: unknown
  4344. ): this;
  4345. /**
  4346. * Returns a new Collection of the same type with only the entries for which
  4347. * the `predicate` function returns false.
  4348. *
  4349. * <!-- runkit:activate -->
  4350. * ```js
  4351. * const { Map } = require('immutable')
  4352. * Map({ a: 1, b: 2, c: 3, d: 4}).filterNot(x => x % 2 === 0)
  4353. * // Map { "a": 1, "c": 3 }
  4354. * ```
  4355. *
  4356. * Note: `filterNot()` always returns a new instance, even if it results in
  4357. * not filtering out any values.
  4358. */
  4359. filterNot(
  4360. predicate: (value: V, key: K, iter: this) => boolean,
  4361. context?: unknown
  4362. ): this;
  4363. /**
  4364. * Returns a new Collection with the values for which the `predicate`
  4365. * function returns false and another for which is returns true.
  4366. */
  4367. partition<F extends V, C>(
  4368. predicate: (this: C, value: V, key: K, iter: this) => value is F,
  4369. context?: C
  4370. ): [Collection<K, V>, Collection<K, F>];
  4371. partition<C>(
  4372. predicate: (this: C, value: V, key: K, iter: this) => unknown,
  4373. context?: C
  4374. ): [this, this];
  4375. /**
  4376. * Returns a new Collection of the same type in reverse order.
  4377. */
  4378. reverse(): this;
  4379. /**
  4380. * Returns a new Collection of the same type which includes the same entries,
  4381. * stably sorted by using a `comparator`.
  4382. *
  4383. * If a `comparator` is not provided, a default comparator uses `<` and `>`.
  4384. *
  4385. * `comparator(valueA, valueB)`:
  4386. *
  4387. * * Returns `0` if the elements should not be swapped.
  4388. * * Returns `-1` (or any negative number) if `valueA` comes before `valueB`
  4389. * * Returns `1` (or any positive number) if `valueA` comes after `valueB`
  4390. * * Alternatively, can return a value of the `PairSorting` enum type
  4391. * * Is pure, i.e. it must always return the same value for the same pair
  4392. * of values.
  4393. *
  4394. * When sorting collections which have no defined order, their ordered
  4395. * equivalents will be returned. e.g. `map.sort()` returns OrderedMap.
  4396. *
  4397. * <!-- runkit:activate -->
  4398. * ```js
  4399. * const { Map } = require('immutable')
  4400. * Map({ "c": 3, "a": 1, "b": 2 }).sort((a, b) => {
  4401. * if (a < b) { return -1; }
  4402. * if (a > b) { return 1; }
  4403. * if (a === b) { return 0; }
  4404. * });
  4405. * // OrderedMap { "a": 1, "b": 2, "c": 3 }
  4406. * ```
  4407. *
  4408. * Note: `sort()` Always returns a new instance, even if the original was
  4409. * already sorted.
  4410. *
  4411. * Note: This is always an eager operation.
  4412. */
  4413. sort(comparator?: Comparator<V>): this;
  4414. /**
  4415. * Like `sort`, but also accepts a `comparatorValueMapper` which allows for
  4416. * sorting by more sophisticated means:
  4417. *
  4418. * <!-- runkit:activate -->
  4419. * ```js
  4420. * const { Map } = require('immutable')
  4421. * const beattles = Map({
  4422. * John: { name: "Lennon" },
  4423. * Paul: { name: "McCartney" },
  4424. * George: { name: "Harrison" },
  4425. * Ringo: { name: "Starr" },
  4426. * });
  4427. * beattles.sortBy(member => member.name);
  4428. * ```
  4429. *
  4430. * Note: `sortBy()` Always returns a new instance, even if the original was
  4431. * already sorted.
  4432. *
  4433. * Note: This is always an eager operation.
  4434. */
  4435. sortBy<C>(
  4436. comparatorValueMapper: (value: V, key: K, iter: this) => C,
  4437. comparator?: Comparator<C>
  4438. ): this;
  4439. /**
  4440. * Returns a `Map` of `Collection`, grouped by the return
  4441. * value of the `grouper` function.
  4442. *
  4443. * Note: This is always an eager operation.
  4444. *
  4445. * <!-- runkit:activate -->
  4446. * ```js
  4447. * const { List, Map } = require('immutable')
  4448. * const listOfMaps = List([
  4449. * Map({ v: 0 }),
  4450. * Map({ v: 1 }),
  4451. * Map({ v: 1 }),
  4452. * Map({ v: 0 }),
  4453. * Map({ v: 2 })
  4454. * ])
  4455. * const groupsOfMaps = listOfMaps.groupBy(x => x.get('v'))
  4456. * // Map {
  4457. * // 0: List [ Map{ "v": 0 }, Map { "v": 0 } ],
  4458. * // 1: List [ Map{ "v": 1 }, Map { "v": 1 } ],
  4459. * // 2: List [ Map{ "v": 2 } ],
  4460. * // }
  4461. * ```
  4462. */
  4463. groupBy<G>(
  4464. grouper: (value: V, key: K, iter: this) => G,
  4465. context?: unknown
  4466. ): Map<G, this>;
  4467. // Side effects
  4468. /**
  4469. * The `sideEffect` is executed for every entry in the Collection.
  4470. *
  4471. * Unlike `Array#forEach`, if any call of `sideEffect` returns
  4472. * `false`, the iteration will stop. Returns the number of entries iterated
  4473. * (including the last iteration which returned false).
  4474. */
  4475. forEach(
  4476. sideEffect: (value: V, key: K, iter: this) => unknown,
  4477. context?: unknown
  4478. ): number;
  4479. // Creating subsets
  4480. /**
  4481. * Returns a new Collection of the same type representing a portion of this
  4482. * Collection from start up to but not including end.
  4483. *
  4484. * If begin is negative, it is offset from the end of the Collection. e.g.
  4485. * `slice(-2)` returns a Collection of the last two entries. If it is not
  4486. * provided the new Collection will begin at the beginning of this Collection.
  4487. *
  4488. * If end is negative, it is offset from the end of the Collection. e.g.
  4489. * `slice(0, -1)` returns a Collection of everything but the last entry. If
  4490. * it is not provided, the new Collection will continue through the end of
  4491. * this Collection.
  4492. *
  4493. * If the requested slice is equivalent to the current Collection, then it
  4494. * will return itself.
  4495. */
  4496. slice(begin?: number, end?: number): this;
  4497. /**
  4498. * Returns a new Collection of the same type containing all entries except
  4499. * the first.
  4500. */
  4501. rest(): this;
  4502. /**
  4503. * Returns a new Collection of the same type containing all entries except
  4504. * the last.
  4505. */
  4506. butLast(): this;
  4507. /**
  4508. * Returns a new Collection of the same type which excludes the first `amount`
  4509. * entries from this Collection.
  4510. */
  4511. skip(amount: number): this;
  4512. /**
  4513. * Returns a new Collection of the same type which excludes the last `amount`
  4514. * entries from this Collection.
  4515. */
  4516. skipLast(amount: number): this;
  4517. /**
  4518. * Returns a new Collection of the same type which includes entries starting
  4519. * from when `predicate` first returns false.
  4520. *
  4521. * <!-- runkit:activate -->
  4522. * ```js
  4523. * const { List } = require('immutable')
  4524. * List([ 'dog', 'frog', 'cat', 'hat', 'god' ])
  4525. * .skipWhile(x => x.match(/g/))
  4526. * // List [ "cat", "hat", "god" ]
  4527. * ```
  4528. */
  4529. skipWhile(
  4530. predicate: (value: V, key: K, iter: this) => boolean,
  4531. context?: unknown
  4532. ): this;
  4533. /**
  4534. * Returns a new Collection of the same type which includes entries starting
  4535. * from when `predicate` first returns true.
  4536. *
  4537. * <!-- runkit:activate -->
  4538. * ```js
  4539. * const { List } = require('immutable')
  4540. * List([ 'dog', 'frog', 'cat', 'hat', 'god' ])
  4541. * .skipUntil(x => x.match(/hat/))
  4542. * // List [ "hat", "god" ]
  4543. * ```
  4544. */
  4545. skipUntil(
  4546. predicate: (value: V, key: K, iter: this) => boolean,
  4547. context?: unknown
  4548. ): this;
  4549. /**
  4550. * Returns a new Collection of the same type which includes the first `amount`
  4551. * entries from this Collection.
  4552. */
  4553. take(amount: number): this;
  4554. /**
  4555. * Returns a new Collection of the same type which includes the last `amount`
  4556. * entries from this Collection.
  4557. */
  4558. takeLast(amount: number): this;
  4559. /**
  4560. * Returns a new Collection of the same type which includes entries from this
  4561. * Collection as long as the `predicate` returns true.
  4562. *
  4563. * <!-- runkit:activate -->
  4564. * ```js
  4565. * const { List } = require('immutable')
  4566. * List([ 'dog', 'frog', 'cat', 'hat', 'god' ])
  4567. * .takeWhile(x => x.match(/o/))
  4568. * // List [ "dog", "frog" ]
  4569. * ```
  4570. */
  4571. takeWhile(
  4572. predicate: (value: V, key: K, iter: this) => boolean,
  4573. context?: unknown
  4574. ): this;
  4575. /**
  4576. * Returns a new Collection of the same type which includes entries from this
  4577. * Collection as long as the `predicate` returns false.
  4578. *
  4579. * <!-- runkit:activate -->
  4580. * ```js
  4581. * const { List } = require('immutable')
  4582. * List([ 'dog', 'frog', 'cat', 'hat', 'god' ])
  4583. * .takeUntil(x => x.match(/at/))
  4584. * // List [ "dog", "frog" ]
  4585. * ```
  4586. */
  4587. takeUntil(
  4588. predicate: (value: V, key: K, iter: this) => boolean,
  4589. context?: unknown
  4590. ): this;
  4591. // Combination
  4592. /**
  4593. * Returns a new Collection of the same type with other values and
  4594. * collection-like concatenated to this one.
  4595. *
  4596. * For Seqs, all entries will be present in the resulting Seq, even if they
  4597. * have the same key.
  4598. */
  4599. concat(
  4600. ...valuesOrCollections: Array<unknown>
  4601. ): Collection<unknown, unknown>;
  4602. /**
  4603. * Flattens nested Collections.
  4604. *
  4605. * Will deeply flatten the Collection by default, returning a Collection of the
  4606. * same type, but a `depth` can be provided in the form of a number or
  4607. * boolean (where true means to shallowly flatten one level). A depth of 0
  4608. * (or shallow: false) will deeply flatten.
  4609. *
  4610. * Flattens only others Collection, not Arrays or Objects.
  4611. *
  4612. * Note: `flatten(true)` operates on Collection<unknown, Collection<K, V>> and
  4613. * returns Collection<K, V>
  4614. */
  4615. flatten(depth?: number): Collection<unknown, unknown>;
  4616. flatten(shallow?: boolean): Collection<unknown, unknown>;
  4617. /**
  4618. * Flat-maps the Collection, returning a Collection of the same type.
  4619. *
  4620. * Similar to `collection.map(...).flatten(true)`.
  4621. */
  4622. flatMap<M>(
  4623. mapper: (value: V, key: K, iter: this) => Iterable<M>,
  4624. context?: unknown
  4625. ): Collection<K, M>;
  4626. /**
  4627. * Flat-maps the Collection, returning a Collection of the same type.
  4628. *
  4629. * Similar to `collection.map(...).flatten(true)`.
  4630. * Used for Dictionaries only.
  4631. */
  4632. flatMap<KM, VM>(
  4633. mapper: (value: V, key: K, iter: this) => Iterable<[KM, VM]>,
  4634. context?: unknown
  4635. ): Collection<KM, VM>;
  4636. // Reducing a value
  4637. /**
  4638. * Reduces the Collection to a value by calling the `reducer` for every entry
  4639. * in the Collection and passing along the reduced value.
  4640. *
  4641. * If `initialReduction` is not provided, the first item in the
  4642. * Collection will be used.
  4643. *
  4644. * @see `Array#reduce`.
  4645. */
  4646. reduce<R>(
  4647. reducer: (reduction: R, value: V, key: K, iter: this) => R,
  4648. initialReduction: R,
  4649. context?: unknown
  4650. ): R;
  4651. reduce<R>(
  4652. reducer: (reduction: V | R, value: V, key: K, iter: this) => R
  4653. ): R;
  4654. /**
  4655. * Reduces the Collection in reverse (from the right side).
  4656. *
  4657. * Note: Similar to this.reverse().reduce(), and provided for parity
  4658. * with `Array#reduceRight`.
  4659. */
  4660. reduceRight<R>(
  4661. reducer: (reduction: R, value: V, key: K, iter: this) => R,
  4662. initialReduction: R,
  4663. context?: unknown
  4664. ): R;
  4665. reduceRight<R>(
  4666. reducer: (reduction: V | R, value: V, key: K, iter: this) => R
  4667. ): R;
  4668. /**
  4669. * True if `predicate` returns true for all entries in the Collection.
  4670. */
  4671. every(
  4672. predicate: (value: V, key: K, iter: this) => boolean,
  4673. context?: unknown
  4674. ): boolean;
  4675. /**
  4676. * True if `predicate` returns true for any entry in the Collection.
  4677. */
  4678. some(
  4679. predicate: (value: V, key: K, iter: this) => boolean,
  4680. context?: unknown
  4681. ): boolean;
  4682. /**
  4683. * Joins values together as a string, inserting a separator between each.
  4684. * The default separator is `","`.
  4685. */
  4686. join(separator?: string): string;
  4687. /**
  4688. * Returns true if this Collection includes no values.
  4689. *
  4690. * For some lazy `Seq`, `isEmpty` might need to iterate to determine
  4691. * emptiness. At most one iteration will occur.
  4692. */
  4693. isEmpty(): boolean;
  4694. /**
  4695. * Returns the size of this Collection.
  4696. *
  4697. * Regardless of if this Collection can describe its size lazily (some Seqs
  4698. * cannot), this method will always return the correct size. E.g. it
  4699. * evaluates a lazy `Seq` if necessary.
  4700. *
  4701. * If `predicate` is provided, then this returns the count of entries in the
  4702. * Collection for which the `predicate` returns true.
  4703. */
  4704. count(): number;
  4705. count(
  4706. predicate: (value: V, key: K, iter: this) => boolean,
  4707. context?: unknown
  4708. ): number;
  4709. /**
  4710. * Returns a `Seq.Keyed` of counts, grouped by the return value of
  4711. * the `grouper` function.
  4712. *
  4713. * Note: This is not a lazy operation.
  4714. */
  4715. countBy<G>(
  4716. grouper: (value: V, key: K, iter: this) => G,
  4717. context?: unknown
  4718. ): Map<G, number>;
  4719. // Search for value
  4720. /**
  4721. * Returns the first value for which the `predicate` returns true.
  4722. */
  4723. find(
  4724. predicate: (value: V, key: K, iter: this) => boolean,
  4725. context?: unknown,
  4726. notSetValue?: V
  4727. ): V | undefined;
  4728. /**
  4729. * Returns the last value for which the `predicate` returns true.
  4730. *
  4731. * Note: `predicate` will be called for each entry in reverse.
  4732. */
  4733. findLast(
  4734. predicate: (value: V, key: K, iter: this) => boolean,
  4735. context?: unknown,
  4736. notSetValue?: V
  4737. ): V | undefined;
  4738. /**
  4739. * Returns the first [key, value] entry for which the `predicate` returns true.
  4740. */
  4741. findEntry(
  4742. predicate: (value: V, key: K, iter: this) => boolean,
  4743. context?: unknown,
  4744. notSetValue?: V
  4745. ): [K, V] | undefined;
  4746. /**
  4747. * Returns the last [key, value] entry for which the `predicate`
  4748. * returns true.
  4749. *
  4750. * Note: `predicate` will be called for each entry in reverse.
  4751. */
  4752. findLastEntry(
  4753. predicate: (value: V, key: K, iter: this) => boolean,
  4754. context?: unknown,
  4755. notSetValue?: V
  4756. ): [K, V] | undefined;
  4757. /**
  4758. * Returns the key for which the `predicate` returns true.
  4759. */
  4760. findKey(
  4761. predicate: (value: V, key: K, iter: this) => boolean,
  4762. context?: unknown
  4763. ): K | undefined;
  4764. /**
  4765. * Returns the last key for which the `predicate` returns true.
  4766. *
  4767. * Note: `predicate` will be called for each entry in reverse.
  4768. */
  4769. findLastKey(
  4770. predicate: (value: V, key: K, iter: this) => boolean,
  4771. context?: unknown
  4772. ): K | undefined;
  4773. /**
  4774. * Returns the key associated with the search value, or undefined.
  4775. */
  4776. keyOf(searchValue: V): K | undefined;
  4777. /**
  4778. * Returns the last key associated with the search value, or undefined.
  4779. */
  4780. lastKeyOf(searchValue: V): K | undefined;
  4781. /**
  4782. * Returns the maximum value in this collection. If any values are
  4783. * comparatively equivalent, the first one found will be returned.
  4784. *
  4785. * The `comparator` is used in the same way as `Collection#sort`. If it is not
  4786. * provided, the default comparator is `>`.
  4787. *
  4788. * When two values are considered equivalent, the first encountered will be
  4789. * returned. Otherwise, `max` will operate independent of the order of input
  4790. * as long as the comparator is commutative. The default comparator `>` is
  4791. * commutative *only* when types do not differ.
  4792. *
  4793. * If `comparator` returns 0 and either value is NaN, undefined, or null,
  4794. * that value will be returned.
  4795. */
  4796. max(comparator?: Comparator<V>): V | undefined;
  4797. /**
  4798. * Like `max`, but also accepts a `comparatorValueMapper` which allows for
  4799. * comparing by more sophisticated means:
  4800. *
  4801. * <!-- runkit:activate -->
  4802. * ```js
  4803. * const { List, } = require('immutable');
  4804. * const l = List([
  4805. * { name: 'Bob', avgHit: 1 },
  4806. * { name: 'Max', avgHit: 3 },
  4807. * { name: 'Lili', avgHit: 2 } ,
  4808. * ]);
  4809. * l.maxBy(i => i.avgHit); // will output { name: 'Max', avgHit: 3 }
  4810. * ```
  4811. */
  4812. maxBy<C>(
  4813. comparatorValueMapper: (value: V, key: K, iter: this) => C,
  4814. comparator?: Comparator<C>
  4815. ): V | undefined;
  4816. /**
  4817. * Returns the minimum value in this collection. If any values are
  4818. * comparatively equivalent, the first one found will be returned.
  4819. *
  4820. * The `comparator` is used in the same way as `Collection#sort`. If it is not
  4821. * provided, the default comparator is `<`.
  4822. *
  4823. * When two values are considered equivalent, the first encountered will be
  4824. * returned. Otherwise, `min` will operate independent of the order of input
  4825. * as long as the comparator is commutative. The default comparator `<` is
  4826. * commutative *only* when types do not differ.
  4827. *
  4828. * If `comparator` returns 0 and either value is NaN, undefined, or null,
  4829. * that value will be returned.
  4830. */
  4831. min(comparator?: Comparator<V>): V | undefined;
  4832. /**
  4833. * Like `min`, but also accepts a `comparatorValueMapper` which allows for
  4834. * comparing by more sophisticated means:
  4835. *
  4836. * <!-- runkit:activate -->
  4837. * ```js
  4838. * const { List, } = require('immutable');
  4839. * const l = List([
  4840. * { name: 'Bob', avgHit: 1 },
  4841. * { name: 'Max', avgHit: 3 },
  4842. * { name: 'Lili', avgHit: 2 } ,
  4843. * ]);
  4844. * l.minBy(i => i.avgHit); // will output { name: 'Bob', avgHit: 1 }
  4845. * ```
  4846. */
  4847. minBy<C>(
  4848. comparatorValueMapper: (value: V, key: K, iter: this) => C,
  4849. comparator?: Comparator<C>
  4850. ): V | undefined;
  4851. // Comparison
  4852. /**
  4853. * True if `iter` includes every value in this Collection.
  4854. */
  4855. isSubset(iter: Iterable<V>): boolean;
  4856. /**
  4857. * True if this Collection includes every value in `iter`.
  4858. */
  4859. isSuperset(iter: Iterable<V>): boolean;
  4860. }
  4861. /**
  4862. * The interface to fulfill to qualify as a Value Object.
  4863. */
  4864. interface ValueObject {
  4865. /**
  4866. * True if this and the other Collection have value equality, as defined
  4867. * by `Immutable.is()`.
  4868. *
  4869. * Note: This is equivalent to `Immutable.is(this, other)`, but provided to
  4870. * allow for chained expressions.
  4871. */
  4872. equals(other: unknown): boolean;
  4873. /**
  4874. * Computes and returns the hashed identity for this Collection.
  4875. *
  4876. * The `hashCode` of a Collection is used to determine potential equality,
  4877. * and is used when adding this to a `Set` or as a key in a `Map`, enabling
  4878. * lookup via a different instance.
  4879. *
  4880. * <!-- runkit:activate -->
  4881. * ```js
  4882. * const { List, Set } = require('immutable');
  4883. * const a = List([ 1, 2, 3 ]);
  4884. * const b = List([ 1, 2, 3 ]);
  4885. * assert.notStrictEqual(a, b); // different instances
  4886. * const set = Set([ a ]);
  4887. * assert.equal(set.has(b), true);
  4888. * ```
  4889. *
  4890. * Note: hashCode() MUST return a Uint32 number. The easiest way to
  4891. * guarantee this is to return `myHash | 0` from a custom implementation.
  4892. *
  4893. * If two values have the same `hashCode`, they are [not guaranteed
  4894. * to be equal][Hash Collision]. If two values have different `hashCode`s,
  4895. * they must not be equal.
  4896. *
  4897. * Note: `hashCode()` is not guaranteed to always be called before
  4898. * `equals()`. Most but not all Immutable.js collections use hash codes to
  4899. * organize their internal data structures, while all Immutable.js
  4900. * collections use equality during lookups.
  4901. *
  4902. * [Hash Collision]: https://en.wikipedia.org/wiki/Collision_(computer_science)
  4903. */
  4904. hashCode(): number;
  4905. }
  4906. /**
  4907. * Deeply converts plain JS objects and arrays to Immutable Maps and Lists.
  4908. *
  4909. * `fromJS` will convert Arrays and [array-like objects][2] to a List, and
  4910. * plain objects (without a custom prototype) to a Map. [Iterable objects][3]
  4911. * may be converted to List, Map, or Set.
  4912. *
  4913. * If a `reviver` is optionally provided, it will be called with every
  4914. * collection as a Seq (beginning with the most nested collections
  4915. * and proceeding to the top-level collection itself), along with the key
  4916. * referring to each collection and the parent JS object provided as `this`.
  4917. * For the top level, object, the key will be `""`. This `reviver` is expected
  4918. * to return a new Immutable Collection, allowing for custom conversions from
  4919. * deep JS objects. Finally, a `path` is provided which is the sequence of
  4920. * keys to this value from the starting value.
  4921. *
  4922. * `reviver` acts similarly to the [same parameter in `JSON.parse`][1].
  4923. *
  4924. * If `reviver` is not provided, the default behavior will convert Objects
  4925. * into Maps and Arrays into Lists like so:
  4926. *
  4927. * <!-- runkit:activate -->
  4928. * ```js
  4929. * const { fromJS, isKeyed } = require('immutable')
  4930. * function (key, value) {
  4931. * return isKeyed(value) ? value.toMap() : value.toList()
  4932. * }
  4933. * ```
  4934. *
  4935. * Accordingly, this example converts native JS data to OrderedMap and List:
  4936. *
  4937. * <!-- runkit:activate -->
  4938. * ```js
  4939. * const { fromJS, isKeyed } = require('immutable')
  4940. * fromJS({ a: {b: [10, 20, 30]}, c: 40}, function (key, value, path) {
  4941. * console.log(key, value, path)
  4942. * return isKeyed(value) ? value.toOrderedMap() : value.toList()
  4943. * })
  4944. *
  4945. * > "b", [ 10, 20, 30 ], [ "a", "b" ]
  4946. * > "a", {b: [10, 20, 30]}, [ "a" ]
  4947. * > "", {a: {b: [10, 20, 30]}, c: 40}, []
  4948. * ```
  4949. *
  4950. * Keep in mind, when using JS objects to construct Immutable Maps, that
  4951. * JavaScript Object properties are always strings, even if written in a
  4952. * quote-less shorthand, while Immutable Maps accept keys of any type.
  4953. *
  4954. * <!-- runkit:activate -->
  4955. * ```js
  4956. * const { Map } = require('immutable')
  4957. * let obj = { 1: "one" };
  4958. * Object.keys(obj); // [ "1" ]
  4959. * assert.equal(obj["1"], obj[1]); // "one" === "one"
  4960. *
  4961. * let map = Map(obj);
  4962. * assert.notEqual(map.get("1"), map.get(1)); // "one" !== undefined
  4963. * ```
  4964. *
  4965. * Property access for JavaScript Objects first converts the key to a string,
  4966. * but since Immutable Map keys can be of any type the argument to `get()` is
  4967. * not altered.
  4968. *
  4969. * [1]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/parse#Example.3A_Using_the_reviver_parameter
  4970. * "Using the reviver parameter"
  4971. * [2]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Indexed_collections#working_with_array-like_objects
  4972. * "Working with array-like objects"
  4973. * [3]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Iteration_protocols#the_iterable_protocol
  4974. * "The iterable protocol"
  4975. */
  4976. function fromJS<JSValue>(
  4977. jsValue: JSValue,
  4978. reviver?: undefined
  4979. ): FromJS<JSValue>;
  4980. function fromJS(
  4981. jsValue: unknown,
  4982. reviver?: (
  4983. key: string | number,
  4984. sequence: Collection.Keyed<string, unknown> | Collection.Indexed<unknown>,
  4985. path?: Array<string | number>
  4986. ) => unknown
  4987. ): Collection<unknown, unknown>;
  4988. type FromJS<JSValue> = JSValue extends FromJSNoTransform
  4989. ? JSValue
  4990. : JSValue extends Array<any>
  4991. ? FromJSArray<JSValue>
  4992. : JSValue extends {}
  4993. ? FromJSObject<JSValue>
  4994. : any;
  4995. type FromJSNoTransform =
  4996. | Collection<any, any>
  4997. | number
  4998. | string
  4999. | null
  5000. | undefined;
  5001. type FromJSArray<JSValue> = JSValue extends Array<infer T>
  5002. ? List<FromJS<T>>
  5003. : never;
  5004. type FromJSObject<JSValue> = JSValue extends {}
  5005. ? Map<keyof JSValue, FromJS<JSValue[keyof JSValue]>>
  5006. : never;
  5007. /**
  5008. * Value equality check with semantics similar to `Object.is`, but treats
  5009. * Immutable `Collection`s as values, equal if the second `Collection` includes
  5010. * equivalent values.
  5011. *
  5012. * It's used throughout Immutable when checking for equality, including `Map`
  5013. * key equality and `Set` membership.
  5014. *
  5015. * <!-- runkit:activate -->
  5016. * ```js
  5017. * const { Map, is } = require('immutable')
  5018. * const map1 = Map({ a: 1, b: 1, c: 1 })
  5019. * const map2 = Map({ a: 1, b: 1, c: 1 })
  5020. * assert.equal(map1 !== map2, true)
  5021. * assert.equal(Object.is(map1, map2), false)
  5022. * assert.equal(is(map1, map2), true)
  5023. * ```
  5024. *
  5025. * `is()` compares primitive types like strings and numbers, Immutable.js
  5026. * collections like `Map` and `List`, but also any custom object which
  5027. * implements `ValueObject` by providing `equals()` and `hashCode()` methods.
  5028. *
  5029. * Note: Unlike `Object.is`, `Immutable.is` assumes `0` and `-0` are the same
  5030. * value, matching the behavior of ES6 Map key equality.
  5031. */
  5032. function is(first: unknown, second: unknown): boolean;
  5033. /**
  5034. * The `hash()` function is an important part of how Immutable determines if
  5035. * two values are equivalent and is used to determine how to store those
  5036. * values. Provided with any value, `hash()` will return a 31-bit integer.
  5037. *
  5038. * When designing Objects which may be equal, it's important that when a
  5039. * `.equals()` method returns true, that both values `.hashCode()` method
  5040. * return the same value. `hash()` may be used to produce those values.
  5041. *
  5042. * For non-Immutable Objects that do not provide a `.hashCode()` functions
  5043. * (including plain Objects, plain Arrays, Date objects, etc), a unique hash
  5044. * value will be created for each *instance*. That is, the create hash
  5045. * represents referential equality, and not value equality for Objects. This
  5046. * ensures that if that Object is mutated over time that its hash code will
  5047. * remain consistent, allowing Objects to be used as keys and values in
  5048. * Immutable.js collections.
  5049. *
  5050. * Note that `hash()` attempts to balance between speed and avoiding
  5051. * collisions, however it makes no attempt to produce secure hashes.
  5052. *
  5053. * *New in Version 4.0*
  5054. */
  5055. function hash(value: unknown): number;
  5056. /**
  5057. * True if `maybeImmutable` is an Immutable Collection or Record.
  5058. *
  5059. * Note: Still returns true even if the collections is within a `withMutations()`.
  5060. *
  5061. * <!-- runkit:activate -->
  5062. * ```js
  5063. * const { isImmutable, Map, List, Stack } = require('immutable');
  5064. * isImmutable([]); // false
  5065. * isImmutable({}); // false
  5066. * isImmutable(Map()); // true
  5067. * isImmutable(List()); // true
  5068. * isImmutable(Stack()); // true
  5069. * isImmutable(Map().asMutable()); // true
  5070. * ```
  5071. */
  5072. function isImmutable(
  5073. maybeImmutable: unknown
  5074. ): maybeImmutable is Collection<unknown, unknown>;
  5075. /**
  5076. * True if `maybeCollection` is a Collection, or any of its subclasses.
  5077. *
  5078. * <!-- runkit:activate -->
  5079. * ```js
  5080. * const { isCollection, Map, List, Stack } = require('immutable');
  5081. * isCollection([]); // false
  5082. * isCollection({}); // false
  5083. * isCollection(Map()); // true
  5084. * isCollection(List()); // true
  5085. * isCollection(Stack()); // true
  5086. * ```
  5087. */
  5088. function isCollection(
  5089. maybeCollection: unknown
  5090. ): maybeCollection is Collection<unknown, unknown>;
  5091. /**
  5092. * True if `maybeKeyed` is a Collection.Keyed, or any of its subclasses.
  5093. *
  5094. * <!-- runkit:activate -->
  5095. * ```js
  5096. * const { isKeyed, Map, List, Stack } = require('immutable');
  5097. * isKeyed([]); // false
  5098. * isKeyed({}); // false
  5099. * isKeyed(Map()); // true
  5100. * isKeyed(List()); // false
  5101. * isKeyed(Stack()); // false
  5102. * ```
  5103. */
  5104. function isKeyed(
  5105. maybeKeyed: unknown
  5106. ): maybeKeyed is Collection.Keyed<unknown, unknown>;
  5107. /**
  5108. * True if `maybeIndexed` is a Collection.Indexed, or any of its subclasses.
  5109. *
  5110. * <!-- runkit:activate -->
  5111. * ```js
  5112. * const { isIndexed, Map, List, Stack, Set } = require('immutable');
  5113. * isIndexed([]); // false
  5114. * isIndexed({}); // false
  5115. * isIndexed(Map()); // false
  5116. * isIndexed(List()); // true
  5117. * isIndexed(Stack()); // true
  5118. * isIndexed(Set()); // false
  5119. * ```
  5120. */
  5121. function isIndexed(
  5122. maybeIndexed: unknown
  5123. ): maybeIndexed is Collection.Indexed<unknown>;
  5124. /**
  5125. * True if `maybeAssociative` is either a Keyed or Indexed Collection.
  5126. *
  5127. * <!-- runkit:activate -->
  5128. * ```js
  5129. * const { isAssociative, Map, List, Stack, Set } = require('immutable');
  5130. * isAssociative([]); // false
  5131. * isAssociative({}); // false
  5132. * isAssociative(Map()); // true
  5133. * isAssociative(List()); // true
  5134. * isAssociative(Stack()); // true
  5135. * isAssociative(Set()); // false
  5136. * ```
  5137. */
  5138. function isAssociative(
  5139. maybeAssociative: unknown
  5140. ): maybeAssociative is
  5141. | Collection.Keyed<unknown, unknown>
  5142. | Collection.Indexed<unknown>;
  5143. /**
  5144. * True if `maybeOrdered` is a Collection where iteration order is well
  5145. * defined. True for Collection.Indexed as well as OrderedMap and OrderedSet.
  5146. *
  5147. * <!-- runkit:activate -->
  5148. * ```js
  5149. * const { isOrdered, Map, OrderedMap, List, Set } = require('immutable');
  5150. * isOrdered([]); // false
  5151. * isOrdered({}); // false
  5152. * isOrdered(Map()); // false
  5153. * isOrdered(OrderedMap()); // true
  5154. * isOrdered(List()); // true
  5155. * isOrdered(Set()); // false
  5156. * ```
  5157. */
  5158. function isOrdered(maybeOrdered: unknown): boolean;
  5159. /**
  5160. * True if `maybeValue` is a JavaScript Object which has *both* `equals()`
  5161. * and `hashCode()` methods.
  5162. *
  5163. * Any two instances of *value objects* can be compared for value equality with
  5164. * `Immutable.is()` and can be used as keys in a `Map` or members in a `Set`.
  5165. */
  5166. function isValueObject(maybeValue: unknown): maybeValue is ValueObject;
  5167. /**
  5168. * True if `maybeSeq` is a Seq.
  5169. */
  5170. function isSeq(
  5171. maybeSeq: unknown
  5172. ): maybeSeq is
  5173. | Seq.Indexed<unknown>
  5174. | Seq.Keyed<unknown, unknown>
  5175. | Seq.Set<unknown>;
  5176. /**
  5177. * True if `maybeList` is a List.
  5178. */
  5179. function isList(maybeList: unknown): maybeList is List<unknown>;
  5180. /**
  5181. * True if `maybeMap` is a Map.
  5182. *
  5183. * Also true for OrderedMaps.
  5184. */
  5185. function isMap(maybeMap: unknown): maybeMap is Map<unknown, unknown>;
  5186. /**
  5187. * True if `maybeOrderedMap` is an OrderedMap.
  5188. */
  5189. function isOrderedMap(
  5190. maybeOrderedMap: unknown
  5191. ): maybeOrderedMap is OrderedMap<unknown, unknown>;
  5192. /**
  5193. * True if `maybeStack` is a Stack.
  5194. */
  5195. function isStack(maybeStack: unknown): maybeStack is Stack<unknown>;
  5196. /**
  5197. * True if `maybeSet` is a Set.
  5198. *
  5199. * Also true for OrderedSets.
  5200. */
  5201. function isSet(maybeSet: unknown): maybeSet is Set<unknown>;
  5202. /**
  5203. * True if `maybeOrderedSet` is an OrderedSet.
  5204. */
  5205. function isOrderedSet(
  5206. maybeOrderedSet: unknown
  5207. ): maybeOrderedSet is OrderedSet<unknown>;
  5208. /**
  5209. * True if `maybeRecord` is a Record.
  5210. */
  5211. function isRecord(maybeRecord: unknown): maybeRecord is Record<{}>;
  5212. /**
  5213. * Returns the value within the provided collection associated with the
  5214. * provided key, or notSetValue if the key is not defined in the collection.
  5215. *
  5216. * A functional alternative to `collection.get(key)` which will also work on
  5217. * plain Objects and Arrays as an alternative for `collection[key]`.
  5218. *
  5219. * <!-- runkit:activate -->
  5220. * ```js
  5221. * const { get } = require('immutable')
  5222. * get([ 'dog', 'frog', 'cat' ], 2) // 'frog'
  5223. * get({ x: 123, y: 456 }, 'x') // 123
  5224. * get({ x: 123, y: 456 }, 'z', 'ifNotSet') // 'ifNotSet'
  5225. * ```
  5226. */
  5227. function get<K, V>(collection: Collection<K, V>, key: K): V | undefined;
  5228. function get<K, V, NSV>(
  5229. collection: Collection<K, V>,
  5230. key: K,
  5231. notSetValue: NSV
  5232. ): V | NSV;
  5233. function get<TProps extends object, K extends keyof TProps>(
  5234. record: Record<TProps>,
  5235. key: K,
  5236. notSetValue: unknown
  5237. ): TProps[K];
  5238. function get<V>(collection: Array<V>, key: number): V | undefined;
  5239. function get<V, NSV>(
  5240. collection: Array<V>,
  5241. key: number,
  5242. notSetValue: NSV
  5243. ): V | NSV;
  5244. function get<C extends object, K extends keyof C>(
  5245. object: C,
  5246. key: K,
  5247. notSetValue: unknown
  5248. ): C[K];
  5249. function get<V>(collection: { [key: string]: V }, key: string): V | undefined;
  5250. function get<V, NSV>(
  5251. collection: { [key: string]: V },
  5252. key: string,
  5253. notSetValue: NSV
  5254. ): V | NSV;
  5255. /**
  5256. * Returns true if the key is defined in the provided collection.
  5257. *
  5258. * A functional alternative to `collection.has(key)` which will also work with
  5259. * plain Objects and Arrays as an alternative for
  5260. * `collection.hasOwnProperty(key)`.
  5261. *
  5262. * <!-- runkit:activate -->
  5263. * ```js
  5264. * const { has } = require('immutable')
  5265. * has([ 'dog', 'frog', 'cat' ], 2) // true
  5266. * has([ 'dog', 'frog', 'cat' ], 5) // false
  5267. * has({ x: 123, y: 456 }, 'x') // true
  5268. * has({ x: 123, y: 456 }, 'z') // false
  5269. * ```
  5270. */
  5271. function has(collection: object, key: unknown): boolean;
  5272. /**
  5273. * Returns a copy of the collection with the value at key removed.
  5274. *
  5275. * A functional alternative to `collection.remove(key)` which will also work
  5276. * with plain Objects and Arrays as an alternative for
  5277. * `delete collectionCopy[key]`.
  5278. *
  5279. * <!-- runkit:activate -->
  5280. * ```js
  5281. * const { remove } = require('immutable')
  5282. * const originalArray = [ 'dog', 'frog', 'cat' ]
  5283. * remove(originalArray, 1) // [ 'dog', 'cat' ]
  5284. * console.log(originalArray) // [ 'dog', 'frog', 'cat' ]
  5285. * const originalObject = { x: 123, y: 456 }
  5286. * remove(originalObject, 'x') // { y: 456 }
  5287. * console.log(originalObject) // { x: 123, y: 456 }
  5288. * ```
  5289. */
  5290. function remove<K, C extends Collection<K, unknown>>(
  5291. collection: C,
  5292. key: K
  5293. ): C;
  5294. function remove<
  5295. TProps extends object,
  5296. C extends Record<TProps>,
  5297. K extends keyof TProps
  5298. >(collection: C, key: K): C;
  5299. function remove<C extends Array<unknown>>(collection: C, key: number): C;
  5300. function remove<C, K extends keyof C>(collection: C, key: K): C;
  5301. function remove<C extends { [key: string]: unknown }, K extends keyof C>(
  5302. collection: C,
  5303. key: K
  5304. ): C;
  5305. /**
  5306. * Returns a copy of the collection with the value at key set to the provided
  5307. * value.
  5308. *
  5309. * A functional alternative to `collection.set(key, value)` which will also
  5310. * work with plain Objects and Arrays as an alternative for
  5311. * `collectionCopy[key] = value`.
  5312. *
  5313. * <!-- runkit:activate -->
  5314. * ```js
  5315. * const { set } = require('immutable')
  5316. * const originalArray = [ 'dog', 'frog', 'cat' ]
  5317. * set(originalArray, 1, 'cow') // [ 'dog', 'cow', 'cat' ]
  5318. * console.log(originalArray) // [ 'dog', 'frog', 'cat' ]
  5319. * const originalObject = { x: 123, y: 456 }
  5320. * set(originalObject, 'x', 789) // { x: 789, y: 456 }
  5321. * console.log(originalObject) // { x: 123, y: 456 }
  5322. * ```
  5323. */
  5324. function set<K, V, C extends Collection<K, V>>(
  5325. collection: C,
  5326. key: K,
  5327. value: V
  5328. ): C;
  5329. function set<
  5330. TProps extends object,
  5331. C extends Record<TProps>,
  5332. K extends keyof TProps
  5333. >(record: C, key: K, value: TProps[K]): C;
  5334. function set<V, C extends Array<V>>(collection: C, key: number, value: V): C;
  5335. function set<C, K extends keyof C>(object: C, key: K, value: C[K]): C;
  5336. function set<V, C extends { [key: string]: V }>(
  5337. collection: C,
  5338. key: string,
  5339. value: V
  5340. ): C;
  5341. /**
  5342. * Returns a copy of the collection with the value at key set to the result of
  5343. * providing the existing value to the updating function.
  5344. *
  5345. * A functional alternative to `collection.update(key, fn)` which will also
  5346. * work with plain Objects and Arrays as an alternative for
  5347. * `collectionCopy[key] = fn(collection[key])`.
  5348. *
  5349. * <!-- runkit:activate -->
  5350. * ```js
  5351. * const { update } = require('immutable')
  5352. * const originalArray = [ 'dog', 'frog', 'cat' ]
  5353. * update(originalArray, 1, val => val.toUpperCase()) // [ 'dog', 'FROG', 'cat' ]
  5354. * console.log(originalArray) // [ 'dog', 'frog', 'cat' ]
  5355. * const originalObject = { x: 123, y: 456 }
  5356. * update(originalObject, 'x', val => val * 6) // { x: 738, y: 456 }
  5357. * console.log(originalObject) // { x: 123, y: 456 }
  5358. * ```
  5359. */
  5360. function update<K, V, C extends Collection<K, V>>(
  5361. collection: C,
  5362. key: K,
  5363. updater: (value: V | undefined) => V | undefined
  5364. ): C;
  5365. function update<K, V, C extends Collection<K, V>, NSV>(
  5366. collection: C,
  5367. key: K,
  5368. notSetValue: NSV,
  5369. updater: (value: V | NSV) => V
  5370. ): C;
  5371. function update<
  5372. TProps extends object,
  5373. C extends Record<TProps>,
  5374. K extends keyof TProps
  5375. >(record: C, key: K, updater: (value: TProps[K]) => TProps[K]): C;
  5376. function update<
  5377. TProps extends object,
  5378. C extends Record<TProps>,
  5379. K extends keyof TProps,
  5380. NSV
  5381. >(
  5382. record: C,
  5383. key: K,
  5384. notSetValue: NSV,
  5385. updater: (value: TProps[K] | NSV) => TProps[K]
  5386. ): C;
  5387. function update<V>(
  5388. collection: Array<V>,
  5389. key: number,
  5390. updater: (value: V | undefined) => V | undefined
  5391. ): Array<V>;
  5392. function update<V, NSV>(
  5393. collection: Array<V>,
  5394. key: number,
  5395. notSetValue: NSV,
  5396. updater: (value: V | NSV) => V
  5397. ): Array<V>;
  5398. function update<C, K extends keyof C>(
  5399. object: C,
  5400. key: K,
  5401. updater: (value: C[K]) => C[K]
  5402. ): C;
  5403. function update<C, K extends keyof C, NSV>(
  5404. object: C,
  5405. key: K,
  5406. notSetValue: NSV,
  5407. updater: (value: C[K] | NSV) => C[K]
  5408. ): C;
  5409. function update<V, C extends { [key: string]: V }, K extends keyof C>(
  5410. collection: C,
  5411. key: K,
  5412. updater: (value: V) => V
  5413. ): { [key: string]: V };
  5414. function update<V, C extends { [key: string]: V }, K extends keyof C, NSV>(
  5415. collection: C,
  5416. key: K,
  5417. notSetValue: NSV,
  5418. updater: (value: V | NSV) => V
  5419. ): { [key: string]: V };
  5420. /**
  5421. * Returns the value at the provided key path starting at the provided
  5422. * collection, or notSetValue if the key path is not defined.
  5423. *
  5424. * A functional alternative to `collection.getIn(keypath)` which will also
  5425. * work with plain Objects and Arrays.
  5426. *
  5427. * <!-- runkit:activate -->
  5428. * ```js
  5429. * const { getIn } = require('immutable')
  5430. * getIn({ x: { y: { z: 123 }}}, ['x', 'y', 'z']) // 123
  5431. * getIn({ x: { y: { z: 123 }}}, ['x', 'q', 'p'], 'ifNotSet') // 'ifNotSet'
  5432. * ```
  5433. */
  5434. function getIn(
  5435. collection: unknown,
  5436. keyPath: Iterable<unknown>,
  5437. notSetValue?: unknown
  5438. ): unknown;
  5439. /**
  5440. * Returns true if the key path is defined in the provided collection.
  5441. *
  5442. * A functional alternative to `collection.hasIn(keypath)` which will also
  5443. * work with plain Objects and Arrays.
  5444. *
  5445. * <!-- runkit:activate -->
  5446. * ```js
  5447. * const { hasIn } = require('immutable')
  5448. * hasIn({ x: { y: { z: 123 }}}, ['x', 'y', 'z']) // true
  5449. * hasIn({ x: { y: { z: 123 }}}, ['x', 'q', 'p']) // false
  5450. * ```
  5451. */
  5452. function hasIn(collection: unknown, keyPath: Iterable<unknown>): boolean;
  5453. /**
  5454. * Returns a copy of the collection with the value at the key path removed.
  5455. *
  5456. * A functional alternative to `collection.removeIn(keypath)` which will also
  5457. * work with plain Objects and Arrays.
  5458. *
  5459. * <!-- runkit:activate -->
  5460. * ```js
  5461. * const { removeIn } = require('immutable')
  5462. * const original = { x: { y: { z: 123 }}}
  5463. * removeIn(original, ['x', 'y', 'z']) // { x: { y: {}}}
  5464. * console.log(original) // { x: { y: { z: 123 }}}
  5465. * ```
  5466. */
  5467. function removeIn<C>(collection: C, keyPath: Iterable<unknown>): C;
  5468. /**
  5469. * Returns a copy of the collection with the value at the key path set to the
  5470. * provided value.
  5471. *
  5472. * A functional alternative to `collection.setIn(keypath)` which will also
  5473. * work with plain Objects and Arrays.
  5474. *
  5475. * <!-- runkit:activate -->
  5476. * ```js
  5477. * const { setIn } = require('immutable')
  5478. * const original = { x: { y: { z: 123 }}}
  5479. * setIn(original, ['x', 'y', 'z'], 456) // { x: { y: { z: 456 }}}
  5480. * console.log(original) // { x: { y: { z: 123 }}}
  5481. * ```
  5482. */
  5483. function setIn<C>(
  5484. collection: C,
  5485. keyPath: Iterable<unknown>,
  5486. value: unknown
  5487. ): C;
  5488. /**
  5489. * Returns a copy of the collection with the value at key path set to the
  5490. * result of providing the existing value to the updating function.
  5491. *
  5492. * A functional alternative to `collection.updateIn(keypath)` which will also
  5493. * work with plain Objects and Arrays.
  5494. *
  5495. * <!-- runkit:activate -->
  5496. * ```js
  5497. * const { updateIn } = require('immutable')
  5498. * const original = { x: { y: { z: 123 }}}
  5499. * updateIn(original, ['x', 'y', 'z'], val => val * 6) // { x: { y: { z: 738 }}}
  5500. * console.log(original) // { x: { y: { z: 123 }}}
  5501. * ```
  5502. */
  5503. function updateIn<C>(
  5504. collection: C,
  5505. keyPath: Iterable<unknown>,
  5506. updater: (value: unknown) => unknown
  5507. ): C;
  5508. function updateIn<C>(
  5509. collection: C,
  5510. keyPath: Iterable<unknown>,
  5511. notSetValue: unknown,
  5512. updater: (value: unknown) => unknown
  5513. ): C;
  5514. /**
  5515. * Returns a copy of the collection with the remaining collections merged in.
  5516. *
  5517. * A functional alternative to `collection.merge()` which will also work with
  5518. * plain Objects and Arrays.
  5519. *
  5520. * <!-- runkit:activate -->
  5521. * ```js
  5522. * const { merge } = require('immutable')
  5523. * const original = { x: 123, y: 456 }
  5524. * merge(original, { y: 789, z: 'abc' }) // { x: 123, y: 789, z: 'abc' }
  5525. * console.log(original) // { x: 123, y: 456 }
  5526. * ```
  5527. */
  5528. function merge<C>(
  5529. collection: C,
  5530. ...collections: Array<
  5531. | Iterable<unknown>
  5532. | Iterable<[unknown, unknown]>
  5533. | { [key: string]: unknown }
  5534. >
  5535. ): C;
  5536. /**
  5537. * Returns a copy of the collection with the remaining collections merged in,
  5538. * calling the `merger` function whenever an existing value is encountered.
  5539. *
  5540. * A functional alternative to `collection.mergeWith()` which will also work
  5541. * with plain Objects and Arrays.
  5542. *
  5543. * <!-- runkit:activate -->
  5544. * ```js
  5545. * const { mergeWith } = require('immutable')
  5546. * const original = { x: 123, y: 456 }
  5547. * mergeWith(
  5548. * (oldVal, newVal) => oldVal + newVal,
  5549. * original,
  5550. * { y: 789, z: 'abc' }
  5551. * ) // { x: 123, y: 1245, z: 'abc' }
  5552. * console.log(original) // { x: 123, y: 456 }
  5553. * ```
  5554. */
  5555. function mergeWith<C>(
  5556. merger: (oldVal: unknown, newVal: unknown, key: unknown) => unknown,
  5557. collection: C,
  5558. ...collections: Array<
  5559. | Iterable<unknown>
  5560. | Iterable<[unknown, unknown]>
  5561. | { [key: string]: unknown }
  5562. >
  5563. ): C;
  5564. /**
  5565. * Like `merge()`, but when two compatible collections are encountered with
  5566. * the same key, it merges them as well, recursing deeply through the nested
  5567. * data. Two collections are considered to be compatible (and thus will be
  5568. * merged together) if they both fall into one of three categories: keyed
  5569. * (e.g., `Map`s, `Record`s, and objects), indexed (e.g., `List`s and
  5570. * arrays), or set-like (e.g., `Set`s). If they fall into separate
  5571. * categories, `mergeDeep` will replace the existing collection with the
  5572. * collection being merged in. This behavior can be customized by using
  5573. * `mergeDeepWith()`.
  5574. *
  5575. * Note: Indexed and set-like collections are merged using
  5576. * `concat()`/`union()` and therefore do not recurse.
  5577. *
  5578. * A functional alternative to `collection.mergeDeep()` which will also work
  5579. * with plain Objects and Arrays.
  5580. *
  5581. * <!-- runkit:activate -->
  5582. * ```js
  5583. * const { mergeDeep } = require('immutable')
  5584. * const original = { x: { y: 123 }}
  5585. * mergeDeep(original, { x: { z: 456 }}) // { x: { y: 123, z: 456 }}
  5586. * console.log(original) // { x: { y: 123 }}
  5587. * ```
  5588. */
  5589. function mergeDeep<C>(
  5590. collection: C,
  5591. ...collections: Array<
  5592. | Iterable<unknown>
  5593. | Iterable<[unknown, unknown]>
  5594. | { [key: string]: unknown }
  5595. >
  5596. ): C;
  5597. /**
  5598. * Like `mergeDeep()`, but when two non-collections or incompatible
  5599. * collections are encountered at the same key, it uses the `merger` function
  5600. * to determine the resulting value. Collections are considered incompatible
  5601. * if they fall into separate categories between keyed, indexed, and set-like.
  5602. *
  5603. * A functional alternative to `collection.mergeDeepWith()` which will also
  5604. * work with plain Objects and Arrays.
  5605. *
  5606. * <!-- runkit:activate -->
  5607. * ```js
  5608. * const { mergeDeepWith } = require('immutable')
  5609. * const original = { x: { y: 123 }}
  5610. * mergeDeepWith(
  5611. * (oldVal, newVal) => oldVal + newVal,
  5612. * original,
  5613. * { x: { y: 456 }}
  5614. * ) // { x: { y: 579 }}
  5615. * console.log(original) // { x: { y: 123 }}
  5616. * ```
  5617. */
  5618. function mergeDeepWith<C>(
  5619. merger: (oldVal: unknown, newVal: unknown, key: unknown) => unknown,
  5620. collection: C,
  5621. ...collections: Array<
  5622. | Iterable<unknown>
  5623. | Iterable<[unknown, unknown]>
  5624. | { [key: string]: unknown }
  5625. >
  5626. ): C;
  5627. }
  5628. /**
  5629. * Defines the main export of the immutable module to be the Immutable namespace
  5630. * This supports many common module import patterns:
  5631. *
  5632. * const Immutable = require("immutable");
  5633. * const { List } = require("immutable");
  5634. * import Immutable from "immutable";
  5635. * import * as Immutable from "immutable";
  5636. * import { List } from "immutable";
  5637. *
  5638. */
  5639. export = Immutable;
  5640. /**
  5641. * A global "Immutable" namespace used by UMD modules which allows the use of
  5642. * the full Immutable API.
  5643. *
  5644. * If using Immutable as an imported module, prefer using:
  5645. *
  5646. * import Immutable from 'immutable'
  5647. *
  5648. */
  5649. export as namespace Immutable;