Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add Result#flatMapErr #22

Merged
merged 2 commits into from
Feb 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 35 additions & 10 deletions src/result.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,17 +48,42 @@ describe('Result', () => {
});

it('#flatMapOk', () => {
const matched = Result.Ok('anything')
.flatMapOk(s =>
s === 'something' ? Result.Ok(s) : Result.Err('Something failed'),
)
.match({
Err: err => err,
Ok: value => value,
});
const matched1 = Result.Ok('anything').flatMapOk(s =>
s === 'anything' ? Result.Ok(42) : Result.Err('No'),
);

expectTypeOf(matched).toEqualTypeOf<string>();
expect(matched).toBe('Something failed');
expectTypeOf(matched1).toEqualTypeOf<Result<number, 'No'>>();
expect(matched1.match({ Err: err => err, Ok: value => value })).toBe(42);

const matched2 = Result.Err('input-error').flatMapOk(value =>
value === 'anything' ? Result.Ok(42) : Result.Err('something'),
);

expectTypeOf(matched2).toEqualTypeOf<
Result<number, 'input-error' | 'something'>
>();

expect(matched2.match({ Err: err => err, Ok: value => value })).toBe(
'input-error',
);
});

it('#flatMapErr', () => {
const matched1 = Result.Err<'input-error' | 'critical-error'>(
'input-error',
).flatMapErr(err =>
err === 'input-error' ? Result.Ok(42) : Result.Err(err),
);

expectTypeOf(matched1).toEqualTypeOf<Result<number, 'critical-error'>>();
expect(matched1.match({ Err: err => err, Ok: value => value })).toBe(42);

const matched2 = Result.Ok('42').flatMapErr(err =>
err === 'input-error' ? Result.Ok(42) : Result.Err('error'),
);

expectTypeOf(matched2).toEqualTypeOf<Result<number | string, 'error'>>();
expect(matched2.match({ Err: err => err, Ok: value => value })).toBe('42');
});

it('#getOk', () => {
Expand Down
15 changes: 15 additions & 0 deletions src/result.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,21 @@ export class Result<T, E> {
});
}

/**
* Apply an arbitrary transformation to a failure value inside the box and
* return a new box with the result of that transformation.
* As opposed to `mapErr`, `f` is required to return a new box, not just a
* value.
* This can be useful if you want to turn `Result.Err` into `Result.Ok`
* depending on it's value
*/
flatMapErr<U, F>(f: (err: E) => Result<U, F>) {
return this.match<Result<T | U, F>>({
Err: err => f(err),
Ok: Result.Ok,
});
}

/**
* Turn this `Result` into a `Maybe` of a success value.
*/
Expand Down