@jeengbe/prelude 0.0.0 → 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/either.d.mts.map +1 -1
- package/dist/either.mjs.map +1 -1
- package/package.json +10 -4
package/dist/either.d.mts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"either.d.mts","names":[],"sources":["../src/either.ts"],"mappings":";;uBASe,WAAW,GAAG;;;;EAI3B,IAAI,IAAI,KAAK,GAAG,MAAM,KAAK,OAAO,GAAG;EAIrC,SAAS,IAAI,KAAK,GAAG,MAAM,QAAQ,MAAM,QAAQ,GAAG;;;;EAOpD,YAAY,IAAI,IAAI,KAAK,GAAG,MAAM,OAAO,IAAI,MAAM,OAAO,IAAI,IAAI;EAOlE,iBAAiB,IAAI,IAAI,KAAK,GAAG,MAAM,YAAY,OAAO,IAAI,OAAO,QAAQ,IAAI,IAAI;;;;EAOrF,QAAQ,IAAI,KAAK,GAAG,MAAM,KAAK,OAAO,IAAI;EAO1C,aAAa,IAAI,KAAK,GAAG,MAAM,QAAQ,MAAM,QAAQ,IAAI;;;;EAOzD,MAAM,IAAI,IAAI,SAAS,GAAG,MAAM,IAAI,UAAU,GAAG,MAAM,KAAK,OAAO,IAAI;EAOvE,WAAW,IAAI,IACb,SAAS,GAAG,MAAM,QAAQ,KAC1B,UAAU,GAAG,MAAM,QAAQ,MAC1B,QAAQ,IAAI;;;;EAOf,IAAI,KAAK,GAAG,aAAa,OAAO,GAAG;EAOnC,SAAS,KAAK,GAAG,MAAM,gBAAgB,QAAQ,GAAG;;;;;EAQlD,QAAQ,IAAI,KAAK,GAAG,MAAM,OAAO,YAAY,OAAO,IAAI,IAAI;EAI5D,aAAa,IAAI,KAAK,GAAG,MAAM,YAAY,OAAO,aAAa,QAAQ,IAAI,IAAI;;;;EAO/E,QAAQ,IAAI,IAAI,KAAK,GAAG,MAAM,OAAO,IAAI,MAAM,OAAO,IAAI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I,KAAK,GAAG,MAAM,YAAY,OAAO,IAAI,OAAO,QAAQ,IAAI,IAAI;;;;EAc3E,KAAK,IAAI,IAAI,SAAS,GAAG,MAAM,IAAI,UAAU,GAAG,MAAM,KAAK,QAAQ,KAAK;;;;;;;;;;;;;EAgBxE,QAAQ,SAAS,MAAM,IAAI,MAAM;;;;EAOjC,WAAW;;;;EAOX,UAAU;;;;EAOV,OAAO,QAAQ,MAAM;;;;EAOrB,WAAW,QAAQ,MAAM;;;;EAOzB,UAAU,GAAG,eAAe,GAAG,MAAM,IAAI,QAAQ,IAAI;;;;SAOpD,KAAK,GAAG,OAAO,QAAQ,KAAK,QAAQ;;;;SAOpC,MAAM,GAAG,OAAO,QAAQ,KAAK,eAAe;SAI5C,UAAU,GAAG,GAAG,OAAO,QAAQ,MAAM,KAAK,iBAAiB,QAAQ,KAAK,QAAQ,GAAG;;;;SASnF,WAAW,GAAG,GAAG,QAAQ,YAAY,OAAO,GAAG,MAAM,QAAQ,GAAG;SAIhE,YAAY,GAAG,GAAG,QAAQ,YAAY,OAAO,GAAG,MAAM,QAAQ,GAAG;SAIjE,KAAK,GAAG,GACb,MAAM,kBACN,eAAe,QAAQ,IACvB,cAAc,QAAQ,KACrB,QAAQ,GAAG;EAOd,KAAK,GAAG,eAAe,OAAO,OAAO,GAAG,OAAO,IAAI,YAAY,KAAK,YAAY"}
|
package/dist/either.mjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"either.mjs","names":[],"sources":["../src/either.ts"],"sourcesContent":["import type { Maybe } from './maybe.js';\nimport { isDefined } from './maybe.js';\n\n// Note that xxxAsync implementations in Either all look like:\n// EitherP.fromPromise(toEitherPromise(this)).xxxAsync(...)\n//\n// And conversely, xxx implementations in EitherP all look like:\n// new EitherP(this.value.then(e => e.xxx(...)))\n\nabstract class EitherBase<L, R> {\n /**\n * Maps the value of this Either if it is a Right, performs no operation if this is a Left.\n */\n map<R2>(fn: (t: R) => R2): Either<L, R2> {\n return this.flatMap((value) => Either.right(fn(value)));\n }\n\n mapAsync<R2>(fn: (t: R) => Promise<R2>): EitherP<L, R2> {\n return EitherP.fromPromise(toEitherPromise(this)).mapAsync(fn);\n }\n\n /**\n * Flat maps the value of this Either if it is a Left, performs no operation if this is a Right.\n */\n leftFlatMap<L2, R2>(fn: (t: L) => Either<L2, R2>): Either<L2, R | R2> {\n return this.fold(\n (l) => fn(l),\n (r) => Either.right(r),\n );\n }\n\n leftFlatMapAsync<L2, R2>(fn: (t: L) => PromiseLike<Either<L2, R2>>): EitherP<L2, R | R2> {\n return EitherP.fromPromise(toEitherPromise(this)).leftFlatMapAsync(fn);\n }\n\n /**\n * Maps the value of this Either if it is a Left, performs no operation if this is a Right.\n */\n leftMap<L2>(fn: (t: L) => L2): Either<L2, R> {\n return this.fold(\n (l) => Either.left(fn(l)),\n (r) => Either.right(r),\n );\n }\n\n leftMapAsync<L2>(fn: (t: L) => Promise<L2>): EitherP<L2, R> {\n return EitherP.fromPromise(toEitherPromise(this)).leftMapAsync(fn);\n }\n\n /**\n * Applies the provided functions to the value of this Either, depending on whether it is a Right or a Left and returns a new Either.\n */\n bimap<L2, R2>(leftFn: (l: L) => L2, rightFn: (r: R) => R2): Either<L2, R2> {\n return this.fold(\n (l) => Either.left(leftFn(l)),\n (r) => Either.right(rightFn(r)),\n );\n }\n\n bimapAsync<L2, R2>(\n leftFn: (l: L) => Promise<L2>,\n rightFn: (r: R) => Promise<R2>,\n ): EitherP<L2, R2> {\n return EitherP.fromPromise(toEitherPromise(this)).bimapAsync(leftFn, rightFn);\n }\n\n /**\n * Runs the provided function with the value of this Either if it is a Right, performs no operation if this is a Left.\n */\n tap(fn: (t: R) => void): Either<L, R> {\n return this.flatMap((value) => {\n fn(value);\n return Either.right(value);\n });\n }\n\n tapAsync(fn: (t: R) => Promise<void>): EitherP<L, R> {\n return EitherP.fromPromise(toEitherPromise(this)).tapAsync(fn);\n }\n\n /**\n * Runs the provided function with the value of this Either if it is a Right, performs no operation if this is a Left.\n * If the result of the function is a Left, it will be returned, otherwise the original Right value will be returned.\n */\n flatTap<L2>(fn: (t: R) => Either<L2, void>): Either<L | L2, R> {\n return this.flatMap((value) => fn(value).map(() => value));\n }\n\n flatTapAsync<L2>(fn: (t: R) => PromiseLike<Either<L2, void>>): EitherP<L | L2, R> {\n return EitherP.fromPromise(toEitherPromise(this)).flatTapAsync(fn);\n }\n\n /**\n * Flat maps the value of this Either if it is a Right, performs no operation if this is a Left.\n */\n flatMap<L2, R2>(fn: (t: R) => Either<L2, R2>): Either<L | L2, R2> {\n return this.fold(\n (l) => Either.left(l),\n (r) => fn(r),\n );\n }\n\n flatMapAsync<L2, R2>(fn: (t: R) => PromiseLike<Either<L2, R2>>): EitherP<L | L2, R2> {\n return EitherP.fromPromise(toEitherPromise(this)).flatMapAsync(fn);\n }\n\n /**\n * Applies the provided functions to the value of this Either, depending on whether it is a Right or a Left and returns the result.\n */\n abstract fold<U1, U2>(leftFn: (l: L) => U1, rightFn: (r: R) => U2): U1 | U2;\n\n /**\n * Allows to deconstruct the Either using:\n *\n * @example\n *\n * ```ts\n * declare const e: Either<string, number>;\n *\n * const [left, right] = e.pair();\n * ```\n *\n */\n pair(): [Maybe<L>, Maybe<R>] {\n return [this.getLeft(), this.get()];\n }\n\n /**\n * Returns true if this Either is a Right, false otherwise.\n */\n isRight(): this is Right<R> {\n return this.fold(\n () => false,\n () => true,\n );\n }\n\n /**\n * Returns true if this Either is a Left, false otherwise.\n */\n isLeft(): this is Left<L> {\n return this.fold(\n () => true,\n () => false,\n );\n }\n\n /**\n * Gets the right value if this is a Right or undefined if it's a Left.\n */\n get(): Maybe<R> {\n return this.fold(\n () => undefined,\n (r) => r,\n );\n }\n\n /**\n * Returns the left value if this is a Left or undefined if it's a Right.\n */\n getLeft(): Maybe<L> {\n return this.fold(\n (l) => l,\n () => undefined,\n );\n }\n\n /**\n * Gets the right value if this is a Right or the result of the provided function if it's a Left.\n */\n getOrElse<T>(defaultValue: (l: L) => T): R | T {\n return this.fold(\n (l) => defaultValue(l),\n (r) => r,\n );\n }\n}\n\n// EitherBase is abstract with only Left/Right as concrete subclasses; the cast is always valid.\nfunction toEitherPromise<L, R>(e: EitherBase<L, R>): Promise<Either<L, R>> {\n return Promise.resolve(e as unknown as Either<L, R>);\n}\n\nexport class Left<L> extends EitherBase<L, never> {\n constructor(private readonly value: L) {\n super();\n }\n\n fold<U1, U2>(leftFn: (l: L) => U1, _rightFn: (r: never) => U2): U1 | U2 {\n return leftFn(this.value);\n }\n\n override getLeft(): L {\n return this.value;\n }\n}\n\nexport class Right<R> extends EitherBase<never, R> {\n constructor(private readonly value: R) {\n super();\n }\n\n fold<U1, U2>(_leftFn: (l: never) => U1, rightFn: (r: R) => U2): U1 | U2 {\n return rightFn(this.value);\n }\n\n override get(): R {\n return this.value;\n }\n}\n\n/**\n * Mimics the [Cats Either[L, R]](https://typelevel.org/cats/datatypes/either.html) type.\n *\n * Either<L, R> is a union of Left<L> and Right<R>, so narrowing works in both directions:\n *\n * ```ts\n * declare const e: Either<string, number>;\n *\n * if (e.isLeft()) {\n * e; // Left<string>\n * } else {\n * e; // Right<number>\n * }\n * ```\n */\nexport type Either<L, R> = Left<L> | Right<R>;\n\n// eslint-disable-next-line @typescript-eslint/no-namespace\nexport namespace Either {\n /**\n * Creates a new Left Either with the provided value.\n */\n export function left<L>(value: L): Either<L, never> {\n return new Left(value);\n }\n\n /**\n * Creates a new Right Either with the provided value.\n */\n export function right<R>(value: R): Either<never, R> {\n return new Right(value);\n }\n\n export function fromMaybe<L, R>(value: Maybe<R>, leftValue: () => L): Either<L, R> {\n return isDefined(value) ? Either.right(value) : Either.left(leftValue());\n }\n\n export function cond<L, R>(bool: boolean, rightFn: () => R, leftFn: () => L): Either<L, R> {\n return bool ? Either.right(rightFn()) : Either.left(leftFn());\n }\n}\n\n/**\n * Mimics the [Cats EitherT[Future, L, R]](https://typelevel.org/cats/datatypes/eithert.html) type.\n */\nexport class EitherP<L, R> implements PromiseLike<Either<L, R>> {\n private constructor(private readonly value: PromiseLike<Either<L, R>>) {}\n\n /**\n * Maps the value of this Either if it is a Right, performs no operation if this is a Left.\n */\n map<R2>(fn: (t: R) => R2): EitherP<L, R2> {\n return new EitherP(this.value.then((e) => e.map(fn)));\n }\n\n mapAsync<R2>(fn: (t: R) => Promise<R2>): EitherP<L, R2> {\n return this.flatMapAsync(async (value) => Either.right(await fn(value)));\n }\n\n /**\n * Maps the value of this Either if it is a Left, performs no operation if this is a Right.\n */\n leftMap<L2>(fn: (t: L) => L2): EitherP<L2, R> {\n return new EitherP(this.value.then((e) => e.leftMap(fn)));\n }\n\n leftMapAsync<L2>(fn: (t: L) => Promise<L2>): EitherP<L2, R> {\n return new EitherP(\n this.value.then(\n async (e) =>\n await e.fold(\n async (l) => Either.left(await fn(l)),\n async (r) => Either.right(r),\n ),\n ),\n );\n }\n\n /**\n * Flat maps the value of this Either if it is a Left, performs no operation if this is a Right.\n */\n leftFlatMap<L2, R2>(fn: (t: L) => Either<L2, R2>): EitherP<L2, R | R2> {\n return new EitherP(this.value.then((e) => e.leftFlatMap(fn)));\n }\n\n leftFlatMapAsync<L2, R2>(fn: (t: L) => PromiseLike<Either<L2, R2>>): EitherP<L2, R | R2> {\n return new EitherP(\n this.value.then<Either<L2, R | R2>>(\n async (e) =>\n await e.fold(\n async (left) => await fn(left),\n async (right) => Either.right(right),\n ),\n ),\n );\n }\n\n /**\n * Applies the provided functions to the value of this Either, depending on whether it is a Right or a Left and returns a new Either.\n */\n bimap<L2, R2>(leftFn: (l: L) => L2, rightFn: (r: R) => R2): EitherP<L2, R2> {\n return new EitherP(this.value.then((e) => e.bimap(leftFn, rightFn)));\n }\n\n bimapAsync<L2, R2>(\n leftFn: (l: L) => Promise<L2>,\n rightFn: (r: R) => Promise<R2>,\n ): EitherP<L2, R2> {\n return new EitherP(\n this.value.then(\n async (e) =>\n await e.fold(\n async (l) => Either.left(await leftFn(l)),\n async (r) => Either.right(await rightFn(r)),\n ),\n ),\n );\n }\n\n /**\n * Runs the provided function with the value of this Either if it is a Right, performs no operation if this is a Left.\n */\n tap(fn: (t: R) => void): EitherP<L, R> {\n return new EitherP(this.value.then((e) => e.tap(fn)));\n }\n\n tapAsync(fn: (t: R) => Promise<void>): EitherP<L, R> {\n return this.flatMapAsync(async (value) => {\n await fn(value);\n return Either.right(value);\n });\n }\n\n /**\n * Runs the provided function with the value of this Either if it is a Right, performs no operation if this is a Left.\n * If the result of the function is a Left, it will be returned, otherwise the original Right value will be returned.\n */\n flatTap<L2>(fn: (t: R) => Either<L2, void>): EitherP<L | L2, R> {\n return new EitherP(this.value.then((e) => e.flatTap(fn)));\n }\n\n flatTapAsync<L2>(fn: (t: R) => PromiseLike<Either<L2, void>>): EitherP<L | L2, R> {\n return this.flatMapAsync(async (value) => (await fn(value)).map(() => value));\n }\n\n /**\n * Flat maps the value of this Either if it is a Right, performs no operation if this is a Left.\n */\n flatMap<L2, R2>(fn: (t: R) => Either<L2, R2>): EitherP<L | L2, R2> {\n return new EitherP(this.value.then((e) => e.flatMap(fn)));\n }\n\n flatMapAsync<L2, R2>(fn: (t: R) => PromiseLike<Either<L2, R2>>): EitherP<L | L2, R2> {\n return new EitherP(\n this.value.then<Either<L | L2, R2>>((e) =>\n e.fold(\n async (left) => Either.left(left),\n (right) => fn(right),\n ),\n ),\n );\n }\n\n /**\n * Applies the provided functions to the value of this Either, depending on whether it is a Right or a Left and returns the result.\n */\n async fold<U1, U2>(leftFn: (l: L) => U1, rightFn: (r: R) => U2): Promise<U1 | U2> {\n return (await this.value).fold(leftFn, rightFn);\n }\n\n /**\n * Allows to deconstruct the Either using:\n *\n * @example\n *\n * ```ts\n * declare const e: EitherP<string, number>;\n *\n * const [left, right] = await e.pair();\n * ```\n *\n */\n async pair(): Promise<[Maybe<L>, Maybe<R>]> {\n return [await this.getLeft(), await this.get()];\n }\n\n /**\n * Returns true if this Either is a Right, false otherwise.\n */\n async isRight(): Promise<boolean> {\n return (await this.value).isRight();\n }\n\n /**\n * Returns true if this Either is a Left, false otherwise.\n */\n async isLeft(): Promise<boolean> {\n return !(await this.isRight());\n }\n\n /**\n * Gets the right value if this is a Right or undefined if it's a Left.\n */\n async get(): Promise<Maybe<R>> {\n return (await this.value).get();\n }\n\n /**\n * Returns the left value if this is a Left or undefined if it's a Right.\n */\n async getLeft(): Promise<Maybe<L>> {\n return (await this.value).getLeft();\n }\n\n /**\n * Gets the right value if this is a Right or the result of the provided function if it's a Left.\n */\n async getOrElse<T>(defaultValue: (l: L) => T): Promise<R | T> {\n return (await this.value).getOrElse(defaultValue);\n }\n\n /**\n * Creates a new Left EitherP with the provided value.\n */\n static left<L>(value: Promise<L>): EitherP<L, never> {\n return new EitherP(value.then((v) => Either.left(v)));\n }\n\n /**\n * Creates a new Right EitherP with the provided value.\n */\n static right<R>(value: Promise<R>): EitherP<never, R> {\n return new EitherP(value.then((v) => Either.right(v)));\n }\n\n static fromMaybe<L, R>(value: Promise<Maybe<R>>, leftValue: () => Promise<L>): EitherP<L, R> {\n return new EitherP(\n value.then(async (v) => (isDefined(v) ? Either.right(v) : Either.left(await leftValue()))),\n );\n }\n\n /**\n * @deprecated\n */\n static fromEither<L, R>(either: PromiseLike<Either<L, R>>): EitherP<L, R> {\n return new EitherP(either);\n }\n\n static fromPromise<L, R>(either: PromiseLike<Either<L, R>>): EitherP<L, R> {\n return new EitherP(either);\n }\n\n static cond<L, R>(\n bool: Promise<boolean>,\n rightFn: () => Promise<R>,\n leftFn: () => Promise<L>,\n ): EitherP<L, R> {\n return new EitherP(\n bool.then(async (b) => (b ? Either.right(await rightFn()) : Either.left(await leftFn()))),\n );\n }\n\n then<U>(onfulfilled?: (value: Either<L, R>) => U | PromiseLike<U>): PromiseLike<U> {\n return this.value.then(onfulfilled);\n }\n}\n"],"mappings":";;;AASA,IAAe,aAAf,MAAgC;;;;CAI9B,IAAQ,IAAiC;EACvC,OAAO,KAAK,SAAS,UAAU,OAAO,MAAM,GAAG,KAAK,CAAC,CAAC;CAC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,MAAM,SAA2B;EAC/B,OAAO,CAAE,MAAM,KAAK,QAAQ;CAC9B;;;;CAKA,MAAM,MAAyB;EAC7B,QAAQ,MAAM,KAAK,MAAK,CAAE,IAAI;CAChC;;;;CAKA,MAAM,UAA6B;EACjC,QAAQ,MAAM,KAAK,MAAK,CAAE,QAAQ;CACpC;;;;CAKA,MAAM,UAAa,cAA2C;EAC5D,QAAQ,MAAM,KAAK,MAAK,CAAE,UAAU,YAAY;CAClD;;;;CAKA,OAAO,KAAQ,OAAsC;EACnD,OAAO,IAAI,QAAQ,MAAM,MAAM,MAAM,OAAO,KAAK,CAAC,CAAC,CAAC;CACtD;;;;CAKA,OAAO,MAAS,OAAsC;EACpD,OAAO,IAAI,QAAQ,MAAM,MAAM,MAAM,OAAO,MAAM,CAAC,CAAC,CAAC;CACvD;CAEA,OAAO,UAAgB,OAA0B,WAA4C;EAC3F,OAAO,IAAI,QACT,MAAM,KAAK,OAAO,MAAO,UAAU,CAAC,IAAI,OAAO,MAAM,CAAC,IAAI,OAAO,KAAK,MAAM,UAAU,CAAC,CAAE,CAC3F;CACF;;;;CAKA,OAAO,WAAiB,QAAkD;EACxE,OAAO,IAAI,QAAQ,MAAM;CAC3B;CAEA,OAAO,YAAkB,QAAkD;EACzE,OAAO,IAAI,QAAQ,MAAM;CAC3B;CAEA,OAAO,KACL,MACA,SACA,QACe;EACf,OAAO,IAAI,QACT,KAAK,KAAK,OAAO,MAAO,IAAI,OAAO,MAAM,MAAM,QAAQ,CAAC,IAAI,OAAO,KAAK,MAAM,OAAO,CAAC,CAAE,CAC1F;CACF;CAEA,KAAQ,aAA2E;EACjF,OAAO,KAAK,MAAM,KAAK,WAAW;CACpC;AACF"}
|
|
1
|
+
{"version":3,"file":"either.mjs","names":[],"sources":["../src/either.ts"],"sourcesContent":["import type { Maybe } from './maybe.js';\nimport { isDefined } from './maybe.js';\n\n// Note that xxxAsync implementations in Either all look like:\n// EitherP.fromPromise(toEitherPromise(this)).xxxAsync(...)\n//\n// And conversely, xxx implementations in EitherP all look like:\n// new EitherP(this.value.then(e => e.xxx(...)))\n\nabstract class EitherBase<L, R> {\n /**\n * Maps the value of this Either if it is a Right, performs no operation if this is a Left.\n */\n map<R2>(fn: (t: R) => R2): Either<L, R2> {\n return this.flatMap((value) => Either.right(fn(value)));\n }\n\n mapAsync<R2>(fn: (t: R) => Promise<R2>): EitherP<L, R2> {\n return EitherP.fromPromise(toEitherPromise(this)).mapAsync(fn);\n }\n\n /**\n * Flat maps the value of this Either if it is a Left, performs no operation if this is a Right.\n */\n leftFlatMap<L2, R2>(fn: (t: L) => Either<L2, R2>): Either<L2, R | R2> {\n return this.fold(\n (l) => fn(l),\n (r) => Either.right(r),\n );\n }\n\n leftFlatMapAsync<L2, R2>(fn: (t: L) => PromiseLike<Either<L2, R2>>): EitherP<L2, R | R2> {\n return EitherP.fromPromise(toEitherPromise(this)).leftFlatMapAsync(fn);\n }\n\n /**\n * Maps the value of this Either if it is a Left, performs no operation if this is a Right.\n */\n leftMap<L2>(fn: (t: L) => L2): Either<L2, R> {\n return this.fold(\n (l) => Either.left(fn(l)),\n (r) => Either.right(r),\n );\n }\n\n leftMapAsync<L2>(fn: (t: L) => Promise<L2>): EitherP<L2, R> {\n return EitherP.fromPromise(toEitherPromise(this)).leftMapAsync(fn);\n }\n\n /**\n * Applies the provided functions to the value of this Either, depending on whether it is a Right or a Left and returns a new Either.\n */\n bimap<L2, R2>(leftFn: (l: L) => L2, rightFn: (r: R) => R2): Either<L2, R2> {\n return this.fold(\n (l) => Either.left(leftFn(l)),\n (r) => Either.right(rightFn(r)),\n );\n }\n\n bimapAsync<L2, R2>(\n leftFn: (l: L) => Promise<L2>,\n rightFn: (r: R) => Promise<R2>,\n ): EitherP<L2, R2> {\n return EitherP.fromPromise(toEitherPromise(this)).bimapAsync(leftFn, rightFn);\n }\n\n /**\n * Runs the provided function with the value of this Either if it is a Right, performs no operation if this is a Left.\n */\n tap(fn: (t: R) => void): Either<L, R> {\n return this.flatMap((value) => {\n fn(value);\n return Either.right(value);\n });\n }\n\n tapAsync(fn: (t: R) => Promise<void>): EitherP<L, R> {\n return EitherP.fromPromise(toEitherPromise(this)).tapAsync(fn);\n }\n\n /**\n * Runs the provided function with the value of this Either if it is a Right, performs no operation if this is a Left.\n * If the result of the function is a Left, it will be returned, otherwise the original Right value will be returned.\n */\n flatTap<L2>(fn: (t: R) => Either<L2, void>): Either<L | L2, R> {\n return this.flatMap((value) => fn(value).map(() => value));\n }\n\n flatTapAsync<L2>(fn: (t: R) => PromiseLike<Either<L2, void>>): EitherP<L | L2, R> {\n return EitherP.fromPromise(toEitherPromise(this)).flatTapAsync(fn);\n }\n\n /**\n * Flat maps the value of this Either if it is a Right, performs no operation if this is a Left.\n */\n flatMap<L2, R2>(fn: (t: R) => Either<L2, R2>): Either<L | L2, R2> {\n return this.fold(\n (l) => Either.left(l),\n (r) => fn(r),\n );\n }\n\n flatMapAsync<L2, R2>(fn: (t: R) => PromiseLike<Either<L2, R2>>): EitherP<L | L2, R2> {\n return EitherP.fromPromise(toEitherPromise(this)).flatMapAsync(fn);\n }\n\n /**\n * Applies the provided functions to the value of this Either, depending on whether it is a Right or a Left and returns the result.\n */\n abstract fold<U1, U2>(leftFn: (l: L) => U1, rightFn: (r: R) => U2): U1 | U2;\n\n /**\n * Allows to deconstruct the Either using:\n *\n * @example\n *\n * ```ts\n * declare const e: Either<string, number>;\n *\n * const [left, right] = e.pair();\n * ```\n *\n */\n pair(): [Maybe<L>, Maybe<R>] {\n return [this.getLeft(), this.get()];\n }\n\n /**\n * Returns true if this Either is a Right, false otherwise.\n */\n isRight(): this is Right<R> {\n return this.fold(\n () => false,\n () => true,\n );\n }\n\n /**\n * Returns true if this Either is a Left, false otherwise.\n */\n isLeft(): this is Left<L> {\n return this.fold(\n () => true,\n () => false,\n );\n }\n\n /**\n * Gets the right value if this is a Right or undefined if it's a Left.\n */\n get(): Maybe<R> {\n return this.fold(\n () => undefined,\n (r) => r,\n );\n }\n\n /**\n * Returns the left value if this is a Left or undefined if it's a Right.\n */\n getLeft(): Maybe<L> {\n return this.fold(\n (l) => l,\n () => undefined,\n );\n }\n\n /**\n * Gets the right value if this is a Right or the result of the provided function if it's a Left.\n */\n getOrElse<T>(defaultValue: (l: L) => T): R | T {\n return this.fold(\n (l) => defaultValue(l),\n (r) => r,\n );\n }\n}\n\n// EitherBase is abstract with only Left/Right as concrete subclasses; the cast is always valid.\nfunction toEitherPromise<L, R>(e: EitherBase<L, R>): Promise<Either<L, R>> {\n return Promise.resolve(e as unknown as Either<L, R>);\n}\n\nexport class Left<L> extends EitherBase<L, never> {\n constructor(private readonly value: L) {\n super();\n }\n\n fold<U1, U2>(leftFn: (l: L) => U1, _rightFn: (r: never) => U2): U1 | U2 {\n return leftFn(this.value);\n }\n\n override getLeft(): L {\n return this.value;\n }\n}\n\nexport class Right<R> extends EitherBase<never, R> {\n constructor(private readonly value: R) {\n super();\n }\n\n fold<U1, U2>(_leftFn: (l: never) => U1, rightFn: (r: R) => U2): U1 | U2 {\n return rightFn(this.value);\n }\n\n override get(): R {\n return this.value;\n }\n}\n\n/**\n * Mimics the [Cats Either[L, R]](https://typelevel.org/cats/datatypes/either.html) type.\n *\n * Either<L, R> is a union of Left<L> and Right<R>, so narrowing works in both directions:\n *\n * ```ts\n * declare const e: Either<string, number>;\n *\n * if (e.isLeft()) {\n * e; // Left<string>\n * } else {\n * e; // Right<number>\n * }\n * ```\n */\nexport type Either<L, R> = Left<L> | Right<R>;\n\nexport namespace Either {\n /**\n * Creates a new Left Either with the provided value.\n */\n export function left<L>(value: L): Either<L, never> {\n return new Left(value);\n }\n\n /**\n * Creates a new Right Either with the provided value.\n */\n export function right<R>(value: R): Either<never, R> {\n return new Right(value);\n }\n\n export function fromMaybe<L, R>(value: Maybe<R>, leftValue: () => L): Either<L, R> {\n return isDefined(value) ? Either.right(value) : Either.left(leftValue());\n }\n\n export function cond<L, R>(bool: boolean, rightFn: () => R, leftFn: () => L): Either<L, R> {\n return bool ? Either.right(rightFn()) : Either.left(leftFn());\n }\n}\n\n/**\n * Mimics the [Cats EitherT[Future, L, R]](https://typelevel.org/cats/datatypes/eithert.html) type.\n */\nexport class EitherP<L, R> implements PromiseLike<Either<L, R>> {\n private constructor(private readonly value: PromiseLike<Either<L, R>>) {}\n\n /**\n * Maps the value of this Either if it is a Right, performs no operation if this is a Left.\n */\n map<R2>(fn: (t: R) => R2): EitherP<L, R2> {\n return new EitherP(this.value.then((e) => e.map(fn)));\n }\n\n mapAsync<R2>(fn: (t: R) => Promise<R2>): EitherP<L, R2> {\n return this.flatMapAsync(async (value) => Either.right(await fn(value)));\n }\n\n /**\n * Maps the value of this Either if it is a Left, performs no operation if this is a Right.\n */\n leftMap<L2>(fn: (t: L) => L2): EitherP<L2, R> {\n return new EitherP(this.value.then((e) => e.leftMap(fn)));\n }\n\n leftMapAsync<L2>(fn: (t: L) => Promise<L2>): EitherP<L2, R> {\n return new EitherP(\n this.value.then(\n async (e) =>\n await e.fold(\n async (l) => Either.left(await fn(l)),\n async (r) => Either.right(r),\n ),\n ),\n );\n }\n\n /**\n * Flat maps the value of this Either if it is a Left, performs no operation if this is a Right.\n */\n leftFlatMap<L2, R2>(fn: (t: L) => Either<L2, R2>): EitherP<L2, R | R2> {\n return new EitherP(this.value.then((e) => e.leftFlatMap(fn)));\n }\n\n leftFlatMapAsync<L2, R2>(fn: (t: L) => PromiseLike<Either<L2, R2>>): EitherP<L2, R | R2> {\n return new EitherP(\n this.value.then<Either<L2, R | R2>>(\n async (e) =>\n await e.fold(\n async (left) => await fn(left),\n async (right) => Either.right(right),\n ),\n ),\n );\n }\n\n /**\n * Applies the provided functions to the value of this Either, depending on whether it is a Right or a Left and returns a new Either.\n */\n bimap<L2, R2>(leftFn: (l: L) => L2, rightFn: (r: R) => R2): EitherP<L2, R2> {\n return new EitherP(this.value.then((e) => e.bimap(leftFn, rightFn)));\n }\n\n bimapAsync<L2, R2>(\n leftFn: (l: L) => Promise<L2>,\n rightFn: (r: R) => Promise<R2>,\n ): EitherP<L2, R2> {\n return new EitherP(\n this.value.then(\n async (e) =>\n await e.fold(\n async (l) => Either.left(await leftFn(l)),\n async (r) => Either.right(await rightFn(r)),\n ),\n ),\n );\n }\n\n /**\n * Runs the provided function with the value of this Either if it is a Right, performs no operation if this is a Left.\n */\n tap(fn: (t: R) => void): EitherP<L, R> {\n return new EitherP(this.value.then((e) => e.tap(fn)));\n }\n\n tapAsync(fn: (t: R) => Promise<void>): EitherP<L, R> {\n return this.flatMapAsync(async (value) => {\n await fn(value);\n return Either.right(value);\n });\n }\n\n /**\n * Runs the provided function with the value of this Either if it is a Right, performs no operation if this is a Left.\n * If the result of the function is a Left, it will be returned, otherwise the original Right value will be returned.\n */\n flatTap<L2>(fn: (t: R) => Either<L2, void>): EitherP<L | L2, R> {\n return new EitherP(this.value.then((e) => e.flatTap(fn)));\n }\n\n flatTapAsync<L2>(fn: (t: R) => PromiseLike<Either<L2, void>>): EitherP<L | L2, R> {\n return this.flatMapAsync(async (value) => (await fn(value)).map(() => value));\n }\n\n /**\n * Flat maps the value of this Either if it is a Right, performs no operation if this is a Left.\n */\n flatMap<L2, R2>(fn: (t: R) => Either<L2, R2>): EitherP<L | L2, R2> {\n return new EitherP(this.value.then((e) => e.flatMap(fn)));\n }\n\n flatMapAsync<L2, R2>(fn: (t: R) => PromiseLike<Either<L2, R2>>): EitherP<L | L2, R2> {\n return new EitherP(\n this.value.then<Either<L | L2, R2>>((e) =>\n e.fold(\n async (left) => Either.left(left),\n (right) => fn(right),\n ),\n ),\n );\n }\n\n /**\n * Applies the provided functions to the value of this Either, depending on whether it is a Right or a Left and returns the result.\n */\n async fold<U1, U2>(leftFn: (l: L) => U1, rightFn: (r: R) => U2): Promise<U1 | U2> {\n return (await this.value).fold(leftFn, rightFn);\n }\n\n /**\n * Allows to deconstruct the Either using:\n *\n * @example\n *\n * ```ts\n * declare const e: EitherP<string, number>;\n *\n * const [left, right] = await e.pair();\n * ```\n *\n */\n async pair(): Promise<[Maybe<L>, Maybe<R>]> {\n return [await this.getLeft(), await this.get()];\n }\n\n /**\n * Returns true if this Either is a Right, false otherwise.\n */\n async isRight(): Promise<boolean> {\n return (await this.value).isRight();\n }\n\n /**\n * Returns true if this Either is a Left, false otherwise.\n */\n async isLeft(): Promise<boolean> {\n return !(await this.isRight());\n }\n\n /**\n * Gets the right value if this is a Right or undefined if it's a Left.\n */\n async get(): Promise<Maybe<R>> {\n return (await this.value).get();\n }\n\n /**\n * Returns the left value if this is a Left or undefined if it's a Right.\n */\n async getLeft(): Promise<Maybe<L>> {\n return (await this.value).getLeft();\n }\n\n /**\n * Gets the right value if this is a Right or the result of the provided function if it's a Left.\n */\n async getOrElse<T>(defaultValue: (l: L) => T): Promise<R | T> {\n return (await this.value).getOrElse(defaultValue);\n }\n\n /**\n * Creates a new Left EitherP with the provided value.\n */\n static left<L>(value: Promise<L>): EitherP<L, never> {\n return new EitherP(value.then((v) => Either.left(v)));\n }\n\n /**\n * Creates a new Right EitherP with the provided value.\n */\n static right<R>(value: Promise<R>): EitherP<never, R> {\n return new EitherP(value.then((v) => Either.right(v)));\n }\n\n static fromMaybe<L, R>(value: Promise<Maybe<R>>, leftValue: () => Promise<L>): EitherP<L, R> {\n return new EitherP(\n value.then(async (v) => (isDefined(v) ? Either.right(v) : Either.left(await leftValue()))),\n );\n }\n\n /**\n * @deprecated\n */\n static fromEither<L, R>(either: PromiseLike<Either<L, R>>): EitherP<L, R> {\n return new EitherP(either);\n }\n\n static fromPromise<L, R>(either: PromiseLike<Either<L, R>>): EitherP<L, R> {\n return new EitherP(either);\n }\n\n static cond<L, R>(\n bool: Promise<boolean>,\n rightFn: () => Promise<R>,\n leftFn: () => Promise<L>,\n ): EitherP<L, R> {\n return new EitherP(\n bool.then(async (b) => (b ? Either.right(await rightFn()) : Either.left(await leftFn()))),\n );\n }\n\n // oxlint-disable-next-line unicorn/no-thenable -- Deliberately implementing PromiseLike to allow for `await` usage on EitherP instances.\n then<U>(onfulfilled?: (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,WAAW;CACpC;AACF"}
|
package/package.json
CHANGED
|
@@ -1,7 +1,12 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@jeengbe/prelude",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.1",
|
|
4
4
|
"license": "MIT",
|
|
5
|
+
"repository": {
|
|
6
|
+
"type": "git",
|
|
7
|
+
"url": "git+https://github.com/jeengbe/ts-packages.git",
|
|
8
|
+
"directory": "packages/prelude"
|
|
9
|
+
},
|
|
5
10
|
"files": [
|
|
6
11
|
"dist",
|
|
7
12
|
"src",
|
|
@@ -9,6 +14,8 @@
|
|
|
9
14
|
],
|
|
10
15
|
"type": "module",
|
|
11
16
|
"sideEffects": false,
|
|
17
|
+
"main": "./dist/index.mjs",
|
|
18
|
+
"typings": "./dist/index.d.mts",
|
|
12
19
|
"exports": {
|
|
13
20
|
".": {
|
|
14
21
|
"types": "./dist/index.d.mts",
|
|
@@ -24,12 +31,11 @@
|
|
|
24
31
|
"tsdown": "^0.22.14",
|
|
25
32
|
"typescript": "^7.0.2",
|
|
26
33
|
"vitest": "^4.1.10",
|
|
27
|
-
"@jeengbe/scaffolding": "0.0.
|
|
34
|
+
"@jeengbe/scaffolding": "0.0.1"
|
|
28
35
|
},
|
|
29
36
|
"scripts": {
|
|
30
37
|
"build": "tsdown",
|
|
31
38
|
"lint": "oxlint --fix . && oxfmt --write .",
|
|
32
|
-
"test": "vitest"
|
|
33
|
-
"typecheck": "tsc"
|
|
39
|
+
"test": "vitest"
|
|
34
40
|
}
|
|
35
41
|
}
|