@litert/typeguard
Version:
An easy and powerful data validation code generator by JavaScript.
100 lines • 4 kB
JavaScript
;
/**
* Copyright 2023 Angus Fenying <fenying@litert.org>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
Object.defineProperty(exports, "__esModule", { value: true });
exports.decodePreDefinedCallExpr = exports.validateTypeName = exports.EFlagValue = exports.EFlags = exports.KEY_EQUAL_SUFFIX = exports.KEY_STRICT_SUFFIX = exports.KEY_ARRAY_SUFFIX = exports.KEY_LIST_SUFFIX = exports.KEY_MAP_SUFFIX = exports.NEGATIVE_SYMBOL = exports.PRE_DEF_TYPE_SYMBOL = exports.IMPLICIT_SYMBOL = exports.FILTER_PREFIX = exports.MODIFIER_PREFIX = exports.LIST_SUFFIX = exports.MAP_SUFFIX = void 0;
exports.MAP_SUFFIX = '{}';
exports.LIST_SUFFIX = '[]';
exports.MODIFIER_PREFIX = '$.';
exports.FILTER_PREFIX = '|';
exports.IMPLICIT_SYMBOL = '?';
exports.PRE_DEF_TYPE_SYMBOL = '@';
exports.NEGATIVE_SYMBOL = '!';
exports.KEY_MAP_SUFFIX = `->${exports.MAP_SUFFIX}`;
exports.KEY_LIST_SUFFIX = `->${exports.LIST_SUFFIX}`;
exports.KEY_ARRAY_SUFFIX = /->\[\s*(\d+)(\s*,\s*(\d+)?)?\s*\]$/;
exports.KEY_STRICT_SUFFIX = '->()';
exports.KEY_EQUAL_SUFFIX = '->(=)';
var EFlags;
(function (EFlags) {
EFlags[EFlags["FROM_STRING"] = 0] = "FROM_STRING";
EFlags[EFlags["STRICT"] = 1] = "STRICT";
EFlags[EFlags["OPTIONAL"] = 2] = "OPTIONAL";
EFlags[EFlags["REQUIRED"] = 3] = "REQUIRED";
EFlags[EFlags["ARRAY"] = 4] = "ARRAY";
})(EFlags = exports.EFlags || (exports.EFlags = {}));
var EFlagValue;
(function (EFlagValue) {
/**
* Disabled.
*/
EFlagValue[EFlagValue["NO"] = 0] = "NO";
/**
* Enabled but not inheritable.
*/
EFlagValue[EFlagValue["YES"] = 1] = "YES";
/**
* Enabled and inheritable if not deep into sub element.
*/
EFlagValue[EFlagValue["INHERIT"] = 2] = "INHERIT";
/**
* Enabled and inheritable even deep into sub element.
*/
EFlagValue[EFlagValue["ELEMENT_INHERIT"] = 3] = "ELEMENT_INHERIT";
})(EFlagValue = exports.EFlagValue || (exports.EFlagValue = {}));
const RE_VALID_CUSTOM_TYPE_NAME = /^[-:.\w]+$/;
function validateTypeName(name) {
if (typeof name !== 'string' || !RE_VALID_CUSTOM_TYPE_NAME.test(name)) {
throw new TypeError(`Invalid name ${JSON.stringify(name)} for a pre-defined type.`);
}
}
exports.validateTypeName = validateTypeName;
function decodePreDefinedCallExpr(input) {
let expr = input;
expr = expr.replace(/\\['"]/g, (i) => `\\u00${i.charCodeAt(1).toString(16)}`);
const r = /^([-:.\w]+)\s*(\((.*)\))?$/.exec(expr);
if (!r) {
throw new SyntaxError(`Invalid type expression: ${input}`);
}
const fn = r[1];
expr = r[3];
if (!expr) {
return { name: fn, args: [] };
}
const args = [];
while (expr) {
const m = /^\s*([-]?\d+(\.\d+)?|[-]?0x[a-fA-F0-9]+|true|false|null|"[^"]+"|'[^']+')\s*(,|$)/i.exec(expr);
if (!m) {
throw new SyntaxError(`Invalid type expression "${expr}" in "${input}"`);
}
expr = expr.slice(m[0].length);
if (m[1].startsWith("'") && m[1].endsWith("'")) {
args.push(JSON.parse(`"${m[1].slice(1, -1)}"`));
}
else if (m[1].startsWith('0x')) {
args.push(parseInt(m[1].slice(2), 16));
}
else if (m[1].startsWith('-0x')) {
args.push(-parseInt(m[1].slice(3), 16));
}
else {
args.push(JSON.parse(m[1]));
}
}
return { name: fn, args };
}
exports.decodePreDefinedCallExpr = decodePreDefinedCallExpr;
//# sourceMappingURL=Internal.js.map