n4s
Version:
Assertion library for form validations
55 lines (49 loc) • 1.58 kB
JavaScript
var n4s = require('n4s');
var vestUtils = require('vest-utils');
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);
}));
});
}
}
module.exports = compose;
//# sourceMappingURL=compose.development.js.map
;