fastlion-amis
Version:
一种MIS页面生成工具
48 lines (47 loc) • 1.5 kB
JavaScript
/**
* @file 公式语法解析
*/
Object.defineProperty(exports, "__esModule", { value: true });
exports.collectVariables = void 0;
var memoryParse_1 = require("./memoryParse");
function traverseAst(ast, iterator) {
if (!ast || !ast.type) {
return;
}
iterator(ast);
Object.keys(ast).forEach(function (key) {
var value = ast[key];
if (Array.isArray(value)) {
value.forEach(function (child) { return traverseAst(child, iterator); });
}
else {
traverseAst(value, iterator);
}
});
}
// 缓存,用于提升性能
var COLLECT_EXPRESSION_CACHE = {};
// 提取表达式中有哪些变量
function collectVariables(strOrAst, execMode) {
var variables = [];
if (typeof strOrAst === 'string' && COLLECT_EXPRESSION_CACHE[strOrAst]) {
return COLLECT_EXPRESSION_CACHE[strOrAst];
}
var ast = typeof strOrAst === 'string'
? (0, memoryParse_1.memoryParse)(strOrAst, {
evalMode: execMode !== null && execMode !== void 0 ? execMode : false
})
: strOrAst;
traverseAst(ast, function (item) {
if (item.type === 'variable') {
variables.push(item.name);
}
});
if (typeof strOrAst === 'string') {
COLLECT_EXPRESSION_CACHE[strOrAst] = variables;
}
return variables;
}
exports.collectVariables = collectVariables;
//# sourceMappingURL=./utils/grammar.js.map
;