UNPKG

@mojotech/json-type-validation

Version:

runtime type checking and validation of untyped JSON data

104 lines 3.16 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); /** * Wraps values in an `Ok` type. * * Example: `ok(5) // => {ok: true, result: 5}` */ exports.ok = function (result) { return ({ ok: true, result: result }); }; /** * Typeguard for `Ok`. */ exports.isOk = function (r) { return r.ok === true; }; /** * Wraps errors in an `Err` type. * * Example: `err('on fire') // => {ok: false, error: 'on fire'}` */ exports.err = function (error) { return ({ ok: false, error: error }); }; /** * Typeguard for `Err`. */ exports.isErr = function (r) { return r.ok === false; }; /** * Create a `Promise` that either resolves with the result of `Ok` or rejects * with the error of `Err`. */ exports.asPromise = function (r) { return r.ok === true ? Promise.resolve(r.result) : Promise.reject(r.error); }; /** * Unwraps a `Result` and returns either the result of an `Ok`, or * `defaultValue`. * * Example: * ``` * Result.withDefault(5, number().run(json)) * ``` * * It would be nice if `Decoder` had an instance method that mirrored this * function. Such a method would look something like this: * ``` * class Decoder<A> { * runWithDefault = (defaultValue: A, json: any): A => * Result.withDefault(defaultValue, this.run(json)); * } * * number().runWithDefault(5, json) * ``` * Unfortunately, the type of `defaultValue: A` on the method causes issues * with type inference on the `object` decoder in some situations. While these * inference issues can be solved by providing the optional type argument for * `object`s, the extra trouble and confusion doesn't seem worth it. */ exports.withDefault = function (defaultValue, r) { return r.ok === true ? r.result : defaultValue; }; /** * Return the successful result, or throw an error. */ exports.withException = function (r) { if (r.ok === true) { return r.result; } else { throw r.error; } }; /** * Given an array of `Result`s, return the successful values. */ exports.successes = function (results) { return results.reduce(function (acc, r) { return (r.ok === true ? acc.concat(r.result) : acc); }, []); }; /** * Apply `f` to the result of an `Ok`, or pass the error through. */ exports.map = function (f, r) { return r.ok === true ? exports.ok(f(r.result)) : r; }; /** * Apply `f` to the result of two `Ok`s, or pass an error through. If both * `Result`s are errors then the first one is returned. */ exports.map2 = function (f, ar, br) { return ar.ok === false ? ar : br.ok === false ? br : exports.ok(f(ar.result, br.result)); }; /** * Apply `f` to the error of an `Err`, or pass the success through. */ exports.mapError = function (f, r) { return r.ok === true ? r : exports.err(f(r.error)); }; /** * Chain together a sequence of computations that may fail, similar to a * `Promise`. If the first computation fails then the error will propagate * through. If it succeeds, then `f` will be applied to the value, returning a * new `Result`. */ exports.andThen = function (f, r) { return r.ok === true ? f(r.result) : r; }; //# sourceMappingURL=result.js.map