UNPKG

expression-calculator

Version:

Calculate expressions without using `eval()`, using LL(1) syntax analyzer.

1 lines 19.7 kB
!function(t,e){"object"==typeof exports&&"object"==typeof module?module.exports=e():"function"==typeof define&&define.amd?define([],e):"object"==typeof exports?exports.Calc=e():t.Calc=e()}(this,(function(){return function(){var t={670:function(t,e,r){var n=r(111);t.exports=function(t){if(!n(t))throw TypeError(String(t)+" is not an object");return t}},235:function(t,e,r){var n=r(857),o=r(656),i=r(61),u=r(70).f;t.exports=function(t){var e=n.Symbol||(n.Symbol={});o(e,t)||u(e,t,{value:i.f(t)})}},781:function(t,e,r){var n=r(293);t.exports=!n((function(){return 7!=Object.defineProperty({},1,{get:function(){return 7}})[1]}))},317:function(t,e,r){var n=r(854),o=r(111),i=n.document,u=o(i)&&o(i.createElement);t.exports=function(t){return u?i.createElement(t):{}}},113:function(t,e,r){var n=r(5);t.exports=n("navigator","userAgent")||""},392:function(t,e,r){var n,o,i=r(854),u=r(113),a=i.process,c=i.Deno,f=a&&a.versions||c&&c.version,l=f&&f.v8;l?o=(n=l.split("."))[0]<4?1:n[0]+n[1]:u&&(!(n=u.match(/Edge\/(\d+)/))||n[1]>=74)&&(n=u.match(/Chrome\/(\d+)/))&&(o=n[1]),t.exports=o&&+o},293:function(t){t.exports=function(t){try{return!!t()}catch(t){return!0}}},5:function(t,e,r){var n=r(854),o=function(t){return"function"==typeof t?t:void 0};t.exports=function(t,e){return arguments.length<2?o(n[t]):n[t]&&n[t][e]}},854:function(t,e,r){var n=function(t){return t&&t.Math==Math&&t};t.exports=n("object"==typeof globalThis&&globalThis)||n("object"==typeof window&&window)||n("object"==typeof self&&self)||n("object"==typeof r.g&&r.g)||function(){return this}()||Function("return this")()},656:function(t,e,r){var n=r(908),o={}.hasOwnProperty;t.exports=Object.hasOwn||function(t,e){return o.call(n(t),e)}},664:function(t,e,r){var n=r(781),o=r(293),i=r(317);t.exports=!n&&!o((function(){return 7!=Object.defineProperty(i("div"),"a",{get:function(){return 7}}).a}))},111:function(t){t.exports=function(t){return"object"==typeof t?null!==t:"function"==typeof t}},913:function(t){t.exports=!1},190:function(t,e,r){var n=r(5),o=r(307);t.exports=o?function(t){return"symbol"==typeof t}:function(t){var e=n("Symbol");return"function"==typeof e&&Object(t)instanceof e}},133:function(t,e,r){var n=r(392),o=r(293);t.exports=!!Object.getOwnPropertySymbols&&!o((function(){var t=Symbol();return!String(t)||!(Object(t)instanceof Symbol)||!Symbol.sham&&n&&n<41}))},70:function(t,e,r){var n=r(781),o=r(664),i=r(670),u=r(948),a=Object.defineProperty;e.f=n?a:function(t,e,r){if(i(t),e=u(e),i(r),o)try{return a(t,e,r)}catch(t){}if("get"in r||"set"in r)throw TypeError("Accessors not supported");return"value"in r&&(t[e]=r.value),t}},140:function(t,e,r){var n=r(111);t.exports=function(t,e){var r,o;if("string"===e&&"function"==typeof(r=t.toString)&&!n(o=r.call(t)))return o;if("function"==typeof(r=t.valueOf)&&!n(o=r.call(t)))return o;if("string"!==e&&"function"==typeof(r=t.toString)&&!n(o=r.call(t)))return o;throw TypeError("Can't convert object to primitive value")}},857:function(t,e,r){var n=r(854);t.exports=n},488:function(t){t.exports=function(t){if(null==t)throw TypeError("Can't call method on "+t);return t}},505:function(t,e,r){var n=r(854);t.exports=function(t,e){try{Object.defineProperty(n,t,{value:e,configurable:!0,writable:!0})}catch(r){n[t]=e}return e}},465:function(t,e,r){var n=r(854),o=r(505),i="__core-js_shared__",u=n[i]||o(i,{});t.exports=u},309:function(t,e,r){var n=r(913),o=r(465);(t.exports=function(t,e){return o[t]||(o[t]=void 0!==e?e:{})})("versions",[]).push({version:"3.16.2",mode:n?"pure":"global",copyright:"© 2021 Denis Pushkarev (zloirock.ru)"})},908:function(t,e,r){var n=r(488);t.exports=function(t){return Object(n(t))}},593:function(t,e,r){var n=r(111),o=r(190),i=r(140),u=r(112)("toPrimitive");t.exports=function(t,e){if(!n(t)||o(t))return t;var r,a=t[u];if(void 0!==a){if(void 0===e&&(e="default"),r=a.call(t,e),!n(r)||o(r))return r;throw TypeError("Can't convert object to primitive value")}return void 0===e&&(e="number"),i(t,e)}},948:function(t,e,r){var n=r(593),o=r(190);t.exports=function(t){var e=n(t,"string");return o(e)?e:String(e)}},711:function(t){var e=0,r=Math.random();t.exports=function(t){return"Symbol("+String(void 0===t?"":t)+")_"+(++e+r).toString(36)}},307:function(t,e,r){var n=r(133);t.exports=n&&!Symbol.sham&&"symbol"==typeof Symbol.iterator},61:function(t,e,r){var n=r(112);e.f=n},112:function(t,e,r){var n=r(854),o=r(309),i=r(656),u=r(711),a=r(133),c=r(307),f=o("wks"),l=n.Symbol,s=c?l:l&&l.withoutSetter||u;t.exports=function(t){return i(f,t)&&(a||"string"==typeof f[t])||(a&&i(l,t)?f[t]=l[t]:f[t]=s("Symbol."+t)),f[t]}}},e={};function r(n){var o=e[n];if(void 0!==o)return o.exports;var i=e[n]={exports:{}};return t[n](i,i.exports,r),i.exports}r.d=function(t,e){for(var n in e)r.o(e,n)&&!r.o(t,n)&&Object.defineProperty(t,n,{enumerable:!0,get:e[n]})},r.g=function(){if("object"==typeof globalThis)return globalThis;try{return this||new Function("return this")()}catch(t){if("object"==typeof window)return window}}(),r.o=function(t,e){return Object.prototype.hasOwnProperty.call(t,e)};var n={};return r(235)("iterator"),function(){"use strict";r.d(n,{default:function(){return D}});var t={"+":{binocular:!0,priority:14,exchangeable:!0,processor:function(t,e){return t+e}},"-":{binocular:!0,priority:14,exchangeable:!1,processor:function(t,e){return t-e}},"*":{binocular:!0,priority:15,exchangeable:!0,processor:function(t,e){return t*e}},"/":{binocular:!0,priority:15,exchangeable:!1,processor:function(t,e){return t/e}},"%":{binocular:!0,priority:15,exchangeable:!1,processor:function(t,e){return t%e}},"**":{binocular:!0,priority:16,exchangeable:!1,rightToLeft:!0,processor:function(t,e){return Math.pow(t,e)}},"&":{binocular:!0,priority:10,exchangeable:!0,bitwise:!0,processor:function(t,e){return t&e}},"|":{binocular:!0,priority:8,exchangeable:!0,bitwise:!0,processor:function(t,e){return t|e}},"^":{binocular:!0,priority:9,exchangeable:!0,bitwise:!0,processor:function(t,e){return t^e}},"~":{binocular:!1,priority:17,bitwise:!0,processor:function(t){return~t}},NEG:{match:!1,binocular:!1,priority:17,processor:function(t){return-t}}},e=["(",")"];for(var o in t)!1!==t[o].match&&e.push(o);e.sort((function(t,e){return e.length-t.length}));var i=e.map((function(t){return t.replace(/[.*+?^${}()|[\]\\]/g,"\\$&")})),u=new RegExp("^("+i.join("|")+")"),a=t;function c(t,e){(null==e||e>t.length)&&(e=t.length);for(var r=0,n=new Array(e);r<e;r++)n[r]=t[r];return n}function f(t,e){for(var r=0;r<e.length;r++){var n=e[r];n.enumerable=n.enumerable||!1,n.configurable=!0,"value"in n&&(n.writable=!0),Object.defineProperty(t,n.key,n)}}var l,s,p,y=function(t){function e(t){!function(t,e){if(!(t instanceof e))throw new TypeError("Cannot call a class as a function")}(this,e),this.__input=this.__inputOrig=t,this.__pos=0}var r,n;return r=e,(n=[{key:Symbol.iterator,value:function(){return this}},{key:"__proc",value:function(t){var r,n=function(t,e){var r="undefined"!=typeof Symbol&&t[Symbol.iterator]||t["@@iterator"];if(!r){if(Array.isArray(t)||(r=function(t,e){if(t){if("string"==typeof t)return c(t,e);var r=Object.prototype.toString.call(t).slice(8,-1);return"Object"===r&&t.constructor&&(r=t.constructor.name),"Map"===r||"Set"===r?Array.from(t):"Arguments"===r||/^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(r)?c(t,e):void 0}}(t))||e&&t&&"number"==typeof t.length){r&&(t=r);var n=0,o=function(){};return{s:o,n:function(){return n>=t.length?{done:!0}:{done:!1,value:t[n++]}},e:function(t){throw t},f:o}}throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}var i,u=!0,a=!1;return{s:function(){r=r.call(t)},n:function(){var t=r.next();return u=t.done,t},e:function(t){a=!0,i=t},f:function(){try{u||null==r.return||r.return()}finally{if(a)throw i}}}}(e.__lexTable);try{for(n.s();!(r=n.n()).done;){var o=r.value,i=o.rule.exec(t);if(i&&0==i.index)return{type:o.type,value:i[0]}}}catch(t){n.e(t)}finally{n.f()}return null}},{key:"getPos",value:function(){return this.__pos}},{key:"next",value:function(){for(;this.__input;){var t=this.__proc(this.__input);if(!t)throw new SyntaxError("Unexpected Character at position ".concat(this.__pos));var e=t.type,r=t.value;if(this.__input=this.__input.slice(r.length),this.__pos+=r.length,e)return{done:!1,value:{type:e,value:r}}}return this.__input=this.__inputOrig,this.__pos=0,{done:!0}}}])&&f(r.prototype,n),e}();function h(t){return(h="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(t){return typeof t}:function(t){return t&&"function"==typeof Symbol&&t.constructor===Symbol&&t!==Symbol.prototype?"symbol":typeof t})(t)}function v(t,e){var r=Object.keys(t);if(Object.getOwnPropertySymbols){var n=Object.getOwnPropertySymbols(t);e&&(n=n.filter((function(e){return Object.getOwnPropertyDescriptor(t,e).enumerable}))),r.push.apply(r,n)}return r}function b(t,e){for(var r=0;r<e.length;r++){var n=e[r];n.enumerable=n.enumerable||!1,n.configurable=!0,"value"in n&&(n.writable=!0),Object.defineProperty(t,n.key,n)}}function d(t,e,r){return(d="undefined"!=typeof Reflect&&Reflect.get?Reflect.get:function(t,e,r){var n=function(t,e){for(;!Object.prototype.hasOwnProperty.call(t,e)&&null!==(t=w(t)););return t}(t,e);if(n){var o=Object.getOwnPropertyDescriptor(n,e);return o.get?o.get.call(r):o.value}})(t,e,r||t)}function g(t,e){return(g=Object.setPrototypeOf||function(t,e){return t.__proto__=e,t})(t,e)}function _(t,e){if(e&&("object"===h(e)||"function"==typeof e))return e;if(void 0!==e)throw new TypeError("Derived constructors may only return object or undefined");return function(t){if(void 0===t)throw new ReferenceError("this hasn't been initialised - super() hasn't been called");return t}(t)}function w(t){return(w=Object.setPrototypeOf?Object.getPrototypeOf:function(t){return t.__proto__||Object.getPrototypeOf(t)})(t)}function m(t,e,r){return e in t?Object.defineProperty(t,e,{value:r,enumerable:!0,configurable:!0,writable:!0}):t[e]=r,t}p=[{rule:/^\s+/,type:null},{rule:/^0x[0-9A-Fa-f]+/,type:1},{rule:/^[0-9]+(\.[0-9]+)?([Ee][+\-][0-9]+)?/,type:1},{rule:u,type:3},{rule:/^[A-Za-z_\u0080-\uffff][A-Za-z0-9_\u0080-\uffff]*/,type:2}],(s="__lexTable")in(l=y)?Object.defineProperty(l,s,{value:p,enumerable:!0,configurable:!0,writable:!0}):l[s]=p;var O=function(t,e){!function(t,e){if("function"!=typeof e&&null!==e)throw new TypeError("Super expression must either be null or a function");t.prototype=Object.create(e&&e.prototype,{constructor:{value:t,writable:!0,configurable:!0}}),e&&g(t,e)}(f,t);var r,n,o,i,u,c=(i=f,u=function(){if("undefined"==typeof Reflect||!Reflect.construct)return!1;if(Reflect.construct.sham)return!1;if("function"==typeof Proxy)return!0;try{return Boolean.prototype.valueOf.call(Reflect.construct(Boolean,[],(function(){}))),!0}catch(t){return!1}}(),function(){var t,e=w(i);if(u){var r=w(this).constructor;t=Reflect.construct(e,arguments,r)}else t=e.apply(this,arguments);return _(this,t)});function f(t){var e;return function(t,e){if(!(t instanceof e))throw new TypeError("Cannot call a class as a function")}(this,f),(e=c.call(this,t)).__reset(),e}return r=f,o=[{key:"getTokenType",value:function(t){return t?1==t.type||2==t.type?"N":3==t.type?"("==t.value?"(":")"==t.value?")":a[t.value].binocular?"B":"S":null:"START"}}],(n=[{key:e,value:function(){return this}},{key:"__reset",value:function(){this.__lastToken=null,this.__lastTokenType="START",this.__bracketDepth=0}},{key:"next",value:function(){var t=f.syntaxTable;try{var e=d(w(f.prototype),"next",this).call(this),r=e.done,n=e.value,o=d(w(f.prototype),"getPos",this).call(this),i=null,u=null;if(r)u="END";else{if(o-=n.value.length,3==(i=function(t){for(var e=1;e<arguments.length;e++){var r=null!=arguments[e]?arguments[e]:{};e%2?v(Object(r),!0).forEach((function(e){m(t,e,r[e])})):Object.getOwnPropertyDescriptors?Object.defineProperties(t,Object.getOwnPropertyDescriptors(r)):v(Object(r)).forEach((function(e){Object.defineProperty(t,e,Object.getOwnPropertyDescriptor(r,e))}))}return t}({},n)).type&&"-"==i.value&&f.lastTokenNEGConv[this.__lastTokenType]){var a=this.__lastToken;if(a&&3==a.type&&("NEG"==a.value||"-"==a.value))throw new SyntaxError("Invalid input at position ".concat(o));i.value="NEG"}u=f.getTokenType(i)}if(!t[this.__lastTokenType]||!t[this.__lastTokenType][u])throw new SyntaxError("Invalid input at position ".concat(o));switch(u){case"(":this.__bracketDepth++;break;case")":this.__bracketDepth--;break;case"END":if(this.__bracketDepth)throw new SyntaxError("Bracket mismatch")}return"END"==u?(this.__reset(),{done:!0}):(this.__lastToken=i,this.__lastTokenType=u,{done:!1,value:i})}catch(t){try{this.__reset()}catch(t){console.error(t)}throw t}}}])&&b(r.prototype,n),o&&b(r,o),f}(y,Symbol.iterator);function j(t,e){var r=Object.keys(t);if(Object.getOwnPropertySymbols){var n=Object.getOwnPropertySymbols(t);e&&(n=n.filter((function(e){return Object.getOwnPropertyDescriptor(t,e).enumerable}))),r.push.apply(r,n)}return r}function x(t,e){var r="undefined"!=typeof Symbol&&t[Symbol.iterator]||t["@@iterator"];if(!r){if(Array.isArray(t)||(r=function(t,e){if(t){if("string"==typeof t)return S(t,e);var r=Object.prototype.toString.call(t).slice(8,-1);return"Object"===r&&t.constructor&&(r=t.constructor.name),"Map"===r||"Set"===r?Array.from(t):"Arguments"===r||/^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(r)?S(t,e):void 0}}(t))||e&&t&&"number"==typeof t.length){r&&(t=r);var n=0,o=function(){};return{s:o,n:function(){return n>=t.length?{done:!0}:{done:!1,value:t[n++]}},e:function(t){throw t},f:o}}throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}var i,u=!0,a=!1;return{s:function(){r=r.call(t)},n:function(){var t=r.next();return u=t.done,t},e:function(t){a=!0,i=t},f:function(){try{u||null==r.return||r.return()}finally{if(a)throw i}}}}function S(t,e){(null==e||e>t.length)&&(e=t.length);for(var r=0,n=new Array(e);r<e;r++)n[r]=t[r];return n}function T(t,e){for(var r=0;r<e.length;r++){var n=e[r];n.enumerable=n.enumerable||!1,n.configurable=!0,"value"in n&&(n.writable=!0),Object.defineProperty(t,n.key,n)}}function P(t,e,r){return e in t?Object.defineProperty(t,e,{value:r,enumerable:!0,configurable:!0,writable:!0}):t[e]=r,t}function E(t){return(E="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(t){return typeof t}:function(t){return t&&"function"==typeof Symbol&&t.constructor===Symbol&&t!==Symbol.prototype?"symbol":typeof t})(t)}function k(t,e){return(k=Object.setPrototypeOf||function(t,e){return t.__proto__=e,t})(t,e)}function N(t,e){if(e&&("object"===E(e)||"function"==typeof e))return e;if(void 0!==e)throw new TypeError("Derived constructors may only return object or undefined");return function(t){if(void 0===t)throw new ReferenceError("this hasn't been initialised - super() hasn't been called");return t}(t)}function R(t){return(R=Object.setPrototypeOf?Object.getPrototypeOf:function(t){return t.__proto__||Object.getPrototypeOf(t)})(t)}function A(t,e,r){return e in t?Object.defineProperty(t,e,{value:r,enumerable:!0,configurable:!0,writable:!0}):t[e]=r,t}m(O,"syntaxTable",{START:{N:!0,S:!0,"(":!0},N:{B:!0,")":!0,END:!0},S:{N:!0,"(":!0},B:{N:!0,"(":!0,S:!0},"(":{S:!0,N:!0,"(":!0},")":{")":!0,B:!0,END:!0}}),m(O,"lastTokenNEGConv",{START:!0,"(":!0,B:!0,S:!0});var D=function(t){!function(t,e){if("function"!=typeof e&&null!==e)throw new TypeError("Super expression must either be null or a function");t.prototype=Object.create(e&&e.prototype,{constructor:{value:t,writable:!0,configurable:!0}}),e&&k(t,e)}(o,t);var e,r,n=(e=o,r=function(){if("undefined"==typeof Reflect||!Reflect.construct)return!1;if(Reflect.construct.sham)return!1;if("function"==typeof Proxy)return!0;try{return Boolean.prototype.valueOf.call(Reflect.construct(Boolean,[],(function(){}))),!0}catch(t){return!1}}(),function(){var t,n=R(e);if(r){var o=R(this).constructor;t=Reflect.construct(n,arguments,o)}else t=n.apply(this,arguments);return N(this,t)});function o(t){return function(t,e){if(!(t instanceof e))throw new TypeError("Cannot call a class as a function")}(this,o),n.call(this,t)}return o}(function(){function t(e){!function(t,e){if(!(t instanceof e))throw new TypeError("Cannot call a class as a function")}(this,t),P(this,"__data",[]),e instanceof t&&this.setRPN(e.getRPN()),"string"==typeof e?this.compile(e):Array.isArray(e)&&e.length&&this.setRPN(e)}var e,r;return e=t,(r=[{key:"__numConv",value:function(t){if(isNaN(t))throw new TypeError("Invalid number format");return Number(t)}},{key:"compile",value:function(t){var e,r=[],n=[],o=x(new O(t));try{for(o.s();!(e=o.n()).done;){var i=e.value;if(1==i.type)i.value=this.__numConv(i.value),r.push(i);else if(2==i.type)r.push(i);else if(3==i.type)if(n.length&&"("!=i.value)if(")"==i.value)for(var u=null;(u=n.pop())&&"("!=u.value;)r.push(u);else{for(;n.length;){var c=n[n.length-1].value,f=a[c],l=a[i.value];if("("==c||f.priority<l.priority||f.rightToLeft&&l.rightToLeft)break;r.push(n.pop())}n.push(i)}else n.push(i)}}catch(t){o.e(t)}finally{o.f()}for(;n.length;)r.push(n.pop());return this.__data=r,this}},{key:"compress",value:function(){var t,e=[],r=x(this.__data);try{for(r.s();!(t=r.n()).done;){var n=t.value;if(3==n.type){var o=a[n.value];if(o.binocular&&e.length>1&&1==e[e.length-1].type&&1==e[e.length-2].type){var i=e.pop().value,u=e.pop().value;e.push({type:1,value:o.processor(u,i)})}else!o.binocular&&e.length>0&&1==e[e.length-1].type?e.push({type:1,value:o.processor(o.pop().value)}):e.push(n)}else e.push(n)}}catch(t){r.e(t)}finally{r.f()}return this.__data=e,this}},{key:"getRPN",value:function(){return this.__data.length?this.__data.map((function(t){return function(t){for(var e=1;e<arguments.length;e++){var r=null!=arguments[e]?arguments[e]:{};e%2?j(Object(r),!0).forEach((function(e){P(t,e,r[e])})):Object.getOwnPropertyDescriptors?Object.defineProperties(t,Object.getOwnPropertyDescriptors(r)):j(Object(r)).forEach((function(e){Object.defineProperty(t,e,Object.getOwnPropertyDescriptor(r,e))}))}return t}({},t)})):null}},{key:"toJSON",value:function(){return this.getRPN()}},{key:"setRPN",value:function(t){var e=this;if(!Array.isArray(t))throw new TypeError('Input must be an array with objects contains key "type" and "value"');if(!t.length)return this.__data=[],this;var r=[],n=0;if(t.forEach((function(t,o){var i,u;try{i=t.type,u=t.value}catch(t){throw new TypeError("Invalid RPN token at position ".concat(o))}if(2===i){if("string"!=typeof u||!u)throw new TypeError("Invalid RPN token at position ".concat(o));n++}else if(1===i){try{u=e.__numConv(u)}catch(t){throw new TypeError("Invalid number at position ".concat(o))}n++}else{if(3!==i)throw new TypeError("Invalid RPN token at position ".concat(o));var c=a[u];if(!c)throw new TypeError("Invalid operand at position ".concat(o));if(!c.binocular&&n<1||c.binocular&&n<2)throw new SyntaxError("Invalid RPN");c.binocular&&n--}r.push({type:i,value:u})})),1!=n)throw new SyntaxError("Invalid RPN");return this.__data=r,this}},{key:"calc",value:function(){var t=arguments.length>0&&void 0!==arguments[0]?arguments[0]:{};if(!this.__data.length)return null;var e,r=[],n=x(this.__data);try{for(n.s();!(e=n.n()).done;){var o=e.value;if(1!=o.type)if(2!=o.type){if(3==o.type){var i=a[o.value];if(i.binocular){var u=r.pop(),c=r.pop();r.push(i.processor(c,u))}else r.push(i.processor(r.pop()))}}else{if(!t.hasOwnProperty(o.value))throw new ReferenceError("Value of variable ".concat(o.value," not specified."));r.push(this.__numConv(t[o.value]))}else r.push(o.value)}}catch(t){n.e(t)}finally{n.f()}if(1!=r.length)throw new SyntaxError("Malformed RPN");return r[0]}}])&&T(e.prototype,r),t}());A(D,"TOKEN_NUM",1),A(D,"TOKEN_VAR",2),A(D,"TOKEN_OPER",3)}(),n.default}()}));