Skip to content

Commit

Permalink
Added Result.match()
Browse files Browse the repository at this point in the history
  • Loading branch information
Your Name committed Apr 29, 2023
1 parent 37c3687 commit aeeb294
Show file tree
Hide file tree
Showing 11 changed files with 89 additions and 2 deletions.
3 changes: 3 additions & 0 deletions dist/result/api/match.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { ResultUnion } from "../interfaces.js";
export declare function match<T, E, U>(source: ResultUnion<T, E>, onOk: (v: T) => U, onErr: (e: E) => U): U;
//# sourceMappingURL=match.d.ts.map
1 change: 1 addition & 0 deletions dist/result/api/match.d.ts.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions dist/result/api/match.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
export function match(source, onOk, onErr) {
if (source.type === "Ok") {
return onOk(source.value);
}
else {
return onErr(source.value);
}
}
2 changes: 1 addition & 1 deletion dist/result/index.d.ts.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions dist/result/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import { unwrapErr } from "./api/unwrapErr.js";
import { unwrapErrOr } from "./api/unwrapErrOr.js";
import { unionOk } from "./api/unionOk.js";
import { unionErr } from "./api/unionErr.js";
import { match } from "./api/match.js";
export function createResult(result) {
const api = {
inner: () => result,
Expand All @@ -45,6 +46,7 @@ export function createResult(result) {
or: (new_value) => or(api, new_value),
ok: () => ok(api),
err: () => err(api),
match: (onOk, onErr) => match(result, onOk, onErr),
};
return api;
}
Expand Down
21 changes: 21 additions & 0 deletions dist/result/interfaces.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -248,5 +248,26 @@ export interface Result<T, Err> {
* ```
*/
err(): Option<Err>;
/**
* This function is functional alternative to hand-written conditions for `Result<T, Err>`
*
* It takes two functions as arguments:
* - What to return if Ok
* - What to return if Err
*
* # Notice
* Both functions must return the same type
*
* # Example
* ```ts
* const data = Ok("coolvalue");
* const result = match(
* (v) => v + "!",
* (err) => err
* );
* assert(result, "coolvalue!");
* ```
*/
match<U>(onOk: (v: T) => U, onErr: (e: Err) => U): U;
}
//# sourceMappingURL=interfaces.d.ts.map
2 changes: 1 addition & 1 deletion dist/result/interfaces.d.ts.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 13 additions & 0 deletions src/result/api/match.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { ResultUnion } from "../interfaces.js";

export function match<T, E, U>(
source: ResultUnion<T, E>,
onOk: (v: T) => U,
onErr: (e: E) => U
): U {
if (source.type === "Ok") {
return onOk(source.value);
} else {
return onErr(source.value);
}
}
2 changes: 2 additions & 0 deletions src/result/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import { unwrapErrOr } from "./api/unwrapErrOr.js";
import type { Result, ResultUnion, Ok, Err } from "./interfaces.js";
import { unionOk } from "./api/unionOk.js";
import { unionErr } from "./api/unionErr.js";
import { match } from "./api/match.js";

export function createResult<T, E>(result: ResultUnion<T, E>): Result<T, E> {
const api: Result<T, E> = {
Expand All @@ -48,6 +49,7 @@ export function createResult<T, E>(result: ResultUnion<T, E>): Result<T, E> {
or: (new_value) => or(api, new_value),
ok: () => ok(api),
err: () => err(api),
match: (onOk, onErr) => match(result, onOk, onErr),
};
return api;
}
Expand Down
21 changes: 21 additions & 0 deletions src/result/interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -250,4 +250,25 @@ export interface Result<T, Err> {
* ```
*/
err(): Option<Err>;
/**
* This function is functional alternative to hand-written conditions for `Result<T, Err>`
*
* It takes two functions as arguments:
* - What to return if Ok
* - What to return if Err
*
* # Notice
* Both functions must return the same type
*
* # Example
* ```ts
* const data = Ok("coolvalue");
* const result = match(
* (v) => v + "!",
* (err) => err
* );
* assert(result, "coolvalue!");
* ```
*/
match<U>(onOk: (v: T) => U, onErr: (e: Err) => U): U;
}
16 changes: 16 additions & 0 deletions src/result/result.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -167,3 +167,19 @@ test(`.tryFn()`, (t) => {
}).eq(Ok("good"))
);
});
test(`.match()`, (t) => {
t.is(
Ok("v").match(
(v) => v,
(e) => e
),
"v"
);
t.is(
Err("e").match(
(v) => v,
(e) => e
),
"e"
);
});

0 comments on commit aeeb294

Please sign in to comment.