UNPKG

@zondax/ts-results

Version:

A typescript implementation of Rust's Result and Option objects. (Forked)

208 lines 6.14 kB
import { toString } from './utils'; /** * Contains the error value */ var ErrImpl = /** @class */ (function () { function ErrImpl(val) { if (!(this instanceof ErrImpl)) { return new ErrImpl(val); } this.ok = false; this.err = true; this.val = val; } ErrImpl.prototype[Symbol.iterator] = function () { return { next: function () { return { done: true, value: undefined }; }, }; }; /** * @deprecated in favor of unwrapOr * @see unwrapOr */ ErrImpl.prototype.else = function (val) { return val; }; ErrImpl.prototype.unwrapOr = function (val) { return val; }; ErrImpl.prototype.expect = function (msg) { throw new Error(msg + " - Error: " + toString(this.val)); }; ErrImpl.prototype.unwrap = function () { throw new Error("Tried to unwrap Error: " + toString(this.val)); }; ErrImpl.prototype.map = function (_mapper) { return this; }; ErrImpl.prototype.andThen = function (op) { return this; }; ErrImpl.prototype.mapErr = function (mapper) { return new Err(mapper(this.val)); }; ErrImpl.prototype.toString = function () { return "Err(" + toString(this.val) + ")"; }; /** An empty Err */ ErrImpl.EMPTY = new ErrImpl(undefined); return ErrImpl; }()); export { ErrImpl }; // This allows Err to be callable - possible because of the es5 compilation target export var Err = ErrImpl; /** * Contains the success value */ var OkImpl = /** @class */ (function () { function OkImpl(val) { if (!(this instanceof OkImpl)) { return new OkImpl(val); } this.ok = true; this.err = false; this.val = val; } /** * Helper function if you know you have an Ok<T> and T is iterable */ OkImpl.prototype[Symbol.iterator] = function () { var obj = Object(this.val); return Symbol.iterator in obj ? obj[Symbol.iterator]() : { next: function () { return { done: true, value: undefined }; }, }; }; /** * @see unwrapOr * @deprecated in favor of unwrapOr */ OkImpl.prototype.else = function (_val) { return this.val; }; OkImpl.prototype.unwrapOr = function (_val) { return this.val; }; OkImpl.prototype.expect = function (_msg) { return this.val; }; OkImpl.prototype.unwrap = function () { return this.val; }; OkImpl.prototype.map = function (mapper) { return new Ok(mapper(this.val)); }; OkImpl.prototype.andThen = function (mapper) { return mapper(this.val); }; OkImpl.prototype.mapErr = function (_mapper) { return this; }; /** * Returns the contained `Ok` value, but never throws. * Unlike `unwrap()`, this method doesn't throw and is only callable on an Ok<T> * * Therefore, it can be used instead of `unwrap()` as a maintainability safeguard * that will fail to compile if the error type of the Result is later changed to an error that can actually occur. * * (this is the `into_ok()` in rust) */ OkImpl.prototype.safeUnwrap = function () { return this.val; }; OkImpl.prototype.toString = function () { return "Ok(" + toString(this.val) + ")"; }; OkImpl.EMPTY = new OkImpl(undefined); return OkImpl; }()); export { OkImpl }; // This allows Ok to be callable - possible because of the es5 compilation target export var Ok = OkImpl; export var Result; (function (Result) { /** * Parse a set of `Result`s, returning an array of all `Ok` values. * Short circuits with the first `Err` found, if any */ function all() { var results = []; for (var _i = 0; _i < arguments.length; _i++) { results[_i] = arguments[_i]; } var okResult = []; for (var _a = 0, results_1 = results; _a < results_1.length; _a++) { var result = results_1[_a]; if (result.ok) { okResult.push(result.val); } else { return result; } } return new Ok(okResult); } Result.all = all; /** * Parse a set of `Result`s, short-circuits when an input value is `Ok`. * If no `Ok` is found, returns an `Err` containing the collected error values */ function any() { var results = []; for (var _i = 0; _i < arguments.length; _i++) { results[_i] = arguments[_i]; } var errResult = []; // short-circuits for (var _a = 0, results_2 = results; _a < results_2.length; _a++) { var result = results_2[_a]; if (result.ok) { return result; } else { errResult.push(result.val); } } // it must be a Err return new Err(errResult); } Result.any = any; /** * Wrap an operation that may throw an Error (`try-catch` style) into checked exception style * @param op The operation function */ function wrap(op) { try { return new Ok(op()); } catch (e) { return new Err(e); } } Result.wrap = wrap; /** * Wrap an async operation that may throw an Error (`try-catch` style) into checked exception style * @param op The operation function */ function wrapAsync(op) { try { return op() .then(function (val) { return new Ok(val); }) .catch(function (e) { return new Err(e); }); } catch (e) { return Promise.resolve(new Err(e)); } } Result.wrapAsync = wrapAsync; function isResult(val) { return val instanceof Err || val instanceof Ok; } Result.isResult = isResult; })(Result || (Result = {})); //# sourceMappingURL=result.js.map