Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

eval-formula

Package Overview
Dependencies
Maintainers
1
Versions
8
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

eval-formula - npm Package Compare versions

Comparing version 1.0.2 to 1.0.3

77

grammar.js

@@ -12,33 +12,12 @@ // Generated automatically by nearley, version 2.15.1

{"name": "main", "symbols": ["_", "condition", "_"], "postprocess": function(d) {return d[1]; }},
{"name": "IFELSE", "symbols": ["_", "condition", "_", {"literal":"?"}, "_", "AS", "_", {"literal":":"}, "_", "AS"], "postprocess": d => {
if (d[1]) {
return d[5];
} else {
return d[9];
}
}},
{"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": ["condition", "_", "AND$string$1", "_", "condition"], "postprocess": d => {return (d[0] === true || d[0] > 0) && (d[4] === true || d[4] > 0)}},
{"name": "OR$string$1", "symbols": [{"literal":"|"}, {"literal":"|"}], "postprocess": function joiner(d) {return d.join('');}},
{"name": "OR", "symbols": ["condition", "_", "OR$string$1", "_", "condition"], "postprocess": d => {return d[0] === true || d[0] > 0 || d[4] === true || d[4] > 0}},
{"name": "condition", "symbols": ["AS", "_", "conditional", "_", "AS"], "postprocess": function(d) {
switch (d[2]){
case '==':
return d[0].eq(d[4]);
case '>=':
return d[0].gte(d[4]);
case '<=':
return d[0].lte(d[4]);
case '!=':
return !(d[0].eq(d[4]));
case '>':
return d[0].gt(d[4]);
case '<':
return d[0].lt(d[4]);
}}
},
{"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 d[0] === d[4]}},
{"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 d[0] !== d[4]}},
{"name": "condition", "symbols": ["string", "_", "condition$string$2", "_", "string"], "postprocess": d => {return ['stringCondition', '!=', d[0], d[4]];}},
{"name": "condition", "symbols": ["AND"], "postprocess": id},

@@ -48,2 +27,4 @@ {"name": "condition", "symbols": ["OR"], "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": "condition2", "symbols": ["AS"], "postprocess": id},
{"name": "conditionals$string$1", "symbols": [{"literal":"="}, {"literal":"="}], "postprocess": function joiner(d) {return d.join('');}},

@@ -62,30 +43,30 @@ {"name": "conditionals", "symbols": ["conditionals$string$1"]},

{"name": "P", "symbols": ["N"], "postprocess": id},
{"name": "E", "symbols": ["P", "_", {"literal":"^"}, "_", "E"], "postprocess": function(d) {return new BigNumber(d[0]).pow(d[4]); }},
{"name": "E", "symbols": ["P", "_", {"literal":"^"}, "_", "E"], "postprocess": function(d) {return ['^', d[0], d[4]]; }},
{"name": "E", "symbols": ["P"], "postprocess": id},
{"name": "MD", "symbols": ["MD", "_", {"literal":"*"}, "_", "E"], "postprocess": function(d) {return d[0].times(d[4]); }},
{"name": "MD", "symbols": ["MD", "_", {"literal":"/"}, "_", "E"], "postprocess": function(d) {return d[0].div(d[4]); }},
{"name": "MD", "symbols": ["MD", "_", {"literal":"*"}, "_", "E"], "postprocess": function(d) {return ['*', d[0], d[4]]; }},
{"name": "MD", "symbols": ["MD", "_", {"literal":"/"}, "_", "E"], "postprocess": function(d) {return ['/', d[0], d[4]]; }},
{"name": "MD", "symbols": ["E"], "postprocess": id},
{"name": "AS", "symbols": ["AS", "_", {"literal":"+"}, "_", "MD"], "postprocess": function(d) {return d[0].plus(d[4]); }},
{"name": "AS", "symbols": ["AS", "_", {"literal":"-"}, "_", "MD"], "postprocess": function(d) {return d[0].minus(d[4]); }},
{"name": "AS", "symbols": ["AS", "_", {"literal":"+"}, "_", "MD"], "postprocess": function(d) {return ['+', d[0], d[4]]; }},
{"name": "AS", "symbols": ["AS", "_", {"literal":"-"}, "_", "MD"], "postprocess": function(d) {return ['-', d[0], d[4]]; }},
{"name": "AS", "symbols": ["MD"], "postprocess": id},
{"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 new BigNumber(Math.sin(d[2].toNumber())); }},
{"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 new BigNumber(Math.cos(d[2].toNumber())); }},
{"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 new BigNumber(Math.tan(d[2].toNumber())); }},
{"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 new BigNumber(Math.asin(d[2].toNumber())); }},
{"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 new BigNumber(Math.acos(d[2].toNumber())); }},
{"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 new BigNumber(Math.atan(d[2].toNumber())); }},
{"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 new BigNumber(Math.PI); }},
{"name": "N", "symbols": [{"literal":"e"}], "postprocess": function(d) {return new BigNumber(Math.E); }},
{"name": "N", "symbols": ["N$string$7"], "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 d[2].sqrt(); }},
{"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 new BigNumber(Math.log(d[2].toNumber())); }},
{"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('');}},

@@ -95,3 +76,3 @@ {"name": "N$subexpression$1$ebnf$1", "symbols": [/[0-9\,\s]/]},

{"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 new BigNumber.min(...params); }},
{"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('');}},

@@ -101,9 +82,9 @@ {"name": "N$subexpression$2$ebnf$1", "symbols": [/[0-9\,\s]/]},

{"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 new BigNumber.max(...params); }},
{"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 d[2].dp(0, 2); }},
{"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 d[2].dp(0, 3); }},
{"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 d[2].dp(0, 6); }},
{"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])}},

@@ -110,0 +91,0 @@ {"name": "float", "symbols": ["int"], "postprocess": function(d) {return new BigNumber(d[0])}},

@@ -1,20 +0,413 @@

const nearley = require("nearley");
const grammar = require("./grammar.js");
var nearley = require("nearley");
var grammar = require("./grammar.js");
var BigNumber = require('bignumber.js');
var async = require('async');
module.exports = function (formula) {
const parser = new nearley.Parser(nearley.Grammar.fromCompiled(grammar));
BigNumber.config({EXPONENTIAL_AT: [-1e+9, 1e9], POW_PRECISION: 100, RANGE: 100});
module.exports = function (formula, callback) {
var parser = new nearley.Parser(nearley.Grammar.fromCompiled(grammar));
parser.feed(formula);
if(parser.results.length === 1){
return parser.results[0];
}else{
if(typeof parser.results[0] === 'boolean') {
for (let i = 0; i < parser.results.length; i++) {
if (parser.results[i] === false) return false;
}
return true;
}else{
return parser.results[0];
function evaluate(arr, cb) {
var op = arr[0];
switch (op) {
case '+':
var prevV;
async.eachSeries(arr.slice(1), function (arr2, cb2) {
if (BigNumber.isBigNumber(arr2)) {
if (prevV === undefined) {
prevV = arr2;
} else {
prevV = arr2.plus(prevV);
}
cb2(null, prevV);
} else {
evaluate(arr2, function (res) {
if (prevV === undefined) {
prevV = res;
} else {
prevV = res.plus(prevV);
}
cb2(null, prevV);
});
}
}, function () {
cb(prevV);
});
break;
case '-':
var prevV;
async.eachSeries(arr.slice(1), function (arr2, cb2) {
if (BigNumber.isBigNumber(arr2)) {
if (prevV === undefined) {
prevV = arr2;
} else {
prevV = prevV.minus(arr2);
}
cb2(null, prevV);
} else {
evaluate(arr2, function (res) {
if (prevV === undefined) {
prevV = res;
} else {
prevV = prevV.minus(res);
}
cb2(null, prevV);
});
}
}, function () {
cb(prevV);
});
break;
case '*':
var prevV;
async.eachSeries(arr.slice(1), function (arr2, cb2) {
if (BigNumber.isBigNumber(arr2)) {
if (prevV === undefined) {
prevV = arr2;
} else {
prevV = arr2.times(prevV);
}
cb2(null, prevV);
} else {
evaluate(arr2, function (res) {
if (prevV === undefined) {
prevV = res;
} else {
prevV = res.times(prevV);
}
cb2(null, prevV);
});
}
}, function () {
cb(prevV);
});
break;
case '/':
var prevV;
async.eachSeries(arr.slice(1), function (arr2, cb2) {
if (BigNumber.isBigNumber(arr2)) {
if (prevV === undefined) {
prevV = arr2;
} else {
prevV = prevV.div(arr2);
}
cb2(null, prevV);
} else {
evaluate(arr2, function (res) {
if (prevV === undefined) {
prevV = res;
} else {
prevV = prevV.div(res);
}
cb2(null, prevV);
});
}
}, function () {
cb(prevV);
});
break;
case '^':
var prevV;
async.eachSeries(arr.slice(1), function (arr2, cb2) {
if (BigNumber.isBigNumber(arr2)) {
if (prevV === undefined) {
prevV = arr2;
} else {
prevV = prevV.pow(arr2);
}
cb2(null, prevV);
} else {
evaluate(arr2, function (res) {
if (prevV === undefined) {
prevV = res;
} else {
prevV = prevV.pow(res);
}
cb2(null, prevV);
});
}
}, function () {
cb(prevV);
});
break;
case 'sin':
if (BigNumber.isBigNumber(arr[1])) {
cb(new BigNumber(Math.sin(arr[1].toNumber())));
} else {
evaluate(arr[1], function (res) {
cb(new BigNumber(Math.sin(res.toNumber())));
});
}
break;
case 'cos':
if (BigNumber.isBigNumber(arr[1])) {
cb(new BigNumber(Math.cos(arr[1].toNumber())));
} else {
evaluate(arr[1], function (res) {
cb(new BigNumber(Math.cos(res.toNumber())));
});
}
break;
case 'tan':
if (BigNumber.isBigNumber(arr[1])) {
cb(new BigNumber(Math.tan(arr[1].toNumber())));
} else {
evaluate(arr[1], function (res) {
cb(new BigNumber(Math.tan(res.toNumber())));
});
}
break;
case 'asin':
if (BigNumber.isBigNumber(arr[1])) {
cb(new BigNumber(Math.asin(arr[1].toNumber())));
} else {
evaluate(arr[1], function (res) {
cb(new BigNumber(Math.asin(res.toNumber())));
});
}
break;
case 'acos':
if (BigNumber.isBigNumber(arr[1])) {
cb(new BigNumber(Math.acos(arr[1].toNumber())));
} else {
evaluate(arr[1], function (res) {
cb(new BigNumber(Math.acos(res.toNumber())));
});
}
break;
case 'atan':
if (BigNumber.isBigNumber(arr[1])) {
cb(new BigNumber(Math.atan(arr[1].toNumber())));
} else {
evaluate(arr[1], function (res) {
cb(new BigNumber(Math.atan(res.toNumber())));
});
}
break;
case 'log':
if (BigNumber.isBigNumber(arr[1])) {
cb(new BigNumber(Math.log(arr[1].toNumber())));
} else {
evaluate(arr[1], function (res) {
cb(new BigNumber(Math.log(res.toNumber())));
});
}
break;
case 'sqrt':
if (BigNumber.isBigNumber(arr[1])) {
cb(arr[1].sqrt());
} else {
evaluate(arr[1], function (res) {
cb(res.sqrt());
});
}
break;
case 'ceil':
if (BigNumber.isBigNumber(arr[1])) {
cb(arr[1].dp(0, 2));
} else {
evaluate(arr[1], function (res) {
cb(res.dp(0, 2));
});
}
break;
case 'floor':
if (BigNumber.isBigNumber(arr[1])) {
cb(arr[1].dp(0, 3));
} else {
evaluate(arr[1], function (res) {
cb(res.dp(0, 3));
});
}
break;
case 'round':
if (BigNumber.isBigNumber(arr[1])) {
cb(arr[1].dp(0, 6));
} else {
evaluate(arr[1], function (res) {
cb(res.dp(0, 6));
});
}
break;
case 'min':
cb(arr[1].reduce(function (a, b) {
return BigNumber.min(a, b);
}));
break;
case 'max':
cb(arr[1].reduce(function (a, b) {
return BigNumber.max(a, b);
}));
break;
case 'pi':
cb(new BigNumber(Math.PI));
break;
case 'and':
var prevV = true;
async.eachSeries(arr.slice(1), function (arr2, cb2) {
if (BigNumber.isBigNumber(arr2)) {
prevV = prevV && !(arr2.eq(0));
cb2(null, prevV);
} else {
evaluate(arr2, function (res) {
prevV = prevV && res;
cb2(null, prevV);
});
}
}, function () {
cb(prevV);
});
break;
case 'or':
var prevV = false;
async.eachSeries(arr.slice(1), function (arr2, cb2) {
if (BigNumber.isBigNumber(arr2)) {
prevV = prevV || !(arr2.eq(0));
cb2(null, prevV);
} else {
evaluate(arr2, function (res) {
prevV = prevV || res;
cb2(null, prevV);
});
}
}, function () {
cb(prevV);
});
break;
case 'condition':
var val1;
async.eachSeries([arr[2]], function (arr2, cb2) {
if (BigNumber.isBigNumber(arr2)) {
val1 = arr2;
cb2();
} else {
evaluate(arr2, function (res) {
val1 = res;
cb2();
});
}
}, function () {
var val2;
async.eachSeries([arr[3]], function (arr3, cb2) {
if (BigNumber.isBigNumber(arr3)) {
val2 = arr3;
cb2();
} else {
evaluate(arr3, function (res) {
val2 = res;
cb2();
});
}
}, function () {
switch (arr[1]) {
case '==':
return cb(val1.eq(val2));
case '>=':
return cb(val1.gte(val2));
case '<=':
return cb(val1.lte(val2));
case '!=':
return cb(!(val1.eq(val2)));
case '>':
return cb(val1.gt(val2));
case '<':
return cb(val1.lt(val2));
}
});
});
break;
case 'stringCondition':
var val1;
async.eachSeries([arr[2]], function (arr2, cb2) {
if (typeof arr2 === 'string') {
val1 = arr2;
cb2();
} else {
evaluate(arr2, function (res) {
val1 = res;
cb2();
});
}
}, function () {
var val2;
async.eachSeries([arr[3]], function (arr3, cb2) {
if (typeof arr3 === 'string') {
val2 = arr3;
cb2();
} else {
evaluate(arr3, function (res) {
val2 = res;
cb2();
});
}
}, function () {
if (arr[1] === '==') {
return cb(val1 === val2);
} else {
return cb(val1 !== val2);
}
});
});
break;
case 'ifelse':
var conditionResult;
async.eachSeries([arr[1]], function (arr2, cb2) {
if (BigNumber.isBigNumber(arr2)) {
conditionResult = !arr2.eq(0);
cb2();
} else if (typeof arr2 === 'boolean') {
conditionResult = arr2;
cb2();
} else {
evaluate(arr2, function (res) {
conditionResult = res;
cb2();
});
}
}, function () {
if(conditionResult){
var result;
async.eachSeries([arr[2]], function (arr3, cb3) {
if (BigNumber.isBigNumber(arr3)) {
result = arr3;
cb3();
} else {
evaluate(arr3, function (res) {
result = res;
cb3();
});
}
}, function () {
cb(result);
})
}else{
async.eachSeries([arr[3]], function (arr3, cb3) {
if (BigNumber.isBigNumber(arr3)) {
result = arr3;
cb3();
} else {
evaluate(arr3, function (res) {
result = res;
cb3();
});
}
}, function () {
cb(result);
})
}
});
break;
case 'e':
cb(new BigNumber(Math.E));
break;
default:
if (BigNumber.isBigNumber(arr[0])) return cb(arr[0]);
if (typeof arr[0] === 'boolean') return cb(arr[0]);
throw new Error('Incorrect formula');
break;
}
}
return parser.results;
evaluate(parser.results[0], res => {callback(res)});
};

@@ -5,212 +5,321 @@ var evalFormula = require('./index');

test('1 + 1', t => {
t.deepEqual(evalFormula("1 + 1").eq(2), true);
evalFormula("1 + 1", res => {
t.deepEqual(res.eq(2), true);
});
});
test('1 - 1', t => {
t.deepEqual(evalFormula("1 - 1").eq(0), true);
evalFormula("1 - 1", res => {
t.deepEqual(res.eq(0), true);
});
});
test('2 * 2', t => {
t.deepEqual(evalFormula("2 * 2").eq(4), true);
evalFormula("2 * 2", res => {
t.deepEqual(res.eq(4), true);
});
});
test('2 / 2', t => {
t.deepEqual(evalFormula("2 / 2").eq(1), true);
evalFormula("2 / 2", res => {
t.deepEqual(res.eq(1), true);
});
});
test('2 ^ 4', t => {
t.deepEqual(evalFormula("2 ^ 4").eq(16), true);
evalFormula("2 ^ 4", res => {
t.deepEqual(res.eq(16), true);
});
});
test('(2 + 2) * 2', t => {
t.deepEqual(evalFormula("(2 + 2) * 2").eq(8), true);
evalFormula("(2 + 2) * 2", res => {
t.deepEqual(res.eq(8), true);
});
});
test('2 + 2 * 2', t => {
t.deepEqual(evalFormula("2 + 2 * 2").eq(6), true);
evalFormula("2 + 2 * 2", res => {
t.deepEqual(res.eq(6), true);
});
});
test('pi + 2', t => {
t.deepEqual(evalFormula("pi + 2").eq(Math.PI + 2), true);
evalFormula("pi + 2", res => {
t.deepEqual(res.eq(Math.PI + 2), true);
});
});
test('e + 2', t => {
t.deepEqual(evalFormula("e + 2").eq(Math.E + 2), true);
evalFormula("e + 2", res => {
t.deepEqual(res.eq(Math.E + 2), true);
});
});
test('sin(2)', t => {
t.deepEqual(evalFormula("sin(2)").eq(0.9092974268256817), true);
evalFormula("sin(2)", res => {
t.deepEqual(res.eq(0.9092974268256817), true);
});
});
test('cos(2)', t => {
t.deepEqual(evalFormula("cos(2)").eq(-0.4161468365471424), true);
evalFormula("cos(2)", res => {
t.deepEqual(res.eq(-0.4161468365471424), true);
});
});
test('tan(2)', t => {
t.deepEqual(evalFormula("tan(2)").eq(-2.185039863261519), true);
evalFormula("tan(2)", res => {
t.deepEqual(res.eq(-2.185039863261519), true);
});
});
test('asin(1)', t => {
t.deepEqual(evalFormula("asin(1)").eq(1.5707963267948966), true);
evalFormula("asin(1)", res => {
t.deepEqual(res.eq(1.5707963267948966), true);
});
});
test('acos(1)', t => {
t.deepEqual(evalFormula("acos(1)").eq(0), true);
evalFormula("acos(1)", res => {
t.deepEqual(res.eq(0), true);
});
});
test('atan(2)', t => {
t.deepEqual(evalFormula("atan(2)").eq(1.1071487177940904), true);
evalFormula("atan(2)", res => {
t.deepEqual(res.eq(1.1071487177940904), true);
});
});
test('sqrt(2)', t => {
t.deepEqual(evalFormula("sqrt(2)").eq('1.4142135623730950488'), true);
evalFormula("sqrt(2)", res => {
t.deepEqual(res.eq('1.4142135623730950488'), true);
});
});
test('ln(2)', t => {
t.deepEqual(evalFormula("ln(2)").eq(0.6931471805599453), true);
evalFormula("ln(2)", res => {
t.deepEqual(res.eq(0.6931471805599453), true);
});
});
test('1 == 1', t => {
t.deepEqual(evalFormula("1 == 1"), true);
evalFormula("1 == 1", res => {
t.deepEqual(res, true);
});
});
test('1 != 1', t => {
t.deepEqual(evalFormula("1 != 1"), false);
evalFormula("1 != 1", res => {
t.deepEqual(res, false);
});
});
test('1 != 2', t => {
t.deepEqual(evalFormula("1 != 2"), true);
evalFormula("1 != 2", res => {
t.deepEqual(res, true);
});
});
test('1 < 2', t => {
t.deepEqual(evalFormula("1 < 2"), true);
evalFormula("1 < 2", res => {
t.deepEqual(res, true);
});
});
test('1 > 2', t => {
t.deepEqual(evalFormula("1 > 2"), false);
evalFormula("1 > 2", res => {
t.deepEqual(res, false);
});
});
test('1 >= 2', t => {
t.deepEqual(evalFormula("2 >= 2"), true);
evalFormula("2 >= 2", res => {
t.deepEqual(res, true);
});
});
test('1 <= 2', t => {
t.deepEqual(evalFormula("1 <= 2"), true);
evalFormula("1 <= 2", res => {
t.deepEqual(res, true);
});
});
test('0 >= 2', t => {
t.deepEqual(evalFormula("0 >= 2"), false);
evalFormula("0 >= 2", res => {
t.deepEqual(res, false);
});
});
test('3 <= 2', t => {
t.deepEqual(evalFormula("3 <= 1"), false);
evalFormula("3 <= 1", res => {
t.deepEqual(res, false);
});
});
test('"test" == "test"', t => {
t.deepEqual(evalFormula('"test" == "test"'), true);
evalFormula('"test" == "test"', res => {
t.deepEqual(res, true);
});
});
test('"test" != "test"', t => {
t.deepEqual(evalFormula('"test" != "test"'), false);
evalFormula('"test" != "test"', res => {
t.deepEqual(res, false);
});
});
test('"test 1" != "test 2"', t => {
t.deepEqual(evalFormula('"test 1" != "test 2"'), true);
evalFormula('"test 1" != "test 2"', res => {
t.deepEqual(res, true);
});
});
test('"test 2" != "test 2"', t => {
t.deepEqual(evalFormula('"test 2" != "test 2"'), false);
evalFormula('"test 2" != "test 2"', res => {
t.deepEqual(res, false);
});
});
test('"test 3" == "test 3"', t => {
t.deepEqual(evalFormula('"test 3" == "test 3"'), true);
evalFormula('"test 3" == "test 3"', res => {
t.deepEqual(res, true);
});
});
test('1 && 1', t => {
t.deepEqual(evalFormula("1 && 1"), true);
evalFormula("1 && 1", res => {
t.deepEqual(res, true);
});
});
test('0 && 0', t => {
t.deepEqual(evalFormula("0 && 0"), false);
evalFormula("0 && 0", res => {
t.deepEqual(res, false);
});
});
test('0 && 1', t => {
t.deepEqual(evalFormula("0 && 1"), false);
evalFormula("0 && 1", res => {
t.deepEqual(res, false);
});
});
test('0 || 1', t => {
t.deepEqual(evalFormula("0 || 1"), true);
evalFormula("0 || 1", res => {
t.deepEqual(res, true);
});
});
test('1 == 1 && 1 == 1', t => {
t.deepEqual(evalFormula("1 == 1 && 1 == 1"), true);
evalFormula("1 == 1 && 1 == 1", res => {
t.deepEqual(res, true);
});
});
test('1 == 1 && 1 == 2', t => {
t.deepEqual(evalFormula("1 == 1 && 1 == 2"), false);
evalFormula("1 == 1 && 1 == 2", res => {
t.deepEqual(res, false);
});
});
test('1 == 1 || 1 == 2', t => {
t.deepEqual(evalFormula("1 == 1 || 1 == 2"), true);
evalFormula("1 == 1 || 1 == 2", res => {
t.deepEqual(res, true);
});
});
test('1 == 2 || 1 == 2', t => {
t.deepEqual(evalFormula("1 == 2 || 1 == 2"), false);
evalFormula("1 == 2 || 1 == 2", res => {
t.deepEqual(res, false);
});
});
test('10 == 10 ? 1 : 2', t => {
t.deepEqual(evalFormula("10 == 10 ? 1 : 2").eq(1), true);
evalFormula("10 == 10 ? 1 : 2", res => {
t.deepEqual(res.eq(1), true);
});
});
test('10 != 10 ? 1 : 2', t => {
t.deepEqual(evalFormula("10 != 10 ? 1 : 2").eq(2), true);
evalFormula("10 != 10 ? 1 : 2", res => {
t.deepEqual(res.eq(2), true);
});
});
test('10 == 10 ? 1 + 1 : 2 + 2', t => {
t.deepEqual(evalFormula("10 == 10 ? 1 + 1 : 2 + 2").eq(2), true);
evalFormula("10 == 10 ? 1 + 1 : 2 + 2", res => {
t.deepEqual(res.eq(2), true);
});
});
test('10 != 10 ? 1 + 1 : 2 + 2', t => {
t.deepEqual(evalFormula("10 != 10 ? 1 + 1 : 2 + 2").eq(4), true);
evalFormula("10 != 10 ? 1 + 1 : 2 + 2", res => {
t.deepEqual(res.eq(4), true);
});
});
test('1000000000000000000000000000000 == 1000000000000000000000000000000', t => {
t.deepEqual(evalFormula("1000000000000000000000000000000 == 1000000000000000000000000000000"), true);
evalFormula("1000000000000000000000000000000 == 1000000000000000000000000000000", res => {
t.deepEqual(res, true);
});
});
test('1000000000000000000000000000000 == 1000000000000000000000000000001', t => {
t.deepEqual(evalFormula("1000000000000000000000000000000 == 1000000000000000000000000000001"), false);
evalFormula("1000000000000000000000000000000 == 1000000000000000000000000000001", res => {
t.deepEqual(res, false);
});
});
test('min 1,2', t => {
t.deepEqual(evalFormula('min(1,2)').eq(1), true);
evalFormula('min(1,2)', res => {
t.deepEqual(res.eq(1), true);
});
});
test('min 1,2,4', t => {
t.deepEqual(evalFormula('min(1,2,4)').eq(1), true);
evalFormula("min(1,2,4)", res => {
t.deepEqual(res.eq(1), true);
});
});
test('min 2,3,5,7', t => {
t.deepEqual(evalFormula('min(2,3,5,7)').eq(2), true);
evalFormula("min(2,3,5,7)", res => {
t.deepEqual(res.eq(2), true);
});
});
test('max 1,2', t => {
t.deepEqual(evalFormula('max(1,2)').eq(2), true);
evalFormula("max(1,2)", res => {
t.deepEqual(res.eq(2), true);
});
});
test('max 1,2,4', t => {
t.deepEqual(evalFormula('max(1,2,4)').eq(4), true);
evalFormula("max(1,2,4)", res => {
t.deepEqual(res.eq(4), true);
});
});
test('max 2,3,5,7', t => {
t.deepEqual(evalFormula('max(2,3,5,7)').eq(7), true);
evalFormula("max(2,3,5,7)", res => {
t.deepEqual(res.eq(7), true);
});
});
test('ceil 2.5', t => {
t.deepEqual(evalFormula('ceil(2.5)').eq(3), true);
evalFormula("ceil(2.5)", res => {
t.deepEqual(res.eq(3), true);
});
});
test('floor 2.5', t => {
t.deepEqual(evalFormula('floor(2.5)').eq(2), true);
evalFormula('floor(2.5)', res => {
t.deepEqual(res.eq(2), true);
});
});
test('round 2.5', t => {
t.deepEqual(evalFormula('round(2.9)').eq(3), true);
evalFormula('round(2.9)', res => {
t.deepEqual(res.eq(3), true);
});
});
{
"name": "eval-formula",
"version": "1.0.2",
"version": "1.0.3",
"dependencies": {
"async": "^2.6.1",
"bignumber.js": "^8.0.1",

@@ -6,0 +7,0 @@ "nearley": "^2.15.1"

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc