essy-evaluator
Advanced tools
Comparing version 1.0.10 to 1.1.0
@@ -397,2 +397,10 @@ (function (global, factory) { | ||
/***************************************************************** | ||
* | ||
* Evaluates expressions. | ||
* @class Evaluator | ||
* @constructor | ||
* | ||
****************************************************************/ | ||
function Evaluator () { | ||
@@ -436,13 +444,13 @@ this.symbolTable = {}; | ||
if (typeof t === 'number') { | ||
if (t.type === 'number' || t.type === 'string') { | ||
o = this.symbolTable[LITERAL_TOKEN_ID]; | ||
this.activeSymbol = Object.create(o); | ||
this.activeSymbol.value = t; | ||
this.activeSymbol.value = t.value; | ||
} | ||
else { | ||
o = this.symbolTable[t]; | ||
o = this.symbolTable[t.value]; | ||
if (!o) { | ||
throw new EvaluateException( | ||
EvaluateException.TYPE_UNDEFINED_SYMBOL, | ||
'The symbol "' + t + '" is not defined.' | ||
'The symbol "' + t.value + '" is not defined.' | ||
); | ||
@@ -464,4 +472,2 @@ } | ||
var tokens = []; | ||
if (typeof exp === 'string') { | ||
@@ -471,5 +477,2 @@ var parser = new Parser(); | ||
} | ||
for (var i = 0; i < exp.length; i++) { | ||
tokens.push(exp[i].value); | ||
} | ||
@@ -480,3 +483,3 @@ if (names) { | ||
this.tokenSet = tokens; | ||
this.tokenSet = exp; | ||
this.tokenIndex = 0; | ||
@@ -958,2 +961,3 @@ this.activeSymbol = 0; | ||
function fac (x) { | ||
if (x < 0) return -1; | ||
if (x === 0) return 1; | ||
@@ -960,0 +964,0 @@ return x * fac(x - 1); |
@@ -1,1 +0,1 @@ | ||
!function(t,e){"object"==typeof exports&&"undefined"!=typeof module?module.exports=e():"function"==typeof define&&define.amd?define(e):t.essyExpr=e()}(this,function(){"use strict";function t(t,e){this.type=t,this.message=e}function e(t,e){this.type=t,this.message=e}function n(t){t=t||{},this.operators=a,this.updateOperators(t.operators||{})}function i(t,e){if(e)for(var n in e)e.hasOwnProperty(n)&&(t[n]=e[n]);return t}function r(){this.symbolTable={},this.tokenIndex=0,this.tokenSet=null,this.activeSymbol=null,this.defineSymbols()}t.prototype.toString=function(){return this.type+": "+this.message},t.TYPE_ARGUMENT_RANGE="Argument Range",t.TYPE_BAD_TOKEN="Bad Token",t.TYPE_DIVIDE_BY_ZERO="Divide by Zero",t.TYPE_UNDEFINED_SYMBOL="Undefined Symbol",module.exports=t,e.prototype.toString=function(){return this.type+": "+this.message},e.TYPE_INVALID_EXPONENT="Invalid Exponent",e.TYPE_INVALID_NUMBER="Invalid Number",e.TYPE_INVALID_OPERATOR="Invalid Operator",e.TYPE_TRAILING_OPERATOR="Trailing Operator",e.TYPE_UNTERMINATED_STRING="Unterminated String",module.exports=e;var a={"(":1,")":1,"[":1,"]":1,"+":1,"-":1,"*":1,"/":1,"^":1,"%":1,"?":1,":":1,",":1,"<":1,"<=":1,">":1,">=":1,"==":1,"!=":1,"!":1,"&&":1,"||":1};n.prototype.updateOperators=function(t){for(var e in t)this.operators[e]=t[e];this.opPrefix="",this.opSuffix="";for(var n in this.operators)n.length>1&&(this.opPrefix+=n[0],this.opSuffix+=n[n.length-1])},n.prototype.parse=function(t){var n,i,r,a,s=0,o=t.length,u=[],h=function(t,e){return{type:t,value:e}};if(!(t=String(t)))return[];for(n=t.charAt(s);n;)if(n<=" ")s+=1,n=t.charAt(s);else if(n>="a"&&n<="z"||n>="A"&&n<="Z"||"_"===n){for(a=n,s+=1;(n=t.charAt(s))>="a"&&n<="z"||n>="A"&&n<="Z"||n>="0"&&n<="9"||"_"===n;)a+=n,s+=1;u.push(h("name",a))}else if(n>="0"&&n<="9"||"."===n){for(a=n,s+=1;!((n=t.charAt(s))<"0"||n>"9");)s+=1,a+=n;if("."===n)for(s+=1,a+=n;!((n=t.charAt(s))<"0"||n>"9");)s+=1,a+=n;if("e"===n||"E"===n){if(s+=1,a+=n,"-"!==(n=t.charAt(s))&&"+"!==n||(s+=1,a+=n,n=t.charAt(s)),n<"0"||n>"9")throw new e(e.TYPE_INVALID_EXPONENT,a+n);do{s+=1,a+=n,n=t.charAt(s)}while(n>="0"&&n<="9")}if(n>="a"&&n<="z")throw new e(e.TYPE_INVALID_NUMBER,a+n);if(i=+a,!isFinite(i))throw new e(e.TYPE_INVALID_NUMBER,a);u.push(h("number",i))}else if("'"===n||'"'===n){for(a="",r=n,s+=1;;){if((n=t.charAt(s))<" ")throw new e(e.TYPE_UNTERMINATED_STRING,a);if(n===r)break;a+=n,s+=1}s+=1,u.push(h("string",a)),n=t.charAt(s)}else if(this.opPrefix.indexOf(n)>=0){for(a=n,s+=1;s<o&&(n=t.charAt(s),!(this.opSuffix.indexOf(n)<0));)a+=n,s+=1;if(!this.operators[a])throw new e(e.TYPE_INVALID_OPERATOR,a);if(u.push(h("operator",a)),s>=o)throw new e(e.TYPE_TRAILING_OPERATOR,a)}else{if(s+=1,!this.operators[n])throw new e(e.TYPE_INVALID_OPERATOR,n);if(u.push(h("operator",n)),s>=o&&")"!==n&&"]"!==n)throw new e(e.TYPE_TRAILING_OPERATOR,n);n=t.charAt(s)}return u},module.exports=n;var s="(end)";r.prototype={advance:function(e){var n,i;if(e&&this.activeSymbol&&this.activeSymbol.id!==e)throw new t(t.TYPE_BAD_TOKEN,'Expected token with id "'+e+'".');if(this.tokenIndex>=this.tokenSet.length)return this.activeSymbol=this.symbolTable[s],this.activeSymbol;if(i=this.tokenSet[this.tokenIndex],this.tokenIndex+=1,"number"==typeof i)n=this.symbolTable["(literal)"],this.activeSymbol=Object.create(n),this.activeSymbol.value=i;else{if(!(n=this.symbolTable[i]))throw new t(t.TYPE_UNDEFINED_SYMBOL,'The symbol "'+i+'" is not defined.');this.activeSymbol=Object.create(n)}return this.activeSymbol},evaluate:function(t,e){var i=[];if("string"==typeof t){t=(new n).parse(t)}for(var r=0;r<t.length;r++)i.push(t[r].value);e&&this.defineNames(e),this.tokenSet=i,this.tokenIndex=0,this.activeSymbol=0,this.advance();return this.expression(0).ev()},expression:function(t){var e,n=this.activeSymbol;for(this.advance(),e=n.nud();t<this.activeSymbol.lbp;)n=this.activeSymbol,this.advance(),e=n.led(e);return e},defineFunction:function(t,e,n){this.defineSymbol(t,i({nud:function(){if(this.args=[],this.evaluator.advance("("),!n)for(;;){if(this.args.push(this.evaluator.expression(0)),","!=this.evaluator.activeSymbol.id)break;this.evaluator.advance(",")}return this.evaluator.advance(")"),this},argValue:function(t){return this.args[t].ev()},argValues:function(){for(var t=[],e=0;e<this.args.length;e++)t.push(this.args[e].ev());return t}},{ev:e}))},defineFunctions:function(t){for(var e in t)this.defineFunction(e,t[e])},defineInfixOperator:function(t,e,n){this.defineSymbol(t,i({lbp:e,led:function(t){return this.first=t,this.second=this.evaluator.expression(this.lbp),this},firstValue:function(){return this.first.ev()},secondValue:function(){return this.second.ev()}},n))},defineInfixOperators:function(t){for(var e in t){var n=t[e],i={};for(var r in n)"lbp"!==r&&(i[r]=n[r]);this.defineInfixOperator(e,n.lbp,i)}},defineInfixROperator:function(t,e,n){this.defineSymbol(t,{lbp:e,led:function(t){return this.first=t,this.second=this.evaluator.expression(this.lbp-1),this},ev:n})},defineInfixROperators:function(t){for(var e in t)this.defineInfixROperator(e,t[e].lbp,t[e].ev)},defineName:function(t,e){this.defineSymbol(t,{value:e,ev:function(){return this.value}})},defineNames:function(t){for(var e in t)this.defineName(e,t[e])},definePrefixOperator:function(t,e){this.defineSymbol(t,i({nud:function(){return this.first=this.evaluator.expression(70),this}},e))},definePrefixOperators:function(t){for(var e in t)this.definePrefixOperator(e,t[e])},defineSymbols:function(){var e=this;[":",",",")","]","}",s].forEach(function(t){e.defineSymbol(t)}),this.defineNames({FALSE:0,TRUE:1,E:Math.E,LN2:Math.log(2),LN10:Math.log(10),PI:Math.PI,SQRT1_2:1/Math.sqrt(2),SQRT2:Math.sqrt(2)}),this.defineName("(literal)",0),this.definePrefixOperators({"!":{ev:function(){return this.first.ev()>0?0:1}},"(":{nud:function(){var t=this.evaluator.expression(0);return this.evaluator.advance(")"),t}},"[":{nud:function(){var t=this.evaluator.expression(0);return this.evaluator.advance("]"),t}},"{":{nud:function(){var t=this.evaluator.expression(0);return this.evaluator.advance("}"),t}}}),this.defineInfixOperators({"+":{lbp:50,ev:function(){return this.firstValue()+this.secondValue()}},"-":{lbp:50,nud:function(){return this.first=this.evaluator.expression(70),this.isPrefix=!0,this},ev:function(){return this.isPrefix?-this.firstValue():this.firstValue()-this.secondValue()}},"*":{lbp:60,ev:function(){return this.firstValue()*this.secondValue()}},"/":{lbp:60,ev:function(){var e=this.secondValue();if(0===e)throw new t(t.TYPE_DIVIDE_BY_ZERO,'Attempt to divide by 0 using the "/" operator.');return this.firstValue()/e}},"^":{lbp:70,ev:function(){return Math.pow(this.firstValue(),this.secondValue())}},"==":{lbp:40,ev:function(){return this.firstValue()===this.secondValue()?1:0}},"!=":{lbp:40,ev:function(){return this.firstValue()===this.secondValue()?0:1}},"<":{lbp:40,ev:function(){return this.firstValue()<this.secondValue()?1:0}},"<=":{lbp:40,ev:function(){return this.firstValue()<=this.secondValue()?1:0}},">":{lbp:40,ev:function(){return this.firstValue()>this.secondValue()?1:0}},">=":{lbp:40,ev:function(){return this.firstValue()>=this.secondValue()?1:0}},"?":{lbp:20,led:function(t){return this.first=t,this.second=this.evaluator.expression(0),this.evaluator.advance(":"),this.third=this.evaluator.expression(0),this},ev:function(){return this.firstValue()?this.secondValue():this.third.ev()}}}),this.defineInfixROperators({"&&":{lbp:30,ev:function(){return this.first.ev()&&this.second.ev()?1:0}},"||":{lbp:30,ev:function(){return this.first.ev()||this.second.ev()?1:0}}}),this.defineFunctions({abs:function(){return Math.abs(this.argValue(0))},acos:function(){var e=this.argValue(0);if(e<-1||e>1)throw new t(t.TYPE_ARGUMENT_RANGE,'At "acos('+e+')": argument must be in range [-1, 1].');return Math.acos(e)},and:function(){for(var t=0;t<this.args.length;t++)if(this.argValue(t)<=0)return 0;return 1},asin:function(){var e=this.argValue(0);if(e<0||e>1)throw new t(t.TYPE_ARGUMENT_RANGE,'At "asin('+e+')": argument must be in range [0, 1].');return Math.asin(e)},atan:function(){var e=this.argValue(0);if(e<-1||e>1)throw new t(t.TYPE_ARGUMENT_RANGE,'At "atan('+e+')": argument must be in range [-1, 1].');return Math.atan(e)},ceiling:function(){return Math.ceil(this.argValue(0))},choose:function(){var e=this.argValue(0);if(e<1||e>this.args.length)throw new t(t.TYPE_ARGUMENT_RANGE,'At "choose('+e+',...)": the index is out of bounds.');return this.argValue(e)},cos:function(){return Math.cos(this.argValue(0))},exp:function(){return Math.exp(this.argValue(0))},fac:function(){function t(e){return 0===e?1:e*t(e-1)}return t(this.argValue(0))},floor:function(){return Math.floor(this.argValue(0))},if:function(){return this.argValue(0)>0?this.argValue(1):this.argValue(2)},log:function(){var e=this.argValue(0);if(e<=0)throw new t(t.TYPE_ARGUMENT_RANGE,'At "log('+e+')": argument must be greater than 0.');return Math.log(10)/Math.log(e)},ln:function(){var e=this.argValue(0);if(e<=0)throw new t(t.TYPE_ARGUMENT_RANGE,'At "ln('+e+')": argument must be greater than 0.');return Math.log(e)},max:function(){return Math.max.apply(null,this.argValues())},mean:function(){for(var t=0,e=this.argValues(),n=0;n<e.length;n++)t+=e[n];return t/e.length},median:function(){var t=this.argValues().sort(function(t,e){return t-e});return t.length%2==0?(t[t.length/2-1]+t[t.length/2])/2:t[(t.length-1)/2]},min:function(){return Math.min.apply(null,this.argValues())},mod:function(){var e=this.argValue(1);if(0===e)throw new t(t.TYPE_DIVIDE_BY_ZERO,'At "mod('+e+')": cannot divide by 0.');return this.argValue(0)%e},not:function(){return this.argValue(0)>0?0:1},or:function(){for(var t=0,e=this.args.length;t<e;t++)if(this.argValue(t)>0)return 1;return 0},pow:function(){return Math.pow(this.argValue(0),this.argValue(1))},product:function(){for(var t=1,e=this.argValues(),n=0;n<e.length;n++)t*=e[n];return t},quotient:function(){var e=this.argValue(0),n=this.argValue(1);if(0===n)throw new t(t.TYPE_DIVIDE_BY_ZERO,'At "quotient('+e+", "+n+')": cannot divide by 0.');return Math.floor(e/n)},randInt:function(){var t=this.argValue(0);return t+parseInt(Math.random()*(this.argValue(1)-t))},randRange:function(){var t=this.argValue(0);return t+Math.random()*(this.argValue(1)-t)},round:function(){return Math.round(this.argValue(0))},sin:function(){return Math.sin(this.argValue(0))},sqrt:function(){var e=this.argValue(0);if(e<=0)throw new t(t.TYPE_ARGUMENT_RANGE,'At "sqrt('+e+')": argument must be greater than 0.');return Math.sqrt(e)},sum:function(){for(var t=0,e=this.argValues(),n=0;n<e.length;n++)t+=e[n];return t},tan:function(){return Math.tan(this.argValue(0))}}),this.defineFunction("rand",function(){return Math.random()},!0)},defineSymbol:function(t,e){(e=e||{}).evaluator=this,this.symbolTable[t]=function(t,e){return i(Object.create({id:t,lbp:0,nud:function(){return this},led:function(){return this},ev:function(){return 0}}),e)}(t,e)},deleteSymbol:function(t){return delete this.symbolTable[t]}};return{Evaluator:r,EvaluateException:t,Parser:n,ParseException:e}}); | ||
!function(t,e){"object"==typeof exports&&"undefined"!=typeof module?module.exports=e():"function"==typeof define&&define.amd?define(e):t.essyExpr=e()}(this,function(){"use strict";function t(t,e){this.type=t,this.message=e}function e(t,e){this.type=t,this.message=e}function n(t){t=t||{},this.operators=a,this.updateOperators(t.operators||{})}function i(t,e){if(e)for(var n in e)e.hasOwnProperty(n)&&(t[n]=e[n]);return t}function r(){this.symbolTable={},this.tokenIndex=0,this.tokenSet=null,this.activeSymbol=null,this.defineSymbols()}t.prototype.toString=function(){return this.type+": "+this.message},t.TYPE_ARGUMENT_RANGE="Argument Range",t.TYPE_BAD_TOKEN="Bad Token",t.TYPE_DIVIDE_BY_ZERO="Divide by Zero",t.TYPE_UNDEFINED_SYMBOL="Undefined Symbol",module.exports=t,e.prototype.toString=function(){return this.type+": "+this.message},e.TYPE_INVALID_EXPONENT="Invalid Exponent",e.TYPE_INVALID_NUMBER="Invalid Number",e.TYPE_INVALID_OPERATOR="Invalid Operator",e.TYPE_TRAILING_OPERATOR="Trailing Operator",e.TYPE_UNTERMINATED_STRING="Unterminated String",module.exports=e;var a={"(":1,")":1,"[":1,"]":1,"+":1,"-":1,"*":1,"/":1,"^":1,"%":1,"?":1,":":1,",":1,"<":1,"<=":1,">":1,">=":1,"==":1,"!=":1,"!":1,"&&":1,"||":1};n.prototype.updateOperators=function(t){for(var e in t)this.operators[e]=t[e];this.opPrefix="",this.opSuffix="";for(var n in this.operators)n.length>1&&(this.opPrefix+=n[0],this.opSuffix+=n[n.length-1])},n.prototype.parse=function(t){var n,i,r,a,s=0,o=t.length,u=[],h=function(t,e){return{type:t,value:e}};if(!(t=String(t)))return[];for(n=t.charAt(s);n;)if(n<=" ")s+=1,n=t.charAt(s);else if(n>="a"&&n<="z"||n>="A"&&n<="Z"||"_"===n){for(a=n,s+=1;(n=t.charAt(s))>="a"&&n<="z"||n>="A"&&n<="Z"||n>="0"&&n<="9"||"_"===n;)a+=n,s+=1;u.push(h("name",a))}else if(n>="0"&&n<="9"||"."===n){for(a=n,s+=1;!((n=t.charAt(s))<"0"||n>"9");)s+=1,a+=n;if("."===n)for(s+=1,a+=n;!((n=t.charAt(s))<"0"||n>"9");)s+=1,a+=n;if("e"===n||"E"===n){if(s+=1,a+=n,"-"!==(n=t.charAt(s))&&"+"!==n||(s+=1,a+=n,n=t.charAt(s)),n<"0"||n>"9")throw new e(e.TYPE_INVALID_EXPONENT,a+n);do{s+=1,a+=n,n=t.charAt(s)}while(n>="0"&&n<="9")}if(n>="a"&&n<="z")throw new e(e.TYPE_INVALID_NUMBER,a+n);if(i=+a,!isFinite(i))throw new e(e.TYPE_INVALID_NUMBER,a);u.push(h("number",i))}else if("'"===n||'"'===n){for(a="",r=n,s+=1;;){if((n=t.charAt(s))<" ")throw new e(e.TYPE_UNTERMINATED_STRING,a);if(n===r)break;a+=n,s+=1}s+=1,u.push(h("string",a)),n=t.charAt(s)}else if(this.opPrefix.indexOf(n)>=0){for(a=n,s+=1;s<o&&(n=t.charAt(s),!(this.opSuffix.indexOf(n)<0));)a+=n,s+=1;if(!this.operators[a])throw new e(e.TYPE_INVALID_OPERATOR,a);if(u.push(h("operator",a)),s>=o)throw new e(e.TYPE_TRAILING_OPERATOR,a)}else{if(s+=1,!this.operators[n])throw new e(e.TYPE_INVALID_OPERATOR,n);if(u.push(h("operator",n)),s>=o&&")"!==n&&"]"!==n)throw new e(e.TYPE_TRAILING_OPERATOR,n);n=t.charAt(s)}return u},module.exports=n;var s="(end)";r.prototype={advance:function(e){var n,i;if(e&&this.activeSymbol&&this.activeSymbol.id!==e)throw new t(t.TYPE_BAD_TOKEN,'Expected token with id "'+e+'".');if(this.tokenIndex>=this.tokenSet.length)return this.activeSymbol=this.symbolTable[s],this.activeSymbol;if(i=this.tokenSet[this.tokenIndex],this.tokenIndex+=1,"number"===i.type||"string"===i.type)n=this.symbolTable["(literal)"],this.activeSymbol=Object.create(n),this.activeSymbol.value=i.value;else{if(!(n=this.symbolTable[i.value]))throw new t(t.TYPE_UNDEFINED_SYMBOL,'The symbol "'+i.value+'" is not defined.');this.activeSymbol=Object.create(n)}return this.activeSymbol},evaluate:function(t,e){if("string"==typeof t){t=(new n).parse(t)}e&&this.defineNames(e),this.tokenSet=t,this.tokenIndex=0,this.activeSymbol=0,this.advance();return this.expression(0).ev()},expression:function(t){var e,n=this.activeSymbol;for(this.advance(),e=n.nud();t<this.activeSymbol.lbp;)n=this.activeSymbol,this.advance(),e=n.led(e);return e},defineFunction:function(t,e,n){this.defineSymbol(t,i({nud:function(){if(this.args=[],this.evaluator.advance("("),!n)for(;;){if(this.args.push(this.evaluator.expression(0)),","!=this.evaluator.activeSymbol.id)break;this.evaluator.advance(",")}return this.evaluator.advance(")"),this},argValue:function(t){return this.args[t].ev()},argValues:function(){for(var t=[],e=0;e<this.args.length;e++)t.push(this.args[e].ev());return t}},{ev:e}))},defineFunctions:function(t){for(var e in t)this.defineFunction(e,t[e])},defineInfixOperator:function(t,e,n){this.defineSymbol(t,i({lbp:e,led:function(t){return this.first=t,this.second=this.evaluator.expression(this.lbp),this},firstValue:function(){return this.first.ev()},secondValue:function(){return this.second.ev()}},n))},defineInfixOperators:function(t){for(var e in t){var n=t[e],i={};for(var r in n)"lbp"!==r&&(i[r]=n[r]);this.defineInfixOperator(e,n.lbp,i)}},defineInfixROperator:function(t,e,n){this.defineSymbol(t,{lbp:e,led:function(t){return this.first=t,this.second=this.evaluator.expression(this.lbp-1),this},ev:n})},defineInfixROperators:function(t){for(var e in t)this.defineInfixROperator(e,t[e].lbp,t[e].ev)},defineName:function(t,e){this.defineSymbol(t,{value:e,ev:function(){return this.value}})},defineNames:function(t){for(var e in t)this.defineName(e,t[e])},definePrefixOperator:function(t,e){this.defineSymbol(t,i({nud:function(){return this.first=this.evaluator.expression(70),this}},e))},definePrefixOperators:function(t){for(var e in t)this.definePrefixOperator(e,t[e])},defineSymbols:function(){var e=this;[":",",",")","]","}",s].forEach(function(t){e.defineSymbol(t)}),this.defineNames({FALSE:0,TRUE:1,E:Math.E,LN2:Math.log(2),LN10:Math.log(10),PI:Math.PI,SQRT1_2:1/Math.sqrt(2),SQRT2:Math.sqrt(2)}),this.defineName("(literal)",0),this.definePrefixOperators({"!":{ev:function(){return this.first.ev()>0?0:1}},"(":{nud:function(){var t=this.evaluator.expression(0);return this.evaluator.advance(")"),t}},"[":{nud:function(){var t=this.evaluator.expression(0);return this.evaluator.advance("]"),t}},"{":{nud:function(){var t=this.evaluator.expression(0);return this.evaluator.advance("}"),t}}}),this.defineInfixOperators({"+":{lbp:50,ev:function(){return this.firstValue()+this.secondValue()}},"-":{lbp:50,nud:function(){return this.first=this.evaluator.expression(70),this.isPrefix=!0,this},ev:function(){return this.isPrefix?-this.firstValue():this.firstValue()-this.secondValue()}},"*":{lbp:60,ev:function(){return this.firstValue()*this.secondValue()}},"/":{lbp:60,ev:function(){var e=this.secondValue();if(0===e)throw new t(t.TYPE_DIVIDE_BY_ZERO,'Attempt to divide by 0 using the "/" operator.');return this.firstValue()/e}},"^":{lbp:70,ev:function(){return Math.pow(this.firstValue(),this.secondValue())}},"==":{lbp:40,ev:function(){return this.firstValue()===this.secondValue()?1:0}},"!=":{lbp:40,ev:function(){return this.firstValue()===this.secondValue()?0:1}},"<":{lbp:40,ev:function(){return this.firstValue()<this.secondValue()?1:0}},"<=":{lbp:40,ev:function(){return this.firstValue()<=this.secondValue()?1:0}},">":{lbp:40,ev:function(){return this.firstValue()>this.secondValue()?1:0}},">=":{lbp:40,ev:function(){return this.firstValue()>=this.secondValue()?1:0}},"?":{lbp:20,led:function(t){return this.first=t,this.second=this.evaluator.expression(0),this.evaluator.advance(":"),this.third=this.evaluator.expression(0),this},ev:function(){return this.firstValue()?this.secondValue():this.third.ev()}}}),this.defineInfixROperators({"&&":{lbp:30,ev:function(){return this.first.ev()&&this.second.ev()?1:0}},"||":{lbp:30,ev:function(){return this.first.ev()||this.second.ev()?1:0}}}),this.defineFunctions({abs:function(){return Math.abs(this.argValue(0))},acos:function(){var e=this.argValue(0);if(e<-1||e>1)throw new t(t.TYPE_ARGUMENT_RANGE,'At "acos('+e+')": argument must be in range [-1, 1].');return Math.acos(e)},and:function(){for(var t=0;t<this.args.length;t++)if(this.argValue(t)<=0)return 0;return 1},asin:function(){var e=this.argValue(0);if(e<0||e>1)throw new t(t.TYPE_ARGUMENT_RANGE,'At "asin('+e+')": argument must be in range [0, 1].');return Math.asin(e)},atan:function(){var e=this.argValue(0);if(e<-1||e>1)throw new t(t.TYPE_ARGUMENT_RANGE,'At "atan('+e+')": argument must be in range [-1, 1].');return Math.atan(e)},ceiling:function(){return Math.ceil(this.argValue(0))},choose:function(){var e=this.argValue(0);if(e<1||e>this.args.length)throw new t(t.TYPE_ARGUMENT_RANGE,'At "choose('+e+',...)": the index is out of bounds.');return this.argValue(e)},cos:function(){return Math.cos(this.argValue(0))},exp:function(){return Math.exp(this.argValue(0))},fac:function(){function t(e){return e<0?-1:0===e?1:e*t(e-1)}return t(this.argValue(0))},floor:function(){return Math.floor(this.argValue(0))},if:function(){return this.argValue(0)>0?this.argValue(1):this.argValue(2)},log:function(){var e=this.argValue(0);if(e<=0)throw new t(t.TYPE_ARGUMENT_RANGE,'At "log('+e+')": argument must be greater than 0.');return Math.log(10)/Math.log(e)},ln:function(){var e=this.argValue(0);if(e<=0)throw new t(t.TYPE_ARGUMENT_RANGE,'At "ln('+e+')": argument must be greater than 0.');return Math.log(e)},max:function(){return Math.max.apply(null,this.argValues())},mean:function(){for(var t=0,e=this.argValues(),n=0;n<e.length;n++)t+=e[n];return t/e.length},median:function(){var t=this.argValues().sort(function(t,e){return t-e});return t.length%2==0?(t[t.length/2-1]+t[t.length/2])/2:t[(t.length-1)/2]},min:function(){return Math.min.apply(null,this.argValues())},mod:function(){var e=this.argValue(1);if(0===e)throw new t(t.TYPE_DIVIDE_BY_ZERO,'At "mod('+e+')": cannot divide by 0.');return this.argValue(0)%e},not:function(){return this.argValue(0)>0?0:1},or:function(){for(var t=0,e=this.args.length;t<e;t++)if(this.argValue(t)>0)return 1;return 0},pow:function(){return Math.pow(this.argValue(0),this.argValue(1))},product:function(){for(var t=1,e=this.argValues(),n=0;n<e.length;n++)t*=e[n];return t},quotient:function(){var e=this.argValue(0),n=this.argValue(1);if(0===n)throw new t(t.TYPE_DIVIDE_BY_ZERO,'At "quotient('+e+", "+n+')": cannot divide by 0.');return Math.floor(e/n)},randInt:function(){var t=this.argValue(0);return t+parseInt(Math.random()*(this.argValue(1)-t))},randRange:function(){var t=this.argValue(0);return t+Math.random()*(this.argValue(1)-t)},round:function(){return Math.round(this.argValue(0))},sin:function(){return Math.sin(this.argValue(0))},sqrt:function(){var e=this.argValue(0);if(e<=0)throw new t(t.TYPE_ARGUMENT_RANGE,'At "sqrt('+e+')": argument must be greater than 0.');return Math.sqrt(e)},sum:function(){for(var t=0,e=this.argValues(),n=0;n<e.length;n++)t+=e[n];return t},tan:function(){return Math.tan(this.argValue(0))}}),this.defineFunction("rand",function(){return Math.random()},!0)},defineSymbol:function(t,e){(e=e||{}).evaluator=this,this.symbolTable[t]=function(t,e){return i(Object.create({id:t,lbp:0,nud:function(){return this},led:function(){return this},ev:function(){return 0}}),e)}(t,e)},deleteSymbol:function(t){return delete this.symbolTable[t]}};return{Evaluator:r,EvaluateException:t,Parser:n,ParseException:e}}); |
{ | ||
"name": "essy-evaluator", | ||
"version": "1.0.10", | ||
"version": "1.1.0", | ||
"description": "Parses and evaluates mathematical expressions from Javascript strings.", | ||
@@ -5,0 +5,0 @@ "main": "dist/bundle.js", |
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
50608
1056