immutable.d.ts 163 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126
  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<R>(
  3828. reducer: (reduction: R, value: V, key: K, iter: this) => R,
  3829. initialReduction: R,
  3830. context?: unknown
  3831. ): R;
  3832. reduce<R>(
  3833. reducer: (reduction: V | R, value: V, key: K, iter: this) => R
  3834. ): R;
  3835. /**
  3836. * Reduces the Collection in reverse (from the right side).
  3837. *
  3838. * Note: Similar to this.reverse().reduce(), and provided for parity
  3839. * with `Array#reduceRight`.
  3840. */
  3841. reduceRight<R>(
  3842. reducer: (reduction: R, value: V, key: K, iter: this) => R,
  3843. initialReduction: R,
  3844. context?: unknown
  3845. ): R;
  3846. reduceRight<R>(
  3847. reducer: (reduction: V | R, value: V, key: K, iter: this) => R
  3848. ): R;
  3849. /**
  3850. * True if `predicate` returns true for all entries in the Collection.
  3851. */
  3852. every(
  3853. predicate: (value: V, key: K, iter: this) => boolean,
  3854. context?: unknown
  3855. ): boolean;
  3856. /**
  3857. * True if `predicate` returns true for any entry in the Collection.
  3858. */
  3859. some(
  3860. predicate: (value: V, key: K, iter: this) => boolean,
  3861. context?: unknown
  3862. ): boolean;
  3863. /**
  3864. * Joins values together as a string, inserting a separator between each.
  3865. * The default separator is `","`.
  3866. */
  3867. join(separator?: string): string;
  3868. /**
  3869. * Returns true if this Collection includes no values.
  3870. *
  3871. * For some lazy `Seq`, `isEmpty` might need to iterate to determine
  3872. * emptiness. At most one iteration will occur.
  3873. */
  3874. isEmpty(): boolean;
  3875. /**
  3876. * Returns the size of this Collection.
  3877. *
  3878. * Regardless of if this Collection can describe its size lazily (some Seqs
  3879. * cannot), this method will always return the correct size. E.g. it
  3880. * evaluates a lazy `Seq` if necessary.
  3881. *
  3882. * If `predicate` is provided, then this returns the count of entries in the
  3883. * Collection for which the `predicate` returns true.
  3884. */
  3885. count(): number;
  3886. count(
  3887. predicate: (value: V, key: K, iter: this) => boolean,
  3888. context?: unknown
  3889. ): number;
  3890. /**
  3891. * Returns a `Seq.Keyed` of counts, grouped by the return value of
  3892. * the `grouper` function.
  3893. *
  3894. * Note: This is not a lazy operation.
  3895. */
  3896. countBy<G>(
  3897. grouper: (value: V, key: K, iter: this) => G,
  3898. context?: unknown
  3899. ): Map<G, number>;
  3900. // Search for value
  3901. /**
  3902. * Returns the first value for which the `predicate` returns true.
  3903. */
  3904. find(
  3905. predicate: (value: V, key: K, iter: this) => boolean,
  3906. context?: unknown,
  3907. notSetValue?: V
  3908. ): V | undefined;
  3909. /**
  3910. * Returns the last value for which the `predicate` returns true.
  3911. *
  3912. * Note: `predicate` will be called for each entry in reverse.
  3913. */
  3914. findLast(
  3915. predicate: (value: V, key: K, iter: this) => boolean,
  3916. context?: unknown,
  3917. notSetValue?: V
  3918. ): V | undefined;
  3919. /**
  3920. * Returns the first [key, value] entry for which the `predicate` returns true.
  3921. */
  3922. findEntry(
  3923. predicate: (value: V, key: K, iter: this) => boolean,
  3924. context?: unknown,
  3925. notSetValue?: V
  3926. ): [K, V] | undefined;
  3927. /**
  3928. * Returns the last [key, value] entry for which the `predicate`
  3929. * returns true.
  3930. *
  3931. * Note: `predicate` will be called for each entry in reverse.
  3932. */
  3933. findLastEntry(
  3934. predicate: (value: V, key: K, iter: this) => boolean,
  3935. context?: unknown,
  3936. notSetValue?: V
  3937. ): [K, V] | undefined;
  3938. /**
  3939. * Returns the key for which the `predicate` returns true.
  3940. */
  3941. findKey(
  3942. predicate: (value: V, key: K, iter: this) => boolean,
  3943. context?: unknown
  3944. ): K | undefined;
  3945. /**
  3946. * Returns the last key for which the `predicate` returns true.
  3947. *
  3948. * Note: `predicate` will be called for each entry in reverse.
  3949. */
  3950. findLastKey(
  3951. predicate: (value: V, key: K, iter: this) => boolean,
  3952. context?: unknown
  3953. ): K | undefined;
  3954. /**
  3955. * Returns the key associated with the search value, or undefined.
  3956. */
  3957. keyOf(searchValue: V): K | undefined;
  3958. /**
  3959. * Returns the last key associated with the search value, or undefined.
  3960. */
  3961. lastKeyOf(searchValue: V): K | undefined;
  3962. /**
  3963. * Returns the maximum value in this collection. If any values are
  3964. * comparatively equivalent, the first one found will be returned.
  3965. *
  3966. * The `comparator` is used in the same way as `Collection#sort`. If it is not
  3967. * provided, the default comparator is `>`.
  3968. *
  3969. * When two values are considered equivalent, the first encountered will be
  3970. * returned. Otherwise, `max` will operate independent of the order of input
  3971. * as long as the comparator is commutative. The default comparator `>` is
  3972. * commutative *only* when types do not differ.
  3973. *
  3974. * If `comparator` returns 0 and either value is NaN, undefined, or null,
  3975. * that value will be returned.
  3976. */
  3977. max(comparator?: Comparator<V>): V | undefined;
  3978. /**
  3979. * Like `max`, but also accepts a `comparatorValueMapper` which allows for
  3980. * comparing by more sophisticated means:
  3981. */
  3982. maxBy<C>(
  3983. comparatorValueMapper: (value: V, key: K, iter: this) => C,
  3984. comparator?: Comparator<C>
  3985. ): V | undefined;
  3986. /**
  3987. * Returns the minimum value in this collection. If any values are
  3988. * comparatively equivalent, the first one found will be returned.
  3989. *
  3990. * The `comparator` is used in the same way as `Collection#sort`. If it is not
  3991. * provided, the default comparator is `<`.
  3992. *
  3993. * When two values are considered equivalent, the first encountered will be
  3994. * returned. Otherwise, `min` will operate independent of the order of input
  3995. * as long as the comparator is commutative. The default comparator `<` is
  3996. * commutative *only* when types do not differ.
  3997. *
  3998. * If `comparator` returns 0 and either value is NaN, undefined, or null,
  3999. * that value will be returned.
  4000. */
  4001. min(comparator?: Comparator<V>): V | undefined;
  4002. /**
  4003. * Like `min`, but also accepts a `comparatorValueMapper` which allows for
  4004. * comparing by more sophisticated means:
  4005. */
  4006. minBy<C>(
  4007. comparatorValueMapper: (value: V, key: K, iter: this) => C,
  4008. comparator?: Comparator<C>
  4009. ): V | undefined;
  4010. // Comparison
  4011. /**
  4012. * True if `iter` includes every value in this Collection.
  4013. */
  4014. isSubset(iter: Iterable<V>): boolean;
  4015. /**
  4016. * True if this Collection includes every value in `iter`.
  4017. */
  4018. isSuperset(iter: Iterable<V>): boolean;
  4019. }
  4020. /**
  4021. * The interface to fulfill to qualify as a Value Object.
  4022. */
  4023. interface ValueObject {
  4024. /**
  4025. * True if this and the other Collection have value equality, as defined
  4026. * by `Immutable.is()`.
  4027. *
  4028. * Note: This is equivalent to `Immutable.is(this, other)`, but provided to
  4029. * allow for chained expressions.
  4030. */
  4031. equals(other: unknown): boolean;
  4032. /**
  4033. * Computes and returns the hashed identity for this Collection.
  4034. *
  4035. * The `hashCode` of a Collection is used to determine potential equality,
  4036. * and is used when adding this to a `Set` or as a key in a `Map`, enabling
  4037. * lookup via a different instance.
  4038. *
  4039. * Note: hashCode() MUST return a Uint32 number. The easiest way to
  4040. * guarantee this is to return `myHash | 0` from a custom implementation.
  4041. *
  4042. * If two values have the same `hashCode`, they are [not guaranteed
  4043. * to be equal][Hash Collision]. If two values have different `hashCode`s,
  4044. * they must not be equal.
  4045. *
  4046. * Note: `hashCode()` is not guaranteed to always be called before
  4047. * `equals()`. Most but not all Immutable.js collections use hash codes to
  4048. * organize their internal data structures, while all Immutable.js
  4049. * collections use equality during lookups.
  4050. *
  4051. * [Hash Collision]: https://en.wikipedia.org/wiki/Collision_(computer_science)
  4052. */
  4053. hashCode(): number;
  4054. }
  4055. /**
  4056. * Interface representing all oredered collections.
  4057. * This includes `List`, `Stack`, `Map`, `OrderedMap`, `Set`, and `OrderedSet`.
  4058. * return of `isOrdered()` return true in that case.
  4059. */
  4060. interface OrderedCollection<T> {
  4061. /**
  4062. * Shallowly converts this collection to an Array.
  4063. */
  4064. toArray(): Array<T>;
  4065. [Symbol.iterator](): IterableIterator<T>;
  4066. }
  4067. /**
  4068. * Deeply converts plain JS objects and arrays to Immutable Maps and Lists.
  4069. *
  4070. * `fromJS` will convert Arrays and [array-like objects][2] to a List, and
  4071. * plain objects (without a custom prototype) to a Map. [Iterable objects][3]
  4072. * may be converted to List, Map, or Set.
  4073. *
  4074. * If a `reviver` is optionally provided, it will be called with every
  4075. * collection as a Seq (beginning with the most nested collections
  4076. * and proceeding to the top-level collection itself), along with the key
  4077. * referring to each collection and the parent JS object provided as `this`.
  4078. * For the top level, object, the key will be `""`. This `reviver` is expected
  4079. * to return a new Immutable Collection, allowing for custom conversions from
  4080. * deep JS objects. Finally, a `path` is provided which is the sequence of
  4081. * keys to this value from the starting value.
  4082. *
  4083. * `reviver` acts similarly to the [same parameter in `JSON.parse`][1].
  4084. *
  4085. * If `reviver` is not provided, the default behavior will convert Objects
  4086. * into Maps and Arrays into Lists like so:
  4087. *
  4088. * Accordingly, this example converts native JS data to OrderedMap and List:
  4089. *
  4090. * Keep in mind, when using JS objects to construct Immutable Maps, that
  4091. * JavaScript Object properties are always strings, even if written in a
  4092. * quote-less shorthand, while Immutable Maps accept keys of any type.
  4093. *
  4094. * Property access for JavaScript Objects first converts the key to a string,
  4095. * but since Immutable Map keys can be of any type the argument to `get()` is
  4096. * not altered.
  4097. *
  4098. * [1]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/parse#Example.3A_Using_the_reviver_parameter
  4099. * "Using the reviver parameter"
  4100. * [2]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Indexed_collections#working_with_array-like_objects
  4101. * "Working with array-like objects"
  4102. * [3]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Iteration_protocols#the_iterable_protocol
  4103. * "The iterable protocol"
  4104. */
  4105. function fromJS<JSValue>(
  4106. jsValue: JSValue,
  4107. reviver?: undefined
  4108. ): FromJS<JSValue>;
  4109. function fromJS(
  4110. jsValue: unknown,
  4111. reviver?: (
  4112. key: string | number,
  4113. sequence: Collection.Keyed<string, unknown> | Collection.Indexed<unknown>,
  4114. path?: Array<string | number>
  4115. ) => unknown
  4116. ): Collection<unknown, unknown>;
  4117. type FromJS<JSValue> = JSValue extends FromJSNoTransform
  4118. ? JSValue
  4119. : JSValue extends Array<unknown>
  4120. ? FromJSArray<JSValue>
  4121. : JSValue extends object
  4122. ? FromJSObject<JSValue>
  4123. : unknown;
  4124. type FromJSNoTransform =
  4125. | Collection<unknown, unknown>
  4126. | number
  4127. | string
  4128. | null
  4129. | undefined;
  4130. type FromJSArray<JSValue> =
  4131. JSValue extends Array<infer T> ? List<FromJS<T>> : never;
  4132. type FromJSObject<JSValue> = JSValue extends object
  4133. ? Map<keyof JSValue, FromJS<JSValue[keyof JSValue]>>
  4134. : never;
  4135. /**
  4136. * Value equality check with semantics similar to `Object.is`, but treats
  4137. * Immutable `Collection`s as values, equal if the second `Collection` includes
  4138. * equivalent values.
  4139. *
  4140. * It's used throughout Immutable when checking for equality, including `Map`
  4141. * key equality and `Set` membership.
  4142. *
  4143. * `is()` compares primitive types like strings and numbers, Immutable.js
  4144. * collections like `Map` and `List`, but also any custom object which
  4145. * implements `ValueObject` by providing `equals()` and `hashCode()` methods.
  4146. *
  4147. * Note: Unlike `Object.is`, `Immutable.is` assumes `0` and `-0` are the same
  4148. * value, matching the behavior of ES6 Map key equality.
  4149. */
  4150. function is(first: unknown, second: unknown): boolean;
  4151. /**
  4152. * The `hash()` function is an important part of how Immutable determines if
  4153. * two values are equivalent and is used to determine how to store those
  4154. * values. Provided with any value, `hash()` will return a 31-bit integer.
  4155. *
  4156. * When designing Objects which may be equal, it's important that when a
  4157. * `.equals()` method returns true, that both values `.hashCode()` method
  4158. * return the same value. `hash()` may be used to produce those values.
  4159. *
  4160. * For non-Immutable Objects that do not provide a `.hashCode()` functions
  4161. * (including plain Objects, plain Arrays, Date objects, etc), a unique hash
  4162. * value will be created for each *instance*. That is, the create hash
  4163. * represents referential equality, and not value equality for Objects. This
  4164. * ensures that if that Object is mutated over time that its hash code will
  4165. * remain consistent, allowing Objects to be used as keys and values in
  4166. * Immutable.js collections.
  4167. *
  4168. * Note that `hash()` attempts to balance between speed and avoiding
  4169. * collisions, however it makes no attempt to produce secure hashes.
  4170. *
  4171. * *New in Version 4.0*
  4172. */
  4173. function hash(value: unknown): number;
  4174. /**
  4175. * True if `maybeImmutable` is an Immutable Collection or Record.
  4176. *
  4177. * Note: Still returns true even if the collections is within a `withMutations()`.
  4178. */
  4179. function isImmutable(
  4180. maybeImmutable: unknown
  4181. ): maybeImmutable is Collection<unknown, unknown>;
  4182. /**
  4183. * True if `maybeCollection` is a Collection, or any of its subclasses.
  4184. */
  4185. function isCollection(
  4186. maybeCollection: unknown
  4187. ): maybeCollection is Collection<unknown, unknown>;
  4188. /**
  4189. * True if `maybeKeyed` is a Collection.Keyed, or any of its subclasses.
  4190. */
  4191. function isKeyed(
  4192. maybeKeyed: unknown
  4193. ): maybeKeyed is Collection.Keyed<unknown, unknown>;
  4194. /**
  4195. * True if `maybeIndexed` is a Collection.Indexed, or any of its subclasses.
  4196. */
  4197. function isIndexed(
  4198. maybeIndexed: unknown
  4199. ): maybeIndexed is Collection.Indexed<unknown>;
  4200. /**
  4201. * True if `maybeAssociative` is either a Keyed or Indexed Collection.
  4202. */
  4203. function isAssociative(
  4204. maybeAssociative: unknown
  4205. ): maybeAssociative is
  4206. | Collection.Keyed<unknown, unknown>
  4207. | Collection.Indexed<unknown>;
  4208. /**
  4209. * True if `maybeOrdered` is a Collection where iteration order is well
  4210. * defined. True for Collection.Indexed as well as OrderedMap and OrderedSet.
  4211. */
  4212. function isOrdered<T>(
  4213. maybeOrdered: Iterable<T>
  4214. ): maybeOrdered is OrderedCollection<T>;
  4215. function isOrdered(
  4216. maybeOrdered: unknown
  4217. ): maybeOrdered is OrderedCollection<unknown>;
  4218. /**
  4219. * True if `maybeValue` is a JavaScript Object which has *both* `equals()`
  4220. * and `hashCode()` methods.
  4221. *
  4222. * Any two instances of *value objects* can be compared for value equality with
  4223. * `Immutable.is()` and can be used as keys in a `Map` or members in a `Set`.
  4224. */
  4225. function isValueObject(maybeValue: unknown): maybeValue is ValueObject;
  4226. /**
  4227. * True if `maybeSeq` is a Seq.
  4228. */
  4229. function isSeq(
  4230. maybeSeq: unknown
  4231. ): maybeSeq is
  4232. | Seq.Indexed<unknown>
  4233. | Seq.Keyed<unknown, unknown>
  4234. | Seq.Set<unknown>;
  4235. /**
  4236. * True if `maybeList` is a List.
  4237. */
  4238. function isList(maybeList: unknown): maybeList is List<unknown>;
  4239. /**
  4240. * True if `maybeMap` is a Map.
  4241. *
  4242. * Also true for OrderedMaps.
  4243. */
  4244. function isMap(maybeMap: unknown): maybeMap is Map<unknown, unknown>;
  4245. /**
  4246. * True if `maybeOrderedMap` is an OrderedMap.
  4247. */
  4248. function isOrderedMap(
  4249. maybeOrderedMap: unknown
  4250. ): maybeOrderedMap is OrderedMap<unknown, unknown>;
  4251. /**
  4252. * True if `maybeStack` is a Stack.
  4253. */
  4254. function isStack(maybeStack: unknown): maybeStack is Stack<unknown>;
  4255. /**
  4256. * True if `maybeSet` is a Set.
  4257. *
  4258. * Also true for OrderedSets.
  4259. */
  4260. function isSet(maybeSet: unknown): maybeSet is Set<unknown>;
  4261. /**
  4262. * True if `maybeOrderedSet` is an OrderedSet.
  4263. */
  4264. function isOrderedSet(
  4265. maybeOrderedSet: unknown
  4266. ): maybeOrderedSet is OrderedSet<unknown>;
  4267. /**
  4268. * True if `maybeRecord` is a Record.
  4269. */
  4270. function isRecord(maybeRecord: unknown): maybeRecord is Record<object>;
  4271. /**
  4272. * Returns the value within the provided collection associated with the
  4273. * provided key, or notSetValue if the key is not defined in the collection.
  4274. *
  4275. * A functional alternative to `collection.get(key)` which will also work on
  4276. * plain Objects and Arrays as an alternative for `collection[key]`.
  4277. */
  4278. function get<K, V>(collection: Collection<K, V>, key: K): V | undefined;
  4279. function get<K, V, NSV>(
  4280. collection: Collection<K, V>,
  4281. key: K,
  4282. notSetValue: NSV
  4283. ): V | NSV;
  4284. function get<TProps extends object, K extends keyof TProps>(
  4285. record: Record<TProps>,
  4286. key: K,
  4287. notSetValue: unknown
  4288. ): TProps[K];
  4289. function get<V>(collection: Array<V>, key: number): V | undefined;
  4290. function get<V, NSV>(
  4291. collection: Array<V>,
  4292. key: number,
  4293. notSetValue: NSV
  4294. ): V | NSV;
  4295. function get<C extends object, K extends keyof C>(
  4296. object: C,
  4297. key: K,
  4298. notSetValue: unknown
  4299. ): C[K];
  4300. function get<V>(
  4301. collection: { [key: PropertyKey]: V },
  4302. key: string
  4303. ): V | undefined;
  4304. function get<V, NSV>(
  4305. collection: { [key: PropertyKey]: V },
  4306. key: string,
  4307. notSetValue: NSV
  4308. ): V | NSV;
  4309. /**
  4310. * Returns true if the key is defined in the provided collection.
  4311. *
  4312. * A functional alternative to `collection.has(key)` which will also work with
  4313. * plain Objects and Arrays as an alternative for
  4314. * `collection.hasOwnProperty(key)`.
  4315. */
  4316. function has(collection: object, key: unknown): boolean;
  4317. /**
  4318. * Returns a copy of the collection with the value at key removed.
  4319. *
  4320. * A functional alternative to `collection.remove(key)` which will also work
  4321. * with plain Objects and Arrays as an alternative for
  4322. * `delete collectionCopy[key]`.
  4323. */
  4324. function remove<K, C extends Collection<K, unknown>>(
  4325. collection: C,
  4326. key: K
  4327. ): C;
  4328. function remove<
  4329. TProps extends object,
  4330. C extends Record<TProps>,
  4331. K extends keyof TProps,
  4332. >(collection: C, key: K): C;
  4333. function remove<C extends Array<unknown>>(collection: C, key: number): C;
  4334. function remove<C, K extends keyof C>(collection: C, key: K): C;
  4335. function remove<C extends { [key: string]: unknown }, K extends keyof C>(
  4336. collection: C,
  4337. key: K
  4338. ): C;
  4339. /**
  4340. * Returns a copy of the collection with the value at key set to the provided
  4341. * value.
  4342. *
  4343. * A functional alternative to `collection.set(key, value)` which will also
  4344. * work with plain Objects and Arrays as an alternative for
  4345. * `collectionCopy[key] = value`.
  4346. */
  4347. function set<K, V, C extends Collection<K, V>>(
  4348. collection: C,
  4349. key: K,
  4350. value: V
  4351. ): C;
  4352. function set<
  4353. TProps extends object,
  4354. C extends Record<TProps>,
  4355. K extends keyof TProps,
  4356. >(record: C, key: K, value: TProps[K]): C;
  4357. function set<V, C extends Array<V>>(collection: C, key: number, value: V): C;
  4358. function set<C, K extends keyof C>(object: C, key: K, value: C[K]): C;
  4359. function set<V, C extends { [key: string]: V }>(
  4360. collection: C,
  4361. key: string,
  4362. value: V
  4363. ): C;
  4364. /**
  4365. * Returns a copy of the collection with the value at key set to the result of
  4366. * providing the existing value to the updating function.
  4367. *
  4368. * A functional alternative to `collection.update(key, fn)` which will also
  4369. * work with plain Objects and Arrays as an alternative for
  4370. * `collectionCopy[key] = fn(collection[key])`.
  4371. */
  4372. function update<K, V, C extends Collection<K, V>>(
  4373. collection: C,
  4374. key: K,
  4375. updater: (value: V | undefined) => V | undefined
  4376. ): C;
  4377. function update<K, V, C extends Collection<K, V>, NSV>(
  4378. collection: C,
  4379. key: K,
  4380. notSetValue: NSV,
  4381. updater: (value: V | NSV) => V
  4382. ): C;
  4383. function update<
  4384. TProps extends object,
  4385. C extends Record<TProps>,
  4386. K extends keyof TProps,
  4387. >(record: C, key: K, updater: (value: TProps[K]) => TProps[K]): C;
  4388. function update<
  4389. TProps extends object,
  4390. C extends Record<TProps>,
  4391. K extends keyof TProps,
  4392. NSV,
  4393. >(
  4394. record: C,
  4395. key: K,
  4396. notSetValue: NSV,
  4397. updater: (value: TProps[K] | NSV) => TProps[K]
  4398. ): C;
  4399. function update<V>(
  4400. collection: Array<V>,
  4401. key: number,
  4402. updater: (value: V | undefined) => V | undefined
  4403. ): Array<V>;
  4404. function update<V, NSV>(
  4405. collection: Array<V>,
  4406. key: number,
  4407. notSetValue: NSV,
  4408. updater: (value: V | NSV) => V
  4409. ): Array<V>;
  4410. function update<C, K extends keyof C>(
  4411. object: C,
  4412. key: K,
  4413. updater: (value: C[K]) => C[K]
  4414. ): C;
  4415. function update<C, K extends keyof C, NSV>(
  4416. object: C,
  4417. key: K,
  4418. notSetValue: NSV,
  4419. updater: (value: C[K] | NSV) => C[K]
  4420. ): C;
  4421. function update<V, C extends { [key: string]: V }, K extends keyof C>(
  4422. collection: C,
  4423. key: K,
  4424. updater: (value: V) => V
  4425. ): { [key: string]: V };
  4426. function update<V, C extends { [key: string]: V }, K extends keyof C, NSV>(
  4427. collection: C,
  4428. key: K,
  4429. notSetValue: NSV,
  4430. updater: (value: V | NSV) => V
  4431. ): { [key: string]: V };
  4432. // TODO `<const P extends ...>` can be used after dropping support for TypeScript 4.x
  4433. // reference: https://www.typescriptlang.org/docs/handbook/release-notes/typescript-5-0.html#const-type-parameters
  4434. // after this change, `as const` assertions can be remove from the type tests
  4435. /**
  4436. * Returns the value at the provided key path starting at the provided
  4437. * collection, or notSetValue if the key path is not defined.
  4438. *
  4439. * A functional alternative to `collection.getIn(keypath)` which will also
  4440. * work with plain Objects and Arrays.
  4441. */
  4442. function getIn<C, P extends ReadonlyArray<PropertyKey>>(
  4443. object: C,
  4444. keyPath: [...P]
  4445. ): RetrievePath<C, P>;
  4446. function getIn<C, P extends KeyPath<unknown>>(object: C, keyPath: P): unknown;
  4447. function getIn<C, P extends ReadonlyArray<PropertyKey>, NSV>(
  4448. collection: C,
  4449. keyPath: [...P],
  4450. notSetValue: NSV
  4451. ): RetrievePath<C, P> extends never ? NSV : RetrievePath<C, P>;
  4452. function getIn<C, P extends KeyPath<unknown>, NSV>(
  4453. object: C,
  4454. keyPath: P,
  4455. notSetValue: NSV
  4456. ): unknown;
  4457. /**
  4458. * Returns true if the key path is defined in the provided collection.
  4459. *
  4460. * A functional alternative to `collection.hasIn(keypath)` which will also
  4461. * work with plain Objects and Arrays.
  4462. */
  4463. function hasIn(
  4464. collection: string | boolean | number,
  4465. keyPath: KeyPath<unknown>
  4466. ): never;
  4467. function hasIn<K>(collection: unknown, keyPath: KeyPath<K>): boolean;
  4468. /**
  4469. * Returns a copy of the collection with the value at the key path removed.
  4470. *
  4471. * A functional alternative to `collection.removeIn(keypath)` which will also
  4472. * work with plain Objects and Arrays.
  4473. */
  4474. function removeIn<C>(collection: C, keyPath: Iterable<unknown>): C;
  4475. /**
  4476. * Returns a copy of the collection with the value at the key path set to the
  4477. * provided value.
  4478. *
  4479. * A functional alternative to `collection.setIn(keypath)` which will also
  4480. * work with plain Objects and Arrays.
  4481. */
  4482. function setIn<C>(
  4483. collection: C,
  4484. keyPath: Iterable<unknown>,
  4485. value: unknown
  4486. ): C;
  4487. /**
  4488. * Returns a copy of the collection with the value at key path set to the
  4489. * result of providing the existing value to the updating function.
  4490. *
  4491. * A functional alternative to `collection.updateIn(keypath)` which will also
  4492. * work with plain Objects and Arrays.
  4493. */
  4494. function updateIn<K extends PropertyKey, V, C extends Collection<K, V>>(
  4495. collection: C,
  4496. keyPath: KeyPath<K>,
  4497. updater: (
  4498. value: RetrievePath<C, Array<K>> | undefined
  4499. ) => unknown | undefined
  4500. ): C;
  4501. function updateIn<K extends PropertyKey, V, C extends Collection<K, V>, NSV>(
  4502. collection: C,
  4503. keyPath: KeyPath<K>,
  4504. notSetValue: NSV,
  4505. updater: (value: RetrievePath<C, Array<K>> | NSV) => unknown
  4506. ): C;
  4507. function updateIn<
  4508. TProps extends object,
  4509. C extends Record<TProps>,
  4510. K extends keyof TProps,
  4511. >(
  4512. record: C,
  4513. keyPath: KeyPath<K>,
  4514. updater: (value: RetrievePath<C, Array<K>>) => unknown
  4515. ): C;
  4516. function updateIn<
  4517. TProps extends object,
  4518. C extends Record<TProps>,
  4519. K extends keyof TProps,
  4520. NSV,
  4521. >(
  4522. record: C,
  4523. keyPath: KeyPath<K>,
  4524. notSetValue: NSV,
  4525. updater: (value: RetrievePath<C, Array<K>> | NSV) => unknown
  4526. ): C;
  4527. function updateIn<K extends PropertyKey, V, C extends Array<V>>(
  4528. collection: Array<V>,
  4529. keyPath: KeyPath<string | number>,
  4530. updater: (
  4531. value: RetrievePath<C, Array<K>> | undefined
  4532. ) => unknown | undefined
  4533. ): Array<V>;
  4534. function updateIn<K extends PropertyKey, V, C extends Array<V>, NSV>(
  4535. collection: Array<V>,
  4536. keyPath: KeyPath<K>,
  4537. notSetValue: NSV,
  4538. updater: (value: RetrievePath<C, Array<K>> | NSV) => unknown
  4539. ): Array<V>;
  4540. function updateIn<K extends PropertyKey, C>(
  4541. object: C,
  4542. keyPath: KeyPath<K>,
  4543. updater: (value: RetrievePath<C, Array<K>>) => unknown
  4544. ): C;
  4545. function updateIn<K extends PropertyKey, C, NSV>(
  4546. object: C,
  4547. keyPath: KeyPath<K>,
  4548. notSetValue: NSV,
  4549. updater: (value: RetrievePath<C, Array<K>> | NSV) => unknown
  4550. ): C;
  4551. function updateIn<
  4552. K extends PropertyKey,
  4553. V,
  4554. C extends { [key: PropertyKey]: V },
  4555. >(
  4556. collection: C,
  4557. keyPath: KeyPath<K>,
  4558. updater: (value: RetrievePath<C, Array<K>>) => unknown
  4559. ): { [key: PropertyKey]: V };
  4560. function updateIn<
  4561. K extends PropertyKey,
  4562. V,
  4563. C extends { [key: PropertyKey]: V },
  4564. NSV,
  4565. >(
  4566. collection: C,
  4567. keyPath: KeyPath<K>,
  4568. notSetValue: NSV,
  4569. updater: (value: RetrievePath<C, Array<K>> | NSV) => unknown
  4570. ): { [key: PropertyKey]: V };
  4571. /**
  4572. * Returns a copy of the collection with the remaining collections merged in.
  4573. *
  4574. * A functional alternative to `collection.merge()` which will also work with
  4575. * plain Objects and Arrays.
  4576. */
  4577. function merge<C>(
  4578. collection: C,
  4579. ...collections: Array<
  4580. | Iterable<unknown>
  4581. | Iterable<[unknown, unknown]>
  4582. | { [key: string]: unknown }
  4583. >
  4584. ): C;
  4585. /**
  4586. * Returns a copy of the collection with the remaining collections merged in,
  4587. * calling the `merger` function whenever an existing value is encountered.
  4588. *
  4589. * A functional alternative to `collection.mergeWith()` which will also work
  4590. * with plain Objects and Arrays.
  4591. */
  4592. function mergeWith<C>(
  4593. merger: (oldVal: unknown, newVal: unknown, key: unknown) => unknown,
  4594. collection: C,
  4595. ...collections: Array<
  4596. | Iterable<unknown>
  4597. | Iterable<[unknown, unknown]>
  4598. | { [key: string]: unknown }
  4599. >
  4600. ): C;
  4601. /**
  4602. * Like `merge()`, but when two compatible collections are encountered with
  4603. * the same key, it merges them as well, recursing deeply through the nested
  4604. * data. Two collections are considered to be compatible (and thus will be
  4605. * merged together) if they both fall into one of three categories: keyed
  4606. * (e.g., `Map`s, `Record`s, and objects), indexed (e.g., `List`s and
  4607. * arrays), or set-like (e.g., `Set`s). If they fall into separate
  4608. * categories, `mergeDeep` will replace the existing collection with the
  4609. * collection being merged in. This behavior can be customized by using
  4610. * `mergeDeepWith()`.
  4611. *
  4612. * Note: Indexed and set-like collections are merged using
  4613. * `concat()`/`union()` and therefore do not recurse.
  4614. *
  4615. * A functional alternative to `collection.mergeDeep()` which will also work
  4616. * with plain Objects and Arrays.
  4617. */
  4618. function mergeDeep<C>(
  4619. collection: C,
  4620. ...collections: Array<
  4621. | Iterable<unknown>
  4622. | Iterable<[unknown, unknown]>
  4623. | { [key: string]: unknown }
  4624. >
  4625. ): C;
  4626. /**
  4627. * Like `mergeDeep()`, but when two non-collections or incompatible
  4628. * collections are encountered at the same key, it uses the `merger` function
  4629. * to determine the resulting value. Collections are considered incompatible
  4630. * if they fall into separate categories between keyed, indexed, and set-like.
  4631. *
  4632. * A functional alternative to `collection.mergeDeepWith()` which will also
  4633. * work with plain Objects and Arrays.
  4634. */
  4635. function mergeDeepWith<C>(
  4636. merger: (oldVal: unknown, newVal: unknown, key: unknown) => unknown,
  4637. collection: C,
  4638. ...collections: Array<
  4639. | Iterable<unknown>
  4640. | Iterable<[unknown, unknown]>
  4641. | { [key: string]: unknown }
  4642. >
  4643. ): C;
  4644. }
  4645. /**
  4646. * Defines the main export of the immutable module to be the Immutable namespace
  4647. * This supports many common module import patterns:
  4648. *
  4649. * const Immutable = require("immutable");
  4650. * const { List } = require("immutable");
  4651. * import Immutable from "immutable";
  4652. * import * as Immutable from "immutable";
  4653. * import { List } from "immutable";
  4654. *
  4655. */
  4656. export = Immutable;
  4657. /**
  4658. * A global "Immutable" namespace used by UMD modules which allows the use of
  4659. * the full Immutable API.
  4660. *
  4661. * If using Immutable as an imported module, prefer using:
  4662. *
  4663. * import Immutable from 'immutable'
  4664. *
  4665. */
  4666. export as namespace Immutable;