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