@jeengbe/prelude 0.0.5 → 0.1.0
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/LICENSE +21 -0
- package/dist/either.d.mts +2 -2
- package/dist/either.d.mts.map +1 -1
- package/dist/either.mjs +4 -4
- package/dist/either.mjs.map +1 -1
- package/dist/maybe.mjs +6 -10
- package/dist/maybe.mjs.map +1 -1
- package/package.json +37 -15
- package/src/either.ts +2 -2
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Jesper Engberg
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/dist/either.d.mts
CHANGED
|
@@ -113,7 +113,7 @@ declare namespace Either {
|
|
|
113
113
|
*/
|
|
114
114
|
function right<R>(value: R): Either<never, R>;
|
|
115
115
|
function fromMaybe<L, R>(value: Maybe<R>, leftValue: () => L): Either<L, R>;
|
|
116
|
-
function cond<L, R>(bool: boolean,
|
|
116
|
+
function cond<L, R>(bool: boolean, leftFn: () => L, rightFn: () => R): Either<L, R>;
|
|
117
117
|
}
|
|
118
118
|
/**
|
|
119
119
|
* Mimics the [Cats EitherT[Future, L, R]](https://typelevel.org/cats/datatypes/eithert.html) type.
|
|
@@ -208,7 +208,7 @@ declare class EitherP<L, R> implements PromiseLike<Either<L, R>> {
|
|
|
208
208
|
*/
|
|
209
209
|
static fromEither<L, R>(either: PromiseLike<Either<L, R>>): EitherP<L, R>;
|
|
210
210
|
static fromPromise<L, R>(either: PromiseLike<Either<L, R>>): EitherP<L, R>;
|
|
211
|
-
static cond<L, R>(bool: Promise<boolean>,
|
|
211
|
+
static cond<L, R>(bool: Promise<boolean>, leftFn: () => Promise<L>, rightFn: () => Promise<R>): EitherP<L, R>;
|
|
212
212
|
then<U>(onfulfilled?: (value: Either<L, R>) => U | PromiseLike<U>): PromiseLike<U>;
|
|
213
213
|
}
|
|
214
214
|
//#endregion
|
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,K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AAK,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,cAAc,QAAQ,IACtB,eAAe,QAAQ,KACtB,QAAQ,GAAG;EAOd,KAAK,GAAG,eAAe,OAAO,OAAO,GAAG,OAAO,IAAI,YAAY,KAAK,YAAY"}
|
package/dist/either.mjs
CHANGED
|
@@ -100,13 +100,13 @@ var EitherBase = class {
|
|
|
100
100
|
* Gets the right value if this is a Right or undefined if it's a Left.
|
|
101
101
|
*/
|
|
102
102
|
get() {
|
|
103
|
-
return this.fold(() =>
|
|
103
|
+
return this.fold(() => void 0, (r) => r);
|
|
104
104
|
}
|
|
105
105
|
/**
|
|
106
106
|
* Returns the left value if this is a Left or undefined if it's a Right.
|
|
107
107
|
*/
|
|
108
108
|
getLeft() {
|
|
109
|
-
return this.fold((l) => l, () =>
|
|
109
|
+
return this.fold((l) => l, () => void 0);
|
|
110
110
|
}
|
|
111
111
|
/**
|
|
112
112
|
* Gets the right value if this is a Right or the result of the provided function if it's a Left.
|
|
@@ -158,7 +158,7 @@ let Either;
|
|
|
158
158
|
return isDefined(value) ? Either.right(value) : Either.left(leftValue());
|
|
159
159
|
}
|
|
160
160
|
_Either.fromMaybe = fromMaybe;
|
|
161
|
-
function cond(bool,
|
|
161
|
+
function cond(bool, leftFn, rightFn) {
|
|
162
162
|
return bool ? Either.right(rightFn()) : Either.left(leftFn());
|
|
163
163
|
}
|
|
164
164
|
_Either.cond = cond;
|
|
@@ -313,7 +313,7 @@ var EitherP = class EitherP {
|
|
|
313
313
|
static fromPromise(either) {
|
|
314
314
|
return new EitherP(either);
|
|
315
315
|
}
|
|
316
|
-
static cond(bool,
|
|
316
|
+
static cond(bool, leftFn, rightFn) {
|
|
317
317
|
return new EitherP(bool.then(async (b) => b ? Either.right(await rightFn()) : Either.left(await leftFn())));
|
|
318
318
|
}
|
|
319
319
|
then(onfulfilled) {
|
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\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?: (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;CACxD;CAEA,SAAa,IAA2C;EACtD,OAAO,QAAQ,YAAY,gBAAgB,IAAI,CAAC,CAAC,CAAC,SAAS,EAAE;CAC/D;;;;CAKA,YAAoB,IAAkD;EACpE,OAAO,KAAK,MACT,MAAM,GAAG,CAAC,IACV,MAAM,OAAO,MAAM,CAAC,CACvB;CACF;CAEA,iBAAyB,IAAgE;EACvF,OAAO,QAAQ,YAAY,gBAAgB,IAAI,CAAC,CAAC,CAAC,iBAAiB,EAAE;CACvE;;;;CAKA,QAAY,IAAiC;EAC3C,OAAO,KAAK,MACT,MAAM,OAAO,KAAK,GAAG,CAAC,CAAC,IACvB,MAAM,OAAO,MAAM,CAAC,CACvB;CACF;CAEA,aAAiB,IAA2C;EAC1D,OAAO,QAAQ,YAAY,gBAAgB,IAAI,CAAC,CAAC,CAAC,aAAa,EAAE;CACnE;;;;CAKA,MAAc,QAAsB,SAAuC;EACzE,OAAO,KAAK,MACT,MAAM,OAAO,KAAK,OAAO,CAAC,CAAC,IAC3B,MAAM,OAAO,MAAM,QAAQ,CAAC,CAAC,C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AC,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;CAGA,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, leftFn: () => L, rightFn: () => R): 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 leftFn: () => Promise<L>,\n rightFn: () => Promise<R>,\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?: (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;CACxD;CAEA,SAAa,IAA2C;EACtD,OAAO,QAAQ,YAAY,gBAAgB,IAAI,CAAC,CAAC,CAAC,SAAS,EAAE;CAC/D;;;;CAKA,YAAoB,IAAkD;EACpE,OAAO,KAAK,MACT,MAAM,GAAG,CAAC,IACV,MAAM,OAAO,MAAM,CAAC,CACvB;CACF;CAEA,iBAAyB,IAAgE;EACvF,OAAO,QAAQ,YAAY,gBAAgB,IAAI,CAAC,CAAC,CAAC,iBAAiB,EAAE;CACvE;;;;CAKA,QAAY,IAAiC;EAC3C,OAAO,KAAK,MACT,MAAM,OAAO,KAAK,GAAG,CAAC,CAAC,IACvB,MAAM,OAAO,MAAM,CAAC,CACvB;CACF;CAEA,aAAiB,IAA2C;EAC1D,OAAO,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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,QACA,SACe;EACf,OAAO,IAAI,QACT,KAAK,KAAK,OAAO,MAAO,IAAI,OAAO,MAAM,MAAM,QAAQ,CAAC,IAAI,OAAO,KAAK,MAAM,OAAO,CAAC,CAAE,CAC1F;CACF;CAGA,KAAQ,aAA2E;EACjF,OAAO,KAAK,MAAM,KAAK,WAAW;CACpC;AACF"}
|
package/dist/maybe.mjs
CHANGED
|
@@ -1,29 +1,25 @@
|
|
|
1
1
|
//#region src/maybe.ts
|
|
2
2
|
function mapMaybe(value, fn) {
|
|
3
|
-
return value ===
|
|
3
|
+
return value === void 0 ? void 0 : fn(value);
|
|
4
4
|
}
|
|
5
5
|
function isDefined(value) {
|
|
6
|
-
return value !==
|
|
6
|
+
return value !== void 0;
|
|
7
7
|
}
|
|
8
8
|
function flattenMaybe(value) {
|
|
9
9
|
return mapMaybe(value, (v) => v);
|
|
10
10
|
}
|
|
11
11
|
function ifTrue(value, fn) {
|
|
12
|
-
return value ? fn() :
|
|
12
|
+
return value ? fn() : void 0;
|
|
13
13
|
}
|
|
14
14
|
function ifFalse(value, fn) {
|
|
15
|
-
return !value ? fn() :
|
|
15
|
+
return !value ? fn() : void 0;
|
|
16
16
|
}
|
|
17
17
|
function matchPair([a, b], cases) {
|
|
18
18
|
if (!isDefined(a)) {
|
|
19
|
-
if (!isDefined(b))
|
|
20
|
-
return cases.neither();
|
|
21
|
-
}
|
|
19
|
+
if (!isDefined(b)) return cases.neither();
|
|
22
20
|
return cases.b(b);
|
|
23
21
|
}
|
|
24
|
-
if (!isDefined(b))
|
|
25
|
-
return cases.a(a);
|
|
26
|
-
}
|
|
22
|
+
if (!isDefined(b)) return cases.a(a);
|
|
27
23
|
return cases.both(a, b);
|
|
28
24
|
}
|
|
29
25
|
|
package/dist/maybe.mjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"maybe.mjs","names":[],"sources":["../src/maybe.ts"],"sourcesContent":["export type Maybe<T> = T | undefined;\n\nexport function mapMaybe<T, U>(value: Maybe<T>, fn: (value: T) => U): Maybe<U> {\n return value === undefined ? undefined : fn(value);\n}\n\nexport function isDefined<T>(value: Maybe<T>): value is T {\n return value !== undefined;\n}\n\nexport function flattenMaybe<T>(value: Maybe<Maybe<T>>): Maybe<T> {\n return mapMaybe(value, (v) => v);\n}\n\nexport function ifTrue<T>(value: boolean, fn: () => T): Maybe<T> {\n return value ? fn() : undefined;\n}\n\nexport function ifFalse<T>(value: boolean, fn: () => T): Maybe<T> {\n return !value ? fn() : undefined;\n}\n\nexport function matchPair<A, B, R>(\n [a, b]: [Maybe<A>, Maybe<B>],\n cases: {\n neither: () => R;\n a: (a: A) => R;\n b: (b: B) => R;\n both: (a: A, b: B) => R;\n },\n): R {\n if (!isDefined(a)) {\n if (!isDefined(b)) {\n return cases.neither();\n }\n\n return cases.b(b);\n }\n\n if (!isDefined(b)) {\n return cases.a(a);\n }\n\n return cases.both(a, b);\n}\n"],"mappings":";AAEA,SAAgB,SAAe,OAAiB,IAA+B;CAC7E,OAAO,UAAU,
|
|
1
|
+
{"version":3,"file":"maybe.mjs","names":[],"sources":["../src/maybe.ts"],"sourcesContent":["export type Maybe<T> = T | undefined;\n\nexport function mapMaybe<T, U>(value: Maybe<T>, fn: (value: T) => U): Maybe<U> {\n return value === undefined ? undefined : fn(value);\n}\n\nexport function isDefined<T>(value: Maybe<T>): value is T {\n return value !== undefined;\n}\n\nexport function flattenMaybe<T>(value: Maybe<Maybe<T>>): Maybe<T> {\n return mapMaybe(value, (v) => v);\n}\n\nexport function ifTrue<T>(value: boolean, fn: () => T): Maybe<T> {\n return value ? fn() : undefined;\n}\n\nexport function ifFalse<T>(value: boolean, fn: () => T): Maybe<T> {\n return !value ? fn() : undefined;\n}\n\nexport function matchPair<A, B, R>(\n [a, b]: [Maybe<A>, Maybe<B>],\n cases: {\n neither: () => R;\n a: (a: A) => R;\n b: (b: B) => R;\n both: (a: A, b: B) => R;\n },\n): R {\n if (!isDefined(a)) {\n if (!isDefined(b)) {\n return cases.neither();\n }\n\n return cases.b(b);\n }\n\n if (!isDefined(b)) {\n return cases.a(a);\n }\n\n return cases.both(a, b);\n}\n"],"mappings":";AAEA,SAAgB,SAAe,OAAiB,IAA+B;CAC7E,OAAO,UAAU,SAAY,SAAY,GAAG,KAAK;AACnD;AAEA,SAAgB,UAAa,OAA6B;CACxD,OAAO,UAAU;AACnB;AAEA,SAAgB,aAAgB,OAAkC;CAChE,OAAO,SAAS,QAAQ,MAAM,CAAC;AACjC;AAEA,SAAgB,OAAU,OAAgB,IAAuB;CAC/D,OAAO,QAAQ,GAAG,IAAI;AACxB;AAEA,SAAgB,QAAW,OAAgB,IAAuB;CAChE,OAAO,CAAC,QAAQ,GAAG,IAAI;AACzB;AAEA,SAAgB,UACd,CAAC,GAAG,IACJ,OAMG;CACH,IAAI,CAAC,UAAU,CAAC,GAAG;EACjB,IAAI,CAAC,UAAU,CAAC,GACd,OAAO,MAAM,QAAQ;EAGvB,OAAO,MAAM,EAAE,CAAC;CAClB;CAEA,IAAI,CAAC,UAAU,CAAC,GACd,OAAO,MAAM,EAAE,CAAC;CAGlB,OAAO,MAAM,KAAK,GAAG,CAAC;AACxB"}
|
package/package.json
CHANGED
|
@@ -1,38 +1,60 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@jeengbe/prelude",
|
|
3
|
-
"version": "0.0
|
|
4
|
-
"
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "A small, dependency-free functional programming toolkit for TypeScript.",
|
|
5
|
+
"keywords": [
|
|
6
|
+
"either",
|
|
7
|
+
"fp",
|
|
8
|
+
"functional-programming",
|
|
9
|
+
"maybe",
|
|
10
|
+
"monad",
|
|
11
|
+
"option",
|
|
12
|
+
"result",
|
|
13
|
+
"typescript"
|
|
14
|
+
],
|
|
15
|
+
"homepage": "https://github.com/jeengbe/ts-packages/tree/master/packages/prelude#readme",
|
|
16
|
+
"bugs": {
|
|
17
|
+
"url": "https://github.com/jeengbe/ts-packages/issues"
|
|
18
|
+
},
|
|
5
19
|
"repository": {
|
|
6
20
|
"type": "git",
|
|
7
21
|
"url": "git+https://github.com/jeengbe/ts-packages.git",
|
|
8
22
|
"directory": "packages/prelude"
|
|
9
23
|
},
|
|
10
|
-
"
|
|
11
|
-
|
|
12
|
-
"
|
|
13
|
-
"
|
|
14
|
-
"
|
|
15
|
-
|
|
16
|
-
"type": "module",
|
|
24
|
+
"license": "MIT",
|
|
25
|
+
"author": {
|
|
26
|
+
"name": "Jesper Engberg",
|
|
27
|
+
"email": "jeengbe@gmail.com",
|
|
28
|
+
"url": "https://github.com/jeengbe"
|
|
29
|
+
},
|
|
17
30
|
"sideEffects": false,
|
|
18
|
-
"
|
|
19
|
-
"typings": "./dist/index.d.mts",
|
|
31
|
+
"type": "module",
|
|
20
32
|
"exports": {
|
|
21
33
|
".": {
|
|
22
34
|
"types": "./dist/index.d.mts",
|
|
23
35
|
"default": "./dist/index.mjs"
|
|
24
36
|
}
|
|
25
37
|
},
|
|
26
|
-
"
|
|
27
|
-
|
|
28
|
-
|
|
38
|
+
"main": "./dist/index.mjs",
|
|
39
|
+
"typings": "./dist/index.d.mts",
|
|
40
|
+
"files": [
|
|
41
|
+
"dist",
|
|
42
|
+
"src",
|
|
43
|
+
"!src/**/*.spec.ts",
|
|
44
|
+
"!src/__snapshots__/*",
|
|
45
|
+
"!src/**/fake.ts"
|
|
46
|
+
],
|
|
29
47
|
"devDependencies": {
|
|
30
48
|
"oxfmt": "^0.60.0",
|
|
31
49
|
"oxlint": "^1.75.0",
|
|
32
50
|
"tsdown": "^0.22.14",
|
|
33
51
|
"typescript": "^7.0.2",
|
|
34
52
|
"vitest": "^4.1.10",
|
|
35
|
-
"@jeengbe/scaffolding": "0.0.
|
|
53
|
+
"@jeengbe/scaffolding": "0.0.0"
|
|
54
|
+
},
|
|
55
|
+
"publishConfig": {
|
|
56
|
+
"access": "public",
|
|
57
|
+
"provenance": true
|
|
36
58
|
},
|
|
37
59
|
"scripts": {
|
|
38
60
|
"build": "tsdown",
|
package/src/either.ts
CHANGED
|
@@ -245,7 +245,7 @@ export namespace Either {
|
|
|
245
245
|
return isDefined(value) ? Either.right(value) : Either.left(leftValue());
|
|
246
246
|
}
|
|
247
247
|
|
|
248
|
-
export function cond<L, R>(bool: boolean,
|
|
248
|
+
export function cond<L, R>(bool: boolean, leftFn: () => L, rightFn: () => R): Either<L, R> {
|
|
249
249
|
return bool ? Either.right(rightFn()) : Either.left(leftFn());
|
|
250
250
|
}
|
|
251
251
|
}
|
|
@@ -462,8 +462,8 @@ export class EitherP<L, R> implements PromiseLike<Either<L, R>> {
|
|
|
462
462
|
|
|
463
463
|
static cond<L, R>(
|
|
464
464
|
bool: Promise<boolean>,
|
|
465
|
-
rightFn: () => Promise<R>,
|
|
466
465
|
leftFn: () => Promise<L>,
|
|
466
|
+
rightFn: () => Promise<R>,
|
|
467
467
|
): EitherP<L, R> {
|
|
468
468
|
return new EitherP(
|
|
469
469
|
bool.then(async (b) => (b ? Either.right(await rightFn()) : Either.left(await leftFn()))),
|