Skip to content

Commit

Permalink
Merge branch 'main' of github.com:bkiac/patina
Browse files Browse the repository at this point in the history
  • Loading branch information
bkiac committed Apr 29, 2024
2 parents dbcaa68 + efcddc5 commit 1fcdc11
Show file tree
Hide file tree
Showing 21 changed files with 590 additions and 856 deletions.
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
{
"name": "patina",
"name": "@patina/std",
"version": "0.1.0",
"type": "module",
"description": "Type-safe error-handling library for TypeScript",
"description": "Type-safe nothing-handling and error-handling library",
"repository": {
"type": "git",
"url": "git+https://github.com/bkiac/patina.git"
Expand Down
4 changes: 2 additions & 2 deletions scripts/benchmark.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,14 @@ function formatTime(ms: number) {

const a = asyncFn(async () => {
const one = await getOne();
if (one.isErr) {
if (one.isErr()) {
return one;
}
const rand = Math.random();
if (Math.random() < 0.5) {
return Err("error");
}
return Ok(rand + one.value);
return Ok(rand + one.value());
});

const b = asyncGenFn(async function* () {
Expand Down
27 changes: 19 additions & 8 deletions src/async_option.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {AsyncResult} from ".";
import type {Option, OptionMatch} from "./option";
import type {Option, OptionMatch, OptionMatchAsync} from "./option";

/**
* A promise that resolves to an `Option`.
Expand Down Expand Up @@ -33,6 +33,24 @@ export class AsyncOption<T> implements PromiseLike<Option<T>> {
);
}

/**
* Async version of `Option#match`.
*/
async match<A, B>(matcher: OptionMatch<T, A, B>): Promise<A | B> {
return (await this).match(matcher);
}

async matchAsync<A, B>(matcher: OptionMatchAsync<T, A, B>): Promise<A | B> {
return (await this).matchAsync(matcher);
}

/**
* Async version of `Option#value`.
*/
async value(): Promise<T | undefined> {
return (await this).value();
}

/**
* Async version of `Option#okOr`.
*/
Expand Down Expand Up @@ -157,11 +175,4 @@ export class AsyncOption<T> implements PromiseLike<Option<T>> {
this.then((thisOption) => other.then((otherOption) => thisOption.xor(otherOption))),
);
}

/**
* Async version of `Option#match`.
*/
async match<A, B>(matcher: OptionMatch<T, A, B>): Promise<A | B> {
return (await this).match(matcher);
}
}
28 changes: 20 additions & 8 deletions src/async_result.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {AsyncOption} from "./async_option";
import type {Result, ResultImpl, ResultMatch} from "./result";
import type {Result, ResultImpl, ResultMatch, ResultMatchAsync} from "./result";

/**
* A promise that resolves to a `Result`.
Expand Down Expand Up @@ -39,6 +39,25 @@ export class AsyncResult<T, E> implements PromiseLike<Result<T, E>> {
);
}

/**
* Async version of `Result#match`.
*/
async match<A, B>(matcher: ResultMatch<T, E, A, B>): Promise<A | B> {
return (await this).match(matcher);
}

async matchAsync<A, B>(matcher: ResultMatchAsync<T, E, A, B>): Promise<A | B> {
return (await this).matchAsync(matcher);
}

async value(): Promise<T | undefined> {
return (await this).value();
}

async error(): Promise<E | undefined> {
return (await this).error();
}

/**
* Async version of `Result#ok`.
*/
Expand Down Expand Up @@ -219,11 +238,4 @@ export class AsyncResult<T, E> implements PromiseLike<Result<T, E>> {
async unwrapOrElse<U>(defaultValue: (error: E) => U): Promise<T | U> {
return (await this).unwrapOrElse(defaultValue);
}

/**
* Async version of `Result#match`.
*/
async match<A, B>(matcher: ResultMatch<T, E, A, B>): Promise<A | B> {
return (await this).match(matcher);
}
}
2 changes: 0 additions & 2 deletions src/common.ts

This file was deleted.

10 changes: 5 additions & 5 deletions src/fn.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {type Result} from "./result";
import {AsyncResult} from "./async_result";
import {run, runAsync} from "./run";
import {trySync, tryAsync} from "./try";
import type {InferErr, InferOk} from "./util";

/**
Expand Down Expand Up @@ -67,11 +67,11 @@ export function asyncFn(f: any): any {
* })
* ```
*/
export function genFn<A extends any[], R extends Result<any, any>, T>(
export function gen<A extends any[], R extends Result<any, any>, T>(
fn: (...args: A) => Generator<R, T, any>,
): (...args: A) => Result<T, InferErr<R>> {
return function (...args: any[]) {
return run(() => fn(...(args as A)));
return trySync(() => fn(...(args as A)));
};
}

Expand All @@ -93,10 +93,10 @@ export function genFn<A extends any[], R extends Result<any, any>, T>(
* })
* ```
*/
export function asyncGenFn<A extends any[], R extends AsyncResult<any, any> | Result<any, any>, T>(
export function asyncGen<A extends any[], R extends AsyncResult<any, any> | Result<any, any>, T>(
fn: (...args: A) => AsyncGenerator<R, T, any>,
): (...args: A) => AsyncResult<T, InferErr<Awaited<R>>> {
return function (...args: any[]) {
return runAsync(() => fn(...(args as A)));
return tryAsync(() => fn(...(args as A)));
};
}
31 changes: 0 additions & 31 deletions src/guard.ts

This file was deleted.

2 changes: 0 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
export * from "./fn";
export * from "./guard";
export * from "./async_option";
export * from "./option";
export * from "./error";
export * from "./async_result";
export * from "./result";
export * from "./run";
export * from "./try";
export * from "./util";
Loading

0 comments on commit 1fcdc11

Please sign in to comment.