UNPKG

@fileverse-dev/formula-parser

Version:

Formula parser

70 lines (68 loc) 3.4 kB
function _toConsumableArray(r) { return _arrayWithoutHoles(r) || _iterableToArray(r) || _unsupportedIterableToArray(r) || _nonIterableSpread(); } function _nonIterableSpread() { throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } function _unsupportedIterableToArray(r, a) { if (r) { if ("string" == typeof r) return _arrayLikeToArray(r, a); var t = {}.toString.call(r).slice(8, -1); return "Object" === t && r.constructor && (t = r.constructor.name), "Map" === t || "Set" === t ? Array.from(r) : "Arguments" === t || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(t) ? _arrayLikeToArray(r, a) : void 0; } } function _iterableToArray(r) { if ("undefined" != typeof Symbol && null != r[Symbol.iterator] || null != r["@@iterator"]) return Array.from(r); } function _arrayWithoutHoles(r) { if (Array.isArray(r)) return _arrayLikeToArray(r); } function _arrayLikeToArray(r, a) { (null == a || a > r.length) && (a = r.length); for (var e = 0, n = Array(a); e < a; e++) n[e] = r[e]; return n; } /* eslint-disable import/no-named-as-default-member */ import add from "./operator/add"; import ampersand from "./operator/ampersand"; import divide from "./operator/divide"; import equal from "./operator/equal"; import formulaFunction from "./operator/formula-function"; import greaterThan from "./operator/greater-than"; import greaterThanOrEqual from "./operator/greater-than-or-equal"; import lessThan from "./operator/less-than"; import lessThanOrEqual from "./operator/less-than-or-equal"; import minus from "./operator/minus"; import multiply from "./operator/multiply"; import notEqual from "./operator/not-equal"; import power from "./operator/power"; import { ERROR_NAME } from "./../error"; var availableOperators = Object.create(null); /** * Evaluate values by operator id.git * * @param {String} operator Operator id. * @param {Array} [params=[]] Arguments to evaluate. * @returns {*} */ export default function evaluateByOperator(operator) { var params = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : []; operator = operator.toUpperCase(); if (!availableOperators[operator]) { throw Error(ERROR_NAME); } return availableOperators[operator].apply(availableOperators, _toConsumableArray(params)); } /** * Register operator. * * @param {String|Array} symbol Symbol to register. * @param {Function} func Logic to register for this symbol. */ export function registerOperation(symbol, func) { if (!Array.isArray(symbol)) { symbol = [symbol.toUpperCase()]; } symbol.forEach(function (s) { if (func.isFactory) { availableOperators[s] = func(s); } else { availableOperators[s] = func; } }); } registerOperation(add.SYMBOL, add); registerOperation(ampersand.SYMBOL, ampersand); registerOperation(divide.SYMBOL, divide); registerOperation(equal.SYMBOL, equal); registerOperation(power.SYMBOL, power); registerOperation(formulaFunction.SYMBOL, formulaFunction); registerOperation(greaterThan.SYMBOL, greaterThan); registerOperation(greaterThanOrEqual.SYMBOL, greaterThanOrEqual); registerOperation(lessThan.SYMBOL, lessThan); registerOperation(lessThanOrEqual.SYMBOL, lessThanOrEqual); registerOperation(multiply.SYMBOL, multiply); registerOperation(notEqual.SYMBOL, notEqual); registerOperation(minus.SYMBOL, minus);