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

math-expression-evaluator

Package Overview
Dependencies
Maintainers
1
Versions
51
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

math-expression-evaluator - npm Package Compare versions

Comparing version 1.1.0 to 1.2.0

.npmignore

37

dist/browser/math-expression-evaluator.js

@@ -1,3 +0,3 @@

/** math-expression-evaluator version 1.1.0
Dated:2015-07-30 */
/** math-expression-evaluator version 1.2.0
Dated:2015-08-01 */

@@ -174,4 +174,3 @@ (function(f){if(typeof exports==="object"&&typeof module!=="undefined"){module.exports=f()}else if(typeof define==="function"&&define.amd){define([],f)}else{var g;if(typeof window!=="undefined"){g=window}else if(typeof global!=="undefined"){g=global}else if(typeof self!=="undefined"){g=self}else{g=this}g.mexp = f()}})(function(){var define,module,exports;return (function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a=typeof require=="function"&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);var f=new Error("Cannot find module '"+o+"'");throw f.code="MODULE_NOT_FOUND",f}var l=n[o]={exports:{}};t[o][0].call(l.exports,function(e){var n=t[o][1][e];return s(n?n:e)},l,l.exports,e,t,n,r)}return n[o].exports}var i=typeof require=="function"&&require;for(var o=0;o<r.length;o++)s(r[o]);return s})({1:[function(require,module,exports){

if(key===''){
console.error("Can't understand after "+inpStr.slice(i));
return;
throw(new Mexp.exception("Can't understand after "+inpStr.slice(i)));
}

@@ -187,2 +186,5 @@ var index=token.indexOf(key);

if([0,2,3,5,9,21,22,23].indexOf(cType)!==-1){
if(allowed[cType]!==true){
throw(new Mexp.exception(key+" is not allowed after "+prevKey));
}
str.push({value:")",type:5,pre:3,show:")"});

@@ -196,4 +198,3 @@ allowed=type1;

if(allowed[cType]!==true){
console.error(key+" is not allowed after "+prevKey);
return;
throw(new Mexp.exception(key+" is not allowed after "+prevKey));
}

@@ -238,3 +239,3 @@ if(asterick[cType]===true){

else if(cType===4){
inc(ptc,2);
inc(ptc,1);
bracToClose++;

@@ -247,4 +248,3 @@ allowed=type0;

if(!bracToClose){
console.error("Closing parenthesis are more than opening one, wait What!!!");
return;
throw(new Mexp.exception("Closing parenthesis are more than opening one, wait What!!!"));
}

@@ -258,4 +258,3 @@ bracToClose--;

if(pre.hasDec){
console.error("Two decimals are not allowed in one number");
return;
throw(new Mexp.exception("Two decimals are not allowed in one number"));
}

@@ -341,2 +340,5 @@ if(pre.type!==1){

}
if (allowed[5]!==true) {
throw(new Mexp.exception("complete the expression"));
}
while(bracToClose--)

@@ -350,5 +352,8 @@ str.push({value:")",show:")",type:5,pre:3});

},{"./math_function.js":3}],3:[function(require,module,exports){
var Mexp=function(parsed){
"use strict";
var Mexp=function(parsed,error){
if (error) {
this.value=[];
}
this.value=parsed;
};

@@ -467,2 +472,5 @@

};
Mexp.exception=function(message){
this.message=message;
};
module.exports=Mexp;

@@ -613,2 +621,5 @@ },{}],4:[function(require,module,exports){

}
if (stack.length>1) {
throw(new Mexp.exception("Math error"));
}
return stack[0].value>1000000000000000?"Infinity":Number(stack[0].value.toFixed(15)).toPrecision();

@@ -615,0 +626,0 @@ };

@@ -24,4 +24,4 @@ /*

*/
/** math-expression-evaluator version 1.1.0
Dated:2015-07-30 */
!function(a){if("object"==typeof exports&&"undefined"!=typeof module)module.exports=a();else if("function"==typeof define&&define.amd)define([],a);else{var b;b="undefined"!=typeof window?window:"undefined"!=typeof global?global:"undefined"!=typeof self?self:this,b.mexp=a()}}(function(){return function a(b,c,d){function e(g,h){if(!c[g]){if(!b[g]){var i="function"==typeof require&&require;if(!h&&i)return i(g,!0);if(f)return f(g,!0);var j=new Error("Cannot find module '"+g+"'");throw j.code="MODULE_NOT_FOUND",j}var k=c[g]={exports:{}};b[g][0].call(k.exports,function(a){var c=b[g][1][a];return e(c?c:a)},k,k.exports,a,b,c,d)}return c[g].exports}for(var f="function"==typeof require&&require,g=0;g<d.length;g++)e(d[g]);return e}({1:[function(a,b,c){var d=a("./postfix_evaluator.js");d.prototype.formulaEval=function(){"use strict";for(var a,b,c,d=[],e=this.value,f=0;f<e.length;f++)1===e[f].type||3===e[f].type?d.push({value:3===e[f].type?e[f].show:e[f].value,type:1}):23===e[f].type?d.push({value:e[f].show,type:1}):0===e[f].type?d[d.length-1]={value:e[f].show+d[d.length-1].value+("-"!=e[f].show?")":""),type:0}:7===e[f].type?d[d.length-1]={value:(1!=d[d.length-1].type?"(":"")+d[d.length-1].value+(1!=d[d.length-1].type?")":"")+e[f].show,type:7}:11===e[f].type?(a=d.pop(),b=d.pop(),"P"===e[f].show||"C"===e[f].show?d.push({value:"<sup>"+b+"</sup>"+e[f].show+"<sub>"+a+"</sub>",type:11}):d.push({value:(1!=b.type?"(":"")+b.value+(1!=b.type?")":"")+"<sup>"+a.value+"</sup>",type:1})):2===e[f].type||9===e[f].type?(a=d.pop(),b=d.pop(),d.push({value:(1!=b.type?"(":"")+b.value+(1!=b.type?")":"")+e[f].show+(1!=a.type?"(":"")+a.value+(1!=a.type?")":""),type:e[f].type})):22===e[f].type&&(a=d.pop(),b=d.pop(),c=d.pop(),d.push({value:e[f].show+"("+c.value+","+b.value+","+a.value+")",type:22}));return d[0].value},b.exports=d},{"./postfix_evaluator.js":5}],2:[function(a,b,c){function d(a,b){for(var c=0;c<a.length;c++)a[c]+=b;return a}function e(a,b,c,d){for(var e=0;d>e;e++)if(a[c+e]!==b[e])return!1;return!0}var f=a("./math_function.js"),g=["sin","cos","tan","pi","(",")","Del","P","C","asin","acos","atan","7","8","9","int","cosh","acosh","ln","^","root","4","5","6","/","!","tanh","atanh","Mod","1","2","3","*","=","sinh","asinh","e","log","10^x","0",".","+","-",",","Sigma","n","Pi","pow"],h=["sin(","cos(","tan(","&pi;","(",")","Del","P","C","asin(","acos(","atan(","7","8","9","Int(","cosh(","acosh("," ln(","^","root(","4","5","6","&divide;","!","tanh(","atanh("," Mod ","1","2","3","&times;","=","sinh(","asinh(","e"," log("," 10^(","0",".","+","-",",","&Sigma;","n","&Pi;","pow("],j=[f.math.sin,f.math.cos,f.math.tan,"PI","(",")","Del",f.math.P,f.math.C,f.math.asin,f.math.acos,f.math.atan,"7","8","9",Math.floor,f.math.cosh,f.math.acosh,Math.log,Math.pow,Math.pow,"4","5","6",f.math.div,f.math.fact,f.math.tanh,f.math.atanh,f.math.mod,"1","2","3",f.math.mul,"=",f.math.sinh,f.math.asinh,"E",f.math.log,f.math.pow10x,"0",".",f.math.add,f.math.sub,",",f.math.sigma,"n",f.math.Pi,Math.pow],k=[11,11,11,0,0,0,0,7,7,11,11,10,0,0,0,11,11,11,10,10,11,0,0,0,3,11,11,11,10,0,0,0,3,0,11,11,0,11,11,0,0,1,1,0,11,0,11,11],l=[0,0,0,3,4,5,12,11,11,0,0,0,1,1,1,0,0,0,0,11,11,1,1,1,2,7,0,0,2,1,1,1,2,13,0,0,3,0,0,1,6,9,9,21,22,23,22,8],m={0:!0,1:!0,3:!0,4:!0,6:!0,8:!0,9:!0,17:!0,22:!0,23:!0},n={0:!0,1:!0,2:!0,3:!0,4:!0,5:!0,6:!0,7:!0,8:!0,9:!0,10:!0,11:!0,13:!0,17:!0,21:!0,22:!0,23:!0},o={0:!0,3:!0,4:!0,8:!0,17:!0,22:!0,23:!0},p={},q={0:!0,1:!0,3:!0,4:!0,6:!0,8:!0,17:!0,22:!0,23:!0},r={1:!0},s=[[],["1","2","3","7","8","9","4","5","6","+","-","*","/","(",")","^","!","P","C","=","e","0",".",",","n"],["pi","ln","Pi"],["Ans","sin","cos","tan","Del","int","Mod","log","pow"],["asin","acos","atan","cosh","root","tanh","sinh","10^x"],["acosh","atanh","asinh","Sigma"]];f.addToken=function(a){for(i=0;i<a.length;i++){x=a[i].token.length;var b=-1;if(x<s.length)for(y=0;y<s[x].length;y++)if(a[i].token===s[x][y]){b=g.indexOf(s[x][y]);break}-1===b?(g.push(a[i].token),l.push(a[i].type),s.length<=a[i].token.length&&(s[a[i].token.length]=[]),s[a[i].token.length].push(a[i].token),j.push(a[i].ev),k.push(a[i].preced),h.push(a[i].show)):(g[b]=a[i].token,l[b]=a[i].type,j[b]=a[i].ev,k[b]=a[i].preced,h[b]=a[i].show)}},f.lex=function(a,b){"use strict";var c,i,t,u=[{type:4,value:"(",show:"(",pre:0}],v=[],w=a,x="",y=m,z=0,A=p,B="";"undefined"!=typeof b&&f.addToken(b);var C={};for(c=0;c<w.length;c++)if(" "!=w[c]){a:for(i=w.length-c>s.length-2?s.length-1:w.length-c;i>0;i--)for(t=0;t<s[i].length;t++)if(e(w,s[i][t],c,i)){x=s[i][t];break a}if(c+=x.length-1,""===x)return void console.error("Can't understand after "+w.slice(c));var D=g.indexOf(x),E=l[D],F=j[D],G=k[D],H=h[D],I=u[u.length-1];for(J=v.length;J--;)0===v[J]&&-1!==[0,2,3,5,9,21,22,23].indexOf(E)&&(u.push({value:")",type:5,pre:3,show:")"}),y=n,A=q,d(v,-1).pop());if(y[E]!==!0)return void console.error(x+" is not allowed after "+B);if(A[E]===!0&&(E=2,F=f.math.mul,H="&times;",G=3,c-=x.length),C={value:F,type:E,pre:G,show:H},0===E)y=m,A=p,d(v,2).push(2),u.push(C),u.push({value:"(",type:4,pre:0,show:"("});else if(1===E)1===I.type?(I.value+=F,d(v,1)):u.push(C),y=n,A=o;else if(2===E)y=m,A=p,d(v,2),u.push(C);else if(3===E)u.push(C),y=n,A=q;else if(4===E)d(v,2),z++,y=m,A=p,u.push(C);else if(5===E){if(!z)return void console.error("Closing parenthesis are more than opening one, wait What!!!");z--,y=n,A=q,u.push(C)}else if(6===E){if(I.hasDec)return void console.error("Two decimals are not allowed in one number");1!==I.type&&(u.push({value:0,type:1,pre:0}),d(v,-1)),y=r,d(v,1),A=p,I.value+=F,I.hasDec=!0}else 7===E&&(y=n,A=q,d(v,1),u.push(C));8===E?(y=m,A=p,d(v,4).push(4),u.push(C),u.push({value:"(",type:4,pre:0,show:"("})):9===E?(9===I.type?"+"===I.value?(I.value=F,I.show=H,d(v,1)):"-"===I.value&&"-"===H&&(I.value="+",I.show="+",d(v,1)):5!==I.type&&7!==I.type&&1!==I.type&&3!==I.type&&23!==I.type?(u.push({value:f.math.changeSign,type:0,pre:21,show:"-"}),d(v,1)):(u.push(C),d(v,2)),y=m,A=p):11===E?(y=m,A=p,d(v,2),u.push(C)):21===E?(y=m,A=p,u.push(C)):22===E?(y=m,A=p,d(v,6).push(6),u.push(C),u.push({value:"(",type:4,pre:0})):23===E&&(y=n,A=q,u.push(C)),d(v,-1),B=x}for(var J=v.length;J--;)0===v[J]&&(u.push({value:")",show:")",type:5,pre:3}),d(v,-1).pop());for(;z--;)u.push({value:")",show:")",type:5,pre:3});return u.push({type:5,value:")",show:")",pre:0}),new f(u)},b.exports=f},{"./math_function.js":3}],3:[function(a,b,c){var d=function(a){"use strict";this.value=a};d.math={isDegree:!1,acos:function(a){return d.isDegree?180/Math.PI*Math.acos(a):Math.acos(a)},add:function(a,b){return a+b},asin:function(a){return d.isDegree?180/Math.PI*Math.asin(a):Math.asin(a)},atan:function(a){return d.isDegree?180/Math.PI*Math.atan(a):Math.atan(a)},acosh:function(a){return Math.log(a+Math.sqrt(a*a-1))},asinh:function(a){return Math.log(a+Math.sqrt(a*a+1))},atanh:function(a){return Math.log((1+a)/(1-a))},C:function(a,b){var c=1,e=a-b,f=b;e>f&&(f=e,e=b);for(var g=f+1;a>=g;g++)c*=g;return c/d.fact(e)},changeSign:function(a){return-a},cos:function(a){return d.isDegree&&(a=d.math.toRadian(a)),Math.cos(a)},cosh:function(a){return(Math.pow(Math.E,a)+Math.pow(Math.E,-1*a))/2},div:function(a,b){return a/b},fact:function(a){if(a%1!==0)return"NAN";for(var b=1,c=2;a>=c;c++)b*=c;return b},inverse:function(a){return 1/a},log:function(a){return Math.log(a)/Math.log(10)},mod:function(a,b){return a%b},mul:function(a,b){return a*b},P:function(a,b){for(var c=1,d=Math.floor(a)-Math.floor(b)+1;d<=Math.floor(a);d++)c*=d;return c},Pi:function(a,b,c){for(var d=1,e=a;b>=e;e++)d*=c.postfixEval({n:e});return d},pow10x:function(a){for(var b=1;a--;)b*=10;return b},sigma:function(a,b,c){for(var d=0,e=a;b>=e;e++)d+=Number(c.postfixEval({n:e}));return d},sin:function(a){return d.isDegree&&(a=d.math.toRadian(a)),Math.sin(a)},sinh:function(a){return(Math.pow(Math.E,a)-Math.pow(Math.E,-1*a))/2},sub:function(a,b){return a-b},tan:function(a){return d.isDegree&&(a=d.math.toRadian(a)),Math.tan(a)},tanh:function(a){return d.sinha(a)/d.cosha(a)},toRadian:function(a){return a*Math.PI/180}},b.exports=d},{}],4:[function(a,b,c){var d=a("./lexer.js");Array.indexOf||(Array.prototype.indexOf=function(a){for(var b=0;b<this.length;b++)if(a==this[b])return b;return-1}),d.prototype.toPostfix=function(){"use strict";for(var a,b,c,e,f,g=[],h=[{value:"(",type:4,pre:0}],i=this.value,j=1;j<i.length;j++)if(1===i[j].type||3===i[j].type||23===i[j].type)1===i[j].type&&(i[j].value=Number(i[j].value)),g.push(i[j]);else if(4===i[j].type)h.push(i[j]);else if(5===i[j].type)for(;4!==(b=h.pop()).type;)g.push(b);else if(21===i[j].type){for(;4!==(b=h.pop()).type;)g.push(b);h.push(b)}else{a=i[j],e=a.pre,f=h[h.length-1],c=f.pre;var k="Math.pow"==f.value&&"Math.pow"==a.value;if(e>c)h.push(a);else{for(;c>=e&&!k||k&&c>e;)b=h.pop(),f=h[h.length-1],g.push(b),c=f.pre,k="Math.pow"==a.value&&"Math.pow"==f.value;h.push(a)}}return new d(g)},b.exports=d},{"./lexer.js":2}],5:[function(a,b,c){var d=a("./postfix.js");d.prototype.postfixEval=function(a){"use strict";a=a||{},a.PI=Math.PI,a.E=Math.E;for(var b,c,e,f=[],g=this.value,h="undefined"!=typeof a.n,i=0;i<g.length;i++)1===g[i].type?f.push({value:g[i].value,type:1}):3===g[i].type?f.push({value:a[g[i].value],type:1}):0===g[i].type?"undefined"==typeof f[f.length-1].type?f[f.length-1].push(g[i]):f[f.length-1].value=g[i].value(f[f.length-1].value):7===g[i].type?"undefined"==typeof f[f.length-1].type?f[f.length-1].push(g[i]):f[f.length-1].value=g[i].value(f[f.length-1].value):8===g[i].type?(b=f.pop(),c=f.pop(),f.push({type:1,value:g[i].value(c.value,b.value)})):11===g[i].type?(b=f.pop(),c=f.pop(),"undefined"==typeof c.type?(c=c.concat(b),c.push(g[i]),f.push(c)):"undefined"==typeof b.type?(b.unshift(c),b.push(g[i]),f.push(b)):f.push({type:1,value:g[i].value(c.value,b.value)})):2===g[i].type||9===g[i].type?(b=f.pop(),c=f.pop(),"undefined"==typeof c.type?(c=c.concat(b),c.push(g[i]),f.push(c)):"undefined"==typeof b.type?(b.unshift(c),b.push(g[i]),f.push(b)):f.push({type:1,value:g[i].value(c.value,b.value)})):22===g[i].type?(b=f.pop(),c=f.pop(),e=f.pop(),b.constructor!==Array&&(b=[b]),f.push({type:1,value:g[i].value(e.value,c.value,new d(b))})):23===g[i].type&&(h?f.push({value:a[g[i].value],type:3}):f.push([g[i]]));return f[0].value>1e15?"Infinity":Number(f[0].value.toFixed(15)).toPrecision()},d.eval=function(a,b,c){return"undefined"==typeof b?this.lex(a).toPostfix().postfixEval():"undefined"==typeof c?"undefined"!=typeof b.length?this.lex(a,b).toPostfix().postfixEval():this.lex(a).toPostfix().postfixEval(b):this.lex(a,b).toPostfix().postfixEval(c)},b.exports=d},{"./postfix.js":4}]},{},[1])(1)});
/** math-expression-evaluator version 1.2.0
Dated:2015-08-01 */
!function(a){if("object"==typeof exports&&"undefined"!=typeof module)module.exports=a();else if("function"==typeof define&&define.amd)define([],a);else{var b;b="undefined"!=typeof window?window:"undefined"!=typeof global?global:"undefined"!=typeof self?self:this,b.mexp=a()}}(function(){return function a(b,c,d){function e(g,h){if(!c[g]){if(!b[g]){var i="function"==typeof require&&require;if(!h&&i)return i(g,!0);if(f)return f(g,!0);var j=new Error("Cannot find module '"+g+"'");throw j.code="MODULE_NOT_FOUND",j}var k=c[g]={exports:{}};b[g][0].call(k.exports,function(a){var c=b[g][1][a];return e(c?c:a)},k,k.exports,a,b,c,d)}return c[g].exports}for(var f="function"==typeof require&&require,g=0;g<d.length;g++)e(d[g]);return e}({1:[function(a,b,c){var d=a("./postfix_evaluator.js");d.prototype.formulaEval=function(){"use strict";for(var a,b,c,d=[],e=this.value,f=0;f<e.length;f++)1===e[f].type||3===e[f].type?d.push({value:3===e[f].type?e[f].show:e[f].value,type:1}):23===e[f].type?d.push({value:e[f].show,type:1}):0===e[f].type?d[d.length-1]={value:e[f].show+d[d.length-1].value+("-"!=e[f].show?")":""),type:0}:7===e[f].type?d[d.length-1]={value:(1!=d[d.length-1].type?"(":"")+d[d.length-1].value+(1!=d[d.length-1].type?")":"")+e[f].show,type:7}:11===e[f].type?(a=d.pop(),b=d.pop(),"P"===e[f].show||"C"===e[f].show?d.push({value:"<sup>"+b+"</sup>"+e[f].show+"<sub>"+a+"</sub>",type:11}):d.push({value:(1!=b.type?"(":"")+b.value+(1!=b.type?")":"")+"<sup>"+a.value+"</sup>",type:1})):2===e[f].type||9===e[f].type?(a=d.pop(),b=d.pop(),d.push({value:(1!=b.type?"(":"")+b.value+(1!=b.type?")":"")+e[f].show+(1!=a.type?"(":"")+a.value+(1!=a.type?")":""),type:e[f].type})):22===e[f].type&&(a=d.pop(),b=d.pop(),c=d.pop(),d.push({value:e[f].show+"("+c.value+","+b.value+","+a.value+")",type:22}));return d[0].value},b.exports=d},{"./postfix_evaluator.js":5}],2:[function(a,b,c){function d(a,b){for(var c=0;c<a.length;c++)a[c]+=b;return a}function e(a,b,c,d){for(var e=0;d>e;e++)if(a[c+e]!==b[e])return!1;return!0}var f=a("./math_function.js"),g=["sin","cos","tan","pi","(",")","Del","P","C","asin","acos","atan","7","8","9","int","cosh","acosh","ln","^","root","4","5","6","/","!","tanh","atanh","Mod","1","2","3","*","=","sinh","asinh","e","log","10^x","0",".","+","-",",","Sigma","n","Pi","pow"],h=["sin(","cos(","tan(","&pi;","(",")","Del","P","C","asin(","acos(","atan(","7","8","9","Int(","cosh(","acosh("," ln(","^","root(","4","5","6","&divide;","!","tanh(","atanh("," Mod ","1","2","3","&times;","=","sinh(","asinh(","e"," log("," 10^(","0",".","+","-",",","&Sigma;","n","&Pi;","pow("],j=[f.math.sin,f.math.cos,f.math.tan,"PI","(",")","Del",f.math.P,f.math.C,f.math.asin,f.math.acos,f.math.atan,"7","8","9",Math.floor,f.math.cosh,f.math.acosh,Math.log,Math.pow,Math.pow,"4","5","6",f.math.div,f.math.fact,f.math.tanh,f.math.atanh,f.math.mod,"1","2","3",f.math.mul,"=",f.math.sinh,f.math.asinh,"E",f.math.log,f.math.pow10x,"0",".",f.math.add,f.math.sub,",",f.math.sigma,"n",f.math.Pi,Math.pow],k=[11,11,11,0,0,0,0,7,7,11,11,10,0,0,0,11,11,11,10,10,11,0,0,0,3,11,11,11,10,0,0,0,3,0,11,11,0,11,11,0,0,1,1,0,11,0,11,11],l=[0,0,0,3,4,5,12,11,11,0,0,0,1,1,1,0,0,0,0,11,11,1,1,1,2,7,0,0,2,1,1,1,2,13,0,0,3,0,0,1,6,9,9,21,22,23,22,8],m={0:!0,1:!0,3:!0,4:!0,6:!0,8:!0,9:!0,17:!0,22:!0,23:!0},n={0:!0,1:!0,2:!0,3:!0,4:!0,5:!0,6:!0,7:!0,8:!0,9:!0,10:!0,11:!0,13:!0,17:!0,21:!0,22:!0,23:!0},o={0:!0,3:!0,4:!0,8:!0,17:!0,22:!0,23:!0},p={},q={0:!0,1:!0,3:!0,4:!0,6:!0,8:!0,17:!0,22:!0,23:!0},r={1:!0},s=[[],["1","2","3","7","8","9","4","5","6","+","-","*","/","(",")","^","!","P","C","=","e","0",".",",","n"],["pi","ln","Pi"],["Ans","sin","cos","tan","Del","int","Mod","log","pow"],["asin","acos","atan","cosh","root","tanh","sinh","10^x"],["acosh","atanh","asinh","Sigma"]];f.addToken=function(a){for(i=0;i<a.length;i++){x=a[i].token.length;var b=-1;if(x<s.length)for(y=0;y<s[x].length;y++)if(a[i].token===s[x][y]){b=g.indexOf(s[x][y]);break}-1===b?(g.push(a[i].token),l.push(a[i].type),s.length<=a[i].token.length&&(s[a[i].token.length]=[]),s[a[i].token.length].push(a[i].token),j.push(a[i].ev),k.push(a[i].preced),h.push(a[i].show)):(g[b]=a[i].token,l[b]=a[i].type,j[b]=a[i].ev,k[b]=a[i].preced,h[b]=a[i].show)}},f.lex=function(a,b){"use strict";var c,i,t,u=[{type:4,value:"(",show:"(",pre:0}],v=[],w=a,x="",y=m,z=0,A=p,B="";"undefined"!=typeof b&&f.addToken(b);var C={};for(c=0;c<w.length;c++)if(" "!=w[c]){a:for(i=w.length-c>s.length-2?s.length-1:w.length-c;i>0;i--)for(t=0;t<s[i].length;t++)if(e(w,s[i][t],c,i)){x=s[i][t];break a}if(c+=x.length-1,""===x)throw new f.exception("Can't understand after "+w.slice(c));var D=g.indexOf(x),E=l[D],F=j[D],G=k[D],H=h[D],I=u[u.length-1];for(J=v.length;J--;)if(0===v[J]&&-1!==[0,2,3,5,9,21,22,23].indexOf(E)){if(y[E]!==!0)throw new f.exception(x+" is not allowed after "+B);u.push({value:")",type:5,pre:3,show:")"}),y=n,A=q,d(v,-1).pop()}if(y[E]!==!0)throw new f.exception(x+" is not allowed after "+B);if(A[E]===!0&&(E=2,F=f.math.mul,H="&times;",G=3,c-=x.length),C={value:F,type:E,pre:G,show:H},0===E)y=m,A=p,d(v,2).push(2),u.push(C),u.push({value:"(",type:4,pre:0,show:"("});else if(1===E)1===I.type?(I.value+=F,d(v,1)):u.push(C),y=n,A=o;else if(2===E)y=m,A=p,d(v,2),u.push(C);else if(3===E)u.push(C),y=n,A=q;else if(4===E)d(v,1),z++,y=m,A=p,u.push(C);else if(5===E){if(!z)throw new f.exception("Closing parenthesis are more than opening one, wait What!!!");z--,y=n,A=q,u.push(C)}else if(6===E){if(I.hasDec)throw new f.exception("Two decimals are not allowed in one number");1!==I.type&&(u.push({value:0,type:1,pre:0}),d(v,-1)),y=r,d(v,1),A=p,I.value+=F,I.hasDec=!0}else 7===E&&(y=n,A=q,d(v,1),u.push(C));8===E?(y=m,A=p,d(v,4).push(4),u.push(C),u.push({value:"(",type:4,pre:0,show:"("})):9===E?(9===I.type?"+"===I.value?(I.value=F,I.show=H,d(v,1)):"-"===I.value&&"-"===H&&(I.value="+",I.show="+",d(v,1)):5!==I.type&&7!==I.type&&1!==I.type&&3!==I.type&&23!==I.type?(u.push({value:f.math.changeSign,type:0,pre:21,show:"-"}),d(v,1)):(u.push(C),d(v,2)),y=m,A=p):11===E?(y=m,A=p,d(v,2),u.push(C)):21===E?(y=m,A=p,u.push(C)):22===E?(y=m,A=p,d(v,6).push(6),u.push(C),u.push({value:"(",type:4,pre:0})):23===E&&(y=n,A=q,u.push(C)),d(v,-1),B=x}for(var J=v.length;J--;)0===v[J]&&(u.push({value:")",show:")",type:5,pre:3}),d(v,-1).pop());if(y[5]!==!0)throw new f.exception("complete the expression");for(;z--;)u.push({value:")",show:")",type:5,pre:3});return u.push({type:5,value:")",show:")",pre:0}),new f(u)},b.exports=f},{"./math_function.js":3}],3:[function(a,b,c){var d=function(a,b){b&&(this.value=[]),this.value=a};d.math={isDegree:!1,acos:function(a){return d.isDegree?180/Math.PI*Math.acos(a):Math.acos(a)},add:function(a,b){return a+b},asin:function(a){return d.isDegree?180/Math.PI*Math.asin(a):Math.asin(a)},atan:function(a){return d.isDegree?180/Math.PI*Math.atan(a):Math.atan(a)},acosh:function(a){return Math.log(a+Math.sqrt(a*a-1))},asinh:function(a){return Math.log(a+Math.sqrt(a*a+1))},atanh:function(a){return Math.log((1+a)/(1-a))},C:function(a,b){var c=1,e=a-b,f=b;e>f&&(f=e,e=b);for(var g=f+1;a>=g;g++)c*=g;return c/d.fact(e)},changeSign:function(a){return-a},cos:function(a){return d.isDegree&&(a=d.math.toRadian(a)),Math.cos(a)},cosh:function(a){return(Math.pow(Math.E,a)+Math.pow(Math.E,-1*a))/2},div:function(a,b){return a/b},fact:function(a){if(a%1!==0)return"NAN";for(var b=1,c=2;a>=c;c++)b*=c;return b},inverse:function(a){return 1/a},log:function(a){return Math.log(a)/Math.log(10)},mod:function(a,b){return a%b},mul:function(a,b){return a*b},P:function(a,b){for(var c=1,d=Math.floor(a)-Math.floor(b)+1;d<=Math.floor(a);d++)c*=d;return c},Pi:function(a,b,c){for(var d=1,e=a;b>=e;e++)d*=c.postfixEval({n:e});return d},pow10x:function(a){for(var b=1;a--;)b*=10;return b},sigma:function(a,b,c){for(var d=0,e=a;b>=e;e++)d+=Number(c.postfixEval({n:e}));return d},sin:function(a){return d.isDegree&&(a=d.math.toRadian(a)),Math.sin(a)},sinh:function(a){return(Math.pow(Math.E,a)-Math.pow(Math.E,-1*a))/2},sub:function(a,b){return a-b},tan:function(a){return d.isDegree&&(a=d.math.toRadian(a)),Math.tan(a)},tanh:function(a){return d.sinha(a)/d.cosha(a)},toRadian:function(a){return a*Math.PI/180}},d.exception=function(a){this.message=a},b.exports=d},{}],4:[function(a,b,c){var d=a("./lexer.js");Array.indexOf||(Array.prototype.indexOf=function(a){for(var b=0;b<this.length;b++)if(a==this[b])return b;return-1}),d.prototype.toPostfix=function(){"use strict";for(var a,b,c,e,f,g=[],h=[{value:"(",type:4,pre:0}],i=this.value,j=1;j<i.length;j++)if(1===i[j].type||3===i[j].type||23===i[j].type)1===i[j].type&&(i[j].value=Number(i[j].value)),g.push(i[j]);else if(4===i[j].type)h.push(i[j]);else if(5===i[j].type)for(;4!==(b=h.pop()).type;)g.push(b);else if(21===i[j].type){for(;4!==(b=h.pop()).type;)g.push(b);h.push(b)}else{a=i[j],e=a.pre,f=h[h.length-1],c=f.pre;var k="Math.pow"==f.value&&"Math.pow"==a.value;if(e>c)h.push(a);else{for(;c>=e&&!k||k&&c>e;)b=h.pop(),f=h[h.length-1],g.push(b),c=f.pre,k="Math.pow"==a.value&&"Math.pow"==f.value;h.push(a)}}return new d(g)},b.exports=d},{"./lexer.js":2}],5:[function(a,b,c){var d=a("./postfix.js");d.prototype.postfixEval=function(a){"use strict";a=a||{},a.PI=Math.PI,a.E=Math.E;for(var b,c,e,f=[],g=this.value,h="undefined"!=typeof a.n,i=0;i<g.length;i++)1===g[i].type?f.push({value:g[i].value,type:1}):3===g[i].type?f.push({value:a[g[i].value],type:1}):0===g[i].type?"undefined"==typeof f[f.length-1].type?f[f.length-1].push(g[i]):f[f.length-1].value=g[i].value(f[f.length-1].value):7===g[i].type?"undefined"==typeof f[f.length-1].type?f[f.length-1].push(g[i]):f[f.length-1].value=g[i].value(f[f.length-1].value):8===g[i].type?(b=f.pop(),c=f.pop(),f.push({type:1,value:g[i].value(c.value,b.value)})):11===g[i].type?(b=f.pop(),c=f.pop(),"undefined"==typeof c.type?(c=c.concat(b),c.push(g[i]),f.push(c)):"undefined"==typeof b.type?(b.unshift(c),b.push(g[i]),f.push(b)):f.push({type:1,value:g[i].value(c.value,b.value)})):2===g[i].type||9===g[i].type?(b=f.pop(),c=f.pop(),"undefined"==typeof c.type?(c=c.concat(b),c.push(g[i]),f.push(c)):"undefined"==typeof b.type?(b.unshift(c),b.push(g[i]),f.push(b)):f.push({type:1,value:g[i].value(c.value,b.value)})):22===g[i].type?(b=f.pop(),c=f.pop(),e=f.pop(),b.constructor!==Array&&(b=[b]),f.push({type:1,value:g[i].value(e.value,c.value,new d(b))})):23===g[i].type&&(h?f.push({value:a[g[i].value],type:3}):f.push([g[i]]));if(f.length>1)throw new d.exception("Math error");return f[0].value>1e15?"Infinity":Number(f[0].value.toFixed(15)).toPrecision()},d.eval=function(a,b,c){return"undefined"==typeof b?this.lex(a).toPostfix().postfixEval():"undefined"==typeof c?"undefined"!=typeof b.length?this.lex(a,b).toPostfix().postfixEval():this.lex(a).toPostfix().postfixEval(b):this.lex(a,b).toPostfix().postfixEval(c)},b.exports=d},{"./postfix.js":4}]},{},[1])(1)});
{
"name": "math-expression-evaluator",
"version": "1.1.0",
"version": "1.2.0",
"description": "A flexible math expression evaluator",

@@ -5,0 +5,0 @@ "main": "src/formula_evaluator.js",

@@ -128,4 +128,3 @@

if(key===''){
console.error("Can't understand after "+inpStr.slice(i));
return;
throw(new Mexp.exception("Can't understand after "+inpStr.slice(i)));
}

@@ -141,2 +140,5 @@ var index=token.indexOf(key);

if([0,2,3,5,9,21,22,23].indexOf(cType)!==-1){
if(allowed[cType]!==true){
throw(new Mexp.exception(key+" is not allowed after "+prevKey));
}
str.push({value:")",type:5,pre:3,show:")"});

@@ -150,4 +152,3 @@ allowed=type1;

if(allowed[cType]!==true){
console.error(key+" is not allowed after "+prevKey);
return;
throw(new Mexp.exception(key+" is not allowed after "+prevKey));
}

@@ -192,3 +193,3 @@ if(asterick[cType]===true){

else if(cType===4){
inc(ptc,2);
inc(ptc,1);
bracToClose++;

@@ -201,4 +202,3 @@ allowed=type0;

if(!bracToClose){
console.error("Closing parenthesis are more than opening one, wait What!!!");
return;
throw(new Mexp.exception("Closing parenthesis are more than opening one, wait What!!!"));
}

@@ -212,4 +212,3 @@ bracToClose--;

if(pre.hasDec){
console.error("Two decimals are not allowed in one number");
return;
throw(new Mexp.exception("Two decimals are not allowed in one number"));
}

@@ -295,2 +294,5 @@ if(pre.type!==1){

}
if (allowed[5]!==true) {
throw(new Mexp.exception("complete the expression"));
}
while(bracToClose--)

@@ -297,0 +299,0 @@ str.push({value:")",show:")",type:5,pre:3});

@@ -1,4 +0,7 @@

var Mexp=function(parsed){
"use strict";
var Mexp=function(parsed,error){
if (error) {
this.value=[];
}
this.value=parsed;
};

@@ -117,2 +120,5 @@

};
Mexp.exception=function(message){
this.message=message;
};
module.exports=Mexp;

@@ -85,2 +85,5 @@ var Mexp=require('./postfix.js');

}
if (stack.length>1) {
throw(new Mexp.exception("Math error"));
}
return stack[0].value>1000000000000000?"Infinity":Number(stack[0].value.toFixed(15)).toPrecision();

@@ -87,0 +90,0 @@ };

@@ -72,2 +72,16 @@ // This test is for node JS

});
it('check if expression ends with cos', function () {
var str=a.eval("sin(2,3)");
var dc=''
console.log(str);
});
var date=new Date;
for (var i=0;i<100000;i++) {
var res=a.eval("15*3");
}
console.log(new Date-date);
console.log(res);
});
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