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.0.4 to 1.0.5

LICENSE

23

dist/browser/math-expression-evaluator.js

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

/** math-expression-evaluator version 1.0.4
Dated:2015-07-18 */
/** math-expression-evaluator version 1.0.5
Dated:2015-07-19 */

@@ -284,3 +284,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){

else if(pre.type!==5&&pre.type!==7&&pre.type!==1&&pre.type!==3&&pre.type!==23){
str.push({value:Mexp.changeSigna,type:0,pre:21,show:"-"});
str.push({value:Mexp.math.changeSign,type:0,pre:21,show:"-"});
inc(ptc,1);

@@ -552,3 +552,3 @@ }

pop2=stack.pop();
if(typeof pop1.type==="undefined"||typeof pop2.type==="undefined"){
if(typeof pop2.type==="undefined"){
pop2=pop2.concat(pop1);

@@ -558,2 +558,7 @@ pop2.push(arr[i]);

}
else if (typeof pop1.type==="undefined") {
pop1.unshift(pop2);
pop1.push(arr[i]);
stack.push(pop1);
}
else

@@ -565,3 +570,3 @@ stack.push({type:1,value:arr[i].value(pop2.value,pop1.value)});

pop2=stack.pop();
if(typeof pop1.type==="undefined"||typeof pop2.type==="undefined"){
if(typeof pop2.type==="undefined"){
pop2=pop2.concat(pop1);

@@ -571,2 +576,7 @@ pop2.push(arr[i]);

}
else if (typeof pop1.type==="undefined") {
pop1.unshift(pop2);
pop1.push(arr[i]);
stack.push(pop1);
}
else{

@@ -580,2 +590,5 @@ stack.push({type:1,value:arr[i].value(pop2.value,pop1.value)});

pop3=stack.pop();
if (pop1.constructor!==Array) { //pop1 needs to be constructor
pop1=[pop1];
}
stack.push({type:1,value:arr[i].value(pop3.value,pop2.value,new Mexp(pop1))});

@@ -582,0 +595,0 @@ }

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

*/
/** math-expression-evaluator version 1.0.4
Dated:2015-07-18 */
!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){a:for(i=0;i<a.length;i++){if(x=a[i].token.length,x<s.length)for(y=0;y<s[x].length;y++)if(a[i].token===s[x][y])continue a;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)}},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=0;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).splice(J,1),J=-1);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.changeSigna,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=0;J<v.length;J++)0===v[J]&&(u.push({value:")",show:")",type:5,pre:3}),d(v,-1).pop(J),J--);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 b.type||"undefined"==typeof c.type?(c=c.concat(b),c.push(g[i]),f.push(c)):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 b.type||"undefined"==typeof c.type?(c=c.concat(b),c.push(g[i]),f.push(c)):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(),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()},b.exports=d},{"./postfix.js":4}]},{},[1])(1)});
/** math-expression-evaluator version 1.0.5
Dated:2015-07-19 */
!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){a:for(i=0;i<a.length;i++){if(x=a[i].token.length,x<s.length)for(y=0;y<s[x].length;y++)if(a[i].token===s[x][y])continue a;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)}},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=0;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).splice(J,1),J=-1);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=0;J<v.length;J++)0===v[J]&&(u.push({value:")",show:")",type:5,pre:3}),d(v,-1).pop(J),J--);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()},b.exports=d},{"./postfix.js":4}]},{},[1])(1)});
{
"name": "math-expression-evaluator",
"version": "1.0.4",
"version": "1.0.5",
"description": "A flexible math expression evaluator",

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

@@ -237,3 +237,3 @@

else if(pre.type!==5&&pre.type!==7&&pre.type!==1&&pre.type!==3&&pre.type!==23){
str.push({value:Mexp.changeSigna,type:0,pre:21,show:"-"});
str.push({value:Mexp.math.changeSign,type:0,pre:21,show:"-"});
inc(ptc,1);

@@ -240,0 +240,0 @@ }

@@ -40,3 +40,3 @@

pop2=stack.pop();
if(typeof pop1.type==="undefined"||typeof pop2.type==="undefined"){
if(typeof pop2.type==="undefined"){
pop2=pop2.concat(pop1);

@@ -46,2 +46,7 @@ pop2.push(arr[i]);

}
else if (typeof pop1.type==="undefined") {
pop1.unshift(pop2);
pop1.push(arr[i]);
stack.push(pop1);
}
else

@@ -53,3 +58,3 @@ stack.push({type:1,value:arr[i].value(pop2.value,pop1.value)});

pop2=stack.pop();
if(typeof pop1.type==="undefined"||typeof pop2.type==="undefined"){
if(typeof pop2.type==="undefined"){
pop2=pop2.concat(pop1);

@@ -59,2 +64,7 @@ pop2.push(arr[i]);

}
else if (typeof pop1.type==="undefined") {
pop1.unshift(pop2);
pop1.push(arr[i]);
stack.push(pop1);
}
else{

@@ -68,2 +78,5 @@ stack.push({type:1,value:arr[i].value(pop2.value,pop1.value)});

pop3=stack.pop();
if (pop1.constructor!==Array) { //pop1 needs to be constructor
pop1=[pop1];
}
stack.push({type:1,value:arr[i].value(pop3.value,pop2.value,new Mexp(pop1))});

@@ -70,0 +83,0 @@ }

@@ -39,15 +39,19 @@ // This test is for node JS

});
var str="";
var x=a.lex("sin4!").value;
for(i=0;i<x.length;i++)
str+=x[i].show;
console.log(str);
var str="";
var x=a.lex("sin(4)!").value;
for(i=0;i<x.length;i++)
str+=x[i].show;
console.log(str);
it('test to check auto correct of parenthesis mismatch if opening>closing', function () {
assert.equal(a.lex("(2+(3-4").toPostfix().postfixEval(),"1");
});
it('check for negative of a constant', function () {
assert.equal(a.lex("-e").toPostfix().postfixEval(),-Math.E);
});
it('check for constant inside Sigma', function () {
assert.equal(a.lex("Sigma1,3,x",[{type:3,preced:0,ev:"x",show:"x",token:"x"}]).toPostfix().postfixEval({x:2}),6);
});
it('check when arithmetic and n are present inside sigma', function () {
assert.equal(a.lex("Sigma1,2,(1+n!)").toPostfix().postfixEval(),5);
});
var temp="";
var str=a.lex("Sigma1,2,(1+n!)").toPostfix().value;
for(var i=0;i<str.length;i++)
temp+=str[i].show;
console.log(temp);
});
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