UNPKG

n4s

Version:

Assertion library for form validations

58 lines (52 loc) 2.02 kB
(function (global, factory) { typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory(require('n4s'), require('vest-utils')) : typeof define === 'function' && define.amd ? define(['n4s', 'vest-utils'], factory) : (global = typeof globalThis !== 'undefined' ? globalThis : global || self, global.compose = factory(global.n4s, global["vest-utils"])); })(this, (function (n4s, vestUtils) { 'use strict'; function ruleReturn(pass, message) { const output = { pass }; return output; } function failing() { return ruleReturn(false); } function passing() { return ruleReturn(true); } function defaultToPassing(callback) { return vestUtils.defaultTo(callback, passing()); } function runLazyRule(lazyRule, currentValue) { try { return lazyRule.run(currentValue); } catch (_a) { return failing(); } } /* eslint-disable max-lines-per-function */ function compose(...composites) { return vestUtils.assign((value) => { const res = run(value); vestUtils.invariant(res.pass, vestUtils.StringObject(res.message)); }, { run, test: (value) => run(value).pass, }); function run(value) { return n4s.ctx.run({ value }, () => { return defaultToPassing(vestUtils.mapFirst(composites, (composite, breakout) => { /* HACK: Just a small white lie. ~~HELP WANTED~~. The ideal is that instead of `LazyRuleRunners` We would simply use `Lazy` to begin with. The problem is that lazy rules can't really be passed to this function due to some generic hell so we're limiting it to a small set of functions. */ const res = runLazyRule(composite, value); breakout(!res.pass, res); })); }); } } return compose; })); //# sourceMappingURL=compose.development.js.map