immutable.d.ts 163 KB

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