immutable.d.ts 184 KB

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