indicative-compiler
Version:
Indicative compiler to compile parsed schema into highly optimized functions
47 lines (46 loc) • 1.48 kB
JavaScript
/**
* @module compiler/sanitizer
*/
Object.defineProperty(exports, "__esModule", { value: true });
/*
* indicative-compiler
*
* (c) Harminder Virk <virk@adonisjs.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
const indicative_parser_1 = require("indicative-parser");
const TreeWalker_1 = require("../TreeWalker");
const ArrayWrapper_1 = require("./ArrayWrapper");
const SanitizationsRunner_1 = require("./SanitizationsRunner");
/**
* Compiles rules and messages schema to an array of top level
* functions highly optimized for speed.
*/
class Compiler {
constructor(schema, sanitizations) {
this.sanitizations = sanitizations;
this.parsedSchema = indicative_parser_1.rulesParser(schema);
}
/**
* Compiles the schema to an array of functions
*/
compile() {
return new TreeWalker_1.TreeWalker(
/**
* Consume each node inside the tree
*/
(field, _, rules, dotPath) => {
return new SanitizationsRunner_1.SanitizationsRunner(field, dotPath, rules, this.sanitizations);
},
/**
* Wraps array children, since the length of array is unknown.
*/
(index, field, children, dotPath) => {
return new ArrayWrapper_1.ArrayWrapper(field, index, children, dotPath);
}).walk(this.parsedSchema);
}
}
exports.Compiler = Compiler;
;