immutable.d.ts 184 KB

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