immutable.d.ts 176 KB

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