mirror of https://github.com/jkjoy/sunpeiwen.git
148 lines
5.8 KiB
JavaScript
148 lines
5.8 KiB
JavaScript
"use strict";
|
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
exports.func = exports.setEvaluated = exports.evaluatedPropsToName = exports.mergeEvaluated = exports.eachItem = exports.unescapeJsonPointer = exports.escapeJsonPointer = exports.escapeFragment = exports.unescapeFragment = exports.schemaRefOrVal = exports.schemaHasRulesButRef = exports.schemaHasRules = exports.checkUnknownRules = exports.alwaysValidSchema = exports.toHash = void 0;
|
|
const codegen_1 = require("./codegen");
|
|
const validate_1 = require("./validate");
|
|
// TODO refactor to use Set
|
|
function toHash(arr) {
|
|
const hash = {};
|
|
for (const item of arr)
|
|
hash[item] = true;
|
|
return hash;
|
|
}
|
|
exports.toHash = toHash;
|
|
function alwaysValidSchema(it, schema) {
|
|
if (typeof schema == "boolean")
|
|
return schema;
|
|
if (Object.keys(schema).length === 0)
|
|
return true;
|
|
checkUnknownRules(it, schema);
|
|
return !schemaHasRules(schema, it.self.RULES.all);
|
|
}
|
|
exports.alwaysValidSchema = alwaysValidSchema;
|
|
function checkUnknownRules(it, schema = it.schema) {
|
|
const { opts, self } = it;
|
|
if (!opts.strict)
|
|
return;
|
|
if (typeof schema === "boolean")
|
|
return;
|
|
const rules = self.RULES.keywords;
|
|
for (const key in schema) {
|
|
if (!rules[key])
|
|
validate_1.checkStrictMode(it, `unknown keyword: "${key}"`);
|
|
}
|
|
}
|
|
exports.checkUnknownRules = checkUnknownRules;
|
|
function schemaHasRules(schema, rules) {
|
|
if (typeof schema == "boolean")
|
|
return !schema;
|
|
for (const key in schema)
|
|
if (rules[key])
|
|
return true;
|
|
return false;
|
|
}
|
|
exports.schemaHasRules = schemaHasRules;
|
|
function schemaHasRulesButRef(schema, RULES) {
|
|
if (typeof schema == "boolean")
|
|
return !schema;
|
|
for (const key in schema)
|
|
if (key !== "$ref" && RULES.all[key])
|
|
return true;
|
|
return false;
|
|
}
|
|
exports.schemaHasRulesButRef = schemaHasRulesButRef;
|
|
function schemaRefOrVal({ topSchemaRef, schemaPath }, schema, keyword, $data) {
|
|
if (!$data) {
|
|
if (typeof schema == "number" || typeof schema == "boolean")
|
|
return schema;
|
|
if (typeof schema == "string")
|
|
return codegen_1._ `${schema}`;
|
|
}
|
|
return codegen_1._ `${topSchemaRef}${schemaPath}${codegen_1.getProperty(keyword)}`;
|
|
}
|
|
exports.schemaRefOrVal = schemaRefOrVal;
|
|
function unescapeFragment(str) {
|
|
return unescapeJsonPointer(decodeURIComponent(str));
|
|
}
|
|
exports.unescapeFragment = unescapeFragment;
|
|
function escapeFragment(str) {
|
|
return encodeURIComponent(escapeJsonPointer(str));
|
|
}
|
|
exports.escapeFragment = escapeFragment;
|
|
function escapeJsonPointer(str) {
|
|
if (typeof str == "number")
|
|
return `${str}`;
|
|
return str.replace(/~/g, "~0").replace(/\//g, "~1");
|
|
}
|
|
exports.escapeJsonPointer = escapeJsonPointer;
|
|
function unescapeJsonPointer(str) {
|
|
return str.replace(/~1/g, "/").replace(/~0/g, "~");
|
|
}
|
|
exports.unescapeJsonPointer = unescapeJsonPointer;
|
|
function eachItem(xs, f) {
|
|
if (Array.isArray(xs)) {
|
|
for (const x of xs)
|
|
f(x);
|
|
}
|
|
else {
|
|
f(xs);
|
|
}
|
|
}
|
|
exports.eachItem = eachItem;
|
|
function makeMergeEvaluated({ mergeNames, mergeToName, mergeValues, resultToName, }) {
|
|
return (gen, from, to, toName) => {
|
|
const res = to === undefined
|
|
? from
|
|
: to instanceof codegen_1.Name
|
|
? (from instanceof codegen_1.Name ? mergeNames(gen, from, to) : mergeToName(gen, from, to), to)
|
|
: from instanceof codegen_1.Name
|
|
? (mergeToName(gen, to, from), from)
|
|
: mergeValues(from, to);
|
|
return toName === codegen_1.Name && !(res instanceof codegen_1.Name) ? resultToName(gen, res) : res;
|
|
};
|
|
}
|
|
exports.mergeEvaluated = {
|
|
props: makeMergeEvaluated({
|
|
mergeNames: (gen, from, to) => gen.if(codegen_1._ `${to} !== true && ${from} !== undefined`, () => {
|
|
gen.if(codegen_1._ `${from} === true`, () => gen.assign(to, true), () => gen.assign(to, codegen_1._ `${to} || {}`).code(codegen_1._ `Object.assign(${to}, ${from})`));
|
|
}),
|
|
mergeToName: (gen, from, to) => gen.if(codegen_1._ `${to} !== true`, () => {
|
|
if (from === true) {
|
|
gen.assign(to, true);
|
|
}
|
|
else {
|
|
gen.assign(to, codegen_1._ `${to} || {}`);
|
|
setEvaluated(gen, to, from);
|
|
}
|
|
}),
|
|
mergeValues: (from, to) => (from === true ? true : { ...from, ...to }),
|
|
resultToName: evaluatedPropsToName,
|
|
}),
|
|
items: makeMergeEvaluated({
|
|
mergeNames: (gen, from, to) => gen.if(codegen_1._ `${to} !== true && ${from} !== undefined`, () => gen.assign(to, codegen_1._ `${from} === true ? true : ${to} > ${from} ? ${to} : ${from}`)),
|
|
mergeToName: (gen, from, to) => gen.if(codegen_1._ `${to} !== true`, () => gen.assign(to, from === true ? true : codegen_1._ `${to} > ${from} ? ${to} : ${from}`)),
|
|
mergeValues: (from, to) => (from === true ? true : Math.max(from, to)),
|
|
resultToName: (gen, items) => gen.var("items", items),
|
|
}),
|
|
};
|
|
function evaluatedPropsToName(gen, ps) {
|
|
if (ps === true)
|
|
return gen.var("props", true);
|
|
const props = gen.var("props", codegen_1._ `{}`);
|
|
if (ps !== undefined)
|
|
setEvaluated(gen, props, ps);
|
|
return props;
|
|
}
|
|
exports.evaluatedPropsToName = evaluatedPropsToName;
|
|
function setEvaluated(gen, props, ps) {
|
|
Object.keys(ps).forEach((p) => gen.assign(codegen_1._ `${props}${codegen_1.getProperty(p)}`, true));
|
|
}
|
|
exports.setEvaluated = setEvaluated;
|
|
function func(gen, f) {
|
|
return gen.scopeValue("func", {
|
|
ref: f,
|
|
code: f.code,
|
|
});
|
|
}
|
|
exports.func = func;
|
|
//# sourceMappingURL=util.js.map
|