eval-formula
Advanced tools
Comparing version 1.0.6 to 1.0.8
146
grammar.js
@@ -7,35 +7,37 @@ // Generated automatically by nearley, version 2.15.1 | ||
var BigNumber = require('bignumber.js'); | ||
BigNumber.config({EXPONENTIAL_AT: [-1e+9, 1e9], POW_PRECISION: 100, RANGE: 100}); | ||
var moo = require("moo"); | ||
var lexer = moo.compile({ | ||
number: /[0-9]+/, | ||
string: /"[\w\s]+"/, | ||
WS: /[ ]+/, | ||
op: ["+", "-", "/", "*", '&&', '||', '^'], | ||
name: ['sin', 'cos', 'tan', 'asin', 'acos', 'atan', 'min', 'max', 'pi', 'e', 'sqrt', 'ln', 'ceil', 'floor', 'round'], | ||
l: '(', | ||
r: ')', | ||
comma: ',', | ||
dot: '.', | ||
conditionals: ["==", ">=", "<=", "!=", ">", "<"], | ||
quote: '"', | ||
ternary: ['?', ':'] | ||
}); | ||
var grammar = { | ||
Lexer: undefined, | ||
Lexer: lexer, | ||
ParserRules: [ | ||
{"name": "main", "symbols": ["_", "condition", "_"], "postprocess": function(d) {return d[1]; }}, | ||
{"name": "IFELSE", "symbols": ["_", "condition", "_", {"literal":"?"}, "_", "AS", "_", {"literal":":"}, "_", "AS"], "postprocess": d => {return ['ifelse', d[1], d[5], d[9]];}}, | ||
{"name": "OR$string$1", "symbols": [{"literal":"|"}, {"literal":"|"}], "postprocess": function joiner(d) {return d.join('');}}, | ||
{"name": "OR", "symbols": ["condition2", "_", "OR$string$1", "_", "condition"], "postprocess": d => {return ['or', d[0], d[4]];}}, | ||
{"name": "AND$string$1", "symbols": [{"literal":"&"}, {"literal":"&"}], "postprocess": function joiner(d) {return d.join('');}}, | ||
{"name": "AND", "symbols": ["condition2", "_", "AND$string$1", "_", "condition"], "postprocess": d => {return ['and', d[0], d[4]];}}, | ||
{"name": "condition", "symbols": ["AS", "_", "conditional", "_", "AS"], "postprocess": function(d) {return ['condition', d[2], d[0], d[4]];}}, | ||
{"name": "condition$string$1", "symbols": [{"literal":"="}, {"literal":"="}], "postprocess": function joiner(d) {return d.join('');}}, | ||
{"name": "condition", "symbols": ["string", "_", "condition$string$1", "_", "string"], "postprocess": d => {return ['stringCondition', '==', d[0], d[4]];}}, | ||
{"name": "condition$string$2", "symbols": [{"literal":"!"}, {"literal":"="}], "postprocess": function joiner(d) {return d.join('');}}, | ||
{"name": "condition", "symbols": ["string", "_", "condition$string$2", "_", "string"], "postprocess": d => {return ['stringCondition', '!=', d[0], d[4]];}}, | ||
{"name": "main", "symbols": ["sCondition"], "postprocess": id}, | ||
{"name": "ternary", "symbols": ["sCondition", {"literal":"?"}, "_", "AS", "_", {"literal":":"}, "_", "AS"], "postprocess": function(d) {return ['ternary', d[0], d[3], d[7]];}}, | ||
{"name": "OR", "symbols": ["condition2", "_", {"literal":"||"}, "sCondition"], "postprocess": function(d) {return ['or', d[0], d[3]];}}, | ||
{"name": "AND", "symbols": ["condition2", "_", {"literal":"&&"}, "sCondition"], "postprocess": function(d) {return ['and', d[0], d[3]];}}, | ||
{"name": "condition", "symbols": ["AS", "conditional", "AS"], "postprocess": function(d) {return ['condition', d[1], d[0], d[2]];}}, | ||
{"name": "condition", "symbols": ["string", "_", {"literal":"=="}, "_", "string"], "postprocess": function(d) {return ['stringCondition', '==', d[0], d[4]];}}, | ||
{"name": "condition", "symbols": ["string", "_", {"literal":"!="}, "_", "string"], "postprocess": function(d) {return ['stringCondition', '!=', d[0], d[4]];}}, | ||
{"name": "condition", "symbols": ["AND"], "postprocess": id}, | ||
{"name": "condition", "symbols": ["OR"], "postprocess": id}, | ||
{"name": "condition", "symbols": ["AS"], "postprocess": id}, | ||
{"name": "condition", "symbols": ["IFELSE"], "postprocess": id}, | ||
{"name": "condition2", "symbols": ["AS", "_", "conditional", "_", "AS"], "postprocess": function(d) {return ['condition', d[2], d[0], d[4]];}}, | ||
{"name": "condition", "symbols": ["ternary"], "postprocess": id}, | ||
{"name": "condition2", "symbols": ["AS", "conditional", "AS"], "postprocess": function(d) {return ['condition', d[1], d[0], d[2]];}}, | ||
{"name": "condition2", "symbols": ["AS"], "postprocess": id}, | ||
{"name": "conditionals$string$1", "symbols": [{"literal":"="}, {"literal":"="}], "postprocess": function joiner(d) {return d.join('');}}, | ||
{"name": "conditionals", "symbols": ["conditionals$string$1"]}, | ||
{"name": "conditionals$string$2", "symbols": [{"literal":">"}, {"literal":"="}], "postprocess": function joiner(d) {return d.join('');}}, | ||
{"name": "conditionals", "symbols": ["conditionals$string$2"]}, | ||
{"name": "conditionals$string$3", "symbols": [{"literal":"<"}, {"literal":"="}], "postprocess": function joiner(d) {return d.join('');}}, | ||
{"name": "conditionals", "symbols": ["conditionals$string$3"]}, | ||
{"name": "conditionals$string$4", "symbols": [{"literal":"!"}, {"literal":"="}], "postprocess": function joiner(d) {return d.join('');}}, | ||
{"name": "conditionals", "symbols": ["conditionals$string$4"]}, | ||
{"name": "conditionals", "symbols": [{"literal":">"}]}, | ||
{"name": "conditionals", "symbols": [{"literal":"<"}]}, | ||
{"name": "conditional", "symbols": ["conditionals"], "postprocess": function(d) { return d[0][0] }}, | ||
{"name": "P", "symbols": [{"literal":"("}, "_", "condition", "_", {"literal":")"}], "postprocess": function(d) {return d[2]; }}, | ||
{"name": "sCondition", "symbols": ["_", "condition", "_"], "postprocess": function(d){ return d[1]; }}, | ||
{"name": "conditional", "symbols": ["_", (lexer.has("conditionals") ? {type: "conditionals"} : conditionals), "_"], "postprocess": function(d) { return d[1].value }}, | ||
{"name": "P", "symbols": [{"literal":"("}, "sCondition", {"literal":")"}], "postprocess": function(d) {return d[1]; }}, | ||
{"name": "P", "symbols": ["N"], "postprocess": id}, | ||
@@ -51,48 +53,52 @@ {"name": "E", "symbols": ["P", "_", {"literal":"^"}, "_", "E"], "postprocess": function(d) {return ['^', d[0], d[4]]; }}, | ||
{"name": "N", "symbols": ["float"], "postprocess": id}, | ||
{"name": "N$string$1", "symbols": [{"literal":"s"}, {"literal":"i"}, {"literal":"n"}], "postprocess": function joiner(d) {return d.join('');}}, | ||
{"name": "N", "symbols": ["N$string$1", "_", "P"], "postprocess": function(d) {return ['sin', d[2]]; }}, | ||
{"name": "N$string$2", "symbols": [{"literal":"c"}, {"literal":"o"}, {"literal":"s"}], "postprocess": function joiner(d) {return d.join('');}}, | ||
{"name": "N", "symbols": ["N$string$2", "_", "P"], "postprocess": function(d) {return ['cos', d[2]]; }}, | ||
{"name": "N$string$3", "symbols": [{"literal":"t"}, {"literal":"a"}, {"literal":"n"}], "postprocess": function joiner(d) {return d.join('');}}, | ||
{"name": "N", "symbols": ["N$string$3", "_", "P"], "postprocess": function(d) {return ['tan', d[2]]; }}, | ||
{"name": "N$string$4", "symbols": [{"literal":"a"}, {"literal":"s"}, {"literal":"i"}, {"literal":"n"}], "postprocess": function joiner(d) {return d.join('');}}, | ||
{"name": "N", "symbols": ["N$string$4", "_", "P"], "postprocess": function(d) {return ['asin', d[2]]; }}, | ||
{"name": "N$string$5", "symbols": [{"literal":"a"}, {"literal":"c"}, {"literal":"o"}, {"literal":"s"}], "postprocess": function joiner(d) {return d.join('');}}, | ||
{"name": "N", "symbols": ["N$string$5", "_", "P"], "postprocess": function(d) {return ['acos', d[2]]; }}, | ||
{"name": "N$string$6", "symbols": [{"literal":"a"}, {"literal":"t"}, {"literal":"a"}, {"literal":"n"}], "postprocess": function joiner(d) {return d.join('');}}, | ||
{"name": "N", "symbols": ["N$string$6", "_", "P"], "postprocess": function(d) {return ['atan', d[2]]; }}, | ||
{"name": "N$string$7", "symbols": [{"literal":"p"}, {"literal":"i"}], "postprocess": function joiner(d) {return d.join('');}}, | ||
{"name": "N", "symbols": ["N$string$7"], "postprocess": function(d) {return ['pi']; }}, | ||
{"name": "N", "symbols": [{"literal":"sin"}, "_", "P"], "postprocess": function(d) {return ['sin', d[2]]; }}, | ||
{"name": "N", "symbols": [{"literal":"cos"}, "_", "P"], "postprocess": function(d) {return ['cos', d[2]]; }}, | ||
{"name": "N", "symbols": [{"literal":"tan"}, "_", "P"], "postprocess": function(d) {return ['tan', d[2]]; }}, | ||
{"name": "N", "symbols": [{"literal":"asin"}, "_", "P"], "postprocess": function(d) {return ['asin', d[2]]; }}, | ||
{"name": "N", "symbols": [{"literal":"acos"}, "_", "P"], "postprocess": function(d) {return ['acos', d[2]]; }}, | ||
{"name": "N", "symbols": [{"literal":"atan"}, "_", "P"], "postprocess": function(d) {return ['atan', d[2]]; }}, | ||
{"name": "N", "symbols": [{"literal":"pi"}], "postprocess": function(d) {return ['pi']; }}, | ||
{"name": "N", "symbols": [{"literal":"e"}], "postprocess": function(d) {return ['e']; }}, | ||
{"name": "N$string$8", "symbols": [{"literal":"s"}, {"literal":"q"}, {"literal":"r"}, {"literal":"t"}], "postprocess": function joiner(d) {return d.join('');}}, | ||
{"name": "N", "symbols": ["N$string$8", "_", "P"], "postprocess": function(d) {return ['sqrt', d[2]]; }}, | ||
{"name": "N$string$9", "symbols": [{"literal":"l"}, {"literal":"n"}], "postprocess": function joiner(d) {return d.join('');}}, | ||
{"name": "N", "symbols": ["N$string$9", "_", "P"], "postprocess": function(d) {return ['log', d[2]]; }}, | ||
{"name": "N$string$10", "symbols": [{"literal":"m"}, {"literal":"i"}, {"literal":"n"}, {"literal":"("}], "postprocess": function joiner(d) {return d.join('');}}, | ||
{"name": "N$subexpression$1$ebnf$1", "symbols": [/[0-9\,\s]/]}, | ||
{"name": "N$subexpression$1$ebnf$1", "symbols": ["N$subexpression$1$ebnf$1", /[0-9\,\s]/], "postprocess": function arrpush(d) {return d[0].concat([d[1]]);}}, | ||
{"name": "N$subexpression$1", "symbols": ["N$subexpression$1$ebnf$1"]}, | ||
{"name": "N", "symbols": ["N$string$10", "_", "N$subexpression$1", "_", {"literal":")"}], "postprocess": function(d) {var params = d[2][0].join('').split(','); return ['min', params]; }}, | ||
{"name": "N$string$11", "symbols": [{"literal":"m"}, {"literal":"a"}, {"literal":"x"}, {"literal":"("}], "postprocess": function joiner(d) {return d.join('');}}, | ||
{"name": "N$subexpression$2$ebnf$1", "symbols": [/[0-9\,\s]/]}, | ||
{"name": "N$subexpression$2$ebnf$1", "symbols": ["N$subexpression$2$ebnf$1", /[0-9\,\s]/], "postprocess": function arrpush(d) {return d[0].concat([d[1]]);}}, | ||
{"name": "N$subexpression$2", "symbols": ["N$subexpression$2$ebnf$1"]}, | ||
{"name": "N", "symbols": ["N$string$11", "_", "N$subexpression$2", "_", {"literal":")"}], "postprocess": function(d) {var params = d[2][0].join('').split(','); return ['max', params]; }}, | ||
{"name": "N$string$12", "symbols": [{"literal":"c"}, {"literal":"e"}, {"literal":"i"}, {"literal":"l"}], "postprocess": function joiner(d) {return d.join('');}}, | ||
{"name": "N", "symbols": ["N$string$12", "_", "P"], "postprocess": function(d) {return ['ceil', d[2]]; }}, | ||
{"name": "N$string$13", "symbols": [{"literal":"f"}, {"literal":"l"}, {"literal":"o"}, {"literal":"o"}, {"literal":"r"}], "postprocess": function joiner(d) {return d.join('');}}, | ||
{"name": "N", "symbols": ["N$string$13", "_", "P"], "postprocess": function(d) {return ['floor', d[2]]; }}, | ||
{"name": "N$string$14", "symbols": [{"literal":"r"}, {"literal":"o"}, {"literal":"u"}, {"literal":"n"}, {"literal":"d"}], "postprocess": function joiner(d) {return d.join('');}}, | ||
{"name": "N", "symbols": ["N$string$14", "_", "P"], "postprocess": function(d) {return ['round', d[2]]; }}, | ||
{"name": "float", "symbols": ["int", {"literal":"."}, "int"], "postprocess": function(d) {return new BigNumber(d[0] + d[1] + d[2])}}, | ||
{"name": "float", "symbols": ["int"], "postprocess": function(d) {return new BigNumber(d[0])}}, | ||
{"name": "N", "symbols": [{"literal":"sqrt"}, "_", "P"], "postprocess": function(d) {return ['sqrt', d[2]]; }}, | ||
{"name": "N", "symbols": [{"literal":"ln"}, "_", "P"], "postprocess": function(d) {return ['log', d[2]]; }}, | ||
{"name": "N$ebnf$1$subexpression$1$ebnf$1", "symbols": []}, | ||
{"name": "N$ebnf$1$subexpression$1$ebnf$1", "symbols": ["N$ebnf$1$subexpression$1$ebnf$1", {"literal":","}], "postprocess": function arrpush(d) {return d[0].concat([d[1]]);}}, | ||
{"name": "N$ebnf$1$subexpression$1", "symbols": ["_", "AS", "_", "N$ebnf$1$subexpression$1$ebnf$1"]}, | ||
{"name": "N$ebnf$1", "symbols": ["N$ebnf$1$subexpression$1"]}, | ||
{"name": "N$ebnf$1$subexpression$2$ebnf$1", "symbols": []}, | ||
{"name": "N$ebnf$1$subexpression$2$ebnf$1", "symbols": ["N$ebnf$1$subexpression$2$ebnf$1", {"literal":","}], "postprocess": function arrpush(d) {return d[0].concat([d[1]]);}}, | ||
{"name": "N$ebnf$1$subexpression$2", "symbols": ["_", "AS", "_", "N$ebnf$1$subexpression$2$ebnf$1"]}, | ||
{"name": "N$ebnf$1", "symbols": ["N$ebnf$1", "N$ebnf$1$subexpression$2"], "postprocess": function arrpush(d) {return d[0].concat([d[1]]);}}, | ||
{"name": "N", "symbols": [{"literal":"min"}, "_", {"literal":"("}, "N$ebnf$1", "_", {"literal":")"}], "postprocess": function(d) {var params = d[3].map(function(v){return v[1]});return ['min', params]; }}, | ||
{"name": "N$ebnf$2$subexpression$1$ebnf$1", "symbols": []}, | ||
{"name": "N$ebnf$2$subexpression$1$ebnf$1", "symbols": ["N$ebnf$2$subexpression$1$ebnf$1", {"literal":","}], "postprocess": function arrpush(d) {return d[0].concat([d[1]]);}}, | ||
{"name": "N$ebnf$2$subexpression$1", "symbols": ["_", "AS", "_", "N$ebnf$2$subexpression$1$ebnf$1"]}, | ||
{"name": "N$ebnf$2", "symbols": ["N$ebnf$2$subexpression$1"]}, | ||
{"name": "N$ebnf$2$subexpression$2$ebnf$1", "symbols": []}, | ||
{"name": "N$ebnf$2$subexpression$2$ebnf$1", "symbols": ["N$ebnf$2$subexpression$2$ebnf$1", {"literal":","}], "postprocess": function arrpush(d) {return d[0].concat([d[1]]);}}, | ||
{"name": "N$ebnf$2$subexpression$2", "symbols": ["_", "AS", "_", "N$ebnf$2$subexpression$2$ebnf$1"]}, | ||
{"name": "N$ebnf$2", "symbols": ["N$ebnf$2", "N$ebnf$2$subexpression$2"], "postprocess": function arrpush(d) {return d[0].concat([d[1]]);}}, | ||
{"name": "N", "symbols": [{"literal":"max"}, "_", {"literal":"("}, "N$ebnf$2", "_", {"literal":")"}], "postprocess": function(d) {var params = d[3].map(function(v){return v[1]});return ['max', params]; }}, | ||
{"name": "N", "symbols": [{"literal":"ceil"}, "_", "P"], "postprocess": function(d) {return ['ceil', d[2]]; }}, | ||
{"name": "N", "symbols": [{"literal":"floor"}, "_", "P"], "postprocess": function(d) {return ['floor', d[2]]; }}, | ||
{"name": "N", "symbols": [{"literal":"round"}, "_", "P"], "postprocess": function(d) {return ['round', d[2]]; }}, | ||
{"name": "float$ebnf$1", "symbols": []}, | ||
{"name": "float$ebnf$1", "symbols": ["float$ebnf$1", {"literal":"-"}], "postprocess": function arrpush(d) {return d[0].concat([d[1]]);}}, | ||
{"name": "float$ebnf$2", "symbols": []}, | ||
{"name": "float$ebnf$2$subexpression$1", "symbols": [{"literal":"."}, (lexer.has("number") ? {type: "number"} : number)]}, | ||
{"name": "float$ebnf$2", "symbols": ["float$ebnf$2", "float$ebnf$2$subexpression$1"], "postprocess": function arrpush(d) {return d[0].concat([d[1]]);}}, | ||
{"name": "float", "symbols": ["float$ebnf$1", (lexer.has("number") ? {type: "number"} : number), "float$ebnf$2"], "postprocess": function(d,l, reject) { | ||
var number = d[0][0] ? '-' + d[1] : d[1]; | ||
if(d[2][0] && d[2][0][0].type === 'dot'){ | ||
if(d[2].length > 1 || d[2][0][1].type !== 'number'){ | ||
return reject; | ||
}else{ | ||
number = number + '.' + d[2][0][1].value; | ||
} | ||
} | ||
return new BigNumber(number)} }, | ||
{"name": "value", "symbols": ["AS"], "postprocess": id}, | ||
{"name": "int$ebnf$1", "symbols": [/[0-9]/]}, | ||
{"name": "int$ebnf$1", "symbols": ["int$ebnf$1", /[0-9]/], "postprocess": function arrpush(d) {return d[0].concat([d[1]]);}}, | ||
{"name": "int", "symbols": ["int$ebnf$1"], "postprocess": function(d) {return d[0].join(""); }}, | ||
{"name": "string$ebnf$1", "symbols": [/[\w\s]/]}, | ||
{"name": "string$ebnf$1", "symbols": ["string$ebnf$1", /[\w\s]/], "postprocess": function arrpush(d) {return d[0].concat([d[1]]);}}, | ||
{"name": "string", "symbols": [{"literal":"\""}, "string$ebnf$1", {"literal":"\""}], "postprocess": function(d) {return d[1].join("").trim(); }}, | ||
{"name": "string", "symbols": [(lexer.has("string") ? {type: "string"} : string)], "postprocess": function(d) {return d[0].value; }}, | ||
{"name": "_$ebnf$1", "symbols": []}, | ||
{"name": "_$ebnf$1", "symbols": ["_$ebnf$1", /[\s]/], "postprocess": function arrpush(d) {return d[0].concat([d[1]]);}}, | ||
{"name": "_$ebnf$1", "symbols": ["_$ebnf$1", (lexer.has("WS") ? {type: "WS"} : WS)], "postprocess": function arrpush(d) {return d[0].concat([d[1]]);}}, | ||
{"name": "_", "symbols": ["_$ebnf$1"], "postprocess": function(d) {return null; }} | ||
@@ -99,0 +105,0 @@ ] |
44
index.js
@@ -9,2 +9,3 @@ var nearley = require("nearley"); | ||
module.exports = function (formula, callback) { | ||
if(!formula || !formula.length) throw new Error('Incorrect formula'); | ||
var parser = new nearley.Parser(nearley.Grammar.fromCompiled(grammar)); | ||
@@ -236,10 +237,35 @@ parser.feed(formula); | ||
case 'min': | ||
cb(arr[1].reduce(function (a, b) { | ||
return BigNumber.min(a, b); | ||
})); | ||
var vals = []; | ||
async.eachSeries(arr[1], function (el, _cb) { | ||
if (BigNumber.isBigNumber(el)){ | ||
vals.push(el); | ||
return _cb(); | ||
} | ||
evaluate(el, function (res) { | ||
vals.push(res); | ||
return _cb(); | ||
}); | ||
}, function () { | ||
cb(vals.reduce(function (a, b) { | ||
return BigNumber.min(a, b); | ||
})); | ||
}); | ||
break; | ||
case 'max': | ||
cb(arr[1].reduce(function (a, b) { | ||
return BigNumber.max(a, b); | ||
})); | ||
var vals = []; | ||
async.eachSeries(arr[1], function (el, _cb) { | ||
if (BigNumber.isBigNumber(el)){ | ||
vals.push(el); | ||
return _cb(); | ||
} | ||
evaluate(el, function (res) { | ||
vals.push(res); | ||
return _cb(); | ||
}); | ||
}, function () { | ||
cb(vals.reduce(function (a, b) { | ||
return BigNumber.max(a, b); | ||
})); | ||
}); | ||
break; | ||
@@ -356,3 +382,3 @@ case 'pi': | ||
break; | ||
case 'ifelse': | ||
case 'ternary': | ||
var conditionResult; | ||
@@ -373,3 +399,3 @@ async.eachSeries([arr[1]], function (arr2, cb2) { | ||
}, function () { | ||
if(conditionResult){ | ||
if (conditionResult) { | ||
var result; | ||
@@ -389,3 +415,3 @@ async.eachSeries([arr[2]], function (arr3, cb3) { | ||
}) | ||
}else{ | ||
} else { | ||
async.eachSeries([arr[3]], function (arr3, cb3) { | ||
@@ -392,0 +418,0 @@ if (BigNumber.isBigNumber(arr3)) { |
{ | ||
"name": "eval-formula", | ||
"version": "1.0.6", | ||
"version": "1.0.8", | ||
"description": "Library for calculating formulas. Supports very large numbers(uses BigNumber)", | ||
@@ -8,2 +8,3 @@ "dependencies": { | ||
"bignumber.js": "^8.0.1", | ||
"moo": "^0.5.0", | ||
"nearley": "^2.15.1" | ||
@@ -10,0 +11,0 @@ }, |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
7
817
29604
4
+ Addedmoo@^0.5.0