Skip to content

Commit

Permalink
Merge pull request #2 from inaiat/feat/functional-if-statement
Browse files Browse the repository at this point in the history
add functional if statement
  • Loading branch information
Tomazh authored Apr 15, 2024
2 parents 33e0a1c + 3ea64ec commit b1d3026
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 0 deletions.
17 changes: 17 additions & 0 deletions src/result-async.ts
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,23 @@ export class ResultAsync<T, E> implements PromiseLike<Result<T, E>> {
)
}

if(fCondition: (t: T) => boolean) {
return {
true: <X1, Y1>(fTrue: (t: T) => ResultAsync<X1, Y1>) => ({
false: <X2, Y2>(fFalse: (t: T) => ResultAsync<X2, Y2>): ResultAsync<X1 | X2, Y1 | Y2 | E> =>
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<R extends Result<unknown, unknown>>(
f: (e: E) => R,
): ResultAsync<InferOkTypes<R> | T, InferErrTypes<R>>
Expand Down
15 changes: 15 additions & 0 deletions src/result.ts
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,21 @@ export class Result<T, E> implements Resultable<T, E> {
return err(this.error)
}

if(fCondition: (t: T) => boolean) {
return {
true: <X1, Y1>(fTrue: (t: T) => Result<X1, Y1>) => ({
false: <X2, Y2>(fFalse: (t: T) => Result<X2, Y2>): Result<X1 | X2, Y1 | Y2 | E> => {
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<T, SomeNewType>`.
*
Expand Down
39 changes: 39 additions & 0 deletions tests/result-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit b1d3026

Please sign in to comment.