immutable.d.ts 170 KB

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