@consolidados/results
Version:
Result types, ease and simple
301 lines (293 loc) • 8.77 kB
JavaScript
;
// src/helpers/match.ts
function match(matcher, cases) {
if ("isOk" in matcher && matcher.isOk()) {
if (!cases.Ok) throw new Error("Missing case for Ok");
return cases.Ok(matcher.unwrap());
}
if ("isErr" in matcher && matcher.isErr()) {
if (!cases.Err) throw new Error("Missing case for Err");
return cases.Err(matcher.unwrapErr());
}
if ("isSome" in matcher && matcher.isSome()) {
if (!cases.Some) throw new Error("Missing case for Some");
return cases.Some(matcher.unwrap());
}
if ("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");
}
global.match = match;
// src/option/__internal__/return-types/some.ts
var Some = 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;
}
/**
* 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;
}
};
// src/option/__internal__/return-types/none.ts
var None = class _None {
/**
* 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");
}
/**
* 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 A new `None` option.
*/
map(_fn) {
return new _None();
}
/**
* 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 A new `None` option.
*/
flatMap(_fn) {
return new _None();
}
/**
* 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;
}
};
// src/option/option.ts
function Some2(value) {
return new Some(value);
}
function None2() {
return new None();
}
global.Some = Some2;
global.None = None2;
// src/result/__internal__/return-types/err.ts
var Err = class _Err extends Error {
error;
/**
* Creates a new `Err` instance with the given error value.
* @param error The error to wrap in the `Err` instance.
*/
constructor(error) {
super(typeof error === "string" ? error : error.message);
this.error = typeof error === "string" ? new Error(error) : error;
Object.setPrototypeOf(this, _Err.prototype);
if (Error.captureStackTrace) {
Error.captureStackTrace(this, _Err);
}
}
/**
* 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.
* @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`.
* @param _fn The transformation function (ignored in this implementation).
* @returns The original `Err` instance.
*/
flatMap(_fn) {
return this;
}
/**
* Retrieves the error value contained in this `Err`.
* @returns The error value contained in this `Err`.
*/
unwrapErr() {
return this.error;
}
};
// 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;
}
/**
* 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`.
* @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`).
* @param _fn The mapping function for errors (not used).
* @returns The original `Ok` instance.
*/
// mapErr<U extends Error | string>(fn: (err: U) => U): Result<T, never> {
// return this;
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");
}
};
// src/result/result.ts
function Ok2(value) {
return new Ok(value);
}
function Err2(error) {
return new Err(error);
}
global.Ok = Ok2;
global.Err = Err2;
exports.Err = Err2;
exports.None = None2;
exports.Ok = Ok2;
exports.Some = Some2;
exports.match = match;
//# sourceMappingURL=index.cjs.map
//# sourceMappingURL=index.cjs.map