UNPKG

@nehemy/result-monad

Version:

A simple project to help developers work with Result type

167 lines 5.64 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.Result = void 0; var Result = /** @class */ (function () { function Result(valueOrError) { if (valueOrError instanceof Error) { this._error = valueOrError; this._value = undefined; } else { this._value = valueOrError; this._error = undefined; } ; } ; /** * Creates a failure result from an error instance. * @param error - The error instance. * @returns A new failure result containing the error. */ Result.fromError = function (error) { return new Result(error); }; ; /** * Retrieves the value of a result if it exists; otherwise, invokes a factory function to get a value. * @param factoryFunc - The function invoked when the result contains an error to produce a value. * @returns The value of the result if it is successful; otherwise, the value produced by the factory function. */ Result.prototype.orInvoke = function (factoryFunc) { return this._error === undefined ? this.value : factoryFunc(); }; ; /** * Retrieves the value of a result if it exists; otherwise, returns a default value. * @param defaultValue - The default value to be returned if the result is not successful. * @returns The value of the result if it is successful; otherwise, the provided default value. */ Result.prototype.or = function (defaultValue) { return this._error === undefined ? this.value : defaultValue; }; ; /** * Matches a result and calls the appropriate callback based on its success or failure. * @param onSuccess - Callback function to be called with the value of the result if it is successful. * @param onFailure - Callback function to be called with the error of the result if it is a failure. * @returns void */ Result.prototype.match = function (onSuccess, onFailure) { if (this._error !== undefined) { onFailure(this._error); } else if (this._value !== undefined) { onSuccess(this._value); } ; }; ; /** * Applies a transformation function to the value contained in a `successful result` * instance and returns a new Result with the transformed * value. If called on a `Failure` instance, it simply * returns a new Result instance with the same error. * @param transform - Transformation function. * @returns - A new Result instance. */ Result.prototype.map = function (transform) { return this.isSuccessful ? new Result(transform(this.value)) : new Result(this._error); }; ; /** * Applies a transformation function that takes a value and returns a new Result. * If called on a `successful result` instance, it applies the transformation * and returns the resulting Result. If called on a `failed result` instance, * it simply returns a new Failure instance with the same * error message. * @param transform - Transformation function. * @returns - A new Result instance. */ Result.prototype.flatMap = function (transform) { return this.isSuccessful ? transform(this.value) : new Result(this._error); }; ; Object.defineProperty(Result.prototype, "value", { /** * Retrieves the value of the result. * @throws An error if the result is not successful. * @returns The value of the result. */ get: function () { this.validate(); return this._value; }, enumerable: false, configurable: true }); ; Object.defineProperty(Result.prototype, "error", { /** * Retrieves the error of the result, if any. * @returns The error of the result, or undefined if the result is successful. */ get: function () { return this._error; }, enumerable: false, configurable: true }); ; Object.defineProperty(Result.prototype, "isSuccessful", { /** * Checks if the result is successful. * @returns True if the result is successful; false otherwise. */ get: function () { return this._error === undefined; }, enumerable: false, configurable: true }); ; Object.defineProperty(Result.prototype, "isNotSuccessful", { /** * Checks if the result is not successful (i.e., a failure). * @returns True if the result is not successful (a failure); false otherwise. */ get: function () { return !this.isSuccessful; }, enumerable: false, configurable: true }); ; Object.defineProperty(Result.prototype, "hasValue", { /** * Checks if the result has a value. * @returns True if the result has a value; false otherwise. */ get: function () { return this.isSuccessful; }, enumerable: false, configurable: true }); ; /** * Validates the result and throws an error if it is a failure. * @throws An error if the result is a failure. * @returns void */ Result.prototype.validate = function () { if (this._error) { throw new Error("Validation failed"); } ; }; ; return Result; }()); exports.Result = Result; ; //# sourceMappingURL=result.js.map