UNPKG

twitten

Version:

A fluent js/ts implementation of the result monad, utilizing Happy/Sad path terminology.

113 lines 3.87 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); var Sync; (function (Sync) { /** * @class An implementation of the result monad, utilizing Happy/Sad path terminiology. */ var Path = /** @class */ (function () { function Path(outcome) { this.outcome = outcome; } /** * Factory method for creating an instance of the `Path` class with a value. * @param value is the success value of the happy path. */ Path.happy = function (value) { var outcome; outcome = value; return new Path(outcome); }; /** * @description A happy path continuation. * @param callback a function that is executed only when the `outcome` of `Path` is happy. * @returns an instance of `Path<TNewValue>` where `TNewValue` is the return type of `continuation`. */ Path.prototype.onHappyPath = function (callback) { if (this.isHappy(this.outcome)) { return Path.happy(callback(this.outcome)); } else { return Path.sad(this.outcome); } }; /** * A helper function for type-checking an `Outcome`. * @param outcome the `outcome` of a `Path`. * @remark an alternative to using continuations. */ Path.prototype.isHappy = function (outcome) { return outcome.name === undefined; }; /** * A Factory method for creating an instance of the `Path` class with an error. * @param error is the error of the sad path. */ Path.sad = function (error) { var outcome; outcome = error; return new Path(outcome); }; /** * @description A sad path continuation. */ Path.prototype.onSadPath = function (callbackWithParam, callback) { if (this.isSad(this.outcome)) { if (callback !== undefined) { callback(); } else if (callbackWithParam !== undefined) { callbackWithParam(this.outcome); } } return this; }; /** * A helper function for type-checking an `Outcome`. * @param outcome the `outcome` of a `Path`. * @remark an alternative to using continuations. */ Path.prototype.isSad = function (outcome) { return outcome.name !== undefined; }; return Path; }()); Sync.Path = Path; })(Sync = exports.Sync || (exports.Sync = {})); /** * @description A Wrapper around path, so that happy paths can be created more fluent. * @example Happy.path(<value>); */ exports.Happy = { path: Sync.Path.happy }; /** * @description A Wrapper around path, so that sad paths can be created more fluent. * @example Sad.path(<error>); */ exports.Sad = { path: Sync.Path.sad }; var Async; (function (Async) { /** * @description Wrapper around `Promise<T>` */ var Path = /** @class */ (function () { function Path(promise) { this.promise = promise; } Path.fromPromise = function (promise) { return new Path(promise); }; Path.prototype.onHappyPath = function (callback) { return new Path(this.promise.then(callback)); }; Path.prototype.onSadPath = function (callbackWithParam, callback) { if (callback) this.promise.catch(callback); else this.promise.catch(callbackWithParam); return this; }; return Path; }()); Async.Path = Path; })(Async = exports.Async || (exports.Async = {})); //# sourceMappingURL=index.js.map