UNPKG

@qualifyze/airtable-formulator

Version:
67 lines 2.5 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.parse = void 0; const schema_1 = require("./schema"); const ast_1 = require("./ast"); function convertExpression(node) { const converter = convert[node.type]; return converter(node); } const convert = { string: ({ value }) => value, number: ({ value }) => Number(value), modifier: ({ operator, operand }) => { if (convert.operator(operator) === "-") { return -convertExpression(operand); } else { throw new Error(`Unknown modifier operator: ${operator}`); } }, operation: ({ operator, left, right }) => { const op = convert.operator(operator); const l = convertExpression(left); const r = convertExpression(right); return [ op, ...((0, schema_1.isOperation)(l) && l[0] === op ? l.slice(1) : [l]), ...((0, schema_1.isOperation)(r) && r[0] === op ? r.slice(1) : [r]), ]; }, functionCall: ({ reference, argumentList }) => { const functionName = convert.functionReference(reference); const args = convert.argumentList(argumentList); if (args.length === 0 && (functionName === "TRUE" || functionName === "FALSE")) { switch (functionName) { case "TRUE": return true; case "FALSE": return false; } } return [functionName, ...args]; }, fieldReference: ({ value }) => ({ field: value, }), operator: ({ value }) => value, functionReference: ({ value }) => value, enclosedExpression: ({ expression }) => convertExpression(expression), argumentList: ({ args }) => args.map(convertExpression), }; function parse(formula, { validate = false } = {}) { const astExpression = (0, ast_1.parse)(formula, { removeSpace: true }); const arrayNotedFormula = astExpression && convertExpression(astExpression); if (validate && arrayNotedFormula) { const errors = (0, schema_1.validate)(arrayNotedFormula); if (errors.length > 0) { // XXX unfortunately AJV error messages are totally misleading. One should // use a package that outputs more human-readable error messages. throw new Error("Parsed formula failed validation"); } } return arrayNotedFormula; } exports.parse = parse; //# sourceMappingURL=parse.js.map