immutable.d.ts 184 KB

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