From 768fcb5a3202136ffd3b66830c9e6e821e0ef553 Mon Sep 17 00:00:00 2001 From: Ilia Abedianamiri <37903573+iliaamiri@users.noreply.github.com> Date: Wed, 2 Oct 2024 11:27:18 -0700 Subject: [PATCH 1/4] fix: promise static method types --- src/utils.type.ts | 101 ++++++++++++++++++++++++++++------------------ 1 file changed, 62 insertions(+), 39 deletions(-) diff --git a/src/utils.type.ts b/src/utils.type.ts index 4477c6e..a0ef281 100644 --- a/src/utils.type.ts +++ b/src/utils.type.ts @@ -42,16 +42,44 @@ export interface PromiseStaticMethods { * @template T The type of the resolved values * @returns {Promise>} */ - all( - values: Iterable> - ): Promise< + all(values: Iterable>): Promise< + TEitherMode extends "standard" + ?Awaited[]> + : TEitherMode extends "tuple" + ? Awaited[]> + : TEitherMode extends "go" + ? Awaited[]> + : Awaited[]> + >; + /** + * Wraps a Promise.all call in a mightFail function. + * @param values + * @template T The type of the resolved values + * @returns {Promise>} + */ + all(values: T): Promise<{ -readonly [P in keyof T]: TEitherMode extends "standard" - ? StandardEither - : TEitherMode extends "tuple" - ? TupleEither - : TEitherMode extends "go" - ? GoEither - : AnyEither + ? StandardEither> + : TEitherMode extends "tuple" + ? TupleEither> + : TEitherMode extends "go" + ? GoEither> + : AnyEither>; }>; + + /** + * Wraps a Promise.race call in a mightFail function. + * @param values + * @template T The type of the resolved values + * @returns {Promise>} + */ + race(values: Iterable>): Promise< + TEitherMode extends "standard" + ? Awaited> + : TEitherMode extends "tuple" + ? Awaited> + : TEitherMode extends "go" + ?Awaited> + : Awaited> >; /** * Wraps a Promise.race call in a mightFail function. @@ -59,49 +87,44 @@ export interface PromiseStaticMethods { * @template T The type of the resolved values * @returns {Promise>} */ - race( - values: Iterable> - ): Promise< - TEitherMode extends "standard" - ? StandardEither + race(values: T): Promise< + TEitherMode extends "standard" + ? Awaited> : TEitherMode extends "tuple" - ? TupleEither + ? Awaited> : TEitherMode extends "go" - ? GoEither - : AnyEither + ? Awaited> + : Awaited> >; + /** * Wraps a Promise.allSettled call in a mightFail function. * @param values * @template T The type of the resolved values * @returns {Promise[]>>} */ - allSettled( - values: Iterable> - ): Promise< - TEitherMode extends "standard" - ? StandardEither[]> - : TEitherMode extends "tuple" - ? TupleEither[]> - : TEitherMode extends "go" - ? GoEither[]> - : AnyEither[]> - >; + allSettled(values: T): Promise<{ -readonly [P in keyof T]: TEitherMode extends "standard" ? StandardEither>> : TEitherMode extends "tuple" ? TupleEither>> : TEitherMode extends "go" ? GoEither>> : AnyEither>> ; }>; + + /** + * Wraps a Promise.allSettled call in a mightFail function. + * @param values + * @template T The type of the resolved values + * @returns {Promise[]>>} + */ + allSettled(values: Iterable>): Promise>[]>; + /** * Wraps a Promise.any call in a mightFail function. * @param values * @template T The type of the resolved values * @returns {Promise>} */ - any( - values: Iterable> - ): Promise< - TEitherMode extends "standard" - ? StandardEither - : TEitherMode extends "tuple" - ? TupleEither - : TEitherMode extends "go" - ? GoEither - : AnyEither - >; + any(values: T): Promise> : TEitherMode extends "tuple" ? TupleEither> : TEitherMode extends "go" ? GoEither> : AnyEither>>; + /** + * Wraps a Promise.any call in a mightFail function. + * @param values + * @template T The type of the resolved values + * @returns {Promise>} + */ + any(values: Iterable>): Promise> : TEitherMode extends "tuple" ? TupleEither> : TEitherMode extends "go" ? GoEither> : AnyEither>>; } From 9141d91e864e2507faba324d8e7f866f171cc142 Mon Sep 17 00:00:00 2001 From: Ilia Abedianamiri <37903573+iliaamiri@users.noreply.github.com> Date: Wed, 2 Oct 2024 12:16:28 -0700 Subject: [PATCH 2/4] fix: return types of Promise static methods --- src/utils.type.ts | 236 +++++++++++++++++++---------------- test/go/mightFail.test.ts | 2 +- test/tuple/mightFail.test.ts | 2 +- 3 files changed, 132 insertions(+), 108 deletions(-) diff --git a/src/utils.type.ts b/src/utils.type.ts index a0ef281..208a0a2 100644 --- a/src/utils.type.ts +++ b/src/utils.type.ts @@ -1,130 +1,154 @@ -import type { Either as StandardEither } from "./Either"; -import type { Either as TupleEither } from "./tuple"; -import type { Either as GoEither } from "./go"; +import type {Either as StandardEither} from "./Either"; +import type {Either as TupleEither} from "./tuple"; +import type {Either as GoEither} from "./go"; export type EitherMode = "standard" | "tuple" | "go" | "any"; export type AnyEither = StandardEither | TupleEither | GoEither; export type MightFailFunction = ( - promise: Promise + promise: Promise ) => Promise< - TEitherMode extends "standard" - ? StandardEither - : TEitherMode extends "tuple" - ? TupleEither - : TEitherMode extends "go" - ? GoEither - : AnyEither + TEitherMode extends "standard" + ? StandardEither + : TEitherMode extends "tuple" + ? TupleEither + : TEitherMode extends "go" + ? GoEither + : AnyEither >; export type PromiseFulfilledResult = { - status: "fulfilled"; - value: T; + status: "fulfilled"; + value: T; }; export type PromiseRejectedResult = { - status: "rejected"; - reason: any; + status: "rejected"; + reason: any; }; export type PromiseSettledResult = - | PromiseFulfilledResult - | PromiseRejectedResult; + | PromiseFulfilledResult + | PromiseRejectedResult; export type MightFail< - TEitherMode extends EitherMode, - TPromiseStaticMethods = PromiseStaticMethods + TEitherMode extends EitherMode, + TPromiseStaticMethods = PromiseStaticMethods > = MightFailFunction & TPromiseStaticMethods; export interface PromiseStaticMethods { - /** - * Wraps a Promise.all call in a mightFail function. - * @param values - * @template T The type of the resolved values - * @returns {Promise>} - */ - all(values: Iterable>): Promise< - TEitherMode extends "standard" - ?Awaited[]> - : TEitherMode extends "tuple" - ? Awaited[]> - : TEitherMode extends "go" - ? Awaited[]> - : Awaited[]> - >; - /** - * Wraps a Promise.all call in a mightFail function. - * @param values - * @template T The type of the resolved values - * @returns {Promise>} - */ - all(values: T): Promise<{ -readonly [P in keyof T]: - TEitherMode extends "standard" - ? StandardEither> - : TEitherMode extends "tuple" - ? TupleEither> - : TEitherMode extends "go" - ? GoEither> - : AnyEither>; }>; + /** + * Wraps a Promise.all call in a mightFail function. + * @params values + * @template T The type of the resolved values + * @return {Promise} - Promise>> + */ + all(values: Iterable>): Promise< + TEitherMode extends "standard" + ? Awaited> + : TEitherMode extends "tuple" + ? Awaited> + : TEitherMode extends "go" + ? Awaited> + : Awaited> + >; + + /** + * Wraps a Promise.all call in a mightFail function. + * @params values + * @template T The type of the resolved values + * @return {Promise} - Promise; }>> + */ + all(values: T): + Promise< + TEitherMode extends "standard" + ? StandardEither<{ -readonly [P in keyof T]: Awaited; }> + : TEitherMode extends "tuple" + ? TupleEither<{ -readonly [P in keyof T]: Awaited; }> + : TEitherMode extends "go" + ? GoEither<{ -readonly [P in keyof T]: Awaited; }> + : AnyEither<{ -readonly [P in keyof T]: Awaited; }> + >; + + /** + * Wraps a Promise.race call in a mightFail function. + * + * @params values - An iterable of promises + * @template T The type of the resolved values + * @return {Promise} - Promise>> + */ + race(values: Iterable>): Promise< + TEitherMode extends "standard" + ? Awaited> + : TEitherMode extends "tuple" + ? Awaited> + : TEitherMode extends "go" + ? Awaited> + : Awaited> + >; + + /** + * Wraps a Promise.race call in a mightFail function. + * @params values + * @template T The type of the resolved values + * @return {Promise} - Promise>> + */ + race(values: T): Promise< + TEitherMode extends "standard" + ? Awaited> + : TEitherMode extends "tuple" + ? Awaited> + : TEitherMode extends "go" + ? Awaited> + : Awaited> + >; - /** - * Wraps a Promise.race call in a mightFail function. - * @param values - * @template T The type of the resolved values - * @returns {Promise>} - */ - race(values: Iterable>): Promise< - TEitherMode extends "standard" - ? Awaited> - : TEitherMode extends "tuple" - ? Awaited> - : TEitherMode extends "go" - ?Awaited> - : Awaited> - >; - /** - * Wraps a Promise.race call in a mightFail function. - * @param values - * @template T The type of the resolved values - * @returns {Promise>} - */ - race(values: T): Promise< - TEitherMode extends "standard" - ? Awaited> - : TEitherMode extends "tuple" - ? Awaited> - : TEitherMode extends "go" - ? Awaited> - : Awaited> - >; + /** + * Wraps a Promise.allSettled call in a mightFail function. + * @params values + * @template T The type of the resolved values + * @return {Promise} - Promise>}>> + */ + allSettled(values: T): Promise< + TEitherMode extends "standard" + ? StandardEither<{ -readonly [P in keyof T]: PromiseSettledResult> }> + : TEitherMode extends "tuple" + ? TupleEither<{ -readonly [P in keyof T]: PromiseSettledResult> }> + : TEitherMode extends "go" + ? GoEither<{ -readonly [P in keyof T]: PromiseSettledResult> }> + : AnyEither<{ -readonly [P in keyof T]: PromiseSettledResult> }> + >; - /** - * Wraps a Promise.allSettled call in a mightFail function. - * @param values - * @template T The type of the resolved values - * @returns {Promise[]>>} - */ - allSettled(values: T): Promise<{ -readonly [P in keyof T]: TEitherMode extends "standard" ? StandardEither>> : TEitherMode extends "tuple" ? TupleEither>> : TEitherMode extends "go" ? GoEither>> : AnyEither>> ; }>; + /** + * Wraps a Promise.allSettled call in a mightFail function. + * @params values + * @template T The type of the resolved values + * @return {Promise} - Promise>[]>> + */ + allSettled(values: Iterable>): Promise< + TEitherMode extends "standard" + ? StandardEither>[]> + : TEitherMode extends "tuple" + ? TupleEither>[]> + : TEitherMode extends "go" + ? GoEither>[]> + : AnyEither>[]> + >; - /** - * Wraps a Promise.allSettled call in a mightFail function. - * @param values - * @template T The type of the resolved values - * @returns {Promise[]>>} - */ - allSettled(values: Iterable>): Promise>[]>; + /** + * Wraps a Promise.any call in a mightFail function. + * + * @params values + * @template T The type of the resolved values + * @return {Promise} - Promise>> + */ + any(values: T): Promise> : TEitherMode extends "tuple" ? TupleEither> : TEitherMode extends "go" ? GoEither> : AnyEither>>; - /** - * Wraps a Promise.any call in a mightFail function. - * @param values - * @template T The type of the resolved values - * @returns {Promise>} - */ - any(values: T): Promise> : TEitherMode extends "tuple" ? TupleEither> : TEitherMode extends "go" ? GoEither> : AnyEither>>; - /** - * Wraps a Promise.any call in a mightFail function. - * @param values - * @template T The type of the resolved values - * @returns {Promise>} - */ - any(values: Iterable>): Promise> : TEitherMode extends "tuple" ? TupleEither> : TEitherMode extends "go" ? GoEither> : AnyEither>>; + /** + * Wraps a Promise.any call in a mightFail function. + * + * @params values + * @template T The type of the resolved values + * @return {Promise} - Promise>> + */ + any(values: Iterable>): Promise> : TEitherMode extends "tuple" ? TupleEither> : TEitherMode extends "go" ? GoEither> : AnyEither>>; } diff --git a/test/go/mightFail.test.ts b/test/go/mightFail.test.ts index 35b5ab6..5d55949 100644 --- a/test/go/mightFail.test.ts +++ b/test/go/mightFail.test.ts @@ -119,7 +119,7 @@ describe('promise concurrent method wrappers', () => { const promises = [Promise.reject(new Error('Error 1')), Promise.reject(new Error('Error 2'))]; const [result, error] = await mightFail.any(promises); expect(result).toBeUndefined(); - expect(error).toBeInstanceOf(AggregateError); + expect(error).toBeInstanceOf(Error); expect(error!.message).toBe('All promises were rejected'); }); }); diff --git a/test/tuple/mightFail.test.ts b/test/tuple/mightFail.test.ts index fd2c3f7..f7f9402 100644 --- a/test/tuple/mightFail.test.ts +++ b/test/tuple/mightFail.test.ts @@ -118,7 +118,7 @@ describe('promise concurrent method wrappers', () => { const promises = [Promise.reject(new Error('Error 1')), Promise.reject(new Error('Error 2'))]; const [error, result] = await mightFail.any(promises); expect(result).toBeUndefined(); - expect(error).toBeInstanceOf(AggregateError); + expect(error).toBeInstanceOf(Error); expect(error!.message).toBe('All promises were rejected'); }); }); From e6434ccd6d225a4fcf6c0b68f178fab4f5d8bb90 Mon Sep 17 00:00:00 2001 From: sam Date: Wed, 2 Oct 2024 11:12:14 -0700 Subject: [PATCH 3/4] add prettierrc --- .prettierrc | 6 ++++++ 1 file changed, 6 insertions(+) create mode 100644 .prettierrc diff --git a/.prettierrc b/.prettierrc new file mode 100644 index 0000000..a804590 --- /dev/null +++ b/.prettierrc @@ -0,0 +1,6 @@ +{ + "tabWidth": 2, + "useTabs": false, + "semi": false, + "printWidth": 120 +} From b3a5d4afa553d68fcf0c437e1985b1a2c86045db Mon Sep 17 00:00:00 2001 From: sam Date: Wed, 2 Oct 2024 14:20:28 -0700 Subject: [PATCH 4/4] update formatting --- examples/makeMightFailBasic.ts | 16 +- examples/mightFailBasic.ts | 14 +- examples/mightFailBasicGo.ts | 14 +- examples/mightFailBasicTuple.ts | 14 +- examples/mightFailStaticMethods.ts | 171 ++++++++------- src/Either.ts | 10 +- src/go/Either.ts | 2 +- src/go/index.ts | 13 +- src/go/makeMightFail.ts | 24 +-- src/go/mightFail.ts | 30 ++- src/index.ts | 10 +- src/makeMightFail.ts | 22 +- src/mightFail.ts | 36 ++-- src/tuple/Either.ts | 2 +- src/tuple/index.ts | 12 +- src/tuple/makeMightFail.ts | 24 +-- src/tuple/mightFail.ts | 30 ++- src/utils.ts | 34 +-- src/utils.type.ts | 297 +++++++++++++++------------ test/go/makeMightFail.test.ts | 11 +- test/go/makeMightFailSync.test.ts | 7 +- test/go/mightFail.test.ts | 211 ++++++++++--------- test/go/mightFailSync.test.ts | 44 ++-- test/makeMightFail.test.ts | 15 +- test/makeMightFailSync.test.ts | 13 +- test/mightFail.test.ts | 131 ++++++------ test/mightFailSync.test.ts | 44 ++-- test/tuple/makeMightFail.test.ts | 11 +- test/tuple/makeMightFailSync.test.ts | 7 +- test/tuple/mightFail.test.ts | 198 +++++++++--------- test/tuple/mightFailSync.test.ts | 44 ++-- 31 files changed, 772 insertions(+), 739 deletions(-) diff --git a/examples/makeMightFailBasic.ts b/examples/makeMightFailBasic.ts index f4801af..006e18f 100644 --- a/examples/makeMightFailBasic.ts +++ b/examples/makeMightFailBasic.ts @@ -1,14 +1,14 @@ -import { makeMightFail } from "../src/makeMightFail"; +import { makeMightFail } from "../src/makeMightFail" -const resolve = (value: { message: string }) => Promise.resolve(value); +const resolve = (value: { message: string }) => Promise.resolve(value) async function main() { - const func = makeMightFail(resolve); - const { error, result } = await func({ message: "success" }); + const func = makeMightFail(resolve) + const { error, result } = await func({ message: "success" }) if (error) { - console.error(error); - return; + console.error(error) + return } - console.log(result.message); + console.log(result.message) } -main(); +main() diff --git a/examples/mightFailBasic.ts b/examples/mightFailBasic.ts index 03a9955..13cdeb1 100644 --- a/examples/mightFailBasic.ts +++ b/examples/mightFailBasic.ts @@ -1,14 +1,12 @@ -import { mightFail } from "../src"; +import { mightFail } from "../src" async function main() { - const { error, result } = await mightFail( - Promise.resolve({ message: "success" }) - ); + const { error, result } = await mightFail(Promise.resolve({ message: "success" })) if (error) { - console.error(error); - return; + console.error(error) + return } - console.log(result.message); + console.log(result.message) } -main(); +main() diff --git a/examples/mightFailBasicGo.ts b/examples/mightFailBasicGo.ts index 700a6ed..d07b47c 100644 --- a/examples/mightFailBasicGo.ts +++ b/examples/mightFailBasicGo.ts @@ -1,14 +1,12 @@ -import { mightFail } from "../src/go"; +import { mightFail } from "../src/go" async function main() { - const [error, result] = await mightFail( - Promise.resolve({ message: "success" }) - ); + const [error, result] = await mightFail(Promise.resolve({ message: "success" })) if (error) { - console.error(error); - return; + console.error(error) + return } - console.log(result.message); + console.log(result.message) } -main(); +main() diff --git a/examples/mightFailBasicTuple.ts b/examples/mightFailBasicTuple.ts index b3148cb..382609c 100644 --- a/examples/mightFailBasicTuple.ts +++ b/examples/mightFailBasicTuple.ts @@ -1,14 +1,12 @@ -import { mightFail } from "../src/tuple"; +import { mightFail } from "../src/tuple" async function main() { - const [result, error] = await mightFail( - Promise.resolve({ message: "success" }) - ); + const [result, error] = await mightFail(Promise.resolve({ message: "success" })) if (error) { - console.error(error); - return; + console.error(error) + return } - console.log(result.message); + console.log(result.message) } -main(); +main() diff --git a/examples/mightFailStaticMethods.ts b/examples/mightFailStaticMethods.ts index b5befbb..36f4243 100644 --- a/examples/mightFailStaticMethods.ts +++ b/examples/mightFailStaticMethods.ts @@ -1,159 +1,190 @@ -import { mightFail } from "../src"; -import { mightFail as mightFailTuple } from "../src/tuple"; -import { mightFail as mightFailGo } from "../src/go"; +import { mightFail } from "../src" +import { mightFail as mightFailTuple } from "../src/tuple" +import { mightFail as mightFailGo } from "../src/go" async function all() { const { error, result } = await mightFail.all([ Promise.resolve({ message: "success" }), Promise.resolve({ message: "success2" }), - ]); + ]) if (error) { - console.error(error); - return; + console.error(error) + return } - console.log(result.map((r) => r.message)); + console.log(result.map((r) => r.message)) } -all(); +all() + +async function allDifferentTypes() { + const { error, result } = await mightFail.all([Promise.resolve({ message: "success" }), Promise.resolve(5)]) + if (error) { + console.error(error) + return + } + const [res1, res2] = result + console.log(res1, res2) +} + +allDifferentTypes() + +async function allDestructured() { + const { error, result: [result1, result2] = [] } = await mightFail.all([ + Promise.resolve({ message: "success" }), + Promise.resolve(5), + ]) + if (error) { + console.error(error) + return + } + console.log(result1, result2) +} + +allDestructured() async function allSettled() { const { error, result } = await mightFail.allSettled([ Promise.resolve({ message: "success" }), Promise.resolve({ message: "success2" }), - ]); + ]) if (error) { - console.error(error); - return; + console.error(error) + return } - console.log(result.map((r) => r.status)); + console.log(result.map((r) => r.status)) } -allSettled(); +allSettled() async function allTuple() { const [error, result] = await mightFailTuple.all([ Promise.resolve({ message: "success" }), Promise.resolve({ message: "success2" }), - ]); + ]) + if (error) { + console.error(error) + return + } + console.log(result.map((r) => r.message)) +} + +allTuple() + +async function allTupleDestructured() { + const [error, [result1, result2] = []] = await mightFailTuple.all([ + Promise.resolve({ message: "success" }), + Promise.resolve({ message: "success2" }), + ]) if (error) { - console.error(error); - return; + console.error(error) + return } - console.log(result.map((r) => r.message)); + console.log(result1, result2) } -allTuple(); +allTupleDestructured() async function allGo() { const [result, error] = await mightFailGo.all([ Promise.resolve({ message: "success" }), Promise.resolve({ message: "success2" }), - ]); + ]) if (error) { - console.error(error); - return; + console.error(error) + return } - console.log(result.map((r) => r.message)); + console.log(result.map((r) => r.message)) } -allGo(); +allGo() async function returnStringAfter(message: string, time: number) { - await new Promise((resolve) => setTimeout(resolve, time)); - return { message }; + await new Promise((resolve) => setTimeout(resolve, time)) + return { message } } async function race() { - const { error, result } = await mightFail.race([ - returnStringAfter("fast", 100), - returnStringAfter("slow", 200), - ]); + const { error, result } = await mightFail.race([returnStringAfter("fast", 100), returnStringAfter("slow", 200)]) if (error) { - console.error(error); - return; + console.error(error) + return } - console.log(result.message); + console.log(result.message) } -race(); +race() async function any() { const { error, result } = await mightFail.any([ Promise.reject(new Error("Failure 1")), returnStringAfter("success", 100), Promise.reject(new Error("Failure 2")), - ]); + ]) if (error) { - console.error(error); - return; + console.error(error) + return } - console.log(result.message); + console.log(result.message) } -any(); +any() async function raceTuple() { - const [error, result] = await mightFailTuple.race([ - returnStringAfter("fast", 100), - returnStringAfter("slow", 200), - ]); + const [error, result] = await mightFailTuple.race([returnStringAfter("fast", 100), returnStringAfter("slow", 200)]) if (error) { - console.error(error); - return; + console.error(error) + return } - console.log(result.message); + console.log(result.message) } -raceTuple(); +raceTuple() async function anyTuple() { const [error, result] = await mightFailTuple.any([ Promise.reject(new Error("Failure 1")), returnStringAfter("success", 100), Promise.reject(new Error("Failure 2")), - ]); + ]) if (error) { - console.error(error); - return; + console.error(error) + return } - console.log(result.message); + console.log(result.message) } -anyTuple(); +anyTuple() async function raceGo() { - const [result, error] = await mightFailGo.race([ - returnStringAfter("fast", 100), - returnStringAfter("slow", 200), - ]); + const [result, error] = await mightFailGo.race([returnStringAfter("fast", 100), returnStringAfter("slow", 200)]) if (error) { - console.error(error); - return; + console.error(error) + return } - console.log(result.message); + console.log(result.message) } -raceGo(); +raceGo() async function anyGo() { const [result, error] = await mightFailGo.any([ Promise.reject(new Error("Failure 1")), returnStringAfter("success", 100), Promise.reject(new Error("Failure 2")), - ]); + ]) if (error) { - console.error(error); - return; + console.error(error) + return } - console.log(result.message); + console.log(result.message) } -anyGo(); +anyGo() // Call all the new functions -race(); -any(); -raceTuple(); -anyTuple(); -raceGo(); -anyGo(); +race() +any() +raceTuple() +anyTuple() +raceGo() +anyGo() diff --git a/src/Either.ts b/src/Either.ts index a153e49..e74c049 100644 --- a/src/Either.ts +++ b/src/Either.ts @@ -7,10 +7,10 @@ */ export type Either = | { - error: Error; - result: undefined; + error: Error + result: undefined } | { - result: T; - error: undefined; - }; + result: T + error: undefined + } diff --git a/src/go/Either.ts b/src/go/Either.ts index efc7b6c..c3ef383 100644 --- a/src/go/Either.ts +++ b/src/go/Either.ts @@ -9,4 +9,4 @@ * * @template T The type of the result value. */ -export type Either = [undefined, Error] | [T, undefined]; +export type Either = [undefined, Error] | [T, undefined] diff --git a/src/go/index.ts b/src/go/index.ts index 4ea88c8..2ac6678 100644 --- a/src/go/index.ts +++ b/src/go/index.ts @@ -1,4 +1,4 @@ - /** +/** * @module * * This module contains the interface to use the result of mightFail as an error-last tuple. @@ -15,10 +15,9 @@ + * ``` */ +import { type Either } from "./Either" +import { mightFail, mightFailSync } from "./mightFail" +import { makeMightFail, makeMightFailSync } from "./makeMightFail" -import { type Either } from "./Either"; -import { mightFail, mightFailSync } from "./mightFail"; -import { makeMightFail, makeMightFailSync } from "./makeMightFail"; - -export { Either, mightFail, makeMightFail, mightFailSync, makeMightFailSync }; -export default { mightFail, makeMightFail, mightFailSync, makeMightFailSync }; +export { Either, mightFail, makeMightFail, mightFailSync, makeMightFailSync } +export default { mightFail, makeMightFail, mightFailSync, makeMightFailSync } diff --git a/src/go/makeMightFail.ts b/src/go/makeMightFail.ts index a3bb41d..80871fe 100644 --- a/src/go/makeMightFail.ts +++ b/src/go/makeMightFail.ts @@ -1,6 +1,5 @@ -import { type Either } from "./Either"; -import { mightFail, mightFailSync } from "./mightFail"; - +import { type Either } from "./Either" +import { mightFail, mightFailSync } from "./mightFail" /** * Utility type that unwraps a Promise type. If T is a Promise, it extracts the type the Promise resolves to, @@ -8,7 +7,7 @@ import { mightFail, mightFailSync } from "./mightFail"; * * @template T The type to be unwrapped if it's a Promise. */ -type UnwrapPromise = T extends Promise ? U : T; +type UnwrapPromise = T extends Promise ? U : T /** * Wraps a promise-returning function in another function that instead of returning a Promise directly, @@ -43,14 +42,11 @@ type UnwrapPromise = T extends Promise ? U : T; */ export function makeMightFail Promise>( func: T -): ( - ...funcArgs: Parameters -) => Promise>>> { - +): (...funcArgs: Parameters) => Promise>>> { return async (...args: Parameters) => { - const promise = func(...args); - return mightFail(promise); - }; + const promise = func(...args) + return mightFail(promise) + } } /** @@ -83,7 +79,7 @@ export function makeMightFailSync any>( func: T ): (...funcArgs: Parameters) => Either> { return (...args: Parameters) => { - const throwingFunction = () => func(...args); - return mightFailSync(throwingFunction); - }; + const throwingFunction = () => func(...args) + return mightFailSync(throwingFunction) + } } diff --git a/src/go/mightFail.ts b/src/go/mightFail.ts index 645325e..0be3a2b 100644 --- a/src/go/mightFail.ts +++ b/src/go/mightFail.ts @@ -1,14 +1,12 @@ -import standard from "../index"; -import { type Either } from "./Either"; -import { makeProxyHandler } from "../utils"; -import { MightFail, MightFailFunction } from "../utils.type"; +import standard from "../index" +import { type Either } from "./Either" +import { makeProxyHandler } from "../utils" +import { MightFail, MightFailFunction } from "../utils.type" -const mightFailFunction: MightFailFunction<'go'> = async function ( - promise: Promise -) { - const {result, error} = await standard.mightFailFunction(promise); - return error ? [undefined, error] : [result, undefined]; -}; +const mightFailFunction: MightFailFunction<"go"> = async function (promise: Promise) { + const { result, error } = await standard.mightFailFunction(promise) + return error ? [undefined, error] : [result, undefined] +} /** * Wraps a promise in an Either to safely handle both its resolution and rejection. This function @@ -42,10 +40,10 @@ const mightFailFunction: MightFailFunction<'go'> = async function ( * } * console.log('Fetched data:', result); */ -export const mightFail: MightFail<'go'> = new Proxy( - mightFailFunction, - makeProxyHandler(mightFailFunction) -) as MightFail<'go'>; +export const mightFail: MightFail<"go"> = new Proxy( + mightFailFunction, + makeProxyHandler(mightFailFunction) +) as MightFail<"go"> /** * Wraps a synchronous function in an Either type to safely handle exceptions. This function @@ -70,6 +68,6 @@ export const mightFail: MightFail<'go'> = new Proxy( * console.log('Parsed object:', result); */ export function mightFailSync(func: () => T): Either { - const {result, error} = standard.mightFailSync(func); - return error ? [undefined, error] : [result, undefined]; + const { result, error } = standard.mightFailSync(func) + return error ? [undefined, error] : [result, undefined] } diff --git a/src/index.ts b/src/index.ts index 7b52b9d..9f62edb 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,6 +1,6 @@ -import { type Either } from "./Either"; -import { mightFail, mightFailSync, mightFailFunction } from "./mightFail"; -import { makeMightFail, makeMightFailSync } from "./makeMightFail"; +import { type Either } from "./Either" +import { mightFail, mightFailSync, mightFailFunction } from "./mightFail" +import { makeMightFail, makeMightFailSync } from "./makeMightFail" -export { Either, mightFail, makeMightFail, mightFailSync, makeMightFailSync, mightFailFunction }; -export default { mightFail, makeMightFail, mightFailSync, makeMightFailSync, mightFailFunction }; +export { Either, mightFail, makeMightFail, mightFailSync, makeMightFailSync, mightFailFunction } +export default { mightFail, makeMightFail, mightFailSync, makeMightFailSync, mightFailFunction } diff --git a/src/makeMightFail.ts b/src/makeMightFail.ts index bd72b20..f4eb47b 100644 --- a/src/makeMightFail.ts +++ b/src/makeMightFail.ts @@ -1,5 +1,5 @@ -import { type Either } from "./Either"; -import { mightFailSync, mightFailFunction } from "./mightFail"; +import { type Either } from "./Either" +import { mightFailSync, mightFailFunction } from "./mightFail" /** * Utility type that unwraps a Promise type. If T is a Promise, it extracts the type the Promise resolves to, @@ -7,7 +7,7 @@ import { mightFailSync, mightFailFunction } from "./mightFail"; * * @template T The type to be unwrapped if it's a Promise. */ -type UnwrapPromise = T extends Promise ? U : T; +type UnwrapPromise = T extends Promise ? U : T /** * Wraps a promise-returning function in another function that instead of returning a Promise directly, @@ -41,13 +41,11 @@ type UnwrapPromise = T extends Promise ? U : T; */ export function makeMightFail Promise>( func: T -): ( - ...funcArgs: Parameters -) => Promise>>> { +): (...funcArgs: Parameters) => Promise>>> { return async (...args: Parameters) => { - const promise = func(...args); - return mightFailFunction(promise) as Promise>; - }; + const promise = func(...args) + return mightFailFunction(promise) as Promise> + } } /** @@ -79,7 +77,7 @@ export function makeMightFailSync any>( func: T ): (...funcArgs: Parameters) => Either> { return (...args: Parameters) => { - const throwingFunction = () => func(...args); - return mightFailSync(throwingFunction); - }; + const throwingFunction = () => func(...args) + return mightFailSync(throwingFunction) + } } diff --git a/src/mightFail.ts b/src/mightFail.ts index e3c2c56..a79a347 100644 --- a/src/mightFail.ts +++ b/src/mightFail.ts @@ -1,18 +1,18 @@ -import { type Either } from "./Either"; -import { handleError, makeProxyHandler } from "./utils"; -import { MightFail, MightFailFunction } from "./utils.type"; +import { type Either } from "./Either" +import { handleError, makeProxyHandler } from "./utils" +import { MightFail, MightFailFunction } from "./utils.type" -export const mightFailFunction: MightFailFunction<'standard'> = async function ( - promise: Promise +export const mightFailFunction: MightFailFunction<"standard"> = async function ( + promise: Promise ): Promise> { try { - const result = await promise; - return { error: undefined, result } as Either; + const result = await promise + return { error: undefined, result } as Either } catch (err) { - const error = handleError(err); - return { error, result: undefined }; + const error = handleError(err) + return { error, result: undefined } } -}; +} /** * Wraps a promise in an Either to safely handle both its resolution and rejection. This function @@ -46,10 +46,10 @@ export const mightFailFunction: MightFailFunction<'standard'> = async function < * } * console.log('Fetched data:', result); */ -export const mightFail: MightFail<'standard'> = new Proxy( - mightFailFunction, - makeProxyHandler(mightFailFunction) -) as MightFail<'standard'>; +export const mightFail: MightFail<"standard"> = new Proxy( + mightFailFunction, + makeProxyHandler(mightFailFunction) +) as MightFail<"standard"> /** * Wraps a synchronous function in an Either type to safely handle exceptions. This function @@ -77,10 +77,10 @@ export const mightFail: MightFail<'standard'> = new Proxy( export function mightFailSync(func: () => T): Either { try { - const result = func(); - return { error: undefined, result }; + const result = func() + return { error: undefined, result } } catch (err) { - const error = handleError(err); - return { error, result: undefined }; + const error = handleError(err) + return { error, result: undefined } } } diff --git a/src/tuple/Either.ts b/src/tuple/Either.ts index d148a3d..9044d37 100644 --- a/src/tuple/Either.ts +++ b/src/tuple/Either.ts @@ -9,4 +9,4 @@ * * @template T The type of the result value. */ -export type Either = [Error, undefined] | [undefined, T]; \ No newline at end of file +export type Either = [Error, undefined] | [undefined, T] diff --git a/src/tuple/index.ts b/src/tuple/index.ts index f90f415..a0437f3 100644 --- a/src/tuple/index.ts +++ b/src/tuple/index.ts @@ -1,4 +1,4 @@ - /** +/** * @module * * This module contains the interface to use the result of mightFail as an error-first tuple. @@ -15,9 +15,9 @@ + * ``` */ -import { type Either } from "./Either"; -import { mightFail, mightFailSync } from "./mightFail"; -import { makeMightFail, makeMightFailSync } from "./makeMightFail"; +import { type Either } from "./Either" +import { mightFail, mightFailSync } from "./mightFail" +import { makeMightFail, makeMightFailSync } from "./makeMightFail" -export { Either, mightFail, makeMightFail, mightFailSync, makeMightFailSync }; -export default { mightFail, makeMightFail, mightFailSync, makeMightFailSync }; +export { Either, mightFail, makeMightFail, mightFailSync, makeMightFailSync } +export default { mightFail, makeMightFail, mightFailSync, makeMightFailSync } diff --git a/src/tuple/makeMightFail.ts b/src/tuple/makeMightFail.ts index 56104da..af8e481 100644 --- a/src/tuple/makeMightFail.ts +++ b/src/tuple/makeMightFail.ts @@ -1,6 +1,5 @@ -import { type Either } from "./Either"; -import { mightFail, mightFailSync } from "./mightFail"; - +import { type Either } from "./Either" +import { mightFail, mightFailSync } from "./mightFail" /** * Utility type that unwraps a Promise type. If T is a Promise, it extracts the type the Promise resolves to, @@ -8,7 +7,7 @@ import { mightFail, mightFailSync } from "./mightFail"; * * @template T The type to be unwrapped if it's a Promise. */ -type UnwrapPromise = T extends Promise ? U : T; +type UnwrapPromise = T extends Promise ? U : T /** * Wraps a promise-returning function in another function that instead of returning a Promise directly, @@ -43,14 +42,11 @@ type UnwrapPromise = T extends Promise ? U : T; */ export function makeMightFail Promise>( func: T -): ( - ...funcArgs: Parameters -) => Promise>>> { - +): (...funcArgs: Parameters) => Promise>>> { return async (...args: Parameters) => { - const promise = func(...args); - return mightFail(promise); - }; + const promise = func(...args) + return mightFail(promise) + } } /** @@ -83,7 +79,7 @@ export function makeMightFailSync any>( func: T ): (...funcArgs: Parameters) => Either> { return (...args: Parameters) => { - const throwingFunction = () => func(...args); - return mightFailSync(throwingFunction); - }; + const throwingFunction = () => func(...args) + return mightFailSync(throwingFunction) + } } diff --git a/src/tuple/mightFail.ts b/src/tuple/mightFail.ts index d2db596..0474133 100644 --- a/src/tuple/mightFail.ts +++ b/src/tuple/mightFail.ts @@ -1,14 +1,12 @@ -import standard from "../index"; -import { type Either } from "./Either"; -import { makeProxyHandler } from "../utils"; -import { MightFail, MightFailFunction } from "../utils.type"; +import standard from "../index" +import { type Either } from "./Either" +import { makeProxyHandler } from "../utils" +import { MightFail, MightFailFunction } from "../utils.type" -const mightFailFunction: MightFailFunction<'tuple'> = async function ( - promise: Promise -) { - const {result, error} = await standard.mightFailFunction(promise); - return error ? [error, undefined] : [undefined, result]; -}; +const mightFailFunction: MightFailFunction<"tuple"> = async function (promise: Promise) { + const { result, error } = await standard.mightFailFunction(promise) + return error ? [error, undefined] : [undefined, result] +} /** * Wraps a promise in an Either tuple to safely handle both its resolution and rejection. This function @@ -42,10 +40,10 @@ const mightFailFunction: MightFailFunction<'tuple'> = async function ( * } * console.log('Fetched data:', result); */ -export const mightFail: MightFail<'tuple'> = new Proxy( - mightFailFunction, - makeProxyHandler(mightFailFunction) -) as MightFail<'tuple'>; +export const mightFail: MightFail<"tuple"> = new Proxy( + mightFailFunction, + makeProxyHandler(mightFailFunction) +) as MightFail<"tuple"> /** * Wraps a synchronous function in an Either tuple to safely handle exceptions. This function @@ -71,6 +69,6 @@ export const mightFail: MightFail<'tuple'> = new Proxy( * console.log('Parsed object:', result); */ export function mightFailSync(func: () => T): Either { - const {result, error} = standard.mightFailSync(func); - return error ? [error, undefined] : [undefined, result]; + const { result, error } = standard.mightFailSync(func) + return error ? [error, undefined] : [undefined, result] } diff --git a/src/utils.ts b/src/utils.ts index 790dffc..e473d1a 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -1,36 +1,38 @@ -import { EitherMode, MightFailFunction } from "./utils.type"; +import { EitherMode, MightFailFunction } from "./utils.type" export function handleError(error: unknown): Error { if (error instanceof Error) { - return error; + return error } if (typeof error === "string") { - return new Error(error); + return new Error(error) } if (typeof error === "object" && error !== null) { - if ('message' in error && typeof error.message === 'string') { - return new Error(error.message); + if ("message" in error && typeof error.message === "string") { + return new Error(error.message) } - return new Error(error as any); + return new Error(error as any) } - return new Error("Unknown error"); + return new Error("Unknown error") } -export const makeProxyHandler = >(mightFailFunction: TMightFailFunction) => ({ +export const makeProxyHandler = >( + mightFailFunction: TMightFailFunction +) => ({ get(_: TMightFailFunction, property: string) { if (Object.getOwnPropertyDescriptor(Promise, property) === undefined) { - return mightFailFunction(Promise.reject(new Error(`property ${property} not found on Promise`))); + return mightFailFunction(Promise.reject(new Error(`property ${property} not found on Promise`))) } - const value = (Promise as any)[property]; + const value = (Promise as any)[property] - if (typeof value !== 'function') { - return mightFailFunction(Promise.reject(new Error(`property ${property} is not a Promise method`))); + if (typeof value !== "function") { + return mightFailFunction(Promise.reject(new Error(`property ${property} is not a Promise method`))) } return function (...args: any[]) { - const promise = value.apply(Promise, args); - return mightFailFunction(promise); - }; + const promise = value.apply(Promise, args) + return mightFailFunction(promise) + } }, -}); +}) diff --git a/src/utils.type.ts b/src/utils.type.ts index 208a0a2..8a47173 100644 --- a/src/utils.type.ts +++ b/src/utils.type.ts @@ -1,154 +1,183 @@ -import type {Either as StandardEither} from "./Either"; -import type {Either as TupleEither} from "./tuple"; -import type {Either as GoEither} from "./go"; +import type { Either as StandardEither } from "./Either" +import type { Either as TupleEither } from "./tuple" +import type { Either as GoEither } from "./go" -export type EitherMode = "standard" | "tuple" | "go" | "any"; +export type EitherMode = "standard" | "tuple" | "go" | "any" -export type AnyEither = StandardEither | TupleEither | GoEither; +export type AnyEither = StandardEither | TupleEither | GoEither export type MightFailFunction = ( - promise: Promise + promise: Promise ) => Promise< - TEitherMode extends "standard" - ? StandardEither - : TEitherMode extends "tuple" - ? TupleEither - : TEitherMode extends "go" - ? GoEither - : AnyEither ->; + TEitherMode extends "standard" + ? StandardEither + : TEitherMode extends "tuple" + ? TupleEither + : TEitherMode extends "go" + ? GoEither + : AnyEither +> export type PromiseFulfilledResult = { - status: "fulfilled"; - value: T; -}; + status: "fulfilled" + value: T +} export type PromiseRejectedResult = { - status: "rejected"; - reason: any; -}; -export type PromiseSettledResult = - | PromiseFulfilledResult - | PromiseRejectedResult; + status: "rejected" + reason: any +} +export type PromiseSettledResult = PromiseFulfilledResult | PromiseRejectedResult export type MightFail< - TEitherMode extends EitherMode, - TPromiseStaticMethods = PromiseStaticMethods -> = MightFailFunction & TPromiseStaticMethods; + TEitherMode extends EitherMode, + TPromiseStaticMethods = PromiseStaticMethods +> = MightFailFunction & TPromiseStaticMethods export interface PromiseStaticMethods { - /** - * Wraps a Promise.all call in a mightFail function. - * @params values - * @template T The type of the resolved values - * @return {Promise} - Promise>> - */ - all(values: Iterable>): Promise< - TEitherMode extends "standard" - ? Awaited> - : TEitherMode extends "tuple" - ? Awaited> - : TEitherMode extends "go" - ? Awaited> - : Awaited> - >; + /** + * Wraps a Promise.all call in a mightFail function. + * @params values + * @template T The type of the resolved values + * @return {Promise} - Promise>> + */ + all( + values: Iterable> + ): Promise< + TEitherMode extends "standard" + ? Awaited> + : TEitherMode extends "tuple" + ? Awaited> + : TEitherMode extends "go" + ? Awaited> + : Awaited> + > - /** - * Wraps a Promise.all call in a mightFail function. - * @params values - * @template T The type of the resolved values - * @return {Promise} - Promise; }>> - */ - all(values: T): - Promise< - TEitherMode extends "standard" - ? StandardEither<{ -readonly [P in keyof T]: Awaited; }> - : TEitherMode extends "tuple" - ? TupleEither<{ -readonly [P in keyof T]: Awaited; }> - : TEitherMode extends "go" - ? GoEither<{ -readonly [P in keyof T]: Awaited; }> - : AnyEither<{ -readonly [P in keyof T]: Awaited; }> - >; + /** + * Wraps a Promise.all call in a mightFail function. + * @params values + * @template T The type of the resolved values + * @return {Promise} - Promise; }>> + */ + all( + values: T + ): Promise< + TEitherMode extends "standard" + ? StandardEither<{ -readonly [P in keyof T]: Awaited }> + : TEitherMode extends "tuple" + ? TupleEither<{ -readonly [P in keyof T]: Awaited }> + : TEitherMode extends "go" + ? GoEither<{ -readonly [P in keyof T]: Awaited }> + : AnyEither<{ -readonly [P in keyof T]: Awaited }> + > - /** - * Wraps a Promise.race call in a mightFail function. - * - * @params values - An iterable of promises - * @template T The type of the resolved values - * @return {Promise} - Promise>> - */ - race(values: Iterable>): Promise< - TEitherMode extends "standard" - ? Awaited> - : TEitherMode extends "tuple" - ? Awaited> - : TEitherMode extends "go" - ? Awaited> - : Awaited> - >; + /** + * Wraps a Promise.race call in a mightFail function. + * + * @params values - An iterable of promises + * @template T The type of the resolved values + * @return {Promise} - Promise>> + */ + race( + values: Iterable> + ): Promise< + TEitherMode extends "standard" + ? Awaited> + : TEitherMode extends "tuple" + ? Awaited> + : TEitherMode extends "go" + ? Awaited> + : Awaited> + > - /** - * Wraps a Promise.race call in a mightFail function. - * @params values - * @template T The type of the resolved values - * @return {Promise} - Promise>> - */ - race(values: T): Promise< - TEitherMode extends "standard" - ? Awaited> - : TEitherMode extends "tuple" - ? Awaited> - : TEitherMode extends "go" - ? Awaited> - : Awaited> - >; + /** + * Wraps a Promise.race call in a mightFail function. + * @params values + * @template T The type of the resolved values + * @return {Promise} - Promise>> + */ + race( + values: T + ): Promise< + TEitherMode extends "standard" + ? Awaited> + : TEitherMode extends "tuple" + ? Awaited> + : TEitherMode extends "go" + ? Awaited> + : Awaited> + > - /** - * Wraps a Promise.allSettled call in a mightFail function. - * @params values - * @template T The type of the resolved values - * @return {Promise} - Promise>}>> - */ - allSettled(values: T): Promise< - TEitherMode extends "standard" - ? StandardEither<{ -readonly [P in keyof T]: PromiseSettledResult> }> - : TEitherMode extends "tuple" - ? TupleEither<{ -readonly [P in keyof T]: PromiseSettledResult> }> - : TEitherMode extends "go" - ? GoEither<{ -readonly [P in keyof T]: PromiseSettledResult> }> - : AnyEither<{ -readonly [P in keyof T]: PromiseSettledResult> }> - >; + /** + * Wraps a Promise.allSettled call in a mightFail function. + * @params values + * @template T The type of the resolved values + * @return {Promise} - Promise>}>> + */ + allSettled( + values: T + ): Promise< + TEitherMode extends "standard" + ? StandardEither<{ -readonly [P in keyof T]: PromiseSettledResult> }> + : TEitherMode extends "tuple" + ? TupleEither<{ -readonly [P in keyof T]: PromiseSettledResult> }> + : TEitherMode extends "go" + ? GoEither<{ -readonly [P in keyof T]: PromiseSettledResult> }> + : AnyEither<{ -readonly [P in keyof T]: PromiseSettledResult> }> + > - /** - * Wraps a Promise.allSettled call in a mightFail function. - * @params values - * @template T The type of the resolved values - * @return {Promise} - Promise>[]>> - */ - allSettled(values: Iterable>): Promise< - TEitherMode extends "standard" - ? StandardEither>[]> - : TEitherMode extends "tuple" - ? TupleEither>[]> - : TEitherMode extends "go" - ? GoEither>[]> - : AnyEither>[]> - >; + /** + * Wraps a Promise.allSettled call in a mightFail function. + * @params values + * @template T The type of the resolved values + * @return {Promise} - Promise>[]>> + */ + allSettled( + values: Iterable> + ): Promise< + TEitherMode extends "standard" + ? StandardEither>[]> + : TEitherMode extends "tuple" + ? TupleEither>[]> + : TEitherMode extends "go" + ? GoEither>[]> + : AnyEither>[]> + > - /** - * Wraps a Promise.any call in a mightFail function. - * - * @params values - * @template T The type of the resolved values - * @return {Promise} - Promise>> - */ - any(values: T): Promise> : TEitherMode extends "tuple" ? TupleEither> : TEitherMode extends "go" ? GoEither> : AnyEither>>; + /** + * Wraps a Promise.any call in a mightFail function. + * + * @params values + * @template T The type of the resolved values + * @return {Promise} - Promise>> + */ + any( + values: T + ): Promise< + TEitherMode extends "standard" + ? StandardEither> + : TEitherMode extends "tuple" + ? TupleEither> + : TEitherMode extends "go" + ? GoEither> + : AnyEither> + > - /** - * Wraps a Promise.any call in a mightFail function. - * - * @params values - * @template T The type of the resolved values - * @return {Promise} - Promise>> - */ - any(values: Iterable>): Promise> : TEitherMode extends "tuple" ? TupleEither> : TEitherMode extends "go" ? GoEither> : AnyEither>>; + /** + * Wraps a Promise.any call in a mightFail function. + * + * @params values + * @template T The type of the resolved values + * @return {Promise} - Promise>> + */ + any( + values: Iterable> + ): Promise< + TEitherMode extends "standard" + ? StandardEither> + : TEitherMode extends "tuple" + ? TupleEither> + : TEitherMode extends "go" + ? GoEither> + : AnyEither> + > } diff --git a/test/go/makeMightFail.test.ts b/test/go/makeMightFail.test.ts index 24a772b..f693a05 100644 --- a/test/go/makeMightFail.test.ts +++ b/test/go/makeMightFail.test.ts @@ -1,17 +1,16 @@ import { expect, test } from "vitest" import { makeMightFail } from "../../src/go/index" - test("success returns the response", async () => { - const resolve = (value: {message: string}) => Promise.resolve(value); + const resolve = (value: { message: string }) => Promise.resolve(value) const func = makeMightFail(resolve) - const [result, error] = await func({message: "success"}) + const [result, error] = await func({ message: "success" }) expect(error).toBe(undefined) expect(result!.message).toBe("success") }) test("fail with error returns the error", async () => { - const reject = (error: Error) => Promise.reject(error); + const reject = (error: Error) => Promise.reject(error) const func = makeMightFail(reject) const [result, error] = await func(new Error("error")) expect(result).toBe(undefined) @@ -19,9 +18,9 @@ test("fail with error returns the error", async () => { }) test("fail without error returns an error", async () => { - const reject = () => Promise.reject(undefined); + const reject = () => Promise.reject(undefined) const func = makeMightFail(reject) const [result, error] = await func() expect(result).toBe(undefined) expect(error?.message).toBeTruthy() -}) \ No newline at end of file +}) diff --git a/test/go/makeMightFailSync.test.ts b/test/go/makeMightFailSync.test.ts index 3717e08..49e2173 100644 --- a/test/go/makeMightFailSync.test.ts +++ b/test/go/makeMightFailSync.test.ts @@ -5,10 +5,9 @@ function somethingThatThrows(input: string) { if (!input) { throw new Error("error") } - return {message: input} + return { message: input } } - test("success returns the response", async () => { const func = makeMightFailSync(somethingThatThrows) const [result, error] = await func("success") @@ -26,9 +25,9 @@ test("fail with error returns the error", async () => { test("fail without error returns an error", async () => { const reject = () => { throw "a fit" - }; + } const func = makeMightFailSync(reject) const [result, error] = await func() expect(result).toBe(undefined) expect(error?.message).toBeTruthy() -}) \ No newline at end of file +}) diff --git a/test/go/mightFail.test.ts b/test/go/mightFail.test.ts index 5d55949..5242b5c 100644 --- a/test/go/mightFail.test.ts +++ b/test/go/mightFail.test.ts @@ -1,126 +1,125 @@ -import {describe, expect, it, test } from "vitest"; -import { mightFail } from "../../src/go/index"; +import { describe, expect, it, test } from "vitest" +import { mightFail } from "../../src/go/index" test("success returns the response", async () => { - const [result, error] = await mightFail(Promise.resolve("success")); - expect(result).toBe("success"); - expect(error).toBe(undefined); -}); + const [result, error] = await mightFail(Promise.resolve("success")) + expect(result).toBe("success") + expect(error).toBe(undefined) +}) test("fail with error returns the error", async () => { - const [result, error] = await mightFail(Promise.reject(new Error("error"))); - expect(result).toBe(undefined); - expect(error?.message).toBe("error"); -}); + const [result, error] = await mightFail(Promise.reject(new Error("error"))) + expect(result).toBe(undefined) + expect(error?.message).toBe("error") +}) test("fail without error returns an error", async () => { - const [result, error] = await mightFail(Promise.reject(undefined)); - expect(result).toBe(undefined); - expect(error?.message).toBeTruthy(); -}); + const [result, error] = await mightFail(Promise.reject(undefined)) + expect(result).toBe(undefined) + expect(error?.message).toBeTruthy() +}) test("fail with string returns an error with that string as the message", async () => { - const [result, error] = await mightFail(Promise.reject("error")); - expect(result).toBe(undefined); - expect(error?.message).toBe("error"); -}); + const [result, error] = await mightFail(Promise.reject("error")) + expect(result).toBe(undefined) + expect(error?.message).toBe("error") +}) test("fail with object returns an error with that object as the message", async () => { - const [result, error] = await mightFail(Promise.reject({ message: "error" })); - expect(result).toBe(undefined); - expect(error?.message).toBe("error"); -}); + const [result, error] = await mightFail(Promise.reject({ message: "error" })) + expect(result).toBe(undefined) + expect(error?.message).toBe("error") +}) test("async function that throws", async () => { const asyncFn = async () => { - throw new Error("async error"); - }; - const [result, error] = await mightFail(asyncFn()); - expect(result).toBe(undefined); - expect(error?.message).toBe("async error"); -}); + throw new Error("async error") + } + const [result, error] = await mightFail(asyncFn()) + expect(result).toBe(undefined) + expect(error?.message).toBe("async error") +}) test("promise that resolves after delay", async () => { - const delayedPromise = new Promise((resolve) => - setTimeout(() => resolve("delayed success"), 100) - ); - const [result, error] = await mightFail(delayedPromise); - expect(result).toBe("delayed success"); - expect(error).toBe(undefined); -}); + const delayedPromise = new Promise((resolve) => setTimeout(() => resolve("delayed success"), 100)) + const [result, error] = await mightFail(delayedPromise) + expect(result).toBe("delayed success") + expect(error).toBe(undefined) +}) test("promise that rejects after delay", async () => { - const delayedPromise = new Promise((_, reject) => - setTimeout(() => reject("delayed error"), 100) - ); - const [result, error] = await mightFail(delayedPromise); - expect(result).toBe(undefined); - expect(error?.message).toBe("delayed error"); -}); - - -describe('promise concurrent method wrappers', () => { - describe('mightFail.all', () => { - it('should resolve with all values when all promises succeed', async () => { - const promises = [Promise.resolve(1), Promise.resolve(2), Promise.resolve(3)]; - const [result, error] = await mightFail.all(promises); - expect(result).toEqual([1, 2, 3]); - expect(error).toBeUndefined(); - }); - - it('should return an error if any promise fails', async () => { - const promises = [Promise.resolve(1), Promise.reject(new Error('Test Error')), Promise.resolve(3)]; - const [result, error] = await mightFail.all(promises); - expect(result).toBeUndefined(); - expect(error).toBeInstanceOf(Error); - expect(error!.message).toBe('Test Error'); - }); - }); - - describe('mightFail.race', () => { - it('should resolve with the first resolved value', async () => { - const promises = [Promise.resolve(42), new Promise(resolve => setTimeout(() => resolve(100), 100))]; - const [result, error] = await mightFail.race(promises); - expect(result).toBe(42); - expect(error).toBeUndefined(); - }); - - it('should return an error if the first promise to settle is a rejection', async () => { - const promises = [Promise.reject(new Error('Race Error')), Promise.resolve(100)]; - const [result, error] = await mightFail.race(promises); - expect(result).toBeUndefined(); - expect(error).toBeInstanceOf(Error); - expect(error!.message).toBe('Race Error'); - }); - }); + const delayedPromise = new Promise((_, reject) => setTimeout(() => reject("delayed error"), 100)) + const [result, error] = await mightFail(delayedPromise) + expect(result).toBe(undefined) + expect(error?.message).toBe("delayed error") +}) - describe('mightFail.allSettled', () => { - it('should resolve with all settled results, including fulfilled and rejected promises', async () => { - const promises = [Promise.resolve(1), Promise.reject(new Error('AllSettled Error')), Promise.resolve(3)]; - const [result, error] = await mightFail.allSettled(promises); +describe("promise concurrent method wrappers", () => { + describe("mightFail.all", () => { + it("should resolve with all values when all promises succeed", async () => { + const promises = [Promise.resolve(1), Promise.resolve(2), Promise.resolve(3)] + const [result, error] = await mightFail.all(promises) + expect(result).toEqual([1, 2, 3]) + expect(error).toBeUndefined() + }) + + it("should return an error if any promise fails", async () => { + const promises = [Promise.resolve(1), Promise.reject(new Error("Test Error")), Promise.resolve(3)] + const [result, error] = await mightFail.all(promises) + expect(result).toBeUndefined() + expect(error).toBeInstanceOf(Error) + expect(error!.message).toBe("Test Error") + }) + }) + + describe("mightFail.race", () => { + it("should resolve with the first resolved value", async () => { + const promises = [Promise.resolve(42), new Promise((resolve) => setTimeout(() => resolve(100), 100))] + const [result, error] = await mightFail.race(promises) + expect(result).toBe(42) + expect(error).toBeUndefined() + }) + + it("should return an error if the first promise to settle is a rejection", async () => { + const promises = [Promise.reject(new Error("Race Error")), Promise.resolve(100)] + const [result, error] = await mightFail.race(promises) + expect(result).toBeUndefined() + expect(error).toBeInstanceOf(Error) + expect(error!.message).toBe("Race Error") + }) + }) + + describe("mightFail.allSettled", () => { + it("should resolve with all settled results, including fulfilled and rejected promises", async () => { + const promises = [Promise.resolve(1), Promise.reject(new Error("AllSettled Error")), Promise.resolve(3)] + const [result, error] = await mightFail.allSettled(promises) expect(result).toEqual([ - { status: 'fulfilled', value: 1 }, - { status: 'rejected', reason: new Error('AllSettled Error') }, - { status: 'fulfilled', value: 3 }, - ]); - expect(error).toBeUndefined(); - }); - }); - - describe('mightFail.any', () => { - it('should resolve with the first successful promise', async () => { - const promises = [Promise.reject(new Error('Error 1')), Promise.resolve(200), Promise.reject(new Error('Error 2'))]; - const [result, error] = await mightFail.any(promises); - expect(result).toBe(200); - expect(error).toBeUndefined(); - }); - - it('should return an AggregateError if all promises fail', async () => { - const promises = [Promise.reject(new Error('Error 1')), Promise.reject(new Error('Error 2'))]; - const [result, error] = await mightFail.any(promises); - expect(result).toBeUndefined(); - expect(error).toBeInstanceOf(Error); - expect(error!.message).toBe('All promises were rejected'); - }); - }); + { status: "fulfilled", value: 1 }, + { status: "rejected", reason: new Error("AllSettled Error") }, + { status: "fulfilled", value: 3 }, + ]) + expect(error).toBeUndefined() + }) + }) + + describe("mightFail.any", () => { + it("should resolve with the first successful promise", async () => { + const promises = [ + Promise.reject(new Error("Error 1")), + Promise.resolve(200), + Promise.reject(new Error("Error 2")), + ] + const [result, error] = await mightFail.any(promises) + expect(result).toBe(200) + expect(error).toBeUndefined() + }) + + it("should return an AggregateError if all promises fail", async () => { + const promises = [Promise.reject(new Error("Error 1")), Promise.reject(new Error("Error 2"))] + const [result, error] = await mightFail.any(promises) + expect(result).toBeUndefined() + expect(error).toBeInstanceOf(Error) + expect(error!.message).toBe("All promises were rejected") + }) + }) }) diff --git a/test/go/mightFailSync.test.ts b/test/go/mightFailSync.test.ts index c96668b..146c0a2 100644 --- a/test/go/mightFailSync.test.ts +++ b/test/go/mightFailSync.test.ts @@ -1,37 +1,37 @@ -import { expect, test } from "vitest"; -import { mightFailSync } from "../../src/go/index"; +import { expect, test } from "vitest" +import { mightFailSync } from "../../src/go/index" function somethingThatThrows(input: string) { if (!input) { - throw new Error("error"); + throw new Error("error") } - return { message: input }; + return { message: input } } test("success returns the response", async () => { - const [result, error] = mightFailSync(() => somethingThatThrows("success")); - expect(error).toBe(undefined); - expect(result?.message).toBe("success"); -}); + const [result, error] = mightFailSync(() => somethingThatThrows("success")) + expect(error).toBe(undefined) + expect(result?.message).toBe("success") +}) test("fail with error returns the error", async () => { - const [result, error] = mightFailSync(() => somethingThatThrows("")); - expect(result).toBe(undefined); - expect(error?.message).toBe("error"); -}); + const [result, error] = mightFailSync(() => somethingThatThrows("")) + expect(result).toBe(undefined) + expect(error?.message).toBe("error") +}) test("fail without error returns an error", async () => { const [result, error] = await mightFailSync(() => { - throw "a fit"; - }); - expect(result).toBe(undefined); - expect(error?.message).toBeTruthy(); -}); + throw "a fit" + }) + expect(result).toBe(undefined) + expect(error?.message).toBeTruthy() +}) test("fail with string returns an error with that string as the message", async () => { const [result, error] = await mightFailSync(() => { - throw "a fit"; - }); - expect(result).toBe(undefined); - expect(error?.message).toBe("a fit"); -}); + throw "a fit" + }) + expect(result).toBe(undefined) + expect(error?.message).toBe("a fit") +}) diff --git a/test/makeMightFail.test.ts b/test/makeMightFail.test.ts index 4e4485c..5089fca 100644 --- a/test/makeMightFail.test.ts +++ b/test/makeMightFail.test.ts @@ -1,27 +1,26 @@ import { expect, test } from "vitest" import { makeMightFail } from "../src/index" - test("success returns the response", async () => { - const resolve = (value: {message: string}) => Promise.resolve(value); + const resolve = (value: { message: string }) => Promise.resolve(value) const func = makeMightFail(resolve) - const {error, result} = await func({message: "success"}) + const { error, result } = await func({ message: "success" }) expect(error).toBe(undefined) expect(result!.message).toBe("success") }) test("fail with error returns the error", async () => { - const reject = (error: Error) => Promise.reject(error); + const reject = (error: Error) => Promise.reject(error) const func = makeMightFail(reject) - const {error, result} = await func(new Error("error")) + const { error, result } = await func(new Error("error")) expect(result).toBe(undefined) expect(error?.message).toBe("error") }) test("fail without error returns an error", async () => { - const reject = () => Promise.reject(undefined); + const reject = () => Promise.reject(undefined) const func = makeMightFail(reject) - const {error, result} = await func() + const { error, result } = await func() expect(result).toBe(undefined) expect(error?.message).toBeTruthy() -}) \ No newline at end of file +}) diff --git a/test/makeMightFailSync.test.ts b/test/makeMightFailSync.test.ts index 222ed85..7334240 100644 --- a/test/makeMightFailSync.test.ts +++ b/test/makeMightFailSync.test.ts @@ -5,20 +5,19 @@ function somethingThatThrows(input: string) { if (!input) { throw new Error("error") } - return {message: input} + return { message: input } } - test("success returns the response", async () => { const func = makeMightFailSync(somethingThatThrows) - const {error, result} = await func("success") + const { error, result } = await func("success") expect(error).toBe(undefined) expect(result!.message).toBe("success") }) test("fail with error returns the error", async () => { const func = makeMightFailSync(somethingThatThrows) - const {error, result} = await func("") + const { error, result } = await func("") expect(result).toBe(undefined) expect(error?.message).toBe("error") }) @@ -26,9 +25,9 @@ test("fail with error returns the error", async () => { test("fail without error returns an error", async () => { const reject = () => { throw "a fit" - }; + } const func = makeMightFailSync(reject) - const {error, result} = await func() + const { error, result } = await func() expect(result).toBe(undefined) expect(error?.message).toBeTruthy() -}) \ No newline at end of file +}) diff --git a/test/mightFail.test.ts b/test/mightFail.test.ts index 59125e6..b846030 100644 --- a/test/mightFail.test.ts +++ b/test/mightFail.test.ts @@ -1,122 +1,121 @@ -import {describe, expect, it, test} from "vitest" +import { describe, expect, it, test } from "vitest" import { mightFail } from "../src/index" - test("success returns the response", async () => { - const {error, result} = await mightFail(Promise.resolve("success")) + const { error, result } = await mightFail(Promise.resolve("success")) expect(result).toBe("success") expect(error).toBe(undefined) }) test("fail with error returns the error", async () => { - const {error, result} = await mightFail(Promise.reject(new Error("error"))) + const { error, result } = await mightFail(Promise.reject(new Error("error"))) expect(result).toBe(undefined) expect(error?.message).toBe("error") }) test("fail without error returns an error", async () => { - const {error, result} = await mightFail(Promise.reject(undefined)) + const { error, result } = await mightFail(Promise.reject(undefined)) expect(result).toBe(undefined) expect(error?.message).toBeTruthy() }) test("fail with string returns an error with that string as the message", async () => { - const {error, result} = await mightFail(Promise.reject("error")) + const { error, result } = await mightFail(Promise.reject("error")) expect(result).toBe(undefined) expect(error?.message).toBe("error") }) test("fail with object returns an error with that object as the message", async () => { - const {error, result} = await mightFail(Promise.reject({message: "error"})) + const { error, result } = await mightFail(Promise.reject({ message: "error" })) expect(result).toBe(undefined) expect(error?.message).toBe("error") }) test("async function that throws", async () => { const asyncFn = async () => { - throw new Error("async error"); - }; - const { error, result } = await mightFail(asyncFn()); - expect(result).toBe(undefined); - expect(error?.message).toBe("async error"); -}); + throw new Error("async error") + } + const { error, result } = await mightFail(asyncFn()) + expect(result).toBe(undefined) + expect(error?.message).toBe("async error") +}) test("promise that resolves after delay", async () => { - const delayedPromise = new Promise(resolve => setTimeout(() => resolve("delayed success"), 100)); - const { error, result } = await mightFail(delayedPromise); - expect(result).toBe("delayed success"); - expect(error).toBe(undefined); -}); + const delayedPromise = new Promise((resolve) => setTimeout(() => resolve("delayed success"), 100)) + const { error, result } = await mightFail(delayedPromise) + expect(result).toBe("delayed success") + expect(error).toBe(undefined) +}) test("promise that rejects after delay", async () => { - const delayedPromise = new Promise((_, reject) => setTimeout(() => reject("delayed error"), 100)); - const { error, result } = await mightFail(delayedPromise); - expect(result).toBe(undefined); - expect(error?.message).toBe("delayed error"); -}); + const delayedPromise = new Promise((_, reject) => setTimeout(() => reject("delayed error"), 100)) + const { error, result } = await mightFail(delayedPromise) + expect(result).toBe(undefined) + expect(error?.message).toBe("delayed error") +}) describe("promise concurrent method wrappers", () => { describe("mightFail.all", () => { it("should resolve with an array of values when all promises resolve", async () => { - const promises = [Promise.resolve(1), Promise.resolve(2), Promise.resolve(3)]; - const { result, error } = await mightFail.all(promises); - expect(error).toBeUndefined(); - expect(result).toEqual([1, 2, 3]); - }); + const promises = [Promise.resolve(1), Promise.resolve(2), Promise.resolve(3)] + const { result, error } = await mightFail.all(promises) + expect(error).toBeUndefined() + expect(result).toEqual([1, 2, 3]) + }) it("should fail with the first error when any promise rejects", async () => { - const promises = [Promise.resolve(1), Promise.reject(new Error("Error")), Promise.resolve(3)]; - const { result, error } = await mightFail.all(promises); - expect(result).toBeUndefined(); - expect(error).toBeInstanceOf(Error); - expect(error!.message).toBe("Error"); - }); - }); + const promises = [Promise.resolve(1), Promise.reject(new Error("Error")), Promise.resolve(3)] + const { result, error } = await mightFail.all(promises) + expect(result).toBeUndefined() + expect(error).toBeInstanceOf(Error) + expect(error!.message).toBe("Error") + }) + }) describe("mightFail.race", () => { it("should resolve with the first resolved value", async () => { - const promises = [Promise.resolve(1), Promise.reject(new Error("Error")), Promise.resolve(3)]; - const { result, error } = await mightFail.race(promises); - expect(error).toBeUndefined(); - expect(result).toBe(1); - }); + const promises = [Promise.resolve(1), Promise.reject(new Error("Error")), Promise.resolve(3)] + const { result, error } = await mightFail.race(promises) + expect(error).toBeUndefined() + expect(result).toBe(1) + }) it("should reject with the first rejected error if it occurs before any promise resolves", async () => { - const promises = [Promise.reject(new Error("Race Error")), Promise.resolve(1), Promise.resolve(2)]; - const { result, error } = await mightFail.race(promises); - expect(result).toBeUndefined(); - expect(error).toBeInstanceOf(Error); - expect(error!.message).toBe("Race Error"); - }); - }); + const promises = [Promise.reject(new Error("Race Error")), Promise.resolve(1), Promise.resolve(2)] + const { result, error } = await mightFail.race(promises) + expect(result).toBeUndefined() + expect(error).toBeInstanceOf(Error) + expect(error!.message).toBe("Race Error") + }) + }) describe("mightFail.allSettled", () => { it("should resolve with an array of PromiseSettledResult objects", async () => { - const promises = [Promise.resolve(1), Promise.reject(new Error("Error")), Promise.resolve(3)]; - const { result, error } = await mightFail.allSettled(promises); - expect(error).toBeUndefined(); + const promises = [Promise.resolve(1), Promise.reject(new Error("Error")), Promise.resolve(3)] + const { result, error } = await mightFail.allSettled(promises) + expect(error).toBeUndefined() expect(result).toEqual([ { status: "fulfilled", value: 1 }, { status: "rejected", reason: new Error("Error") }, { status: "fulfilled", value: 3 }, - ]); - }); - }); + ]) + }) + }) describe("mightFail.any", () => { it("should resolve with the first fulfilled value", async () => { - const promises = [Promise.reject(new Error("Error 1")), Promise.resolve(2), Promise.reject(new Error("Error 2"))]; - const { result, error } = await mightFail.any(promises); - expect(error).toBeUndefined(); - expect(result).toBe(2); - }); + const promises = [Promise.reject(new Error("Error 1")), Promise.resolve(2), Promise.reject(new Error("Error 2"))] + const { result, error } = await mightFail.any(promises) + expect(error).toBeUndefined() + expect(result).toBe(2) + }) it("should reject with an AggregateError if all promises reject", async () => { - const promises = [Promise.reject(new Error("Error 1")), Promise.reject(new Error("Error 2"))]; - const { result, error } = await mightFail.any(promises); - expect(result).toBeUndefined(); - expect(error).toBeInstanceOf(Error); - expect(error!.message).toBe("All promises were rejected"); - }); - }); -}); + const promises = [Promise.reject(new Error("Error 1")), Promise.reject(new Error("Error 2"))] + const { result, error } = await mightFail.any(promises) + expect(result).toBeUndefined() + expect(error).toBeInstanceOf(Error) + expect(error!.message).toBe("All promises were rejected") + }) + }) +}) diff --git a/test/mightFailSync.test.ts b/test/mightFailSync.test.ts index 8171d1e..35b215f 100644 --- a/test/mightFailSync.test.ts +++ b/test/mightFailSync.test.ts @@ -1,37 +1,37 @@ -import { expect, test } from "vitest"; -import { mightFailSync } from "../src/index"; +import { expect, test } from "vitest" +import { mightFailSync } from "../src/index" function somethingThatThrows(input: string) { if (!input) { - throw new Error("error"); + throw new Error("error") } - return { message: input }; + return { message: input } } test("success returns the response", async () => { - const { error, result } = mightFailSync(() => somethingThatThrows("success")); - expect(error).toBe(undefined); - expect(result?.message).toBe("success"); -}); + const { error, result } = mightFailSync(() => somethingThatThrows("success")) + expect(error).toBe(undefined) + expect(result?.message).toBe("success") +}) test("fail with error returns the error", async () => { - const { error, result } = mightFailSync(() => somethingThatThrows("")); - expect(result).toBe(undefined); - expect(error?.message).toBe("error"); -}); + const { error, result } = mightFailSync(() => somethingThatThrows("")) + expect(result).toBe(undefined) + expect(error?.message).toBe("error") +}) test("fail without error returns an error", async () => { const { error, result } = await mightFailSync(() => { - throw "a fit"; - }); - expect(result).toBe(undefined); - expect(error?.message).toBeTruthy(); -}); + throw "a fit" + }) + expect(result).toBe(undefined) + expect(error?.message).toBeTruthy() +}) test("fail with string returns an error with that string as the message", async () => { const { error, result } = await mightFailSync(() => { - throw "a fit"; - }); - expect(result).toBe(undefined); - expect(error?.message).toBe("a fit"); -}); + throw "a fit" + }) + expect(result).toBe(undefined) + expect(error?.message).toBe("a fit") +}) diff --git a/test/tuple/makeMightFail.test.ts b/test/tuple/makeMightFail.test.ts index 69c7883..a08c5b2 100644 --- a/test/tuple/makeMightFail.test.ts +++ b/test/tuple/makeMightFail.test.ts @@ -1,17 +1,16 @@ import { expect, test } from "vitest" import { makeMightFail } from "../../src/tuple/index" - test("success returns the response", async () => { - const resolve = (value: {message: string}) => Promise.resolve(value); + const resolve = (value: { message: string }) => Promise.resolve(value) const func = makeMightFail(resolve) - const [error, result] = await func({message: "success"}) + const [error, result] = await func({ message: "success" }) expect(error).toBe(undefined) expect(result!.message).toBe("success") }) test("fail with error returns the error", async () => { - const reject = (error: Error) => Promise.reject(error); + const reject = (error: Error) => Promise.reject(error) const func = makeMightFail(reject) const [error, result] = await func(new Error("error")) expect(result).toBe(undefined) @@ -19,9 +18,9 @@ test("fail with error returns the error", async () => { }) test("fail without error returns an error", async () => { - const reject = () => Promise.reject(undefined); + const reject = () => Promise.reject(undefined) const func = makeMightFail(reject) const [error, result] = await func() expect(result).toBe(undefined) expect(error?.message).toBeTruthy() -}) \ No newline at end of file +}) diff --git a/test/tuple/makeMightFailSync.test.ts b/test/tuple/makeMightFailSync.test.ts index a6b159c..32b72a4 100644 --- a/test/tuple/makeMightFailSync.test.ts +++ b/test/tuple/makeMightFailSync.test.ts @@ -5,10 +5,9 @@ function somethingThatThrows(input: string) { if (!input) { throw new Error("error") } - return {message: input} + return { message: input } } - test("success returns the response", async () => { const func = makeMightFailSync(somethingThatThrows) const [error, result] = await func("success") @@ -26,9 +25,9 @@ test("fail with error returns the error", async () => { test("fail without error returns an error", async () => { const reject = () => { throw "a fit" - }; + } const func = makeMightFailSync(reject) const [error, result] = await func() expect(result).toBe(undefined) expect(error?.message).toBeTruthy() -}) \ No newline at end of file +}) diff --git a/test/tuple/mightFail.test.ts b/test/tuple/mightFail.test.ts index f7f9402..5aae06f 100644 --- a/test/tuple/mightFail.test.ts +++ b/test/tuple/mightFail.test.ts @@ -1,125 +1,125 @@ -import {describe, expect, it, test} from "vitest"; -import { mightFail } from "../../src/tuple/index"; +import { describe, expect, it, test } from "vitest" +import { mightFail } from "../../src/tuple/index" test("success returns the response", async () => { - const [error, result] = await mightFail(Promise.resolve("success")); - expect(result).toBe("success"); - expect(error).toBe(undefined); -}); + const [error, result] = await mightFail(Promise.resolve("success")) + expect(result).toBe("success") + expect(error).toBe(undefined) +}) test("fail with error returns the error", async () => { - const [error, result] = await mightFail(Promise.reject(new Error("error"))); - expect(result).toBe(undefined); - expect(error?.message).toBe("error"); -}); + const [error, result] = await mightFail(Promise.reject(new Error("error"))) + expect(result).toBe(undefined) + expect(error?.message).toBe("error") +}) test("fail without error returns an error", async () => { - const [error, result] = await mightFail(Promise.reject(undefined)); - expect(result).toBe(undefined); - expect(error?.message).toBeTruthy(); -}); + const [error, result] = await mightFail(Promise.reject(undefined)) + expect(result).toBe(undefined) + expect(error?.message).toBeTruthy() +}) test("fail with string returns an error with that string as the message", async () => { - const [error, result] = await mightFail(Promise.reject("error")); - expect(result).toBe(undefined); - expect(error?.message).toBe("error"); -}); + const [error, result] = await mightFail(Promise.reject("error")) + expect(result).toBe(undefined) + expect(error?.message).toBe("error") +}) test("fail with object returns an error with that object as the message", async () => { - const [error, result] = await mightFail(Promise.reject({ message: "error" })); - expect(result).toBe(undefined); - expect(error?.message).toBe("error"); -}); + const [error, result] = await mightFail(Promise.reject({ message: "error" })) + expect(result).toBe(undefined) + expect(error?.message).toBe("error") +}) test("async function that throws", async () => { const asyncFn = async () => { - throw new Error("async error"); - }; - const [error, result] = await mightFail(asyncFn()); - expect(result).toBe(undefined); - expect(error?.message).toBe("async error"); -}); + throw new Error("async error") + } + const [error, result] = await mightFail(asyncFn()) + expect(result).toBe(undefined) + expect(error?.message).toBe("async error") +}) test("promise that resolves after delay", async () => { - const delayedPromise = new Promise((resolve) => - setTimeout(() => resolve("delayed success"), 100) - ); - const [error, result] = await mightFail(delayedPromise); - expect(result).toBe("delayed success"); - expect(error).toBe(undefined); -}); + const delayedPromise = new Promise((resolve) => setTimeout(() => resolve("delayed success"), 100)) + const [error, result] = await mightFail(delayedPromise) + expect(result).toBe("delayed success") + expect(error).toBe(undefined) +}) test("promise that rejects after delay", async () => { - const delayedPromise = new Promise((_, reject) => - setTimeout(() => reject("delayed error"), 100) - ); - const [error, result] = await mightFail(delayedPromise); - expect(result).toBe(undefined); - expect(error?.message).toBe("delayed error"); -}); + const delayedPromise = new Promise((_, reject) => setTimeout(() => reject("delayed error"), 100)) + const [error, result] = await mightFail(delayedPromise) + expect(result).toBe(undefined) + expect(error?.message).toBe("delayed error") +}) -describe('promise concurrent method wrappers', () => { - describe('mightFail.all', () => { - it('should resolve with all values when all promises succeed', async () => { - const promises = [Promise.resolve(1), Promise.resolve(2), Promise.resolve(3)]; - const [error, result] = await mightFail.all(promises); - expect(error).toBeUndefined(); - expect(result).toEqual([1, 2, 3]); - }); +describe("promise concurrent method wrappers", () => { + describe("mightFail.all", () => { + it("should resolve with all values when all promises succeed", async () => { + const promises = [Promise.resolve(1), Promise.resolve(2), Promise.resolve(3)] + const [error, result] = await mightFail.all(promises) + expect(error).toBeUndefined() + expect(result).toEqual([1, 2, 3]) + }) - it('should return an error if any promise fails', async () => { - const promises = [Promise.resolve(1), Promise.reject(new Error('Test Error')), Promise.resolve(3)]; - const [error, result] = await mightFail.all(promises); - expect(result).toBeUndefined(); - expect(error).toBeInstanceOf(Error); - expect(error!.message).toBe('Test Error'); - }); - }); + it("should return an error if any promise fails", async () => { + const promises = [Promise.resolve(1), Promise.reject(new Error("Test Error")), Promise.resolve(3)] + const [error, result] = await mightFail.all(promises) + expect(result).toBeUndefined() + expect(error).toBeInstanceOf(Error) + expect(error!.message).toBe("Test Error") + }) + }) - describe('mightFail.race', () => { - it('should resolve with the first resolved value', async () => { - const promises = [Promise.resolve(42), new Promise(resolve => setTimeout(() => resolve(100), 100))]; - const [error, result] = await mightFail.race(promises); - expect(error).toBeUndefined(); - expect(result).toBe(42); - }); + describe("mightFail.race", () => { + it("should resolve with the first resolved value", async () => { + const promises = [Promise.resolve(42), new Promise((resolve) => setTimeout(() => resolve(100), 100))] + const [error, result] = await mightFail.race(promises) + expect(error).toBeUndefined() + expect(result).toBe(42) + }) - it('should return an error if the first promise to settle is a rejection', async () => { - const promises = [Promise.reject(new Error('Race Error')), Promise.resolve(100)]; - const [error, result] = await mightFail.race(promises); - expect(result).toBeUndefined(); - expect(error).toBeInstanceOf(Error); - expect(error!.message).toBe('Race Error'); - }); - }); + it("should return an error if the first promise to settle is a rejection", async () => { + const promises = [Promise.reject(new Error("Race Error")), Promise.resolve(100)] + const [error, result] = await mightFail.race(promises) + expect(result).toBeUndefined() + expect(error).toBeInstanceOf(Error) + expect(error!.message).toBe("Race Error") + }) + }) - describe('mightFail.allSettled', () => { - it('should resolve with all settled results, including fulfilled and rejected promises', async () => { - const promises = [Promise.resolve(1), Promise.reject(new Error('AllSettled Error')), Promise.resolve(3)]; - const [error, result] = await mightFail.allSettled(promises); - expect(error).toBeUndefined(); + describe("mightFail.allSettled", () => { + it("should resolve with all settled results, including fulfilled and rejected promises", async () => { + const promises = [Promise.resolve(1), Promise.reject(new Error("AllSettled Error")), Promise.resolve(3)] + const [error, result] = await mightFail.allSettled(promises) + expect(error).toBeUndefined() expect(result).toEqual([ - { status: 'fulfilled', value: 1 }, - { status: 'rejected', reason: new Error('AllSettled Error') }, - { status: 'fulfilled', value: 3 }, - ]); - }); - }); + { status: "fulfilled", value: 1 }, + { status: "rejected", reason: new Error("AllSettled Error") }, + { status: "fulfilled", value: 3 }, + ]) + }) + }) - describe('mightFail.any', () => { - it('should resolve with the first successful promise', async () => { - const promises = [Promise.reject(new Error('Error 1')), Promise.resolve(200), Promise.reject(new Error('Error 2'))]; - const [error, result] = await mightFail.any(promises); - expect(error).toBeUndefined(); - expect(result).toBe(200); - }); + describe("mightFail.any", () => { + it("should resolve with the first successful promise", async () => { + const promises = [ + Promise.reject(new Error("Error 1")), + Promise.resolve(200), + Promise.reject(new Error("Error 2")), + ] + const [error, result] = await mightFail.any(promises) + expect(error).toBeUndefined() + expect(result).toBe(200) + }) - it('should return an AggregateError if all promises fail', async () => { - const promises = [Promise.reject(new Error('Error 1')), Promise.reject(new Error('Error 2'))]; - const [error, result] = await mightFail.any(promises); - expect(result).toBeUndefined(); - expect(error).toBeInstanceOf(Error); - expect(error!.message).toBe('All promises were rejected'); - }); - }); + it("should return an AggregateError if all promises fail", async () => { + const promises = [Promise.reject(new Error("Error 1")), Promise.reject(new Error("Error 2"))] + const [error, result] = await mightFail.any(promises) + expect(result).toBeUndefined() + expect(error).toBeInstanceOf(Error) + expect(error!.message).toBe("All promises were rejected") + }) + }) }) diff --git a/test/tuple/mightFailSync.test.ts b/test/tuple/mightFailSync.test.ts index e0877a3..a24a91c 100644 --- a/test/tuple/mightFailSync.test.ts +++ b/test/tuple/mightFailSync.test.ts @@ -1,37 +1,37 @@ -import { expect, test } from "vitest"; -import { mightFailSync } from "../../src/tuple/index"; +import { expect, test } from "vitest" +import { mightFailSync } from "../../src/tuple/index" function somethingThatThrows(input: string) { if (!input) { - throw new Error("error"); + throw new Error("error") } - return { message: input }; + return { message: input } } test("success returns the response", async () => { - const [error, result] = mightFailSync(() => somethingThatThrows("success")); - expect(error).toBe(undefined); - expect(result?.message).toBe("success"); -}); + const [error, result] = mightFailSync(() => somethingThatThrows("success")) + expect(error).toBe(undefined) + expect(result?.message).toBe("success") +}) test("fail with error returns the error", async () => { - const [error, result] = mightFailSync(() => somethingThatThrows("")); - expect(result).toBe(undefined); - expect(error?.message).toBe("error"); -}); + const [error, result] = mightFailSync(() => somethingThatThrows("")) + expect(result).toBe(undefined) + expect(error?.message).toBe("error") +}) test("fail without error returns an error", async () => { const [error, result] = await mightFailSync(() => { - throw "a fit"; - }); - expect(result).toBe(undefined); - expect(error?.message).toBeTruthy(); -}); + throw "a fit" + }) + expect(result).toBe(undefined) + expect(error?.message).toBeTruthy() +}) test("fail with string returns an error with that string as the message", async () => { const [error, result] = await mightFailSync(() => { - throw "a fit"; - }); - expect(result).toBe(undefined); - expect(error?.message).toBe("a fit"); -}); + throw "a fit" + }) + expect(result).toBe(undefined) + expect(error?.message).toBe("a fit") +})