版博士V2.0程序
No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.

Either.js 42 KiB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496
  1. "use strict";
  2. var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
  3. if (k2 === undefined) k2 = k;
  4. var desc = Object.getOwnPropertyDescriptor(m, k);
  5. if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
  6. desc = { enumerable: true, get: function() { return m[k]; } };
  7. }
  8. Object.defineProperty(o, k2, desc);
  9. }) : (function(o, m, k, k2) {
  10. if (k2 === undefined) k2 = k;
  11. o[k2] = m[k];
  12. }));
  13. var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
  14. Object.defineProperty(o, "default", { enumerable: true, value: v });
  15. }) : function(o, v) {
  16. o["default"] = v;
  17. });
  18. var __importStar = (this && this.__importStar) || function (mod) {
  19. if (mod && mod.__esModule) return mod;
  20. var result = {};
  21. if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
  22. __setModuleDefault(result, mod);
  23. return result;
  24. };
  25. Object.defineProperty(exports, "__esModule", { value: true });
  26. exports.fold = exports.match = exports.foldW = exports.matchW = exports.isRight = exports.isLeft = exports.fromOption = exports.fromPredicate = exports.FromEither = exports.MonadThrow = exports.throwError = exports.ChainRec = exports.Extend = exports.extend = exports.Alt = exports.alt = exports.altW = exports.Bifunctor = exports.mapLeft = exports.bimap = exports.Traversable = exports.sequence = exports.traverse = exports.Foldable = exports.reduceRight = exports.foldMap = exports.reduce = exports.Monad = exports.Chain = exports.chain = exports.chainW = exports.Applicative = exports.Apply = exports.ap = exports.apW = exports.Pointed = exports.of = exports.Functor = exports.map = exports.getAltValidation = exports.getApplicativeValidation = exports.getWitherable = exports.getFilterable = exports.getCompactable = exports.getSemigroup = exports.getEq = exports.getShow = exports.URI = exports.right = exports.left = void 0;
  27. exports.getValidation = exports.getValidationMonoid = exports.getValidationSemigroup = exports.getApplyMonoid = exports.getApplySemigroup = exports.either = exports.stringifyJSON = exports.parseJSON = exports.sequenceArray = exports.traverseArray = exports.traverseArrayWithIndex = exports.traverseReadonlyArrayWithIndex = exports.traverseReadonlyNonEmptyArrayWithIndex = exports.ApT = exports.apSW = exports.apS = exports.bindW = exports.bind = exports.let = exports.bindTo = exports.Do = exports.exists = exports.elem = exports.toError = exports.toUnion = exports.chainNullableK = exports.fromNullableK = exports.tryCatchK = exports.tryCatch = exports.fromNullable = exports.orElse = exports.orElseW = exports.swap = exports.filterOrElseW = exports.filterOrElse = exports.chainOptionK = exports.fromOptionK = exports.duplicate = exports.flatten = exports.flattenW = exports.chainFirstW = exports.chainFirst = exports.apSecondW = exports.apSecond = exports.apFirstW = exports.apFirst = exports.flap = exports.getOrElse = exports.getOrElseW = void 0;
  28. var Applicative_1 = require("./Applicative");
  29. var Apply_1 = require("./Apply");
  30. var Chain_1 = require("./Chain");
  31. var ChainRec_1 = require("./ChainRec");
  32. var FromEither_1 = require("./FromEither");
  33. var function_1 = require("./function");
  34. var Functor_1 = require("./Functor");
  35. var _ = __importStar(require("./internal"));
  36. var Separated_1 = require("./Separated");
  37. var Witherable_1 = require("./Witherable");
  38. // -------------------------------------------------------------------------------------
  39. // constructors
  40. // -------------------------------------------------------------------------------------
  41. /**
  42. * Constructs a new `Either` holding a `Left` value. This usually represents a failure, due to the right-bias of this
  43. * structure.
  44. *
  45. * @category constructors
  46. * @since 2.0.0
  47. */
  48. exports.left = _.left;
  49. /**
  50. * Constructs a new `Either` holding a `Right` value. This usually represents a successful value due to the right bias
  51. * of this structure.
  52. *
  53. * @category constructors
  54. * @since 2.0.0
  55. */
  56. exports.right = _.right;
  57. var _map = function (fa, f) { return (0, function_1.pipe)(fa, (0, exports.map)(f)); };
  58. var _ap = function (fab, fa) { return (0, function_1.pipe)(fab, (0, exports.ap)(fa)); };
  59. /* istanbul ignore next */
  60. var _chain = function (ma, f) { return (0, function_1.pipe)(ma, (0, exports.chain)(f)); };
  61. /* istanbul ignore next */
  62. var _reduce = function (fa, b, f) { return (0, function_1.pipe)(fa, (0, exports.reduce)(b, f)); };
  63. /* istanbul ignore next */
  64. var _foldMap = function (M) { return function (fa, f) {
  65. var foldMapM = (0, exports.foldMap)(M);
  66. return (0, function_1.pipe)(fa, foldMapM(f));
  67. }; };
  68. /* istanbul ignore next */
  69. var _reduceRight = function (fa, b, f) { return (0, function_1.pipe)(fa, (0, exports.reduceRight)(b, f)); };
  70. var _traverse = function (F) {
  71. var traverseF = (0, exports.traverse)(F);
  72. return function (ta, f) { return (0, function_1.pipe)(ta, traverseF(f)); };
  73. };
  74. var _bimap = function (fa, f, g) { return (0, function_1.pipe)(fa, (0, exports.bimap)(f, g)); };
  75. var _mapLeft = function (fa, f) { return (0, function_1.pipe)(fa, (0, exports.mapLeft)(f)); };
  76. /* istanbul ignore next */
  77. var _alt = function (fa, that) { return (0, function_1.pipe)(fa, (0, exports.alt)(that)); };
  78. /* istanbul ignore next */
  79. var _extend = function (wa, f) { return (0, function_1.pipe)(wa, (0, exports.extend)(f)); };
  80. var _chainRec = function (a, f) {
  81. return (0, ChainRec_1.tailRec)(f(a), function (e) {
  82. return (0, exports.isLeft)(e) ? (0, exports.right)((0, exports.left)(e.left)) : (0, exports.isLeft)(e.right) ? (0, exports.left)(f(e.right.left)) : (0, exports.right)((0, exports.right)(e.right.right));
  83. });
  84. };
  85. /**
  86. * @category type lambdas
  87. * @since 2.0.0
  88. */
  89. exports.URI = 'Either';
  90. /**
  91. * @category instances
  92. * @since 2.0.0
  93. */
  94. var getShow = function (SE, SA) { return ({
  95. show: function (ma) { return ((0, exports.isLeft)(ma) ? "left(".concat(SE.show(ma.left), ")") : "right(".concat(SA.show(ma.right), ")")); }
  96. }); };
  97. exports.getShow = getShow;
  98. /**
  99. * @category instances
  100. * @since 2.0.0
  101. */
  102. var getEq = function (EL, EA) { return ({
  103. equals: function (x, y) {
  104. return x === y || ((0, exports.isLeft)(x) ? (0, exports.isLeft)(y) && EL.equals(x.left, y.left) : (0, exports.isRight)(y) && EA.equals(x.right, y.right));
  105. }
  106. }); };
  107. exports.getEq = getEq;
  108. /**
  109. * Semigroup returning the left-most non-`Left` value. If both operands are `Right`s then the inner values are
  110. * concatenated using the provided `Semigroup`
  111. *
  112. * @example
  113. * import { getSemigroup, left, right } from 'fp-ts/Either'
  114. * import { SemigroupSum } from 'fp-ts/number'
  115. *
  116. * const S = getSemigroup<string, number>(SemigroupSum)
  117. * assert.deepStrictEqual(S.concat(left('a'), left('b')), left('a'))
  118. * assert.deepStrictEqual(S.concat(left('a'), right(2)), right(2))
  119. * assert.deepStrictEqual(S.concat(right(1), left('b')), right(1))
  120. * assert.deepStrictEqual(S.concat(right(1), right(2)), right(3))
  121. *
  122. * @category instances
  123. * @since 2.0.0
  124. */
  125. var getSemigroup = function (S) { return ({
  126. concat: function (x, y) { return ((0, exports.isLeft)(y) ? x : (0, exports.isLeft)(x) ? y : (0, exports.right)(S.concat(x.right, y.right))); }
  127. }); };
  128. exports.getSemigroup = getSemigroup;
  129. /**
  130. * Builds a `Compactable` instance for `Either` given `Monoid` for the left side.
  131. *
  132. * @category filtering
  133. * @since 2.10.0
  134. */
  135. var getCompactable = function (M) {
  136. var empty = (0, exports.left)(M.empty);
  137. return {
  138. URI: exports.URI,
  139. _E: undefined,
  140. compact: function (ma) { return ((0, exports.isLeft)(ma) ? ma : ma.right._tag === 'None' ? empty : (0, exports.right)(ma.right.value)); },
  141. separate: function (ma) {
  142. return (0, exports.isLeft)(ma)
  143. ? (0, Separated_1.separated)(ma, ma)
  144. : (0, exports.isLeft)(ma.right)
  145. ? (0, Separated_1.separated)((0, exports.right)(ma.right.left), empty)
  146. : (0, Separated_1.separated)(empty, (0, exports.right)(ma.right.right));
  147. }
  148. };
  149. };
  150. exports.getCompactable = getCompactable;
  151. /**
  152. * Builds a `Filterable` instance for `Either` given `Monoid` for the left side
  153. *
  154. * @category filtering
  155. * @since 2.10.0
  156. */
  157. var getFilterable = function (M) {
  158. var empty = (0, exports.left)(M.empty);
  159. var _a = (0, exports.getCompactable)(M), compact = _a.compact, separate = _a.separate;
  160. var filter = function (ma, predicate) {
  161. return (0, exports.isLeft)(ma) ? ma : predicate(ma.right) ? ma : empty;
  162. };
  163. var partition = function (ma, p) {
  164. return (0, exports.isLeft)(ma)
  165. ? (0, Separated_1.separated)(ma, ma)
  166. : p(ma.right)
  167. ? (0, Separated_1.separated)(empty, (0, exports.right)(ma.right))
  168. : (0, Separated_1.separated)((0, exports.right)(ma.right), empty);
  169. };
  170. return {
  171. URI: exports.URI,
  172. _E: undefined,
  173. map: _map,
  174. compact: compact,
  175. separate: separate,
  176. filter: filter,
  177. filterMap: function (ma, f) {
  178. if ((0, exports.isLeft)(ma)) {
  179. return ma;
  180. }
  181. var ob = f(ma.right);
  182. return ob._tag === 'None' ? empty : (0, exports.right)(ob.value);
  183. },
  184. partition: partition,
  185. partitionMap: function (ma, f) {
  186. if ((0, exports.isLeft)(ma)) {
  187. return (0, Separated_1.separated)(ma, ma);
  188. }
  189. var e = f(ma.right);
  190. return (0, exports.isLeft)(e) ? (0, Separated_1.separated)((0, exports.right)(e.left), empty) : (0, Separated_1.separated)(empty, (0, exports.right)(e.right));
  191. }
  192. };
  193. };
  194. exports.getFilterable = getFilterable;
  195. /**
  196. * Builds `Witherable` instance for `Either` given `Monoid` for the left side
  197. *
  198. * @category filtering
  199. * @since 2.0.0
  200. */
  201. var getWitherable = function (M) {
  202. var F_ = (0, exports.getFilterable)(M);
  203. var C = (0, exports.getCompactable)(M);
  204. return {
  205. URI: exports.URI,
  206. _E: undefined,
  207. map: _map,
  208. compact: F_.compact,
  209. separate: F_.separate,
  210. filter: F_.filter,
  211. filterMap: F_.filterMap,
  212. partition: F_.partition,
  213. partitionMap: F_.partitionMap,
  214. traverse: _traverse,
  215. sequence: exports.sequence,
  216. reduce: _reduce,
  217. foldMap: _foldMap,
  218. reduceRight: _reduceRight,
  219. wither: (0, Witherable_1.witherDefault)(exports.Traversable, C),
  220. wilt: (0, Witherable_1.wiltDefault)(exports.Traversable, C)
  221. };
  222. };
  223. exports.getWitherable = getWitherable;
  224. /**
  225. * The default [`Applicative`](#applicative) instance returns the first error, if you want to
  226. * get all errors you need to provide a way to concatenate them via a `Semigroup`.
  227. *
  228. * @example
  229. * import * as A from 'fp-ts/Apply'
  230. * import * as E from 'fp-ts/Either'
  231. * import { pipe } from 'fp-ts/function'
  232. * import * as S from 'fp-ts/Semigroup'
  233. * import * as string from 'fp-ts/string'
  234. *
  235. * const parseString = (u: unknown): E.Either<string, string> =>
  236. * typeof u === 'string' ? E.right(u) : E.left('not a string')
  237. *
  238. * const parseNumber = (u: unknown): E.Either<string, number> =>
  239. * typeof u === 'number' ? E.right(u) : E.left('not a number')
  240. *
  241. * interface Person {
  242. * readonly name: string
  243. * readonly age: number
  244. * }
  245. *
  246. * const parsePerson = (
  247. * input: Record<string, unknown>
  248. * ): E.Either<string, Person> =>
  249. * pipe(
  250. * E.Do,
  251. * E.apS('name', parseString(input.name)),
  252. * E.apS('age', parseNumber(input.age))
  253. * )
  254. *
  255. * assert.deepStrictEqual(parsePerson({}), E.left('not a string')) // <= first error
  256. *
  257. * const Applicative = E.getApplicativeValidation(
  258. * pipe(string.Semigroup, S.intercalate(', '))
  259. * )
  260. *
  261. * const apS = A.apS(Applicative)
  262. *
  263. * const parsePersonAll = (
  264. * input: Record<string, unknown>
  265. * ): E.Either<string, Person> =>
  266. * pipe(
  267. * E.Do,
  268. * apS('name', parseString(input.name)),
  269. * apS('age', parseNumber(input.age))
  270. * )
  271. *
  272. * assert.deepStrictEqual(parsePersonAll({}), E.left('not a string, not a number')) // <= all errors
  273. *
  274. * @category error handling
  275. * @since 2.7.0
  276. */
  277. var getApplicativeValidation = function (SE) { return ({
  278. URI: exports.URI,
  279. _E: undefined,
  280. map: _map,
  281. ap: function (fab, fa) {
  282. return (0, exports.isLeft)(fab)
  283. ? (0, exports.isLeft)(fa)
  284. ? (0, exports.left)(SE.concat(fab.left, fa.left))
  285. : fab
  286. : (0, exports.isLeft)(fa)
  287. ? fa
  288. : (0, exports.right)(fab.right(fa.right));
  289. },
  290. of: exports.of
  291. }); };
  292. exports.getApplicativeValidation = getApplicativeValidation;
  293. /**
  294. * The default [`Alt`](#alt) instance returns the last error, if you want to
  295. * get all errors you need to provide a way to concatenate them via a `Semigroup`.
  296. *
  297. * @example
  298. * import * as E from 'fp-ts/Either'
  299. * import { pipe } from 'fp-ts/function'
  300. * import * as S from 'fp-ts/Semigroup'
  301. * import * as string from 'fp-ts/string'
  302. *
  303. * const parseString = (u: unknown): E.Either<string, string> =>
  304. * typeof u === 'string' ? E.right(u) : E.left('not a string')
  305. *
  306. * const parseNumber = (u: unknown): E.Either<string, number> =>
  307. * typeof u === 'number' ? E.right(u) : E.left('not a number')
  308. *
  309. * const parse = (u: unknown): E.Either<string, string | number> =>
  310. * pipe(
  311. * parseString(u),
  312. * E.alt<string, string | number>(() => parseNumber(u))
  313. * )
  314. *
  315. * assert.deepStrictEqual(parse(true), E.left('not a number')) // <= last error
  316. *
  317. * const Alt = E.getAltValidation(pipe(string.Semigroup, S.intercalate(', ')))
  318. *
  319. * const parseAll = (u: unknown): E.Either<string, string | number> =>
  320. * Alt.alt<string | number>(parseString(u), () => parseNumber(u))
  321. *
  322. * assert.deepStrictEqual(parseAll(true), E.left('not a string, not a number')) // <= all errors
  323. *
  324. * @category error handling
  325. * @since 2.7.0
  326. */
  327. var getAltValidation = function (SE) { return ({
  328. URI: exports.URI,
  329. _E: undefined,
  330. map: _map,
  331. alt: function (me, that) {
  332. if ((0, exports.isRight)(me)) {
  333. return me;
  334. }
  335. var ea = that();
  336. return (0, exports.isLeft)(ea) ? (0, exports.left)(SE.concat(me.left, ea.left)) : ea;
  337. }
  338. }); };
  339. exports.getAltValidation = getAltValidation;
  340. /**
  341. * @category mapping
  342. * @since 2.0.0
  343. */
  344. var map = function (f) { return function (fa) {
  345. return (0, exports.isLeft)(fa) ? fa : (0, exports.right)(f(fa.right));
  346. }; };
  347. exports.map = map;
  348. /**
  349. * @category instances
  350. * @since 2.7.0
  351. */
  352. exports.Functor = {
  353. URI: exports.URI,
  354. map: _map
  355. };
  356. /**
  357. * @category constructors
  358. * @since 2.7.0
  359. */
  360. exports.of = exports.right;
  361. /**
  362. * @category instances
  363. * @since 2.10.0
  364. */
  365. exports.Pointed = {
  366. URI: exports.URI,
  367. of: exports.of
  368. };
  369. /**
  370. * Less strict version of [`ap`](#ap).
  371. *
  372. * The `W` suffix (short for **W**idening) means that the error types will be merged.
  373. *
  374. * @since 2.8.0
  375. */
  376. var apW = function (fa) { return function (fab) {
  377. return (0, exports.isLeft)(fab) ? fab : (0, exports.isLeft)(fa) ? fa : (0, exports.right)(fab.right(fa.right));
  378. }; };
  379. exports.apW = apW;
  380. /**
  381. * @since 2.0.0
  382. */
  383. exports.ap = exports.apW;
  384. /**
  385. * @category instances
  386. * @since 2.10.0
  387. */
  388. exports.Apply = {
  389. URI: exports.URI,
  390. map: _map,
  391. ap: _ap
  392. };
  393. /**
  394. * @category instances
  395. * @since 2.7.0
  396. */
  397. exports.Applicative = {
  398. URI: exports.URI,
  399. map: _map,
  400. ap: _ap,
  401. of: exports.of
  402. };
  403. /**
  404. * Less strict version of [`chain`](#chain).
  405. *
  406. * The `W` suffix (short for **W**idening) means that the error types will be merged.
  407. *
  408. * @example
  409. * import * as E from 'fp-ts/Either'
  410. * import { pipe } from 'fp-ts/function'
  411. *
  412. * const e1: E.Either<string, number> = E.right(1)
  413. * const e2: E.Either<number, number> = E.right(2)
  414. *
  415. * export const result1 = pipe(
  416. * // @ts-expect-error
  417. * e1,
  418. * E.chain(() => e2)
  419. * )
  420. *
  421. * // merged error types -----v-------------v
  422. * // const result2: E.Either<string | number, number>
  423. * export const result2 = pipe(
  424. * e1, // no error
  425. * E.chainW(() => e2)
  426. * )
  427. *
  428. * @category sequencing
  429. * @since 2.6.0
  430. */
  431. var chainW = function (f) {
  432. return function (ma) {
  433. return (0, exports.isLeft)(ma) ? ma : f(ma.right);
  434. };
  435. };
  436. exports.chainW = chainW;
  437. /**
  438. * Composes computations in sequence, using the return value of one computation to determine the next computation.
  439. *
  440. * @category sequencing
  441. * @since 2.0.0
  442. */
  443. exports.chain = exports.chainW;
  444. /**
  445. * @category instances
  446. * @since 2.10.0
  447. */
  448. exports.Chain = {
  449. URI: exports.URI,
  450. map: _map,
  451. ap: _ap,
  452. chain: _chain
  453. };
  454. /**
  455. * @category instances
  456. * @since 2.7.0
  457. */
  458. exports.Monad = {
  459. URI: exports.URI,
  460. map: _map,
  461. ap: _ap,
  462. of: exports.of,
  463. chain: _chain
  464. };
  465. /**
  466. * Left-associative fold of a structure.
  467. *
  468. * @example
  469. * import { pipe } from 'fp-ts/function'
  470. * import * as E from 'fp-ts/Either'
  471. *
  472. * const startWith = 'prefix'
  473. * const concat = (a: string, b: string) => `${a}:${b}`
  474. *
  475. * assert.deepStrictEqual(
  476. * pipe(E.right('a'), E.reduce(startWith, concat)),
  477. * 'prefix:a'
  478. * )
  479. *
  480. * assert.deepStrictEqual(
  481. * pipe(E.left('e'), E.reduce(startWith, concat)),
  482. * 'prefix'
  483. * )
  484. *
  485. * @category folding
  486. * @since 2.0.0
  487. */
  488. var reduce = function (b, f) { return function (fa) {
  489. return (0, exports.isLeft)(fa) ? b : f(b, fa.right);
  490. }; };
  491. exports.reduce = reduce;
  492. /**
  493. * Map each element of the structure to a monoid, and combine the results.
  494. *
  495. * @example
  496. * import { pipe } from 'fp-ts/function'
  497. * import * as E from 'fp-ts/Either'
  498. * import * as S from 'fp-ts/string'
  499. *
  500. * const yell = (a: string) => `${a}!`
  501. *
  502. * assert.deepStrictEqual(
  503. * pipe(E.right('a'), E.foldMap(S.Monoid)(yell)),
  504. * 'a!'
  505. * )
  506. *
  507. * assert.deepStrictEqual(
  508. * pipe(E.left('e'), E.foldMap(S.Monoid)(yell)),
  509. * S.Monoid.empty
  510. * )
  511. *
  512. * @category folding
  513. * @since 2.0.0
  514. */
  515. var foldMap = function (M) { return function (f) { return function (fa) {
  516. return (0, exports.isLeft)(fa) ? M.empty : f(fa.right);
  517. }; }; };
  518. exports.foldMap = foldMap;
  519. /**
  520. * Right-associative fold of a structure.
  521. *
  522. * @example
  523. * import { pipe } from 'fp-ts/function'
  524. * import * as E from 'fp-ts/Either'
  525. *
  526. * const startWith = 'postfix'
  527. * const concat = (a: string, b: string) => `${a}:${b}`
  528. *
  529. * assert.deepStrictEqual(
  530. * pipe(E.right('a'), E.reduceRight(startWith, concat)),
  531. * 'a:postfix'
  532. * )
  533. *
  534. * assert.deepStrictEqual(
  535. * pipe(E.left('e'), E.reduceRight(startWith, concat)),
  536. * 'postfix'
  537. * )
  538. *
  539. * @category folding
  540. * @since 2.0.0
  541. */
  542. var reduceRight = function (b, f) { return function (fa) {
  543. return (0, exports.isLeft)(fa) ? b : f(fa.right, b);
  544. }; };
  545. exports.reduceRight = reduceRight;
  546. /**
  547. * @category instances
  548. * @since 2.7.0
  549. */
  550. exports.Foldable = {
  551. URI: exports.URI,
  552. reduce: _reduce,
  553. foldMap: _foldMap,
  554. reduceRight: _reduceRight
  555. };
  556. /**
  557. * Map each element of a structure to an action, evaluate these actions from left to right, and collect the results.
  558. *
  559. * @example
  560. * import { pipe } from 'fp-ts/function'
  561. * import * as RA from 'fp-ts/ReadonlyArray'
  562. * import * as E from 'fp-ts/Either'
  563. * import * as O from 'fp-ts/Option'
  564. *
  565. * assert.deepStrictEqual(
  566. * pipe(E.right(['a']), E.traverse(O.Applicative)(RA.head)),
  567. * O.some(E.right('a'))
  568. * )
  569. *
  570. * assert.deepStrictEqual(
  571. * pipe(E.right([]), E.traverse(O.Applicative)(RA.head)),
  572. * O.none
  573. * )
  574. *
  575. * @category traversing
  576. * @since 2.6.3
  577. */
  578. var traverse = function (F) {
  579. return function (f) {
  580. return function (ta) {
  581. return (0, exports.isLeft)(ta) ? F.of((0, exports.left)(ta.left)) : F.map(f(ta.right), exports.right);
  582. };
  583. };
  584. };
  585. exports.traverse = traverse;
  586. /**
  587. * Evaluate each monadic action in the structure from left to right, and collect the results.
  588. *
  589. * @example
  590. * import { pipe } from 'fp-ts/function'
  591. * import * as E from 'fp-ts/Either'
  592. * import * as O from 'fp-ts/Option'
  593. *
  594. * assert.deepStrictEqual(
  595. * pipe(E.right(O.some('a')), E.sequence(O.Applicative)),
  596. * O.some(E.right('a'))
  597. * )
  598. *
  599. * assert.deepStrictEqual(
  600. * pipe(E.right(O.none), E.sequence(O.Applicative)),
  601. * O.none
  602. * )
  603. *
  604. * @category traversing
  605. * @since 2.6.3
  606. */
  607. var sequence = function (F) {
  608. return function (ma) {
  609. return (0, exports.isLeft)(ma) ? F.of((0, exports.left)(ma.left)) : F.map(ma.right, exports.right);
  610. };
  611. };
  612. exports.sequence = sequence;
  613. /**
  614. * @category instances
  615. * @since 2.7.0
  616. */
  617. exports.Traversable = {
  618. URI: exports.URI,
  619. map: _map,
  620. reduce: _reduce,
  621. foldMap: _foldMap,
  622. reduceRight: _reduceRight,
  623. traverse: _traverse,
  624. sequence: exports.sequence
  625. };
  626. /**
  627. * Map a pair of functions over the two type arguments of the bifunctor.
  628. *
  629. * @category mapping
  630. * @since 2.0.0
  631. */
  632. var bimap = function (f, g) { return function (fa) {
  633. return (0, exports.isLeft)(fa) ? (0, exports.left)(f(fa.left)) : (0, exports.right)(g(fa.right));
  634. }; };
  635. exports.bimap = bimap;
  636. /**
  637. * Map a function over the first type argument of a bifunctor.
  638. *
  639. * @category error handling
  640. * @since 2.0.0
  641. */
  642. var mapLeft = function (f) { return function (fa) {
  643. return (0, exports.isLeft)(fa) ? (0, exports.left)(f(fa.left)) : fa;
  644. }; };
  645. exports.mapLeft = mapLeft;
  646. /**
  647. * @category instances
  648. * @since 2.7.0
  649. */
  650. exports.Bifunctor = {
  651. URI: exports.URI,
  652. bimap: _bimap,
  653. mapLeft: _mapLeft
  654. };
  655. /**
  656. * Less strict version of [`alt`](#alt).
  657. *
  658. * The `W` suffix (short for **W**idening) means that the error and the return types will be merged.
  659. *
  660. * @category error handling
  661. * @since 2.9.0
  662. */
  663. var altW = function (that) { return function (fa) {
  664. return (0, exports.isLeft)(fa) ? that() : fa;
  665. }; };
  666. exports.altW = altW;
  667. /**
  668. * Identifies an associative operation on a type constructor. It is similar to `Semigroup`, except that it applies to
  669. * types of kind `* -> *`.
  670. *
  671. * In case of `Either` returns the left-most non-`Left` value (or the right-most `Left` value if both values are `Left`).
  672. *
  673. * | x | y | pipe(x, alt(() => y) |
  674. * | -------- | -------- | -------------------- |
  675. * | left(a) | left(b) | left(b) |
  676. * | left(a) | right(2) | right(2) |
  677. * | right(1) | left(b) | right(1) |
  678. * | right(1) | right(2) | right(1) |
  679. *
  680. * @example
  681. * import * as E from 'fp-ts/Either'
  682. * import { pipe } from 'fp-ts/function'
  683. *
  684. * assert.deepStrictEqual(
  685. * pipe(
  686. * E.left('a'),
  687. * E.alt(() => E.left('b'))
  688. * ),
  689. * E.left('b')
  690. * )
  691. * assert.deepStrictEqual(
  692. * pipe(
  693. * E.left('a'),
  694. * E.alt(() => E.right(2))
  695. * ),
  696. * E.right(2)
  697. * )
  698. * assert.deepStrictEqual(
  699. * pipe(
  700. * E.right(1),
  701. * E.alt(() => E.left('b'))
  702. * ),
  703. * E.right(1)
  704. * )
  705. * assert.deepStrictEqual(
  706. * pipe(
  707. * E.right(1),
  708. * E.alt(() => E.right(2))
  709. * ),
  710. * E.right(1)
  711. * )
  712. *
  713. * @category error handling
  714. * @since 2.0.0
  715. */
  716. exports.alt = exports.altW;
  717. /**
  718. * @category instances
  719. * @since 2.7.0
  720. */
  721. exports.Alt = {
  722. URI: exports.URI,
  723. map: _map,
  724. alt: _alt
  725. };
  726. /**
  727. * @since 2.0.0
  728. */
  729. var extend = function (f) { return function (wa) {
  730. return (0, exports.isLeft)(wa) ? wa : (0, exports.right)(f(wa));
  731. }; };
  732. exports.extend = extend;
  733. /**
  734. * @category instances
  735. * @since 2.7.0
  736. */
  737. exports.Extend = {
  738. URI: exports.URI,
  739. map: _map,
  740. extend: _extend
  741. };
  742. /**
  743. * @category instances
  744. * @since 2.7.0
  745. */
  746. exports.ChainRec = {
  747. URI: exports.URI,
  748. map: _map,
  749. ap: _ap,
  750. chain: _chain,
  751. chainRec: _chainRec
  752. };
  753. /**
  754. * @since 2.6.3
  755. */
  756. exports.throwError = exports.left;
  757. /**
  758. * @category instances
  759. * @since 2.7.0
  760. */
  761. exports.MonadThrow = {
  762. URI: exports.URI,
  763. map: _map,
  764. ap: _ap,
  765. of: exports.of,
  766. chain: _chain,
  767. throwError: exports.throwError
  768. };
  769. /**
  770. * @category instances
  771. * @since 2.10.0
  772. */
  773. exports.FromEither = {
  774. URI: exports.URI,
  775. fromEither: function_1.identity
  776. };
  777. /**
  778. * @example
  779. * import { fromPredicate, left, right } from 'fp-ts/Either'
  780. * import { pipe } from 'fp-ts/function'
  781. *
  782. * assert.deepStrictEqual(
  783. * pipe(
  784. * 1,
  785. * fromPredicate(
  786. * (n) => n > 0,
  787. * () => 'error'
  788. * )
  789. * ),
  790. * right(1)
  791. * )
  792. * assert.deepStrictEqual(
  793. * pipe(
  794. * -1,
  795. * fromPredicate(
  796. * (n) => n > 0,
  797. * () => 'error'
  798. * )
  799. * ),
  800. * left('error')
  801. * )
  802. *
  803. * @category lifting
  804. * @since 2.0.0
  805. */
  806. exports.fromPredicate = (0, FromEither_1.fromPredicate)(exports.FromEither);
  807. // -------------------------------------------------------------------------------------
  808. // conversions
  809. // -------------------------------------------------------------------------------------
  810. /**
  811. * @example
  812. * import * as E from 'fp-ts/Either'
  813. * import { pipe } from 'fp-ts/function'
  814. * import * as O from 'fp-ts/Option'
  815. *
  816. * assert.deepStrictEqual(
  817. * pipe(
  818. * O.some(1),
  819. * E.fromOption(() => 'error')
  820. * ),
  821. * E.right(1)
  822. * )
  823. * assert.deepStrictEqual(
  824. * pipe(
  825. * O.none,
  826. * E.fromOption(() => 'error')
  827. * ),
  828. * E.left('error')
  829. * )
  830. *
  831. * @category conversions
  832. * @since 2.0.0
  833. */
  834. exports.fromOption =
  835. /*#__PURE__*/ (0, FromEither_1.fromOption)(exports.FromEither);
  836. // -------------------------------------------------------------------------------------
  837. // refinements
  838. // -------------------------------------------------------------------------------------
  839. /**
  840. * Returns `true` if the either is an instance of `Left`, `false` otherwise.
  841. *
  842. * @category refinements
  843. * @since 2.0.0
  844. */
  845. exports.isLeft = _.isLeft;
  846. /**
  847. * Returns `true` if the either is an instance of `Right`, `false` otherwise.
  848. *
  849. * @category refinements
  850. * @since 2.0.0
  851. */
  852. exports.isRight = _.isRight;
  853. /**
  854. * Less strict version of [`match`](#match).
  855. *
  856. * The `W` suffix (short for **W**idening) means that the handler return types will be merged.
  857. *
  858. * @category pattern matching
  859. * @since 2.10.0
  860. */
  861. var matchW = function (onLeft, onRight) {
  862. return function (ma) {
  863. return (0, exports.isLeft)(ma) ? onLeft(ma.left) : onRight(ma.right);
  864. };
  865. };
  866. exports.matchW = matchW;
  867. /**
  868. * Alias of [`matchW`](#matchw).
  869. *
  870. * @category pattern matching
  871. * @since 2.10.0
  872. */
  873. exports.foldW = exports.matchW;
  874. /**
  875. * Takes two functions and an `Either` value, if the value is a `Left` the inner value is applied to the first function,
  876. * if the value is a `Right` the inner value is applied to the second function.
  877. *
  878. * @example
  879. * import { match, left, right } from 'fp-ts/Either'
  880. * import { pipe } from 'fp-ts/function'
  881. *
  882. * function onLeft(errors: Array<string>): string {
  883. * return `Errors: ${errors.join(', ')}`
  884. * }
  885. *
  886. * function onRight(value: number): string {
  887. * return `Ok: ${value}`
  888. * }
  889. *
  890. * assert.strictEqual(
  891. * pipe(
  892. * right(1),
  893. * match(onLeft, onRight)
  894. * ),
  895. * 'Ok: 1'
  896. * )
  897. * assert.strictEqual(
  898. * pipe(
  899. * left(['error 1', 'error 2']),
  900. * match(onLeft, onRight)
  901. * ),
  902. * 'Errors: error 1, error 2'
  903. * )
  904. *
  905. * @category pattern matching
  906. * @since 2.10.0
  907. */
  908. exports.match = exports.matchW;
  909. /**
  910. * Alias of [`match`](#match).
  911. *
  912. * @category pattern matching
  913. * @since 2.0.0
  914. */
  915. exports.fold = exports.match;
  916. /**
  917. * Less strict version of [`getOrElse`](#getorelse).
  918. *
  919. * The `W` suffix (short for **W**idening) means that the handler return type will be merged.
  920. *
  921. * @category error handling
  922. * @since 2.6.0
  923. */
  924. var getOrElseW = function (onLeft) {
  925. return function (ma) {
  926. return (0, exports.isLeft)(ma) ? onLeft(ma.left) : ma.right;
  927. };
  928. };
  929. exports.getOrElseW = getOrElseW;
  930. /**
  931. * Returns the wrapped value if it's a `Right` or a default value if is a `Left`.
  932. *
  933. * @example
  934. * import { getOrElse, left, right } from 'fp-ts/Either'
  935. * import { pipe } from 'fp-ts/function'
  936. *
  937. * assert.deepStrictEqual(
  938. * pipe(
  939. * right(1),
  940. * getOrElse(() => 0)
  941. * ),
  942. * 1
  943. * )
  944. * assert.deepStrictEqual(
  945. * pipe(
  946. * left('error'),
  947. * getOrElse(() => 0)
  948. * ),
  949. * 0
  950. * )
  951. *
  952. * @category error handling
  953. * @since 2.0.0
  954. */
  955. exports.getOrElse = exports.getOrElseW;
  956. // -------------------------------------------------------------------------------------
  957. // combinators
  958. // -------------------------------------------------------------------------------------
  959. /**
  960. * @category mapping
  961. * @since 2.10.0
  962. */
  963. exports.flap = (0, Functor_1.flap)(exports.Functor);
  964. /**
  965. * Combine two effectful actions, keeping only the result of the first.
  966. *
  967. * @since 2.0.0
  968. */
  969. exports.apFirst = (0, Apply_1.apFirst)(exports.Apply);
  970. /**
  971. * Less strict version of [`apFirst`](#apfirst)
  972. *
  973. * The `W` suffix (short for **W**idening) means that the error types will be merged.
  974. *
  975. * @since 2.12.0
  976. */
  977. exports.apFirstW = exports.apFirst;
  978. /**
  979. * Combine two effectful actions, keeping only the result of the second.
  980. *
  981. * @since 2.0.0
  982. */
  983. exports.apSecond = (0, Apply_1.apSecond)(exports.Apply);
  984. /**
  985. * Less strict version of [`apSecond`](#apsecond)
  986. *
  987. * The `W` suffix (short for **W**idening) means that the error types will be merged.
  988. *
  989. * @since 2.12.0
  990. */
  991. exports.apSecondW = exports.apSecond;
  992. /**
  993. * Composes computations in sequence, using the return value of one computation to determine the next computation and
  994. * keeping only the result of the first.
  995. *
  996. * @category sequencing
  997. * @since 2.0.0
  998. */
  999. exports.chainFirst =
  1000. /*#__PURE__*/ (0, Chain_1.chainFirst)(exports.Chain);
  1001. /**
  1002. * Less strict version of [`chainFirst`](#chainfirst)
  1003. *
  1004. * The `W` suffix (short for **W**idening) means that the error types will be merged.
  1005. *
  1006. * @category sequencing
  1007. * @since 2.8.0
  1008. */
  1009. exports.chainFirstW = exports.chainFirst;
  1010. /**
  1011. * Less strict version of [`flatten`](#flatten).
  1012. *
  1013. * The `W` suffix (short for **W**idening) means that the error types will be merged.
  1014. *
  1015. * @category sequencing
  1016. * @since 2.11.0
  1017. */
  1018. exports.flattenW =
  1019. /*#__PURE__*/ (0, exports.chainW)(function_1.identity);
  1020. /**
  1021. * The `flatten` function is the conventional monad join operator. It is used to remove one level of monadic structure, projecting its bound argument into the outer level.
  1022. *
  1023. * @example
  1024. * import * as E from 'fp-ts/Either'
  1025. *
  1026. * assert.deepStrictEqual(E.flatten(E.right(E.right('a'))), E.right('a'))
  1027. * assert.deepStrictEqual(E.flatten(E.right(E.left('e'))), E.left('e'))
  1028. * assert.deepStrictEqual(E.flatten(E.left('e')), E.left('e'))
  1029. *
  1030. * @category sequencing
  1031. * @since 2.0.0
  1032. */
  1033. exports.flatten = exports.flattenW;
  1034. /**
  1035. * @since 2.0.0
  1036. */
  1037. exports.duplicate = (0, exports.extend)(function_1.identity);
  1038. /**
  1039. * @category lifting
  1040. * @since 2.10.0
  1041. */
  1042. exports.fromOptionK =
  1043. /*#__PURE__*/ (0, FromEither_1.fromOptionK)(exports.FromEither);
  1044. /**
  1045. * @category sequencing
  1046. * @since 2.11.0
  1047. */
  1048. exports.chainOptionK = (0, FromEither_1.chainOptionK)(exports.FromEither, exports.Chain);
  1049. /**
  1050. * @example
  1051. * import * as E from 'fp-ts/Either'
  1052. * import { pipe } from 'fp-ts/function'
  1053. *
  1054. * assert.deepStrictEqual(
  1055. * pipe(
  1056. * E.right(1),
  1057. * E.filterOrElse(
  1058. * (n) => n > 0,
  1059. * () => 'error'
  1060. * )
  1061. * ),
  1062. * E.right(1)
  1063. * )
  1064. * assert.deepStrictEqual(
  1065. * pipe(
  1066. * E.right(-1),
  1067. * E.filterOrElse(
  1068. * (n) => n > 0,
  1069. * () => 'error'
  1070. * )
  1071. * ),
  1072. * E.left('error')
  1073. * )
  1074. * assert.deepStrictEqual(
  1075. * pipe(
  1076. * E.left('a'),
  1077. * E.filterOrElse(
  1078. * (n) => n > 0,
  1079. * () => 'error'
  1080. * )
  1081. * ),
  1082. * E.left('a')
  1083. * )
  1084. *
  1085. * @category filtering
  1086. * @since 2.0.0
  1087. */
  1088. exports.filterOrElse = (0, FromEither_1.filterOrElse)(exports.FromEither, exports.Chain);
  1089. /**
  1090. * Less strict version of [`filterOrElse`](#filterorelse).
  1091. *
  1092. * The `W` suffix (short for **W**idening) means that the error types will be merged.
  1093. *
  1094. * @category filtering
  1095. * @since 2.9.0
  1096. */
  1097. exports.filterOrElseW = exports.filterOrElse;
  1098. /**
  1099. * Returns a `Right` if is a `Left` (and vice versa).
  1100. *
  1101. * @since 2.0.0
  1102. */
  1103. var swap = function (ma) { return ((0, exports.isLeft)(ma) ? (0, exports.right)(ma.left) : (0, exports.left)(ma.right)); };
  1104. exports.swap = swap;
  1105. /**
  1106. * Less strict version of [`orElse`](#orelse).
  1107. *
  1108. * The `W` suffix (short for **W**idening) means that the return types will be merged.
  1109. *
  1110. * @category error handling
  1111. * @since 2.10.0
  1112. */
  1113. var orElseW = function (onLeft) {
  1114. return function (ma) {
  1115. return (0, exports.isLeft)(ma) ? onLeft(ma.left) : ma;
  1116. };
  1117. };
  1118. exports.orElseW = orElseW;
  1119. /**
  1120. * Useful for recovering from errors.
  1121. *
  1122. * @category error handling
  1123. * @since 2.0.0
  1124. */
  1125. exports.orElse = exports.orElseW;
  1126. /**
  1127. * Takes a default and a nullable value, if the value is not nully, turn it into a `Right`, if the value is nully use
  1128. * the provided default as a `Left`.
  1129. *
  1130. * @example
  1131. * import { fromNullable, left, right } from 'fp-ts/Either'
  1132. *
  1133. * const parse = fromNullable('nully')
  1134. *
  1135. * assert.deepStrictEqual(parse(1), right(1))
  1136. * assert.deepStrictEqual(parse(null), left('nully'))
  1137. *
  1138. * @category conversions
  1139. * @since 2.0.0
  1140. */
  1141. var fromNullable = function (e) {
  1142. return function (a) {
  1143. return a == null ? (0, exports.left)(e) : (0, exports.right)(a);
  1144. };
  1145. };
  1146. exports.fromNullable = fromNullable;
  1147. /**
  1148. * Constructs a new `Either` from a function that might throw.
  1149. *
  1150. * See also [`tryCatchK`](#trycatchk).
  1151. *
  1152. * @example
  1153. * import * as E from 'fp-ts/Either'
  1154. *
  1155. * const unsafeHead = <A>(as: ReadonlyArray<A>): A => {
  1156. * if (as.length > 0) {
  1157. * return as[0]
  1158. * } else {
  1159. * throw new Error('empty array')
  1160. * }
  1161. * }
  1162. *
  1163. * const head = <A>(as: ReadonlyArray<A>): E.Either<Error, A> =>
  1164. * E.tryCatch(() => unsafeHead(as), e => (e instanceof Error ? e : new Error('unknown error')))
  1165. *
  1166. * assert.deepStrictEqual(head([]), E.left(new Error('empty array')))
  1167. * assert.deepStrictEqual(head([1, 2, 3]), E.right(1))
  1168. *
  1169. * @category interop
  1170. * @since 2.0.0
  1171. */
  1172. var tryCatch = function (f, onThrow) {
  1173. try {
  1174. return (0, exports.right)(f());
  1175. }
  1176. catch (e) {
  1177. return (0, exports.left)(onThrow(e));
  1178. }
  1179. };
  1180. exports.tryCatch = tryCatch;
  1181. /**
  1182. * Converts a function that may throw to one returning a `Either`.
  1183. *
  1184. * @category interop
  1185. * @since 2.10.0
  1186. */
  1187. var tryCatchK = function (f, onThrow) {
  1188. return function () {
  1189. var a = [];
  1190. for (var _i = 0; _i < arguments.length; _i++) {
  1191. a[_i] = arguments[_i];
  1192. }
  1193. return (0, exports.tryCatch)(function () { return f.apply(void 0, a); }, onThrow);
  1194. };
  1195. };
  1196. exports.tryCatchK = tryCatchK;
  1197. /**
  1198. * @category lifting
  1199. * @since 2.9.0
  1200. */
  1201. var fromNullableK = function (e) {
  1202. var from = (0, exports.fromNullable)(e);
  1203. return function (f) { return (0, function_1.flow)(f, from); };
  1204. };
  1205. exports.fromNullableK = fromNullableK;
  1206. /**
  1207. * @category sequencing
  1208. * @since 2.9.0
  1209. */
  1210. var chainNullableK = function (e) {
  1211. var from = (0, exports.fromNullableK)(e);
  1212. return function (f) { return (0, exports.chain)(from(f)); };
  1213. };
  1214. exports.chainNullableK = chainNullableK;
  1215. /**
  1216. * @category conversions
  1217. * @since 2.10.0
  1218. */
  1219. exports.toUnion = (0, exports.foldW)(function_1.identity, function_1.identity);
  1220. // -------------------------------------------------------------------------------------
  1221. // utils
  1222. // -------------------------------------------------------------------------------------
  1223. /**
  1224. * Default value for the `onError` argument of `tryCatch`
  1225. *
  1226. * @since 2.0.0
  1227. */
  1228. function toError(e) {
  1229. return e instanceof Error ? e : new Error(String(e));
  1230. }
  1231. exports.toError = toError;
  1232. function elem(E) {
  1233. return function (a, ma) {
  1234. if (ma === undefined) {
  1235. var elemE_1 = elem(E);
  1236. return function (ma) { return elemE_1(a, ma); };
  1237. }
  1238. return (0, exports.isLeft)(ma) ? false : E.equals(a, ma.right);
  1239. };
  1240. }
  1241. exports.elem = elem;
  1242. /**
  1243. * Returns `false` if `Left` or returns the result of the application of the given predicate to the `Right` value.
  1244. *
  1245. * @example
  1246. * import { exists, left, right } from 'fp-ts/Either'
  1247. *
  1248. * const gt2 = exists((n: number) => n > 2)
  1249. *
  1250. * assert.strictEqual(gt2(left('a')), false)
  1251. * assert.strictEqual(gt2(right(1)), false)
  1252. * assert.strictEqual(gt2(right(3)), true)
  1253. *
  1254. * @since 2.0.0
  1255. */
  1256. var exists = function (predicate) {
  1257. return function (ma) {
  1258. return (0, exports.isLeft)(ma) ? false : predicate(ma.right);
  1259. };
  1260. };
  1261. exports.exists = exists;
  1262. // -------------------------------------------------------------------------------------
  1263. // do notation
  1264. // -------------------------------------------------------------------------------------
  1265. /**
  1266. * @category do notation
  1267. * @since 2.9.0
  1268. */
  1269. exports.Do = (0, exports.of)(_.emptyRecord);
  1270. /**
  1271. * @category do notation
  1272. * @since 2.8.0
  1273. */
  1274. exports.bindTo = (0, Functor_1.bindTo)(exports.Functor);
  1275. var let_ = /*#__PURE__*/ (0, Functor_1.let)(exports.Functor);
  1276. exports.let = let_;
  1277. /**
  1278. * @category do notation
  1279. * @since 2.8.0
  1280. */
  1281. exports.bind = (0, Chain_1.bind)(exports.Chain);
  1282. /**
  1283. * The `W` suffix (short for **W**idening) means that the error types will be merged.
  1284. *
  1285. * @category do notation
  1286. * @since 2.8.0
  1287. */
  1288. exports.bindW = exports.bind;
  1289. /**
  1290. * @category do notation
  1291. * @since 2.8.0
  1292. */
  1293. exports.apS = (0, Apply_1.apS)(exports.Apply);
  1294. /**
  1295. * Less strict version of [`apS`](#aps).
  1296. *
  1297. * The `W` suffix (short for **W**idening) means that the error types will be merged.
  1298. *
  1299. * @category do notation
  1300. * @since 2.8.0
  1301. */
  1302. exports.apSW = exports.apS;
  1303. /**
  1304. * @since 2.11.0
  1305. */
  1306. exports.ApT = (0, exports.of)(_.emptyReadonlyArray);
  1307. // -------------------------------------------------------------------------------------
  1308. // array utils
  1309. // -------------------------------------------------------------------------------------
  1310. /**
  1311. * Equivalent to `ReadonlyNonEmptyArray#traverseWithIndex(Applicative)`.
  1312. *
  1313. * @category traversing
  1314. * @since 2.11.0
  1315. */
  1316. var traverseReadonlyNonEmptyArrayWithIndex = function (f) {
  1317. return function (as) {
  1318. var e = f(0, _.head(as));
  1319. if ((0, exports.isLeft)(e)) {
  1320. return e;
  1321. }
  1322. var out = [e.right];
  1323. for (var i = 1; i < as.length; i++) {
  1324. var e_1 = f(i, as[i]);
  1325. if ((0, exports.isLeft)(e_1)) {
  1326. return e_1;
  1327. }
  1328. out.push(e_1.right);
  1329. }
  1330. return (0, exports.right)(out);
  1331. };
  1332. };
  1333. exports.traverseReadonlyNonEmptyArrayWithIndex = traverseReadonlyNonEmptyArrayWithIndex;
  1334. /**
  1335. * Equivalent to `ReadonlyArray#traverseWithIndex(Applicative)`.
  1336. *
  1337. * @category traversing
  1338. * @since 2.11.0
  1339. */
  1340. var traverseReadonlyArrayWithIndex = function (f) {
  1341. var g = (0, exports.traverseReadonlyNonEmptyArrayWithIndex)(f);
  1342. return function (as) { return (_.isNonEmpty(as) ? g(as) : exports.ApT); };
  1343. };
  1344. exports.traverseReadonlyArrayWithIndex = traverseReadonlyArrayWithIndex;
  1345. /**
  1346. * Equivalent to `ReadonlyArray#traverseWithIndex(Applicative)`.
  1347. *
  1348. * @category traversing
  1349. * @since 2.9.0
  1350. */
  1351. exports.traverseArrayWithIndex = exports.traverseReadonlyArrayWithIndex;
  1352. /**
  1353. * Equivalent to `ReadonlyArray#traverse(Applicative)`.
  1354. *
  1355. * @category traversing
  1356. * @since 2.9.0
  1357. */
  1358. var traverseArray = function (f) { return (0, exports.traverseReadonlyArrayWithIndex)(function (_, a) { return f(a); }); };
  1359. exports.traverseArray = traverseArray;
  1360. /**
  1361. * Equivalent to `ReadonlyArray#sequence(Applicative)`.
  1362. *
  1363. * @category traversing
  1364. * @since 2.9.0
  1365. */
  1366. exports.sequenceArray =
  1367. /*#__PURE__*/ (0, exports.traverseArray)(function_1.identity);
  1368. /**
  1369. * Use [`parse`](./Json.ts.html#parse) instead.
  1370. *
  1371. * @category zone of death
  1372. * @since 2.0.0
  1373. * @deprecated
  1374. */
  1375. function parseJSON(s, onError) {
  1376. return (0, exports.tryCatch)(function () { return JSON.parse(s); }, onError);
  1377. }
  1378. exports.parseJSON = parseJSON;
  1379. /**
  1380. * Use [`stringify`](./Json.ts.html#stringify) instead.
  1381. *
  1382. * @category zone of death
  1383. * @since 2.0.0
  1384. * @deprecated
  1385. */
  1386. var stringifyJSON = function (u, onError) {
  1387. return (0, exports.tryCatch)(function () {
  1388. var s = JSON.stringify(u);
  1389. if (typeof s !== 'string') {
  1390. throw new Error('Converting unsupported structure to JSON');
  1391. }
  1392. return s;
  1393. }, onError);
  1394. };
  1395. exports.stringifyJSON = stringifyJSON;
  1396. /**
  1397. * This instance is deprecated, use small, specific instances instead.
  1398. * For example if a function needs a `Functor` instance, pass `E.Functor` instead of `E.either`
  1399. * (where `E` is from `import E from 'fp-ts/Either'`)
  1400. *
  1401. * @category zone of death
  1402. * @since 2.0.0
  1403. * @deprecated
  1404. */
  1405. exports.either = {
  1406. URI: exports.URI,
  1407. map: _map,
  1408. of: exports.of,
  1409. ap: _ap,
  1410. chain: _chain,
  1411. reduce: _reduce,
  1412. foldMap: _foldMap,
  1413. reduceRight: _reduceRight,
  1414. traverse: _traverse,
  1415. sequence: exports.sequence,
  1416. bimap: _bimap,
  1417. mapLeft: _mapLeft,
  1418. alt: _alt,
  1419. extend: _extend,
  1420. chainRec: _chainRec,
  1421. throwError: exports.throwError
  1422. };
  1423. /**
  1424. * Use [`getApplySemigroup`](./Apply.ts.html#getapplysemigroup) instead.
  1425. *
  1426. * Semigroup returning the left-most `Left` value. If both operands are `Right`s then the inner values
  1427. * are concatenated using the provided `Semigroup`
  1428. *
  1429. * @category zone of death
  1430. * @since 2.0.0
  1431. * @deprecated
  1432. */
  1433. exports.getApplySemigroup =
  1434. /*#__PURE__*/ (0, Apply_1.getApplySemigroup)(exports.Apply);
  1435. /**
  1436. * Use [`getApplicativeMonoid`](./Applicative.ts.html#getapplicativemonoid) instead.
  1437. *
  1438. * @category zone of death
  1439. * @since 2.0.0
  1440. * @deprecated
  1441. */
  1442. exports.getApplyMonoid =
  1443. /*#__PURE__*/ (0, Applicative_1.getApplicativeMonoid)(exports.Applicative);
  1444. /**
  1445. * Use [`getApplySemigroup`](./Apply.ts.html#getapplysemigroup) instead.
  1446. *
  1447. * @category zone of death
  1448. * @since 2.0.0
  1449. * @deprecated
  1450. */
  1451. var getValidationSemigroup = function (SE, SA) {
  1452. return (0, Apply_1.getApplySemigroup)((0, exports.getApplicativeValidation)(SE))(SA);
  1453. };
  1454. exports.getValidationSemigroup = getValidationSemigroup;
  1455. /**
  1456. * Use [`getApplicativeMonoid`](./Applicative.ts.html#getapplicativemonoid) instead.
  1457. *
  1458. * @category zone of death
  1459. * @since 2.0.0
  1460. * @deprecated
  1461. */
  1462. var getValidationMonoid = function (SE, MA) {
  1463. return (0, Applicative_1.getApplicativeMonoid)((0, exports.getApplicativeValidation)(SE))(MA);
  1464. };
  1465. exports.getValidationMonoid = getValidationMonoid;
  1466. /**
  1467. * Use [`getApplicativeValidation`](#getapplicativevalidation) and [`getAltValidation`](#getaltvalidation) instead.
  1468. *
  1469. * @category zone of death
  1470. * @since 2.0.0
  1471. * @deprecated
  1472. */
  1473. function getValidation(SE) {
  1474. var ap = (0, exports.getApplicativeValidation)(SE).ap;
  1475. var alt = (0, exports.getAltValidation)(SE).alt;
  1476. return {
  1477. URI: exports.URI,
  1478. _E: undefined,
  1479. map: _map,
  1480. of: exports.of,
  1481. chain: _chain,
  1482. bimap: _bimap,
  1483. mapLeft: _mapLeft,
  1484. reduce: _reduce,
  1485. foldMap: _foldMap,
  1486. reduceRight: _reduceRight,
  1487. extend: _extend,
  1488. traverse: _traverse,
  1489. sequence: exports.sequence,
  1490. chainRec: _chainRec,
  1491. throwError: exports.throwError,
  1492. ap: ap,
  1493. alt: alt
  1494. };
  1495. }
  1496. exports.getValidation = getValidation;