From 3ea64ecd003c4499b8f6c3d8187dd0a9d46653b6 Mon Sep 17 00:00:00 2001 From: Tomaz Haidar Date: Mon, 15 Apr 2024 17:55:04 -0300 Subject: [PATCH] add functional if statement --- src/result-async.ts | 17 +++++++++++++++++ src/result.ts | 15 +++++++++++++++ tests/result-test.ts | 39 +++++++++++++++++++++++++++++++++++++++ 3 files changed, 71 insertions(+) diff --git a/src/result-async.ts b/src/result-async.ts index 62cbc40..fce3b55 100644 --- a/src/result-async.ts +++ b/src/result-async.ts @@ -252,6 +252,23 @@ export class ResultAsync implements PromiseLike> { ) } + if(fCondition: (t: T) => boolean) { + return { + true: (fTrue: (t: T) => ResultAsync) => ({ + false: (fFalse: (t: T) => ResultAsync): ResultAsync => + new ResultAsync(this.innerPromise.then(async res => { + const condition = fCondition(res.value) + if (res.isOk()) { + return condition ? fTrue(res.value) : fFalse(res.value) + } + + return errAsync(res.error) + })), + + }), + } + } + orElse>( f: (e: E) => R, ): ResultAsync | T, InferErrTypes> diff --git a/src/result.ts b/src/result.ts index f13b91d..3724e5b 100644 --- a/src/result.ts +++ b/src/result.ts @@ -216,6 +216,21 @@ export class Result implements Resultable { return err(this.error) } + if(fCondition: (t: T) => boolean) { + return { + true: (fTrue: (t: T) => Result) => ({ + false: (fFalse: (t: T) => Result): Result => { + const condition = fCondition(this.value) + if (this.state.ok) { + return condition ? fTrue(this.value) : fFalse(this.value) + } + + return err(this.error) + }, + }), + } + } + /** * Takes an `Err` value and maps it to a `Result`. * diff --git a/tests/result-test.ts b/tests/result-test.ts index 546c705..8c29c7e 100644 --- a/tests/result-test.ts +++ b/tests/result-test.ts @@ -108,6 +108,24 @@ await describe('Result.Ok', async () => { }) }) + await describe('if', async () => { + await it('returns the "true" part of the statement', async () => { + const okVal = ok(12) + const result = okVal.if(val => val > 10) + .true(v => ok(`The number ${v} is gerater than 10`)) + .false(v => ok(`The number ${v} is smaller or equal to 10`)) + deepEqual(result, ok('The number 12 is gerater than 10')) + }) + + await it('returns the "false" part of the statement', async () => { + const okVal = ok(9) + const result = okVal.if(val => val > 10) + .true(v => ok(`The number ${v} is gerater than 10`)) + .false(v => ok(`The number ${v} is smaller or equal to 10`)) + deepEqual(result, ok('The number 9 is smaller or equal to 10')) + }) + }) + await describe('orElse', async () => { await it('Skips orElse on an Ok value', async () => { const okVal = ok(12) @@ -566,6 +584,27 @@ await describe('ResultAsync', async () => { }) }) + await describe('if', async () => { + await it('returns the "true" part of the statement', async () => { + const okVal = okAsync(12) + const result = okVal.if(val => val > 10) + .true(v => okAsync(`The number ${v} is gerater than 10`)) + .false(v => okAsync(`The number ${v} is smaller or equal to 10`)) + + isTrue(result instanceof ResultAsync) + deepEqual(result, okAsync('The number 12 is gerater than 10')) + }) + + await it('returns the "false" part of the statement', async () => { + const okVal = okAsync(9) + const result = okVal.if(val => val > 10) + .true(v => okAsync(`The number ${v} is gerater than 10`)) + .false(v => okAsync(`The number ${v} is smaller or equal to 10`)) + isTrue(result instanceof ResultAsync) + deepEqual(result, okAsync('The number 9 is smaller or equal to 10')) + }) + }) + await describe('orElse', async () => { await it('Skips orElse on an Ok value', async () => { const okVal = okAsync(12)