UNPKG

@quenk/preconditions

Version:
73 lines 2.59 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.compile = exports.StringContext = void 0; const maybe_1 = require("@quenk/noni/lib/data/maybe"); const record_1 = require("@quenk/noni/lib/data/record"); const array_1 = require("@quenk/noni/lib/data/array"); const type_1 = require("@quenk/noni/lib/data/type"); const parse_1 = require("../parse"); const _1 = require("."); /** * StringContext is used for compilation of a schema to a synchronous * precondition string of code. */ class StringContext extends _1.CompileContext { constructor() { super(...arguments); this.identity = 'base.identity'; this.optional = (code) => `base.optional(${code})`; this.and = (left, right) => `base.and(${left},${right})`; this.or = (left, right) => `base.or(${left},${right})`; this.properties = (props, addProps = '') => { let obj = [ '{', (0, record_1.mapTo)(props, (code, key) => `${key} : ${code}`).join(','), '}' ].join(''); let propPrec = `object.${this.options.propMode}`; let args = [propPrec, obj, addProps].filter(a => a).join(','); return `object.schemaProperties(${args})`; }; this.items = (prec) => `array.map(${prec})`; this.get = (spec) => { if (Array.isArray(spec)) { let [path, args] = spec; return (0, maybe_1.just)((0, array_1.empty)(args) ? path : [ path, '(', args.map(val => JSON.stringify(val)).join(','), ')' ].join('')); } else if ((0, type_1.isString)(spec)) { return (0, maybe_1.just)(spec); } else { return (0, maybe_1.nothing)(); } }; } } exports.StringContext = StringContext; const defaultOptions = (opts) => (0, record_1.merge)(opts.async ? { key: 'asyncPreconditions', propMode: 'restrict', builtins: false } : { key: 'preconditions', propMode: 'restrict', builtins: true }, opts); /** * compile a schema into a string. * * This function produces a string that can be used for code generation from * templates. */ const compile = (opts, schema) => (0, parse_1.parse)(new StringContext(defaultOptions(opts)), schema); exports.compile = compile; //# sourceMappingURL=string.js.map