UNPKG

@quenk/preconditions

Version:
106 lines 3.6 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.CompileContext = void 0; const record_1 = require("@quenk/noni/lib/data/record"); const array_1 = require("@quenk/noni/lib/data/array"); const allDisabled = { object: [], array: [], string: [], boolean: [], number: [] }; /** * CompileContext is a base class used to handle the stages of compiling a * schema. * * The functions provided in this interface are used to shape the precondition * tree created after parsing. */ class CompileContext { constructor(options) { this.options = options; /** * every joins each precondition in the list into one. */ this.every = (precs) => { let [prec] = precs; for (let i = 1; i < precs.length; i++) { prec = this.and(prec, precs[i]); } return prec; }; this.getPipeline = (schema) => schema[this.options.key] || []; /** * visitObject turns an object node into a single precondition. */ this.visitObject = (node) => { let [, [builtins, rec, addProps, precs = []]] = node; let builtinPrecs; if (!(0, array_1.empty)(builtins)) builtinPrecs = this.every(builtins); let props = this.properties(rec, addProps); let result; if (builtinPrecs && props) { result = this.and(builtinPrecs, props); } else if (builtinPrecs) { result = builtinPrecs; } else if (props) { result = props; } if (!(0, array_1.empty)(precs)) { let prec = this.every(precs); result = result ? this.and(result, prec) : prec; } return result ? result : this.identity; }; /** * visitArray turns an array node into a single precondition. */ this.visitArray = (node) => { let [, [builtins, items, precs = []]] = node; let result = items ? this.items(items) : this.identity; if (!(0, array_1.empty)(builtins)) result = this.and(this.every(builtins), result); if (!(0, array_1.empty)(precs)) result = this.and(result, this.every(precs)); return result; }; /** * visitPrim turns a primitive node into a single precondition. */ this.visitPrim = ([, precs]) => (0, array_1.empty)(precs) ? this.identity : this.every(precs); this.visit = (node) => { let type = node[0]; let result; if (type === 'object') result = this.visitObject(node); else if (type === 'array') result = this.visitArray(node); else result = this.visitPrim(node); return node[2] === true ? this.optional(result) : result; }; } get builtinsAvailable() { let { builtins } = this.options; if (builtins === false) { return allDisabled; } else if (builtins === true) { return {}; } else { let obj = (0, record_1.filter)(builtins, val => val === true); return (0, record_1.map)(obj, val => { if (val === false) return []; return val; }); } } } exports.CompileContext = CompileContext; //# sourceMappingURL=index.js.map