fp-ts-std
Version:
The missing pseudo-standard library for fp-ts.
174 lines • 5.48 kB
TypeScript
/**
* Utility functions to accommodate `fp-ts/Either`.
*
* @since 0.1.0
*/
import type { Bounded } from "fp-ts/Bounded";
import type { Either } from "fp-ts/Either";
import type { Ord } from "fp-ts/Ord";
import type { Show } from "fp-ts/Show";
import type { Enum } from "./Enum";
/**
* Unwrap the value from within an `Either`, throwing the inner value of `Left`
* if `Left`.
*
* @example
* import { unsafeUnwrap } from 'fp-ts-std/Either'
* import * as E from 'fp-ts/Either'
*
* assert.deepStrictEqual(unsafeUnwrap(E.right(5)), 5)
*
* @category 3 Functions
* @since 0.1.0
*/
export declare const unsafeUnwrap: <A>(x: Either<unknown, A>) => A;
/**
* Unwrap the value from within an `Either`, throwing the inner value of `Right`
* if `Right`.
*
* @example
* import { unsafeUnwrapLeft } from 'fp-ts-std/Either'
* import * as E from 'fp-ts/Either'
*
* assert.deepStrictEqual(unsafeUnwrapLeft(E.left(5)), 5)
*
* @category 3 Functions
* @since 0.5.0
*/
export declare const unsafeUnwrapLeft: <E>(x: Either<E, unknown>) => E;
/**
* Unwrap the value from within an `Either`, throwing the inner value of `Left`
* via `Show` if `Left`.
*
* @example
* import { unsafeExpect } from 'fp-ts-std/Either'
* import * as E from 'fp-ts/Either'
* import * as Str from 'fp-ts/string'
*
* assert.throws(
* () => unsafeExpect(Str.Show)(E.left('foo')),
* Error('Unwrapped `Left`', { cause: '"foo"' }),
* )
*
* @category 3 Functions
* @since 0.16.0
*/
export declare const unsafeExpect: <E>(S: Show<E>) => <A>(x: Either<E, A>) => A;
/**
* Unwrap the value from within an `Either`, throwing the inner value of `Right`
* via `Show` if `Right`.
*
* @example
* import { unsafeExpectLeft } from 'fp-ts-std/Either'
* import * as E from 'fp-ts/Either'
* import * as Str from 'fp-ts/string'
*
* assert.throws(
* () => unsafeExpectLeft(Str.Show)(E.right('foo')),
* Error('Unwrapped `Right`', { cause: '"foo"' }),
* )
*
* @category 3 Functions
* @since 0.16.0
*/
export declare const unsafeExpectLeft: <A>(S: Show<A>) => <E>(x: Either<E, A>) => E;
/**
* Apply a function to both elements of an `Either`.
*
* @example
* import * as E from 'fp-ts/Either'
* import { mapBoth } from 'fp-ts-std/Either'
* import { multiply } from 'fp-ts-std/Number'
*
* const f = mapBoth(multiply(2))
*
* assert.deepStrictEqual(f(E.left(3)), E.left(6))
* assert.deepStrictEqual(f(E.right(3)), E.right(6))
*
* @category 2 Typeclass Methods
* @since 0.14.0
*/
export declare const mapBoth: <A, B>(f: (x: A) => B) => (xs: Either<A, A>) => Either<B, B>;
/**
* Pattern match against two `Either`s simultaneously.
*
* @example
* import { withFst } from 'fp-ts-std/Tuple'
* import * as E from 'fp-ts/Either'
* import Either = E.Either
* import { match2 } from 'fp-ts-std/Either'
*
* const pair: (x: string) => (y: string) => [string, string] = withFst
*
* const f: (x: Either<string, string>) => (y: Either<string, string>) => [string, string] =
* match2(pair, pair, pair, pair)
*
* assert.deepStrictEqual(f(E.left('l'))(E.left('l')), ['l', 'l'])
* assert.deepStrictEqual(f(E.left('l'))(E.right('r')), ['l', 'r'])
* assert.deepStrictEqual(f(E.left('r'))(E.right('l')), ['r', 'l'])
* assert.deepStrictEqual(f(E.left('r'))(E.right('r')), ['r', 'r'])
*
* @category 3 Functions
* @since 0.17.0
*/
export declare const match2: <A, B, C, D, E>(onLeftLeft: (x: A) => (y: C) => E, onLeftRight: (x: A) => (y: D) => E, onRightLeft: (x: B) => (y: C) => E, onRightRight: (x: B) => (y: D) => E) => (mab: Either<A, B>) => (mcd: Either<C, D>) => E;
/**
* Derive an `Ord` instance for `Either<E, A>` in which `Left` values are
* considered less than `Right` values.
*
* @example
* import * as E from 'fp-ts/Either'
* import { getOrd } from 'fp-ts-std/Either'
* import * as Num from 'fp-ts/number'
* import { LT, EQ, GT } from 'fp-ts-std/Ordering'
*
* const O = getOrd(Num.Ord)(Num.Ord)
*
* assert.strictEqual(O.compare(E.left(1), E.left(1)), EQ)
* assert.strictEqual(O.compare(E.left(1), E.left(2)), LT)
* assert.strictEqual(O.compare(E.right(1), E.left(2)), GT)
*
* @category 1 Typeclass Instances
* @since 0.17.0
*/
export declare const getOrd: <E>(EO: Ord<E>) => <A>(AO: Ord<A>) => Ord<Either<E, A>>;
/**
* Derive a `Bounded` instance for `Either<E, A>` in which the top and bottom
* bounds are `Right(A.top)` and `Left(E.bottom)` respectively.
*
* @example
* import { getBounded } from 'fp-ts-std/Either'
* import * as E from 'fp-ts/Either'
* import * as Bool from 'fp-ts-std/Boolean'
*
* assert.deepStrictEqual(
* getBounded(Bool.Bounded)(Bool.Bounded).top,
* E.right(true),
* )
*
* @category 1 Typeclass Instances
* @since 0.17.0
*/
export declare const getBounded: <E>(BE: Bounded<E>) => <A>(BA: Bounded<A>) => Bounded<Either<E, A>>;
/**
* Derive an `Enum` instance for `Either<E, A>` given an `Enum` instance for `E`
* and `A`.
*
* @example
* import { universe } from 'fp-ts-std/Enum'
* import { Enum as EnumBool } from 'fp-ts-std/Boolean'
* import * as E from 'fp-ts/Either'
* import { getEnum as getEnumE } from 'fp-ts-std/Either'
*
* const EnumBoolE = getEnumE(EnumBool)(EnumBool)
*
* assert.deepStrictEqual(
* universe(EnumBoolE),
* [E.left(false), E.left(true), E.right(false), E.right(true)],
* )
*
* @category 1 Typeclass Instances
* @since 0.17.0
*/
export declare const getEnum: <E>(EE: Enum<E>) => <A>(EA: Enum<A>) => Enum<Either<E, A>>;
//# sourceMappingURL=Either.d.ts.map