@consolidados/results
Version:
Result types, ease and simple
233 lines (230 loc) • 7.39 kB
JavaScript
// 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;
export { Err2 as Err, Ok2 as Ok };
//# sourceMappingURL=index.js.map
//# sourceMappingURL=index.js.map