UNPKG

@consolidados/results

Version:
485 lines (478 loc) 15.5 kB
// src/helpers/match.ts function match(matcher, cases, discriminant) { if (typeof matcher === "string" || typeof matcher === "number" || typeof matcher === "symbol") { const handler = cases[matcher]; if (handler) { return handler(); } if (cases.default) { return cases.default(); } throw new Error(`No case found for value: ${String(matcher)}`); } if (typeof matcher === "object" && matcher !== null && !discriminant) { for (const key in cases) { if (key === "default") continue; if (key in matcher) { const handler = cases[key]; if (handler) { return typeof handler === "function" ? handler(matcher[key]) : handler(); } } } if (cases.default) { return cases.default(); } } if (discriminant && typeof matcher === "object" && matcher !== null) { const discriminantValue = matcher[discriminant]; const handler = cases[discriminantValue]; if (handler) { return handler(matcher); } if (cases.default) { return cases.default(matcher); } throw new Error( `No case found for discriminant value: ${String(discriminantValue)}` ); } if (typeof matcher === "object" && matcher !== null && "isOk" in matcher && matcher.isOk()) { if (!cases.Ok) throw new Error("Missing case for Ok"); return cases.Ok(matcher.unwrap()); } if (typeof matcher === "object" && matcher !== null && "isErr" in matcher && matcher.isErr()) { if (!cases.Err) throw new Error("Missing case for Err"); return cases.Err(matcher.unwrapErr()); } if (typeof matcher === "object" && matcher !== null && "isSome" in matcher && matcher.isSome()) { if (!cases.Some) throw new Error("Missing case for Some"); return cases.Some(matcher.unwrap()); } if (typeof matcher === "object" && matcher !== null && "isNone" in matcher && matcher.isNone()) { if (!cases.None) throw new Error("Missing case for None"); return cases.None(); } throw new Error("Invalid matcher or missing case"); } globalThis.match = match; // src/result/__internal__/return-types/err.ts var Err = class _Err { error; /** * Creates a new `Err` instance with the given error value. * @param error The error to wrap in the `Err` instance. Can be any type. */ constructor(error) { this.error = error; } /** * Checks if this result is an `Ok`. * @returns `false` because this is an `Err`. */ isOk() { return false; } /** * Checks if this result is an `Err`. * @returns `true` because this is an `Err`. */ isErr() { return true; } /** * Retrieves the value contained in this result. Since this is an `Err`, an error is thrown. * @throws An error because `unwrap` is called on an `Err`. */ unwrap() { throw new Error("Called unwrap on an Err value"); } /** * Maps the value (if any). Since this is an `Err`, the mapping function is ignored, and the original `Err` is returned. * @template U The type of the value (ignored for `Err`). * @param _fn The mapping function for values (not used). * @returns The original `Err` instance. */ map(_fn) { return this; } /** * Maps the error value using a transformation function and returns a new `Result` with the transformed error. * @template U The type of the transformed error. Can be any type. * @param fn The transformation function to apply to the error value. * @returns A new `Err` containing the transformed error. */ mapErr(fn) { return new _Err(fn(this.error)); } /** * Applies a transformation function that returns a `Result` to the value (which does not exist) of this `Err`. * @template U The type of the value in the resulting `Result`. * @template F The type of the error in the resulting `Result`. * @param _fn The transformation function (ignored in this implementation). * @returns The original `Err` instance cast to the new error type. */ flatMap(_fn) { return this; } /** * Retrieves the error value contained in this `Err`. * @returns The error value contained in this `Err`. */ unwrapErr() { return this.error; } /** * Returns the inner error. After an `isErr()` type guard, TypeScript narrows the return type to `E`. * Without a type guard, returns `T | E` (use `isOk()` or `isErr()` for type narrowing). * @returns The error value contained in this `Err`. */ value() { return this.error; } /** * Returns the contained value if `Ok`, otherwise returns the provided default value. * @template U The type of the default value. * @param defaultValue The value to return since this is an `Err`. * @returns The provided default value. */ unwrapOr(defaultValue) { return defaultValue; } /** * Returns the contained value if `Ok`, otherwise computes and returns the result of the provided function. * @template U The type of the default value. * @param fn The function to compute the default value. * @returns The result of calling the provided function with the error. */ unwrapOrElse(fn) { return fn(this.error); } /** * Returns this `Err` if it is `Err`, otherwise returns the result of the provided function. * @template T The type of the alternative success value. * @template F The type of the alternative error. * @param fn The function to compute the alternative result. * @returns The result of calling the provided function with the error. */ orElse(fn) { return fn(this.error); } /** * Converts `Result` type to `Option` type. * @returns `Some` if the result is `Ok`, `None` if the result is `Err`. */ ok() { return None(); } }; // src/result/__internal__/return-types/ok.ts var Ok = class _Ok { /** * Creates a new `Ok` instance with the given value. * @param _value The value to wrap in the `Ok` instance. */ constructor(_value) { this._value = _value; } /** * Checks if this result is an `Ok`. * @returns `true` because this is an `Ok`. */ isOk() { return true; } /** * Checks if this result is an `Err`. * @returns `false` because this is an `Ok`. */ isErr() { return false; } /** * Retrieves the value contained in this `Ok`. * @returns The value contained in this `Ok`. */ unwrap() { return this._value; } /** * Returns the inner value. After an `isOk()` type guard, TypeScript narrows the return type to `T`. * Without a type guard, returns `T | E` (use `isOk()` or `isErr()` for type narrowing). * @returns The value contained in this `Ok`. */ value() { return this._value; } /** * Applies a transformation function to the value contained in this `Ok` and returns a new `Result` with the transformed value. * @template U The type of the transformed value. * @param fn The transformation function to apply to the value. * @returns A new `Ok` containing the transformed value. */ map(fn) { return new _Ok(fn(this._value)); } /** * Applies a transformation function that returns a `Result` to the value contained in this `Ok`. * @template U The type of the value in the resulting `Result`. * @template E The type of the error in the resulting `Result`. * @param fn The transformation function to apply to the value. * @returns The result of applying the transformation function. */ flatMap(fn) { return fn(this._value); } /** * Maps the error value (if any). Since this is an `Ok`, the error mapping function is ignored, and the original `Ok` is returned. * @template U The type of the error (ignored for `Ok`). Can be any type. * @param _fn The mapping function for errors (not used). * @returns The original `Ok` instance. */ mapErr(_fn) { return this; } /** * Retrieves the error contained in this result. Since this is an `Ok`, an error is thrown. * @throws An error because `unwrapErr` is called on an `Ok`. */ unwrapErr() { throw new Error("Called unwrapErr on an Ok value"); } /** * Returns the contained value if `Ok`, otherwise returns the provided default value. * @template U The type of the default value. * @param defaultValue The value to return if this is an `Err`. * @returns The value contained in this `Ok`. */ unwrapOr(defaultValue) { return this._value; } /** * Returns the contained value if `Ok`, otherwise computes and returns the result of the provided function. * @template U The type of the default value. * @param fn The function to compute the default value. * @returns The value contained in this `Ok`. */ unwrapOrElse(fn) { return this._value; } /** * Returns this `Ok` if it is `Ok`, otherwise returns the result of the provided function. * @template F The type of the alternative error. * @param fn The function to compute the alternative result. * @returns The original `Ok` instance. */ orElse(fn) { return this; } /** * Converts `Result` type to `Option` type. * @returns `Some` if the result is `Ok`, `None` if the result is `Err`. */ ok() { return Some(this._value); } }; // src/result/result.ts function Ok2(value) { return new Ok(value); } function Err2(error) { return new Err(error); } globalThis.Ok = Ok2; globalThis.Err = Err2; // src/option/__internal__/return-types/some.ts var Some2 = class _Some { /** * Creates a new `Some` option with the given value. * @param _value The value to be wrapped in the `Some` option. */ constructor(_value) { this._value = _value; } /** * Checks if this option is a `Some`. * @returns `true` if this option is a `Some`, otherwise `false`. */ isSome() { return true; } /** * Checks if this option is a `None`. * @returns `false` because this is a `Some`. */ isNone() { return false; } /** * Unwraps the value held by this `Some` option. * @returns The value held by this `Some` option. */ unwrap() { return this._value; } /** * Returns the inner value. After an `isSome()` type guard, TypeScript narrows the return type to `T`. * Without a type guard, returns `T | undefined` (use `isSome()` or `isNone()` for type narrowing). * @returns The value held by this `Some` option. */ value() { return this._value; } /** * Applies a transformation function to the value held by this `Some` option and returns a new `Option` with the transformed value. * @template U The type of the transformed value. * @param fn The transformation function to apply to the value. * @returns A new `Some` option containing the transformed value. */ map(fn) { return new _Some(fn(this._value)); } /** * Applies a transformation function that returns an `Option` to the value held by this `Some` option. * @template U The type of the value in the resulting `Option`. * @param fn The transformation function to apply to the value. * @returns The result of applying the transformation function. */ flatMap(fn) { return fn(this._value); } /** * Returns the value held by this `Some` option, ignoring the default value provided. * @param _ A default value (ignored in this implementation). * @returns The value held by this `Some` option. */ unwrapOr(_) { return this._value; } /** * Returns the value held by this `Some` option, ignoring the function provided. * @template U The type of the default value. * @param _fn A function to compute the default value (ignored in this implementation). * @returns The value held by this `Some` option. */ unwrapOrElse(_fn) { return this._value; } /** * Converts this `Option` to a `Result`, using the provided error value if this is `None`. * @template E The type of the error. * @param _error The error value to use if this is `None` (ignored since this is `Some`). * @returns An `Ok` result containing the value from this `Some`. */ okOr(_error) { return Ok2(this._value); } /** * Filters this `Option` based on a predicate function. * @param predicate The function to test the value. * @returns This `Some` if the predicate returns true, otherwise `None`. */ filter(predicate) { return predicate(this._value) ? this : None2(); } }; // src/option/__internal__/return-types/none.ts var None3 = class { /** * Checks if this option is a `Some`. * @returns `false` because this is a `None`. */ isSome() { return false; } /** * Checks if this option is a `None`. * @returns `true` because this is a `None`. */ isNone() { return true; } /** * Attempts to unwrap the value from this `None` option. * @throws An error because `None` has no value. */ unwrap() { throw new Error("Called unwrap on a None value"); } /** * Returns `undefined` for a `None` option. After an `isNone()` type guard, TypeScript narrows the return type to `undefined`. * Without a type guard, returns `T | undefined` (use `isSome()` or `isNone()` for type narrowing). * Unlike `unwrap()`, this method does not throw an error. * @returns `undefined` because this is a `None`. */ value() { return void 0; } /** * Applies a transformation function to the value (which does not exist) of this `None` option. * @template U The type of the value that would have been returned. * @param _fn The transformation function (ignored in this implementation). * @returns The singleton `None` instance. */ map(_fn) { return None2(); } /** * Applies a transformation function that returns an `Option` to the value (which does not exist) of this `None` option. * @template U The type of the value in the resulting `Option`. * @param _fn The transformation function (ignored in this implementation). * @returns The singleton `None` instance. */ flatMap(_fn) { return None2(); } /** * Returns the default value provided, since `None` has no value. * @template T The type of the default value. * @param defaultValue The value to return. * @returns The default value provided. */ unwrapOr(defaultValue) { return defaultValue; } /** * Computes and returns the default value using the provided function, since `None` has no value. * @template T The type of the default value. * @param fn The function to compute the default value. * @returns The result of calling the provided function. */ unwrapOrElse(fn) { return fn(); } /** * Converts this `Option` to a `Result`, using the provided error value since this is `None`. * @template E The type of the error. * @param error The error value to use. * @returns An `Err` result containing the provided error. */ okOr(error) { return Err2(error); } /** * Filters this `Option` based on a predicate function. * @param _predicate The function to test the value (ignored since this is `None`). * @returns `None` since there is no value to filter. */ filter(_predicate) { return None2(); } }; // src/option/option.ts function Some3(value) { return new Some2(value); } var NONE_INSTANCE = null; function None2() { if (!NONE_INSTANCE) { NONE_INSTANCE = new None3(); } return NONE_INSTANCE; } globalThis.Some = Some3; globalThis.None = None2; export { Err2 as Err, None2 as None, Ok2 as Ok, Some3 as Some, match }; //# sourceMappingURL=index.js.map //# sourceMappingURL=index.js.map