Skip to content
Draft
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
594 changes: 0 additions & 594 deletions README.md

This file was deleted.

33 changes: 19 additions & 14 deletions benchmarks/tryblock.bench.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,28 @@
import { asyncFn } from "../src/fn.ts";
import { Ok, type Result } from "../src/result.ts";
import { runAsync } from "../src/run.ts";
import { tryBlockAsync } from "../src/try.ts";

// deno-lint-ignore require-await
const getOne = asyncFn(async (): Promise<Result<number, string>> => Ok(1));
function getOne() {
return runAsync(async (): Promise<Result<number, string>> => {
return Ok(1);
});
}

function chain() {
return getOne().map((n) => n + 1);
}

const af = asyncFn(async () => {
const one = await getOne();
if (one.isErr()) {
return one;
}
return Ok(one.expect("ok") + 1);
});
function runner() {
return runAsync(async (): Promise<Result<number, string>> => {
const one = await getOne();
if (one.isErr()) {
return one;
}
return Ok(one.unwrap() + 1);
});
}

function tba() {
function block() {
return tryBlockAsync(async function* () {
const one = yield* getOne();
return Ok(one + 1);
Expand All @@ -33,15 +38,15 @@ Deno.bench({
});

Deno.bench({
name: "asyncFn",
name: "runner",
fn: async () => {
await af();
await runner();
},
});

Deno.bench({
name: "tryBlockAsync",
fn: async () => {
await tba();
await block();
},
});
8 changes: 4 additions & 4 deletions example/main.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { db } from "./db.ts";
import { Err, Ok, Result } from "../src/result.ts";
import { AsyncResult } from "../src/async_result.ts";
import { ResultAsync } from "../src/result_async.ts";
import { asyncFn } from "../src/fn.ts";
import { None, Option, Some } from "../src/option.ts";
import { ErrorWithCause, Panic } from "../src/error.ts";
Expand Down Expand Up @@ -37,14 +37,14 @@ namespace DatabaseError {
export type DatabaseError = DatabaseError.ValidationError | DatabaseError.Unreachable;

// Chain API example:
function findGradesByStudentId(id: string): AsyncResult<Option<number[]>, DatabaseError> {
function findGradesByStudentId(id: string): ResultAsync<Option<number[]>, DatabaseError> {
return Result.fromPromise(db.findGradesByStudentId(id))
.map((grades) => (grades ? Some(grades) : None))
.mapErr(DatabaseError.from);
}

// Or you can use `asyncFn` to wrap functions that return `Promise<Result<T, E>>` to convert return type to `AsyncResult<T, E>`
// Inferred type is `(studentId: string) => AsyncResult<number, Error>`
// Or you can use `asyncFn` to wrap functions that return `Promise<Result<T, E>>` to convert return type to `ResultAsync<T, E>`
// Inferred type is `(studentId: string) => ResultAsync<number, Error>`
// @ts-ignore
const getAverageGrade = asyncFn(async (studentId: string) => {
const grades = await findGradesByStudentId(studentId)
Expand Down
18 changes: 0 additions & 18 deletions src/async_helpers.ts

This file was deleted.

42 changes: 0 additions & 42 deletions src/fn.ts

This file was deleted.

11 changes: 5 additions & 6 deletions src/mod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,12 @@
* @module
*/

export * from "./async_helpers.ts";
export * from "./async_option.ts";
export * from "./async_result.ts";
export * from "./error.ts";
export * from "./fn.ts";
export { None, Option, type OptionMatch, type OptionMatchAsync, Some } from "./option.ts";
export { Err, Ok, Result, type ResultMatch, type ResultMatchAsync } from "./result.ts";
export * from "./none_async.ts";
export * from "./option_async.ts";
export * from "./option.ts";
export * from "./result_async.ts";
export * from "./result.ts";
export * from "./run.ts";
export * from "./try.ts";
export * from "./unwind.ts";
Expand Down
6 changes: 6 additions & 0 deletions src/none_async.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
// NoneAsync alone is defined here to avoid circular dependency between option.ts and option_async.ts

import { None } from "./option.ts";
import { OptionAsync } from "./option_async.ts";

export const NoneAsync: OptionAsync<never> = new OptionAsync(Promise.resolve(None));
46 changes: 23 additions & 23 deletions src/option.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
* @module
*/

import { AsyncOption } from "./async_option.ts";
import { AsyncResult } from "./async_result.ts";
import { OptionAsync } from "./option_async.ts";
import { ResultAsync } from "./result_async.ts";
import { Panic } from "./error.ts";
import { Err, Ok, type Result } from "./result.ts";

Expand Down Expand Up @@ -39,7 +39,7 @@ export type OptionMatchAsync<T, A, B> = {
const unwrapSymbol = Symbol("unwrap");

/**
* @internal The internal implementation of `Option`.
* @internal The internal implementation of the `Option` union type.
*/
export class OptionImpl<T> {
private readonly _some: boolean;
Expand Down Expand Up @@ -301,7 +301,7 @@ export class OptionImpl<T> {
}

/**
* Maps an `Option<T>` to `AsyncOption<U>` by applying an async function to a contained value.
* Maps an `Option<T>` to `OptionAsync<U>` by applying an async function to a contained value.
*
* @param f - The async function to apply to the contained value.
* @returns The result of the async function application.
Expand All @@ -316,11 +316,11 @@ export class OptionImpl<T> {
* assertEquals(await x.mapAsync(async (s) => s.length), None)
* ```
*/
public mapAsync<U>(f: (value: T) => Promise<U>): AsyncOption<U> {
public mapAsync<U>(f: (value: T) => Promise<U>): OptionAsync<U> {
if (this._some) {
return new AsyncOption(f(this._value as T).then(Some));
return new OptionAsync(f(this._value as T).then(Some));
}
return new AsyncOption(Promise.resolve(None));
return new OptionAsync(Promise.resolve(None));
}

/**
Expand Down Expand Up @@ -372,11 +372,11 @@ export class OptionImpl<T> {
* await Option.fromNullish(list[5]).inspectAsync(async (x) => console.log(`got: ${x}`))
* ```
*/
public inspectAsync(f: (value: T) => Promise<void>): AsyncOption<T> {
public inspectAsync(f: (value: T) => Promise<void>): OptionAsync<T> {
if (this._some) {
return new AsyncOption(f(this._value as T).then(() => this as unknown as Some<T>));
return new OptionAsync(f(this._value as T).then(() => this as unknown as Some<T>));
}
return new AsyncOption(Promise.resolve(this as unknown as Some<T>));
return new OptionAsync(Promise.resolve(this as unknown as Some<T>));
}

/**
Expand Down Expand Up @@ -542,7 +542,7 @@ export class OptionImpl<T> {
}

/**
* Transforms the `Option<T>` into a `AsyncResult<T, E>`, mapping `Some(v)` to `Ok(v)` and `None` to `Err(err())`.
* Transforms the `Option<T>` into a `ResultAsync<T, E>`, mapping `Some(v)` to `Ok(v)` and `None` to `Err(err())`.
*
* @param err - The async function to compute the error to return if the option is `None`.
* @returns The result of the transformation.
Expand All @@ -556,11 +556,11 @@ export class OptionImpl<T> {
* assertEquals(await x.okOrElseAsync(async () => 0), Err(0))
* ```
*/
public okOrElseAsync<E>(f: () => Promise<E>): AsyncResult<T, E> {
public okOrElseAsync<E>(f: () => Promise<E>): ResultAsync<T, E> {
if (this._some) {
return new AsyncResult(Promise.resolve(Ok(this._value as T)));
return new ResultAsync(Promise.resolve(Ok(this._value as T)));
}
return new AsyncResult(f().then((err) => Err(err)));
return new ResultAsync(f().then((err) => Err(err)));
}

/**
Expand Down Expand Up @@ -668,11 +668,11 @@ export class OptionImpl<T> {
* assertEquals(item20, None)
* ```
*/
public andThenAsync<U>(f: (value: T) => Promise<Option<U>> | AsyncOption<U>): AsyncOption<U> {
public andThenAsync<U>(f: (value: T) => Promise<Option<U>> | OptionAsync<U>): OptionAsync<U> {
if (this._some) {
return new AsyncOption(f(this._value as T));
return new OptionAsync(f(this._value as T));
}
return new AsyncOption(Promise.resolve(None));
return new OptionAsync(Promise.resolve(None));
}

/**
Expand Down Expand Up @@ -726,15 +726,15 @@ export class OptionImpl<T> {
* assertEquals(await Some(4).filterAsync(isEven), Some(4))
* ```
*/
public filterAsync(predicate: (value: T) => Promise<boolean>): AsyncOption<T> {
public filterAsync(predicate: (value: T) => Promise<boolean>): OptionAsync<T> {
if (this._some) {
return new AsyncOption(
return new OptionAsync(
predicate(this._value as T).then((result) =>
result ? this as unknown as Option<T> : None
),
);
}
return new AsyncOption(Promise.resolve(None));
return new OptionAsync(Promise.resolve(None));
}

/**
Expand Down Expand Up @@ -808,11 +808,11 @@ export class OptionImpl<T> {
* assertEquals(await None.orElseAsync(nobody), None)
* ```
*/
public orElseAsync<U>(f: () => Promise<Option<U>> | AsyncOption<U>): AsyncOption<T | U> {
public orElseAsync<U>(f: () => Promise<Option<U>> | OptionAsync<U>): OptionAsync<T | U> {
if (this._some) {
return new AsyncOption(Promise.resolve(this as unknown as Option<T | U>));
return new OptionAsync(Promise.resolve(this as unknown as Option<T | U>));
}
return new AsyncOption(f());
return new OptionAsync(f());
}

/**
Expand Down
Loading