-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Fix guard types * Fix fn types * Fix fn types * Update option type * Add explicit types to some, none * Remove get tests * Fix promise types * Remove random consolelog
- Loading branch information
Showing
19 changed files
with
366 additions
and
303 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,24 +1,40 @@ | ||
import type { | ||
ResultValueType, | ||
ResultErrorType, | ||
AsyncResultValueType, | ||
AsyncResultErrorType, | ||
} from "../util" | ||
import type {Result} from "../result/interface" | ||
import type {Result} from "../result/result" | ||
import {PromiseResult} from "../result/promise" | ||
import type {Err, Ok} from "../internal" | ||
|
||
export function fn<T extends (...args: any[]) => Result<any, any>>( | ||
f: T, | ||
): (...args: Parameters<T>) => Result<ResultValueType<T>, ResultErrorType<T>> { | ||
export function fn<A extends any[], T>(f: (...args: A) => Ok<T>): (...args: A) => Result<T, never> | ||
export function fn<A extends any[], E>(f: (...args: A) => Err<E>): (...args: A) => Result<never, E> | ||
export function fn<A extends any[], T, E>( | ||
f: (...args: A) => Ok<T> | Err<E>, | ||
): (...args: A) => Result<T, E> | ||
export function fn<A extends any[], T, E>( | ||
f: (...args: A) => Result<T, E>, | ||
): (...args: A) => Result<T, E> | ||
export function fn<A extends any[], T, E>( | ||
f: (...args: A) => Result<T, E>, | ||
): (...args: A) => Result<T, E> { | ||
return f | ||
} | ||
|
||
export function asyncFn< | ||
T extends | ||
| ((...args: any[]) => PromiseResult<any, any>) | ||
| ((...args: any[]) => Promise<Result<any, any>>), | ||
>(f: T) { | ||
return function (...args: Parameters<T>) { | ||
return new PromiseResult<AsyncResultValueType<T>, AsyncResultErrorType<T>>(f(...args)) | ||
export function asyncFn<A extends any[], T>( | ||
f: (...args: A) => Promise<Ok<T>>, | ||
): (...args: A) => PromiseResult<T, never> | ||
export function asyncFn<A extends any[], E>( | ||
f: (...args: A) => Promise<Err<E>>, | ||
): (...args: A) => PromiseResult<never, E> | ||
export function asyncFn<A extends any[], T, E>( | ||
f: (...args: A) => Promise<Ok<T> | Err<E>>, | ||
): (...args: A) => PromiseResult<T, E> | ||
export function asyncFn<A extends any[], T, E>( | ||
f: (...args: A) => Promise<Result<T, E>>, | ||
): (...args: A) => PromiseResult<T, E> | ||
export function asyncFn<A extends any[], T, E>( | ||
f: (...args: A) => PromiseResult<T, E>, | ||
): (...args: A) => PromiseResult<T, E> | ||
export function asyncFn<A extends any[], T, E>( | ||
f: (...args: A) => Promise<Ok<T> | Err<E> | Result<T, E>> | PromiseResult<T, E>, | ||
): (...args: A) => PromiseResult<T, E> { | ||
return function (...args: A) { | ||
return new PromiseResult<T, E>(f(...args)) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,29 +1,32 @@ | ||
import type {ErrorHandler, ResultError} from "../error/result_error" | ||
import {type ErrorHandler, type ResultError} from "../error/result_error" | ||
import {tryAsyncFn, tryAsyncFnWith, tryFn, tryFnWith} from "./try" | ||
|
||
type Fn = (...args: any[]) => any | ||
type AsyncFn = (...args: any[]) => Promise<any> | ||
|
||
export function guard<T extends Fn>(f: T) { | ||
return function (...args: Parameters<T>) { | ||
return tryFn<ReturnType<T>>(() => f(...args)) | ||
export function guard<A extends any[], T>(f: (...args: A) => T) { | ||
return function (...args: A) { | ||
return tryFn<T>(() => f(...args)) | ||
} | ||
} | ||
|
||
export function guardWith<T extends Fn, E extends ResultError>(f: T, h: ErrorHandler<E>) { | ||
return function (...args: Parameters<T>) { | ||
return tryFnWith<ReturnType<T>, E>(() => f(...args), h) | ||
export function guardWith<A extends any[], T, E extends ResultError<Error | null>>( | ||
f: (...args: A) => T, | ||
h: ErrorHandler<E>, | ||
) { | ||
return function (...args: A) { | ||
return tryFnWith<T, E>(() => f(...args), h) | ||
} | ||
} | ||
|
||
export function guardAsync<T extends AsyncFn>(f: T) { | ||
return function (...args: Parameters<T>) { | ||
return tryAsyncFn<Awaited<ReturnType<T>>>(() => f(...args)) | ||
export function guardAsync<A extends any[], T>(f: (...args: A) => Promise<T>) { | ||
return function (...args: A) { | ||
return tryAsyncFn<T>(() => f(...args)) | ||
} | ||
} | ||
|
||
export function guardAsyncWith<T extends AsyncFn, E extends ResultError>(f: T, h: ErrorHandler<E>) { | ||
return function (...args: Parameters<T>) { | ||
return tryAsyncFnWith<Awaited<ReturnType<T>>, E>(() => f(...args), h) | ||
export function guardAsyncWith<A extends any[], T, E extends ResultError<Error | null>>( | ||
f: (...args: A) => Promise<T>, | ||
h: ErrorHandler<E>, | ||
) { | ||
return function (...args: A) { | ||
return tryAsyncFnWith<T, E>(() => f(...args), h) | ||
} | ||
} |
Oops, something went wrong.