|
- "use strict";
- var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
- if (k2 === undefined) k2 = k;
- var desc = Object.getOwnPropertyDescriptor(m, k);
- if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
- desc = { enumerable: true, get: function() { return m[k]; } };
- }
- Object.defineProperty(o, k2, desc);
- }) : (function(o, m, k, k2) {
- if (k2 === undefined) k2 = k;
- o[k2] = m[k];
- }));
- var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
- Object.defineProperty(o, "default", { enumerable: true, value: v });
- }) : function(o, v) {
- o["default"] = v;
- });
- var __importStar = (this && this.__importStar) || function (mod) {
- if (mod && mod.__esModule) return mod;
- var result = {};
- if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
- __setModuleDefault(result, mod);
- return result;
- };
- Object.defineProperty(exports, "__esModule", { value: true });
- 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;
- 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;
- var Applicative_1 = require("./Applicative");
- var Apply_1 = require("./Apply");
- var Chain_1 = require("./Chain");
- var ChainRec_1 = require("./ChainRec");
- var FromEither_1 = require("./FromEither");
- var function_1 = require("./function");
- var Functor_1 = require("./Functor");
- var _ = __importStar(require("./internal"));
- var Separated_1 = require("./Separated");
- var Witherable_1 = require("./Witherable");
- // -------------------------------------------------------------------------------------
- // constructors
- // -------------------------------------------------------------------------------------
- /**
- * Constructs a new `Either` holding a `Left` value. This usually represents a failure, due to the right-bias of this
- * structure.
- *
- * @category constructors
- * @since 2.0.0
- */
- exports.left = _.left;
- /**
- * Constructs a new `Either` holding a `Right` value. This usually represents a successful value due to the right bias
- * of this structure.
- *
- * @category constructors
- * @since 2.0.0
- */
- exports.right = _.right;
- var _map = function (fa, f) { return (0, function_1.pipe)(fa, (0, exports.map)(f)); };
- var _ap = function (fab, fa) { return (0, function_1.pipe)(fab, (0, exports.ap)(fa)); };
- /* istanbul ignore next */
- var _chain = function (ma, f) { return (0, function_1.pipe)(ma, (0, exports.chain)(f)); };
- /* istanbul ignore next */
- var _reduce = function (fa, b, f) { return (0, function_1.pipe)(fa, (0, exports.reduce)(b, f)); };
- /* istanbul ignore next */
- var _foldMap = function (M) { return function (fa, f) {
- var foldMapM = (0, exports.foldMap)(M);
- return (0, function_1.pipe)(fa, foldMapM(f));
- }; };
- /* istanbul ignore next */
- var _reduceRight = function (fa, b, f) { return (0, function_1.pipe)(fa, (0, exports.reduceRight)(b, f)); };
- var _traverse = function (F) {
- var traverseF = (0, exports.traverse)(F);
- return function (ta, f) { return (0, function_1.pipe)(ta, traverseF(f)); };
- };
- var _bimap = function (fa, f, g) { return (0, function_1.pipe)(fa, (0, exports.bimap)(f, g)); };
- var _mapLeft = function (fa, f) { return (0, function_1.pipe)(fa, (0, exports.mapLeft)(f)); };
- /* istanbul ignore next */
- var _alt = function (fa, that) { return (0, function_1.pipe)(fa, (0, exports.alt)(that)); };
- /* istanbul ignore next */
- var _extend = function (wa, f) { return (0, function_1.pipe)(wa, (0, exports.extend)(f)); };
- var _chainRec = function (a, f) {
- return (0, ChainRec_1.tailRec)(f(a), function (e) {
- 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));
- });
- };
- /**
- * @category type lambdas
- * @since 2.0.0
- */
- exports.URI = 'Either';
- /**
- * @category instances
- * @since 2.0.0
- */
- var getShow = function (SE, SA) { return ({
- show: function (ma) { return ((0, exports.isLeft)(ma) ? "left(".concat(SE.show(ma.left), ")") : "right(".concat(SA.show(ma.right), ")")); }
- }); };
- exports.getShow = getShow;
- /**
- * @category instances
- * @since 2.0.0
- */
- var getEq = function (EL, EA) { return ({
- equals: function (x, y) {
- 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));
- }
- }); };
- exports.getEq = getEq;
- /**
- * Semigroup returning the left-most non-`Left` value. If both operands are `Right`s then the inner values are
- * concatenated using the provided `Semigroup`
- *
- * @example
- * import { getSemigroup, left, right } from 'fp-ts/Either'
- * import { SemigroupSum } from 'fp-ts/number'
- *
- * const S = getSemigroup<string, number>(SemigroupSum)
- * assert.deepStrictEqual(S.concat(left('a'), left('b')), left('a'))
- * assert.deepStrictEqual(S.concat(left('a'), right(2)), right(2))
- * assert.deepStrictEqual(S.concat(right(1), left('b')), right(1))
- * assert.deepStrictEqual(S.concat(right(1), right(2)), right(3))
- *
- * @category instances
- * @since 2.0.0
- */
- var getSemigroup = function (S) { return ({
- concat: function (x, y) { return ((0, exports.isLeft)(y) ? x : (0, exports.isLeft)(x) ? y : (0, exports.right)(S.concat(x.right, y.right))); }
- }); };
- exports.getSemigroup = getSemigroup;
- /**
- * Builds a `Compactable` instance for `Either` given `Monoid` for the left side.
- *
- * @category filtering
- * @since 2.10.0
- */
- var getCompactable = function (M) {
- var empty = (0, exports.left)(M.empty);
- return {
- URI: exports.URI,
- _E: undefined,
- compact: function (ma) { return ((0, exports.isLeft)(ma) ? ma : ma.right._tag === 'None' ? empty : (0, exports.right)(ma.right.value)); },
- separate: function (ma) {
- return (0, exports.isLeft)(ma)
- ? (0, Separated_1.separated)(ma, ma)
- : (0, exports.isLeft)(ma.right)
- ? (0, Separated_1.separated)((0, exports.right)(ma.right.left), empty)
- : (0, Separated_1.separated)(empty, (0, exports.right)(ma.right.right));
- }
- };
- };
- exports.getCompactable = getCompactable;
- /**
- * Builds a `Filterable` instance for `Either` given `Monoid` for the left side
- *
- * @category filtering
- * @since 2.10.0
- */
- var getFilterable = function (M) {
- var empty = (0, exports.left)(M.empty);
- var _a = (0, exports.getCompactable)(M), compact = _a.compact, separate = _a.separate;
- var filter = function (ma, predicate) {
- return (0, exports.isLeft)(ma) ? ma : predicate(ma.right) ? ma : empty;
- };
- var partition = function (ma, p) {
- return (0, exports.isLeft)(ma)
- ? (0, Separated_1.separated)(ma, ma)
- : p(ma.right)
- ? (0, Separated_1.separated)(empty, (0, exports.right)(ma.right))
- : (0, Separated_1.separated)((0, exports.right)(ma.right), empty);
- };
- return {
- URI: exports.URI,
- _E: undefined,
- map: _map,
- compact: compact,
- separate: separate,
- filter: filter,
- filterMap: function (ma, f) {
- if ((0, exports.isLeft)(ma)) {
- return ma;
- }
- var ob = f(ma.right);
- return ob._tag === 'None' ? empty : (0, exports.right)(ob.value);
- },
- partition: partition,
- partitionMap: function (ma, f) {
- if ((0, exports.isLeft)(ma)) {
- return (0, Separated_1.separated)(ma, ma);
- }
- var e = f(ma.right);
- 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));
- }
- };
- };
- exports.getFilterable = getFilterable;
- /**
- * Builds `Witherable` instance for `Either` given `Monoid` for the left side
- *
- * @category filtering
- * @since 2.0.0
- */
- var getWitherable = function (M) {
- var F_ = (0, exports.getFilterable)(M);
- var C = (0, exports.getCompactable)(M);
- return {
- URI: exports.URI,
- _E: undefined,
- map: _map,
- compact: F_.compact,
- separate: F_.separate,
- filter: F_.filter,
- filterMap: F_.filterMap,
- partition: F_.partition,
- partitionMap: F_.partitionMap,
- traverse: _traverse,
- sequence: exports.sequence,
- reduce: _reduce,
- foldMap: _foldMap,
- reduceRight: _reduceRight,
- wither: (0, Witherable_1.witherDefault)(exports.Traversable, C),
- wilt: (0, Witherable_1.wiltDefault)(exports.Traversable, C)
- };
- };
- exports.getWitherable = getWitherable;
- /**
- * The default [`Applicative`](#applicative) instance returns the first error, if you want to
- * get all errors you need to provide a way to concatenate them via a `Semigroup`.
- *
- * @example
- * import * as A from 'fp-ts/Apply'
- * import * as E from 'fp-ts/Either'
- * import { pipe } from 'fp-ts/function'
- * import * as S from 'fp-ts/Semigroup'
- * import * as string from 'fp-ts/string'
- *
- * const parseString = (u: unknown): E.Either<string, string> =>
- * typeof u === 'string' ? E.right(u) : E.left('not a string')
- *
- * const parseNumber = (u: unknown): E.Either<string, number> =>
- * typeof u === 'number' ? E.right(u) : E.left('not a number')
- *
- * interface Person {
- * readonly name: string
- * readonly age: number
- * }
- *
- * const parsePerson = (
- * input: Record<string, unknown>
- * ): E.Either<string, Person> =>
- * pipe(
- * E.Do,
- * E.apS('name', parseString(input.name)),
- * E.apS('age', parseNumber(input.age))
- * )
- *
- * assert.deepStrictEqual(parsePerson({}), E.left('not a string')) // <= first error
- *
- * const Applicative = E.getApplicativeValidation(
- * pipe(string.Semigroup, S.intercalate(', '))
- * )
- *
- * const apS = A.apS(Applicative)
- *
- * const parsePersonAll = (
- * input: Record<string, unknown>
- * ): E.Either<string, Person> =>
- * pipe(
- * E.Do,
- * apS('name', parseString(input.name)),
- * apS('age', parseNumber(input.age))
- * )
- *
- * assert.deepStrictEqual(parsePersonAll({}), E.left('not a string, not a number')) // <= all errors
- *
- * @category error handling
- * @since 2.7.0
- */
- var getApplicativeValidation = function (SE) { return ({
- URI: exports.URI,
- _E: undefined,
- map: _map,
- ap: function (fab, fa) {
- return (0, exports.isLeft)(fab)
- ? (0, exports.isLeft)(fa)
- ? (0, exports.left)(SE.concat(fab.left, fa.left))
- : fab
- : (0, exports.isLeft)(fa)
- ? fa
- : (0, exports.right)(fab.right(fa.right));
- },
- of: exports.of
- }); };
- exports.getApplicativeValidation = getApplicativeValidation;
- /**
- * The default [`Alt`](#alt) instance returns the last error, if you want to
- * get all errors you need to provide a way to concatenate them via a `Semigroup`.
- *
- * @example
- * import * as E from 'fp-ts/Either'
- * import { pipe } from 'fp-ts/function'
- * import * as S from 'fp-ts/Semigroup'
- * import * as string from 'fp-ts/string'
- *
- * const parseString = (u: unknown): E.Either<string, string> =>
- * typeof u === 'string' ? E.right(u) : E.left('not a string')
- *
- * const parseNumber = (u: unknown): E.Either<string, number> =>
- * typeof u === 'number' ? E.right(u) : E.left('not a number')
- *
- * const parse = (u: unknown): E.Either<string, string | number> =>
- * pipe(
- * parseString(u),
- * E.alt<string, string | number>(() => parseNumber(u))
- * )
- *
- * assert.deepStrictEqual(parse(true), E.left('not a number')) // <= last error
- *
- * const Alt = E.getAltValidation(pipe(string.Semigroup, S.intercalate(', ')))
- *
- * const parseAll = (u: unknown): E.Either<string, string | number> =>
- * Alt.alt<string | number>(parseString(u), () => parseNumber(u))
- *
- * assert.deepStrictEqual(parseAll(true), E.left('not a string, not a number')) // <= all errors
- *
- * @category error handling
- * @since 2.7.0
- */
- var getAltValidation = function (SE) { return ({
- URI: exports.URI,
- _E: undefined,
- map: _map,
- alt: function (me, that) {
- if ((0, exports.isRight)(me)) {
- return me;
- }
- var ea = that();
- return (0, exports.isLeft)(ea) ? (0, exports.left)(SE.concat(me.left, ea.left)) : ea;
- }
- }); };
- exports.getAltValidation = getAltValidation;
- /**
- * @category mapping
- * @since 2.0.0
- */
- var map = function (f) { return function (fa) {
- return (0, exports.isLeft)(fa) ? fa : (0, exports.right)(f(fa.right));
- }; };
- exports.map = map;
- /**
- * @category instances
- * @since 2.7.0
- */
- exports.Functor = {
- URI: exports.URI,
- map: _map
- };
- /**
- * @category constructors
- * @since 2.7.0
- */
- exports.of = exports.right;
- /**
- * @category instances
- * @since 2.10.0
- */
- exports.Pointed = {
- URI: exports.URI,
- of: exports.of
- };
- /**
- * Less strict version of [`ap`](#ap).
- *
- * The `W` suffix (short for **W**idening) means that the error types will be merged.
- *
- * @since 2.8.0
- */
- var apW = function (fa) { return function (fab) {
- return (0, exports.isLeft)(fab) ? fab : (0, exports.isLeft)(fa) ? fa : (0, exports.right)(fab.right(fa.right));
- }; };
- exports.apW = apW;
- /**
- * @since 2.0.0
- */
- exports.ap = exports.apW;
- /**
- * @category instances
- * @since 2.10.0
- */
- exports.Apply = {
- URI: exports.URI,
- map: _map,
- ap: _ap
- };
- /**
- * @category instances
- * @since 2.7.0
- */
- exports.Applicative = {
- URI: exports.URI,
- map: _map,
- ap: _ap,
- of: exports.of
- };
- /**
- * Less strict version of [`chain`](#chain).
- *
- * The `W` suffix (short for **W**idening) means that the error types will be merged.
- *
- * @example
- * import * as E from 'fp-ts/Either'
- * import { pipe } from 'fp-ts/function'
- *
- * const e1: E.Either<string, number> = E.right(1)
- * const e2: E.Either<number, number> = E.right(2)
- *
- * export const result1 = pipe(
- * // @ts-expect-error
- * e1,
- * E.chain(() => e2)
- * )
- *
- * // merged error types -----v-------------v
- * // const result2: E.Either<string | number, number>
- * export const result2 = pipe(
- * e1, // no error
- * E.chainW(() => e2)
- * )
- *
- * @category sequencing
- * @since 2.6.0
- */
- var chainW = function (f) {
- return function (ma) {
- return (0, exports.isLeft)(ma) ? ma : f(ma.right);
- };
- };
- exports.chainW = chainW;
- /**
- * Composes computations in sequence, using the return value of one computation to determine the next computation.
- *
- * @category sequencing
- * @since 2.0.0
- */
- exports.chain = exports.chainW;
- /**
- * @category instances
- * @since 2.10.0
- */
- exports.Chain = {
- URI: exports.URI,
- map: _map,
- ap: _ap,
- chain: _chain
- };
- /**
- * @category instances
- * @since 2.7.0
- */
- exports.Monad = {
- URI: exports.URI,
- map: _map,
- ap: _ap,
- of: exports.of,
- chain: _chain
- };
- /**
- * Left-associative fold of a structure.
- *
- * @example
- * import { pipe } from 'fp-ts/function'
- * import * as E from 'fp-ts/Either'
- *
- * const startWith = 'prefix'
- * const concat = (a: string, b: string) => `${a}:${b}`
- *
- * assert.deepStrictEqual(
- * pipe(E.right('a'), E.reduce(startWith, concat)),
- * 'prefix:a'
- * )
- *
- * assert.deepStrictEqual(
- * pipe(E.left('e'), E.reduce(startWith, concat)),
- * 'prefix'
- * )
- *
- * @category folding
- * @since 2.0.0
- */
- var reduce = function (b, f) { return function (fa) {
- return (0, exports.isLeft)(fa) ? b : f(b, fa.right);
- }; };
- exports.reduce = reduce;
- /**
- * Map each element of the structure to a monoid, and combine the results.
- *
- * @example
- * import { pipe } from 'fp-ts/function'
- * import * as E from 'fp-ts/Either'
- * import * as S from 'fp-ts/string'
- *
- * const yell = (a: string) => `${a}!`
- *
- * assert.deepStrictEqual(
- * pipe(E.right('a'), E.foldMap(S.Monoid)(yell)),
- * 'a!'
- * )
- *
- * assert.deepStrictEqual(
- * pipe(E.left('e'), E.foldMap(S.Monoid)(yell)),
- * S.Monoid.empty
- * )
- *
- * @category folding
- * @since 2.0.0
- */
- var foldMap = function (M) { return function (f) { return function (fa) {
- return (0, exports.isLeft)(fa) ? M.empty : f(fa.right);
- }; }; };
- exports.foldMap = foldMap;
- /**
- * Right-associative fold of a structure.
- *
- * @example
- * import { pipe } from 'fp-ts/function'
- * import * as E from 'fp-ts/Either'
- *
- * const startWith = 'postfix'
- * const concat = (a: string, b: string) => `${a}:${b}`
- *
- * assert.deepStrictEqual(
- * pipe(E.right('a'), E.reduceRight(startWith, concat)),
- * 'a:postfix'
- * )
- *
- * assert.deepStrictEqual(
- * pipe(E.left('e'), E.reduceRight(startWith, concat)),
- * 'postfix'
- * )
- *
- * @category folding
- * @since 2.0.0
- */
- var reduceRight = function (b, f) { return function (fa) {
- return (0, exports.isLeft)(fa) ? b : f(fa.right, b);
- }; };
- exports.reduceRight = reduceRight;
- /**
- * @category instances
- * @since 2.7.0
- */
- exports.Foldable = {
- URI: exports.URI,
- reduce: _reduce,
- foldMap: _foldMap,
- reduceRight: _reduceRight
- };
- /**
- * Map each element of a structure to an action, evaluate these actions from left to right, and collect the results.
- *
- * @example
- * import { pipe } from 'fp-ts/function'
- * import * as RA from 'fp-ts/ReadonlyArray'
- * import * as E from 'fp-ts/Either'
- * import * as O from 'fp-ts/Option'
- *
- * assert.deepStrictEqual(
- * pipe(E.right(['a']), E.traverse(O.Applicative)(RA.head)),
- * O.some(E.right('a'))
- * )
- *
- * assert.deepStrictEqual(
- * pipe(E.right([]), E.traverse(O.Applicative)(RA.head)),
- * O.none
- * )
- *
- * @category traversing
- * @since 2.6.3
- */
- var traverse = function (F) {
- return function (f) {
- return function (ta) {
- return (0, exports.isLeft)(ta) ? F.of((0, exports.left)(ta.left)) : F.map(f(ta.right), exports.right);
- };
- };
- };
- exports.traverse = traverse;
- /**
- * Evaluate each monadic action in the structure from left to right, and collect the results.
- *
- * @example
- * import { pipe } from 'fp-ts/function'
- * import * as E from 'fp-ts/Either'
- * import * as O from 'fp-ts/Option'
- *
- * assert.deepStrictEqual(
- * pipe(E.right(O.some('a')), E.sequence(O.Applicative)),
- * O.some(E.right('a'))
- * )
- *
- * assert.deepStrictEqual(
- * pipe(E.right(O.none), E.sequence(O.Applicative)),
- * O.none
- * )
- *
- * @category traversing
- * @since 2.6.3
- */
- var sequence = function (F) {
- return function (ma) {
- return (0, exports.isLeft)(ma) ? F.of((0, exports.left)(ma.left)) : F.map(ma.right, exports.right);
- };
- };
- exports.sequence = sequence;
- /**
- * @category instances
- * @since 2.7.0
- */
- exports.Traversable = {
- URI: exports.URI,
- map: _map,
- reduce: _reduce,
- foldMap: _foldMap,
- reduceRight: _reduceRight,
- traverse: _traverse,
- sequence: exports.sequence
- };
- /**
- * Map a pair of functions over the two type arguments of the bifunctor.
- *
- * @category mapping
- * @since 2.0.0
- */
- var bimap = function (f, g) { return function (fa) {
- return (0, exports.isLeft)(fa) ? (0, exports.left)(f(fa.left)) : (0, exports.right)(g(fa.right));
- }; };
- exports.bimap = bimap;
- /**
- * Map a function over the first type argument of a bifunctor.
- *
- * @category error handling
- * @since 2.0.0
- */
- var mapLeft = function (f) { return function (fa) {
- return (0, exports.isLeft)(fa) ? (0, exports.left)(f(fa.left)) : fa;
- }; };
- exports.mapLeft = mapLeft;
- /**
- * @category instances
- * @since 2.7.0
- */
- exports.Bifunctor = {
- URI: exports.URI,
- bimap: _bimap,
- mapLeft: _mapLeft
- };
- /**
- * Less strict version of [`alt`](#alt).
- *
- * The `W` suffix (short for **W**idening) means that the error and the return types will be merged.
- *
- * @category error handling
- * @since 2.9.0
- */
- var altW = function (that) { return function (fa) {
- return (0, exports.isLeft)(fa) ? that() : fa;
- }; };
- exports.altW = altW;
- /**
- * Identifies an associative operation on a type constructor. It is similar to `Semigroup`, except that it applies to
- * types of kind `* -> *`.
- *
- * In case of `Either` returns the left-most non-`Left` value (or the right-most `Left` value if both values are `Left`).
- *
- * | x | y | pipe(x, alt(() => y) |
- * | -------- | -------- | -------------------- |
- * | left(a) | left(b) | left(b) |
- * | left(a) | right(2) | right(2) |
- * | right(1) | left(b) | right(1) |
- * | right(1) | right(2) | right(1) |
- *
- * @example
- * import * as E from 'fp-ts/Either'
- * import { pipe } from 'fp-ts/function'
- *
- * assert.deepStrictEqual(
- * pipe(
- * E.left('a'),
- * E.alt(() => E.left('b'))
- * ),
- * E.left('b')
- * )
- * assert.deepStrictEqual(
- * pipe(
- * E.left('a'),
- * E.alt(() => E.right(2))
- * ),
- * E.right(2)
- * )
- * assert.deepStrictEqual(
- * pipe(
- * E.right(1),
- * E.alt(() => E.left('b'))
- * ),
- * E.right(1)
- * )
- * assert.deepStrictEqual(
- * pipe(
- * E.right(1),
- * E.alt(() => E.right(2))
- * ),
- * E.right(1)
- * )
- *
- * @category error handling
- * @since 2.0.0
- */
- exports.alt = exports.altW;
- /**
- * @category instances
- * @since 2.7.0
- */
- exports.Alt = {
- URI: exports.URI,
- map: _map,
- alt: _alt
- };
- /**
- * @since 2.0.0
- */
- var extend = function (f) { return function (wa) {
- return (0, exports.isLeft)(wa) ? wa : (0, exports.right)(f(wa));
- }; };
- exports.extend = extend;
- /**
- * @category instances
- * @since 2.7.0
- */
- exports.Extend = {
- URI: exports.URI,
- map: _map,
- extend: _extend
- };
- /**
- * @category instances
- * @since 2.7.0
- */
- exports.ChainRec = {
- URI: exports.URI,
- map: _map,
- ap: _ap,
- chain: _chain,
- chainRec: _chainRec
- };
- /**
- * @since 2.6.3
- */
- exports.throwError = exports.left;
- /**
- * @category instances
- * @since 2.7.0
- */
- exports.MonadThrow = {
- URI: exports.URI,
- map: _map,
- ap: _ap,
- of: exports.of,
- chain: _chain,
- throwError: exports.throwError
- };
- /**
- * @category instances
- * @since 2.10.0
- */
- exports.FromEither = {
- URI: exports.URI,
- fromEither: function_1.identity
- };
- /**
- * @example
- * import { fromPredicate, left, right } from 'fp-ts/Either'
- * import { pipe } from 'fp-ts/function'
- *
- * assert.deepStrictEqual(
- * pipe(
- * 1,
- * fromPredicate(
- * (n) => n > 0,
- * () => 'error'
- * )
- * ),
- * right(1)
- * )
- * assert.deepStrictEqual(
- * pipe(
- * -1,
- * fromPredicate(
- * (n) => n > 0,
- * () => 'error'
- * )
- * ),
- * left('error')
- * )
- *
- * @category lifting
- * @since 2.0.0
- */
- exports.fromPredicate = (0, FromEither_1.fromPredicate)(exports.FromEither);
- // -------------------------------------------------------------------------------------
- // conversions
- // -------------------------------------------------------------------------------------
- /**
- * @example
- * import * as E from 'fp-ts/Either'
- * import { pipe } from 'fp-ts/function'
- * import * as O from 'fp-ts/Option'
- *
- * assert.deepStrictEqual(
- * pipe(
- * O.some(1),
- * E.fromOption(() => 'error')
- * ),
- * E.right(1)
- * )
- * assert.deepStrictEqual(
- * pipe(
- * O.none,
- * E.fromOption(() => 'error')
- * ),
- * E.left('error')
- * )
- *
- * @category conversions
- * @since 2.0.0
- */
- exports.fromOption =
- /*#__PURE__*/ (0, FromEither_1.fromOption)(exports.FromEither);
- // -------------------------------------------------------------------------------------
- // refinements
- // -------------------------------------------------------------------------------------
- /**
- * Returns `true` if the either is an instance of `Left`, `false` otherwise.
- *
- * @category refinements
- * @since 2.0.0
- */
- exports.isLeft = _.isLeft;
- /**
- * Returns `true` if the either is an instance of `Right`, `false` otherwise.
- *
- * @category refinements
- * @since 2.0.0
- */
- exports.isRight = _.isRight;
- /**
- * Less strict version of [`match`](#match).
- *
- * The `W` suffix (short for **W**idening) means that the handler return types will be merged.
- *
- * @category pattern matching
- * @since 2.10.0
- */
- var matchW = function (onLeft, onRight) {
- return function (ma) {
- return (0, exports.isLeft)(ma) ? onLeft(ma.left) : onRight(ma.right);
- };
- };
- exports.matchW = matchW;
- /**
- * Alias of [`matchW`](#matchw).
- *
- * @category pattern matching
- * @since 2.10.0
- */
- exports.foldW = exports.matchW;
- /**
- * Takes two functions and an `Either` value, if the value is a `Left` the inner value is applied to the first function,
- * if the value is a `Right` the inner value is applied to the second function.
- *
- * @example
- * import { match, left, right } from 'fp-ts/Either'
- * import { pipe } from 'fp-ts/function'
- *
- * function onLeft(errors: Array<string>): string {
- * return `Errors: ${errors.join(', ')}`
- * }
- *
- * function onRight(value: number): string {
- * return `Ok: ${value}`
- * }
- *
- * assert.strictEqual(
- * pipe(
- * right(1),
- * match(onLeft, onRight)
- * ),
- * 'Ok: 1'
- * )
- * assert.strictEqual(
- * pipe(
- * left(['error 1', 'error 2']),
- * match(onLeft, onRight)
- * ),
- * 'Errors: error 1, error 2'
- * )
- *
- * @category pattern matching
- * @since 2.10.0
- */
- exports.match = exports.matchW;
- /**
- * Alias of [`match`](#match).
- *
- * @category pattern matching
- * @since 2.0.0
- */
- exports.fold = exports.match;
- /**
- * Less strict version of [`getOrElse`](#getorelse).
- *
- * The `W` suffix (short for **W**idening) means that the handler return type will be merged.
- *
- * @category error handling
- * @since 2.6.0
- */
- var getOrElseW = function (onLeft) {
- return function (ma) {
- return (0, exports.isLeft)(ma) ? onLeft(ma.left) : ma.right;
- };
- };
- exports.getOrElseW = getOrElseW;
- /**
- * Returns the wrapped value if it's a `Right` or a default value if is a `Left`.
- *
- * @example
- * import { getOrElse, left, right } from 'fp-ts/Either'
- * import { pipe } from 'fp-ts/function'
- *
- * assert.deepStrictEqual(
- * pipe(
- * right(1),
- * getOrElse(() => 0)
- * ),
- * 1
- * )
- * assert.deepStrictEqual(
- * pipe(
- * left('error'),
- * getOrElse(() => 0)
- * ),
- * 0
- * )
- *
- * @category error handling
- * @since 2.0.0
- */
- exports.getOrElse = exports.getOrElseW;
- // -------------------------------------------------------------------------------------
- // combinators
- // -------------------------------------------------------------------------------------
- /**
- * @category mapping
- * @since 2.10.0
- */
- exports.flap = (0, Functor_1.flap)(exports.Functor);
- /**
- * Combine two effectful actions, keeping only the result of the first.
- *
- * @since 2.0.0
- */
- exports.apFirst = (0, Apply_1.apFirst)(exports.Apply);
- /**
- * Less strict version of [`apFirst`](#apfirst)
- *
- * The `W` suffix (short for **W**idening) means that the error types will be merged.
- *
- * @since 2.12.0
- */
- exports.apFirstW = exports.apFirst;
- /**
- * Combine two effectful actions, keeping only the result of the second.
- *
- * @since 2.0.0
- */
- exports.apSecond = (0, Apply_1.apSecond)(exports.Apply);
- /**
- * Less strict version of [`apSecond`](#apsecond)
- *
- * The `W` suffix (short for **W**idening) means that the error types will be merged.
- *
- * @since 2.12.0
- */
- exports.apSecondW = exports.apSecond;
- /**
- * Composes computations in sequence, using the return value of one computation to determine the next computation and
- * keeping only the result of the first.
- *
- * @category sequencing
- * @since 2.0.0
- */
- exports.chainFirst =
- /*#__PURE__*/ (0, Chain_1.chainFirst)(exports.Chain);
- /**
- * Less strict version of [`chainFirst`](#chainfirst)
- *
- * The `W` suffix (short for **W**idening) means that the error types will be merged.
- *
- * @category sequencing
- * @since 2.8.0
- */
- exports.chainFirstW = exports.chainFirst;
- /**
- * Less strict version of [`flatten`](#flatten).
- *
- * The `W` suffix (short for **W**idening) means that the error types will be merged.
- *
- * @category sequencing
- * @since 2.11.0
- */
- exports.flattenW =
- /*#__PURE__*/ (0, exports.chainW)(function_1.identity);
- /**
- * 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.
- *
- * @example
- * import * as E from 'fp-ts/Either'
- *
- * assert.deepStrictEqual(E.flatten(E.right(E.right('a'))), E.right('a'))
- * assert.deepStrictEqual(E.flatten(E.right(E.left('e'))), E.left('e'))
- * assert.deepStrictEqual(E.flatten(E.left('e')), E.left('e'))
- *
- * @category sequencing
- * @since 2.0.0
- */
- exports.flatten = exports.flattenW;
- /**
- * @since 2.0.0
- */
- exports.duplicate = (0, exports.extend)(function_1.identity);
- /**
- * @category lifting
- * @since 2.10.0
- */
- exports.fromOptionK =
- /*#__PURE__*/ (0, FromEither_1.fromOptionK)(exports.FromEither);
- /**
- * @category sequencing
- * @since 2.11.0
- */
- exports.chainOptionK = (0, FromEither_1.chainOptionK)(exports.FromEither, exports.Chain);
- /**
- * @example
- * import * as E from 'fp-ts/Either'
- * import { pipe } from 'fp-ts/function'
- *
- * assert.deepStrictEqual(
- * pipe(
- * E.right(1),
- * E.filterOrElse(
- * (n) => n > 0,
- * () => 'error'
- * )
- * ),
- * E.right(1)
- * )
- * assert.deepStrictEqual(
- * pipe(
- * E.right(-1),
- * E.filterOrElse(
- * (n) => n > 0,
- * () => 'error'
- * )
- * ),
- * E.left('error')
- * )
- * assert.deepStrictEqual(
- * pipe(
- * E.left('a'),
- * E.filterOrElse(
- * (n) => n > 0,
- * () => 'error'
- * )
- * ),
- * E.left('a')
- * )
- *
- * @category filtering
- * @since 2.0.0
- */
- exports.filterOrElse = (0, FromEither_1.filterOrElse)(exports.FromEither, exports.Chain);
- /**
- * Less strict version of [`filterOrElse`](#filterorelse).
- *
- * The `W` suffix (short for **W**idening) means that the error types will be merged.
- *
- * @category filtering
- * @since 2.9.0
- */
- exports.filterOrElseW = exports.filterOrElse;
- /**
- * Returns a `Right` if is a `Left` (and vice versa).
- *
- * @since 2.0.0
- */
- var swap = function (ma) { return ((0, exports.isLeft)(ma) ? (0, exports.right)(ma.left) : (0, exports.left)(ma.right)); };
- exports.swap = swap;
- /**
- * Less strict version of [`orElse`](#orelse).
- *
- * The `W` suffix (short for **W**idening) means that the return types will be merged.
- *
- * @category error handling
- * @since 2.10.0
- */
- var orElseW = function (onLeft) {
- return function (ma) {
- return (0, exports.isLeft)(ma) ? onLeft(ma.left) : ma;
- };
- };
- exports.orElseW = orElseW;
- /**
- * Useful for recovering from errors.
- *
- * @category error handling
- * @since 2.0.0
- */
- exports.orElse = exports.orElseW;
- /**
- * Takes a default and a nullable value, if the value is not nully, turn it into a `Right`, if the value is nully use
- * the provided default as a `Left`.
- *
- * @example
- * import { fromNullable, left, right } from 'fp-ts/Either'
- *
- * const parse = fromNullable('nully')
- *
- * assert.deepStrictEqual(parse(1), right(1))
- * assert.deepStrictEqual(parse(null), left('nully'))
- *
- * @category conversions
- * @since 2.0.0
- */
- var fromNullable = function (e) {
- return function (a) {
- return a == null ? (0, exports.left)(e) : (0, exports.right)(a);
- };
- };
- exports.fromNullable = fromNullable;
- /**
- * Constructs a new `Either` from a function that might throw.
- *
- * See also [`tryCatchK`](#trycatchk).
- *
- * @example
- * import * as E from 'fp-ts/Either'
- *
- * const unsafeHead = <A>(as: ReadonlyArray<A>): A => {
- * if (as.length > 0) {
- * return as[0]
- * } else {
- * throw new Error('empty array')
- * }
- * }
- *
- * const head = <A>(as: ReadonlyArray<A>): E.Either<Error, A> =>
- * E.tryCatch(() => unsafeHead(as), e => (e instanceof Error ? e : new Error('unknown error')))
- *
- * assert.deepStrictEqual(head([]), E.left(new Error('empty array')))
- * assert.deepStrictEqual(head([1, 2, 3]), E.right(1))
- *
- * @category interop
- * @since 2.0.0
- */
- var tryCatch = function (f, onThrow) {
- try {
- return (0, exports.right)(f());
- }
- catch (e) {
- return (0, exports.left)(onThrow(e));
- }
- };
- exports.tryCatch = tryCatch;
- /**
- * Converts a function that may throw to one returning a `Either`.
- *
- * @category interop
- * @since 2.10.0
- */
- var tryCatchK = function (f, onThrow) {
- return function () {
- var a = [];
- for (var _i = 0; _i < arguments.length; _i++) {
- a[_i] = arguments[_i];
- }
- return (0, exports.tryCatch)(function () { return f.apply(void 0, a); }, onThrow);
- };
- };
- exports.tryCatchK = tryCatchK;
- /**
- * @category lifting
- * @since 2.9.0
- */
- var fromNullableK = function (e) {
- var from = (0, exports.fromNullable)(e);
- return function (f) { return (0, function_1.flow)(f, from); };
- };
- exports.fromNullableK = fromNullableK;
- /**
- * @category sequencing
- * @since 2.9.0
- */
- var chainNullableK = function (e) {
- var from = (0, exports.fromNullableK)(e);
- return function (f) { return (0, exports.chain)(from(f)); };
- };
- exports.chainNullableK = chainNullableK;
- /**
- * @category conversions
- * @since 2.10.0
- */
- exports.toUnion = (0, exports.foldW)(function_1.identity, function_1.identity);
- // -------------------------------------------------------------------------------------
- // utils
- // -------------------------------------------------------------------------------------
- /**
- * Default value for the `onError` argument of `tryCatch`
- *
- * @since 2.0.0
- */
- function toError(e) {
- return e instanceof Error ? e : new Error(String(e));
- }
- exports.toError = toError;
- function elem(E) {
- return function (a, ma) {
- if (ma === undefined) {
- var elemE_1 = elem(E);
- return function (ma) { return elemE_1(a, ma); };
- }
- return (0, exports.isLeft)(ma) ? false : E.equals(a, ma.right);
- };
- }
- exports.elem = elem;
- /**
- * Returns `false` if `Left` or returns the result of the application of the given predicate to the `Right` value.
- *
- * @example
- * import { exists, left, right } from 'fp-ts/Either'
- *
- * const gt2 = exists((n: number) => n > 2)
- *
- * assert.strictEqual(gt2(left('a')), false)
- * assert.strictEqual(gt2(right(1)), false)
- * assert.strictEqual(gt2(right(3)), true)
- *
- * @since 2.0.0
- */
- var exists = function (predicate) {
- return function (ma) {
- return (0, exports.isLeft)(ma) ? false : predicate(ma.right);
- };
- };
- exports.exists = exists;
- // -------------------------------------------------------------------------------------
- // do notation
- // -------------------------------------------------------------------------------------
- /**
- * @category do notation
- * @since 2.9.0
- */
- exports.Do = (0, exports.of)(_.emptyRecord);
- /**
- * @category do notation
- * @since 2.8.0
- */
- exports.bindTo = (0, Functor_1.bindTo)(exports.Functor);
- var let_ = /*#__PURE__*/ (0, Functor_1.let)(exports.Functor);
- exports.let = let_;
- /**
- * @category do notation
- * @since 2.8.0
- */
- exports.bind = (0, Chain_1.bind)(exports.Chain);
- /**
- * The `W` suffix (short for **W**idening) means that the error types will be merged.
- *
- * @category do notation
- * @since 2.8.0
- */
- exports.bindW = exports.bind;
- /**
- * @category do notation
- * @since 2.8.0
- */
- exports.apS = (0, Apply_1.apS)(exports.Apply);
- /**
- * Less strict version of [`apS`](#aps).
- *
- * The `W` suffix (short for **W**idening) means that the error types will be merged.
- *
- * @category do notation
- * @since 2.8.0
- */
- exports.apSW = exports.apS;
- /**
- * @since 2.11.0
- */
- exports.ApT = (0, exports.of)(_.emptyReadonlyArray);
- // -------------------------------------------------------------------------------------
- // array utils
- // -------------------------------------------------------------------------------------
- /**
- * Equivalent to `ReadonlyNonEmptyArray#traverseWithIndex(Applicative)`.
- *
- * @category traversing
- * @since 2.11.0
- */
- var traverseReadonlyNonEmptyArrayWithIndex = function (f) {
- return function (as) {
- var e = f(0, _.head(as));
- if ((0, exports.isLeft)(e)) {
- return e;
- }
- var out = [e.right];
- for (var i = 1; i < as.length; i++) {
- var e_1 = f(i, as[i]);
- if ((0, exports.isLeft)(e_1)) {
- return e_1;
- }
- out.push(e_1.right);
- }
- return (0, exports.right)(out);
- };
- };
- exports.traverseReadonlyNonEmptyArrayWithIndex = traverseReadonlyNonEmptyArrayWithIndex;
- /**
- * Equivalent to `ReadonlyArray#traverseWithIndex(Applicative)`.
- *
- * @category traversing
- * @since 2.11.0
- */
- var traverseReadonlyArrayWithIndex = function (f) {
- var g = (0, exports.traverseReadonlyNonEmptyArrayWithIndex)(f);
- return function (as) { return (_.isNonEmpty(as) ? g(as) : exports.ApT); };
- };
- exports.traverseReadonlyArrayWithIndex = traverseReadonlyArrayWithIndex;
- /**
- * Equivalent to `ReadonlyArray#traverseWithIndex(Applicative)`.
- *
- * @category traversing
- * @since 2.9.0
- */
- exports.traverseArrayWithIndex = exports.traverseReadonlyArrayWithIndex;
- /**
- * Equivalent to `ReadonlyArray#traverse(Applicative)`.
- *
- * @category traversing
- * @since 2.9.0
- */
- var traverseArray = function (f) { return (0, exports.traverseReadonlyArrayWithIndex)(function (_, a) { return f(a); }); };
- exports.traverseArray = traverseArray;
- /**
- * Equivalent to `ReadonlyArray#sequence(Applicative)`.
- *
- * @category traversing
- * @since 2.9.0
- */
- exports.sequenceArray =
- /*#__PURE__*/ (0, exports.traverseArray)(function_1.identity);
- /**
- * Use [`parse`](./Json.ts.html#parse) instead.
- *
- * @category zone of death
- * @since 2.0.0
- * @deprecated
- */
- function parseJSON(s, onError) {
- return (0, exports.tryCatch)(function () { return JSON.parse(s); }, onError);
- }
- exports.parseJSON = parseJSON;
- /**
- * Use [`stringify`](./Json.ts.html#stringify) instead.
- *
- * @category zone of death
- * @since 2.0.0
- * @deprecated
- */
- var stringifyJSON = function (u, onError) {
- return (0, exports.tryCatch)(function () {
- var s = JSON.stringify(u);
- if (typeof s !== 'string') {
- throw new Error('Converting unsupported structure to JSON');
- }
- return s;
- }, onError);
- };
- exports.stringifyJSON = stringifyJSON;
- /**
- * This instance is deprecated, use small, specific instances instead.
- * For example if a function needs a `Functor` instance, pass `E.Functor` instead of `E.either`
- * (where `E` is from `import E from 'fp-ts/Either'`)
- *
- * @category zone of death
- * @since 2.0.0
- * @deprecated
- */
- exports.either = {
- URI: exports.URI,
- map: _map,
- of: exports.of,
- ap: _ap,
- chain: _chain,
- reduce: _reduce,
- foldMap: _foldMap,
- reduceRight: _reduceRight,
- traverse: _traverse,
- sequence: exports.sequence,
- bimap: _bimap,
- mapLeft: _mapLeft,
- alt: _alt,
- extend: _extend,
- chainRec: _chainRec,
- throwError: exports.throwError
- };
- /**
- * Use [`getApplySemigroup`](./Apply.ts.html#getapplysemigroup) instead.
- *
- * Semigroup returning the left-most `Left` value. If both operands are `Right`s then the inner values
- * are concatenated using the provided `Semigroup`
- *
- * @category zone of death
- * @since 2.0.0
- * @deprecated
- */
- exports.getApplySemigroup =
- /*#__PURE__*/ (0, Apply_1.getApplySemigroup)(exports.Apply);
- /**
- * Use [`getApplicativeMonoid`](./Applicative.ts.html#getapplicativemonoid) instead.
- *
- * @category zone of death
- * @since 2.0.0
- * @deprecated
- */
- exports.getApplyMonoid =
- /*#__PURE__*/ (0, Applicative_1.getApplicativeMonoid)(exports.Applicative);
- /**
- * Use [`getApplySemigroup`](./Apply.ts.html#getapplysemigroup) instead.
- *
- * @category zone of death
- * @since 2.0.0
- * @deprecated
- */
- var getValidationSemigroup = function (SE, SA) {
- return (0, Apply_1.getApplySemigroup)((0, exports.getApplicativeValidation)(SE))(SA);
- };
- exports.getValidationSemigroup = getValidationSemigroup;
- /**
- * Use [`getApplicativeMonoid`](./Applicative.ts.html#getapplicativemonoid) instead.
- *
- * @category zone of death
- * @since 2.0.0
- * @deprecated
- */
- var getValidationMonoid = function (SE, MA) {
- return (0, Applicative_1.getApplicativeMonoid)((0, exports.getApplicativeValidation)(SE))(MA);
- };
- exports.getValidationMonoid = getValidationMonoid;
- /**
- * Use [`getApplicativeValidation`](#getapplicativevalidation) and [`getAltValidation`](#getaltvalidation) instead.
- *
- * @category zone of death
- * @since 2.0.0
- * @deprecated
- */
- function getValidation(SE) {
- var ap = (0, exports.getApplicativeValidation)(SE).ap;
- var alt = (0, exports.getAltValidation)(SE).alt;
- return {
- URI: exports.URI,
- _E: undefined,
- map: _map,
- of: exports.of,
- chain: _chain,
- bimap: _bimap,
- mapLeft: _mapLeft,
- reduce: _reduce,
- foldMap: _foldMap,
- reduceRight: _reduceRight,
- extend: _extend,
- traverse: _traverse,
- sequence: exports.sequence,
- chainRec: _chainRec,
- throwError: exports.throwError,
- ap: ap,
- alt: alt
- };
- }
- exports.getValidation = getValidation;
|