UNPKG

@matatbread/typia

Version:

Superfast runtime validators with only one line

28 lines (25 loc) 1.02 kB
import ts from 'typescript'; import { template_to_pattern } from './template_to_pattern.mjs'; /** @internal */ const check_template = (props) => { // TYPEOF STRING & TAGS const conditions = [ ts.factory.createStrictEquality(ts.factory.createStringLiteral("string"), ts.factory.createTypeOfExpression(props.input)), ]; // TEMPLATES const internal = props.templates.map((tpl) => ts.factory.createCallExpression(ts.factory.createIdentifier(`RegExp(/${template_to_pattern({ top: true, template: tpl.row, })}/).test`), undefined, [props.input])); conditions.push(internal.length === 1 ? internal[0] : internal.reduce((x, y) => ts.factory.createLogicalOr(x, y))); // COMBINATION return { expression: conditions.reduce((x, y) => ts.factory.createLogicalAnd(x, y)), conditions: [], expected: props.templates.map((tpl) => tpl.getName()).join(" | "), }; }; export { check_template }; //# sourceMappingURL=check_template.mjs.map