@hazae41/result
Version:
Rust-like Result for TypeScript
482 lines (478 loc) • 12.9 kB
JavaScript
'use strict';
var option = require('@hazae41/option');
var errors = require('./errors.cjs');
class Err {
#inner;
/**
* A failure
* @param inner
*/
constructor(inner) {
this.#inner = inner;
}
/**
* Create an empty `Err`
* @returns `Err(void)`
*/
static void() {
return new Err(undefined);
}
/**
* Create an `Err`
* @param inner
* @returns `Err(inner)`
*/
static create(inner) {
return new Err(inner);
}
/**
* Create an `Err` with an `Error` inside
* @param message
* @param options
* @returns `Err<Error>`
*/
static error(message, options) {
return new Err(new Error(message, options));
}
get inner() {
return this.#inner;
}
[Symbol.dispose]() {
this.#inner[Symbol.dispose]();
}
async [Symbol.asyncDispose]() {
await this.#inner[Symbol.asyncDispose]();
}
/**
* Type guard for `Ok`
* @returns `true` if `Ok`, `false` if `Err`
*/
isOk() {
return false;
}
/**
* Returns true if the result is `Ok` and the value inside of it matches a predicate
* @param okPredicate
* @returns `true` if `Ok` and `await okPredicate(this.inner)`, `false` otherwise
*/
async isOkAnd(okPredicate) {
return false;
}
/**
* Returns true if the result is `Ok` and the value inside of it matches a predicate
* @param okPredicate
* @returns `true` if `Ok` and `await okPredicate(this.inner)`, `false` otherwise
*/
isOkAndSync(okPredicate) {
return false;
}
/**
* Type guard for `Err`
* @returns `true` if `Err`, `false` if `Ok`
*/
isErr() {
return true;
}
/**
* Returns true if the result is `Err` and the value inside of it matches a predicate
* @param errPredicate
* @returns `true` if `Err` and `await errPredicate(this.inner)`, `false` otherwise
*/
async isErrAnd(errPredicate) {
return await errPredicate(this.inner);
}
/**
* Returns true if the result is `Err` and the value inside of it matches a predicate
* @param errPredicate
* @returns `true` if `Err` and `await errPredicate(this.inner)`, `false` otherwise
*/
isErrAndSync(errPredicate) {
return errPredicate(this.inner);
}
/**
* Compile-time safely get Ok's inner type
* @returns `this.inner`
* @throws if `this` is `Err`
*/
get() {
throw new errors.Panic();
}
/**
* Compile-time safely get Err's inner type
* @returns `this.inner`
* @throws if `this` is `Ok`
*/
getErr() {
return this.inner;
}
/**
* Get inner type
* @returns
*/
getAny() {
return this.inner;
}
/**
* Transform `Result<T, E>` into `Option<T>`
* @returns `Some(this.inner)` if `Ok`, `None` if `Err`
*/
ok() {
return new option.None();
}
/**
* Transform `Result<T, E>` into `Option<E>`
* @returns `Some(this.inner)` if `Err`, `None` if `Ok`
*/
err() {
return new option.Some(this.inner);
}
/**
* Returns an iterator over the possibly contained value
* @yields `this.inner` if `Ok`
*/
*[Symbol.iterator]() {
return;
}
/**
* Transform `Result<T,E>` into `[T,E]`
* @returns `[this.inner, undefined]` if `Ok`, `[undefined, this.inner]` if `Err`
*/
split() {
return [undefined, this.inner];
}
/**
* Returns true if the result is an `Ok` value containing the given value
* @param value
* @returns `true` if `Ok` and `this.inner === value`, `false` otherwise
*/
contains(value) {
return false;
}
/**
* Returns true if the result is an `Err` value containing the given value
* @param value
* @returns `true` if `Err` and `this.inner === value`, `false` otherwise
*/
containsErr(value) {
return this.inner === value;
}
/**
* Get the inner value or throw to the closest `Result.unthrow`
* @param thrower The thrower from `Result.unthrow`
* @returns `this.inner` if `Ok`
* @throws `undefined` if `Err`
* @see Result.unthrow
* @see Result.unthrowSync
*/
throw(thrower) {
thrower(this);
throw this;
}
/**
* Get the inner value if Ok or throw the inner error
* @returns `this.inner` if `Ok`
* @throws `this.inner` if `Err`
*/
getOrThrow() {
throw this.inner;
}
/**
* Get the inner error if Err or throw the inner value
* @returns `this.inner` if `Err`
* @throws `this.inner` if `Ok`
*/
getErrOrThrow() {
return this.inner;
}
/**
* Get the inner value if Ok or null if Err
* @returns `this.inner` if `Ok`, `null` if `Err`
*/
getOrNull() {
return null;
}
/**
* Get the inner error if Err or null if Ok
* @returns `this.inner` if `Err`, `null` if `Ok`
*/
getErrOrNull() {
return this.inner;
}
/**
* Get the inner value or a default one
* @param value
* @returns `this.inner` if `Ok`, `value` if `Err`
*/
getOr(value) {
return value;
}
/**
* Get the inner value or compute a default one from the inner error
* @param errMapper
* @returns `this.inner` if `Ok`, `await errMapper(this.inner)` if `Err`
* @throws if `await errMapper(this.inner)` throws
*/
async getOrElse(errMapper) {
return await errMapper(this.inner);
}
/**
* Get the inner value or compute a default one from the inner error
* @param errMapper
* @returns `this.inner` if `Ok`, `errMapper(this.inner)` if `Err`
* @throws if `errMapper(this.inner)` throws
*/
getOrElseSync(errMapper) {
return errMapper(this.inner);
}
/**
* Get this if Ok or throw the inner error
* @returns
*/
checkOrThrow() {
throw this.inner;
}
/**
* Get this if Err or throw the inner value
* @returns
*/
checkErrOrThrow() {
return this;
}
/**
* Get this if Ok or return null
* @returns
*/
checkOrNull() {
return null;
}
/**
* Get this if Err or return null
* @returns
*/
checkErrOrNull() {
return this;
}
/**
* Transform Result<Promise<T>, E> into Promise<Result<T, E>>
* @returns `await this.inner` if `Ok`, `this` if `Err`
*/
async await() {
return this;
}
/**
* Transform Result<T, Promise<E>> into Promise<Result<T, E>>
* @returns `await this.inner` if `Err`, `this` if `Ok`
*/
async awaitErr() {
return new Err(await this.inner);
}
/**
* Transform Result<Promise<T>, Promise<E>> into Promise<Result<T, E>>
* @returns `await this.inner`
*/
async awaitAll() {
return await this.awaitErr();
}
/**
* Transform `Result<T, E>` into `Result<void, E>`
* @returns `Ok<void>` if `Ok<T>`, `Err<E>` if `E<E>`
*/
clear() {
return this;
}
/**
* Transform `Result<T, E>` into `Result<T, void>`
* @returns `Ok<T>` if `Ok<T>`, `Err<void>` if `E<E>`
*/
clearErr() {
return Err.void();
}
/**
* Calls the given callback with the inner value if `Ok`
* @param okCallback
* @returns `this`
*/
async inspect(okCallback) {
return this;
}
/**
* Calls the given callback with the inner value if `Ok`
* @param okCallback
* @returns `this`
*/
inspectSync(okCallback) {
return this;
}
/**
* Calls the given callback with the inner value if `Err`
* @param errCallback
* @returns `this`
*/
async inspectErr(errCallback) {
await errCallback(this.inner);
return this;
}
/**
* Calls the given callback with the inner value if `Err`
* @param errCallback
* @returns `this`
*/
inspectErrSync(errCallback) {
errCallback(this.inner);
return this;
}
/**
* Return a new `Ok` but with the given `inner`
* @param inner
* @returns `Ok(inner)` if `Ok`, `this` if `Err`
*/
set(inner) {
return this;
}
/**
* Return a new `Err` but with the given `inner`
* @param inner
* @returns `Err(inner)` if `Err`, `this` if `Ok`
*/
setErr(inner) {
return new Err(inner);
}
/**
* Map the inner value into another
* @param okMapper
* @returns `Ok(await okMapper(this.inner))` if `Ok`, `this` if `Err`
* @throws if `await okMapper(this.inner)` throws
*/
async map(okMapper) {
return this;
}
/**
* Map the inner value into another
* @param okMapper
* @returns `Ok(okMapper(this.inner))` if `Ok`, `this` if `Err`
* @throws if `okMapper(this.inner)` throws
*/
mapSync(okMapper) {
return this;
}
/**
* Map the inner error into another
* @param errMapper
* @returns `Err(await errMapper(this.inner))` if `Err`, `this` if `Ok`
* @throws if `await errMapper(this.inner)` throws
*/
async mapErr(errMapper) {
return new Err(await errMapper(this.inner));
}
/**
* Map the inner error into another
* @param errMapper
* @returns `Err(errMapper(this.inner))` if `Err`, `this` if `Ok`
* @throws if `errMapper(this.inner)` throws
*/
mapErrSync(errMapper) {
return new Err(errMapper(this.inner));
}
/**
* Map the inner value into another, or a default one
* @param value
* @param okMapper
* @returns `await okMapper(this.inner)` if `Ok`, `value` if `Err`
* @throws if `await okMapper(this.inner)` throws
*/
async mapOr(value, okMapper) {
return value;
}
/**
* Map the inner value into another, or a default one
* @param value
* @param okMapper
* @returns `okMapper(this.inner)` if `Ok`, `value` if `Err`
* @throws if `okMapper(this.inner)` throws
*/
mapOrSync(value, okMapper) {
return value;
}
/**
* Map the inner value into another, or a default one
* @param errMapper
* @param okMapper
* @returns `await okMapper(this.inner)` if `Ok`, `await errMapper(this.inner)` if `Err`
* @throws if `await okMapper(this.inner)` or `await errMapper(this.inner)` throws
*/
async mapOrElse(errMapper, okMapper) {
return await errMapper(this.inner);
}
/**
* Map the inner value into another, or a default one
* @param errMapper
* @param okMapper
* @returns `okMapper(this.inner)` if `Ok`, `errMapper(this.inner)` if `Err`
* @throws if `okMapper(this.inner)` or `errMapper(this.inner)` throws
*/
mapOrElseSync(errMapper, okMapper) {
return errMapper(this.inner);
}
/**
* Return `value` if `Ok`, return `this` if `Err`
* @param value
* @returns `value` if `Ok`, `this` if `Err`
*/
and(value) {
return this;
}
/**
* Return `await okMapper(this.inner)` if `Ok`, return `this` if `Err`
* @param okMapper
* @returns `await okMapper(this.inner)` if `Ok`, `this` if `Err`
* @throws if `await okMapper(this.inner)` throws
*/
async andThen(okMapper) {
return this;
}
/**
* Return `okMapper(this.inner)` if `Ok`, return `this` if `Err`
* @param okMapper
* @returns `okMapper(this.inner)` if `Ok`, `this` if `Err`
* @throws if `okMapper(this.inner)` throws
*/
andThenSync(okMapper) {
return this;
}
/**
* Return `value` if `Err`, return `this` if `Ok`
* @param value
* @returns `value` if `Err`, `this` if `Ok`
*/
or(value) {
return value;
}
/**
* Return `await errMapper(this.inner)` if `Err`, return `this` if `Ok`
* @param errMapper
* @returns `await errMapper(this.inner)` if `Err`, `this` if `Ok`
* @throws if `await errMapper(this.inner)` throws
*/
async orElse(errMapper) {
return await errMapper(this.inner);
}
/**
* Return `errMapper(this.inner)` if `Err`, return `this` if `Ok`
* @param errMapper
* @returns `errMapper(this.inner)` if `Err`, `this` if `Ok`
* @throws if `errMapper(this.inner)` throws
*/
orElseSync(errMapper) {
return errMapper(this.inner);
}
/**
* Transform Result<Result<T, E1>, E2> into Result<T, E1 | E2>
* @param result
* @returns `this` if `Err`, `this.inner` if `Ok`
*/
flatten() {
return this;
}
}
exports.Err = Err;
//# sourceMappingURL=err.cjs.map