UNPKG

@hazae41/result

Version:

Rust-like Result for TypeScript

469 lines (465 loc) 12.5 kB
'use strict'; var option = require('@hazae41/option'); var errors = require('./errors.cjs'); class Ok { #inner; /** * A success * @param inner */ constructor(inner) { this.#inner = inner; } /** * Create an empty `Ok` * @returns `Ok(void)` */ static void() { return new Ok(undefined); } /** * Create an `Ok` * @param inner * @returns `Ok(inner)` */ static new(inner) { return new Ok(inner); } 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 true; } /** * 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 await okPredicate(this.inner); } /** * 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 okPredicate(this.inner); } /** * Type guard for `Err` * @returns `true` if `Err`, `false` if `Ok` */ isErr() { return false; } /** * 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 false; } /** * 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 false; } /** * Compile-time safely get Ok's inner type * @returns `this.inner` * @throws if `this` is `Err` */ get() { return this.inner; } /** * Compile-time safely get Err's inner type * @returns `this.inner` * @throws if `this` is `Ok` */ getErr() { throw new errors.Panic(); } /** * 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.Some(this.inner); } /** * Transform `Result<T, E>` into `Option<E>` * @returns `Some(this.inner)` if `Err`, `None` if `Ok` */ err() { return new option.None(); } /** * Returns an iterator over the possibly contained value * @yields `this.inner` if `Ok` */ *[Symbol.iterator]() { yield this.inner; } /** * Transform `Result<T,E>` into `[T,E]` * @returns `[this.inner, undefined]` if `Ok`, `[undefined, this.inner]` if `Err` */ split() { return [this.inner, undefined]; } /** * 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 this.inner === value; } /** * 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 false; } /** * Just like `unwrap` but it throws to the closest `Result.unthrow` * @returns `this.inner` if `Ok` * @throws `this` if `Err` * @see Result.unthrow * @see Result.unthrowSync */ throw(thrower) { return this.inner; } /** * Get the inner value if Ok or throw the inner error * @returns `this.inner` if `Ok` * @throws `this.inner` if `Err` */ getOrThrow() { return this.inner; } /** * Get the inner error if Err or throw the inner value * @returns `this.inner` if `Err` * @throws `this.inner` if `Ok` */ getErrOrThrow() { throw this.inner; } /** * Get the inner value if Ok or null if Err * @returns `this.inner` if `Ok`, `null` if `Err` */ getOrNull() { return this.inner; } /** * Get the inner error if Err or null if Ok * @returns `this.inner` if `Err`, `null` if `Ok` */ getErrOrNull() { return null; } /** * Get the inner value or a default one * @param value * @returns `this.inner` if `Ok`, `value` if `Err` */ getOr(value) { return this.inner; } /** * 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 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 this.inner; } /** * Get this if Ok or throw the inner error * @returns */ checkOrThrow() { return this; } /** * Get this if Err or throw the inner value */ checkErrOrThrow() { throw this.inner; } /** * Get this if Ok or return null * @returns */ checkOrNull() { return this; } /** * Get this if Err or return null */ checkErrOrNull() { return null; } /** * Transform Result<Promise<T>, E> into Promise<Result<T, E>> * @returns `await this.inner` if `Ok`, `this` if `Err` */ async await() { return new Ok(await this.inner); } /** * Transform Result<T, Promise<E>> into Promise<Result<T, E>> * @returns `await this.inner` if `Err`, `this` if `Ok` */ async awaitErr() { return this; } /** * Transform Result<Promise<T>, Promise<E>> into Promise<Result<T, E>> * @returns `await this.inner` */ async awaitAll() { return await this.await(); } /** * Transform `Result<T, E>` into `Result<void, E>` * @returns `Ok<void>` if `Ok<T>`, `Err<E>` if `E<E>` */ clear() { return Ok.void(); } /** * Transform `Result<T, E>` into `Result<T, void>` * @returns `Ok<T>` if `Ok<T>`, `Err<void>` if `E<E>` */ clearErr() { return this; } /** * Calls the given callback with the inner value if `Ok` * @param okCallback * @returns `this` */ async inspect(okCallback) { await okCallback(this.inner); return this; } /** * Calls the given callback with the inner value if `Ok` * @param okCallback * @returns `this` */ inspectSync(okCallback) { okCallback(this.inner); return this; } /** * Calls the given callback with the inner value if `Err` * @param errCallback * @returns `this` */ async inspectErr(errCallback) { return this; } /** * Calls the given callback with the inner value if `Err` * @param errCallback * @returns `this` */ inspectErrSync(errCallback) { return this; } /** * Return a new `Ok` but with the given `inner` * @param inner * @returns `Ok(inner)` if `Ok`, `this` if `Err` */ set(inner) { return new Ok(inner); } /** * Return a new `Err` but with the given `inner` * @param inner * @returns `Err(inner)` if `Err`, `this` if `Ok` */ setErr(inner) { return this; } /** * 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 new Ok(await okMapper(this.inner)); } /** * 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 new Ok(okMapper(this.inner)); } /** * 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 this; } /** * 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 this; } /** * 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 await okMapper(this.inner); } /** * 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 okMapper(this.inner); } /** * 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 okMapper(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 okMapper(this.inner); } /** * Return `value` if `Ok`, return `this` if `Err` * @param value * @returns `value` if `Ok`, `this` if `Err` */ and(value) { return value; } /** * 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 await okMapper(this.inner); } /** * 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 okMapper(this.inner); } /** * Return `value` if `Err`, return `this` if `Ok` * @param value * @returns `value` if `Err`, `this` if `Ok` */ or(value) { return this; } /** * 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 this; } /** * 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 this; } /** * Transform Result<Result<T, E1>, E2> into Result<T, E1 | E2> * @param result * @returns `this` if `Err`, `this.inner` if `Ok` */ flatten() { return this.inner; } } exports.Ok = Ok; //# sourceMappingURL=ok.cjs.map