UNPKG

expression-calculator

Version:

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

1 lines 8.2 kB
<!doctype html><html><head><meta charset="UTF-8"/><meta name="viewport" id="viewport" content="width=device-width,initial-scale=1,minimum-scale=1,maximum-scale=1,user-scalable=no"/><title>Expression Calculator Test Page</title><style>body{color:#303133;font-family:微软雅黑,sans;margin:0 auto;padding:50px 10px 10px;user-select:none;-ms-user-select:none}:-ms-input-placeholder{color:#c0c4cc!important;font-family:微软雅黑,sans!important;font-size:16px!important}::-moz-placeholder{color:#c0c4cc;font-family:微软雅黑,sans;font-size:16px}::-webkit-input-placeholder{color:#c0c4cc;font-family:微软雅黑,sans;font-size:16px}ribbon{user-drag:none;background-color:#a00;box-shadow:0 0 5px 2px #888;position:fixed;right:0;top:0;user-select:none}ribbon,ribbon a{display:block;overflow:hidden}ribbon a{border-bottom:1px solid #fff;border-top:1px solid #fff;color:#fff;font-size:12px;font-weight:700;line-height:30px;margin:2px 0;text-align:center;text-decoration:none;width:260px}@media only screen and (min-width:600px){body{padding-top:10px}ribbon{transform:translate(83px) rotate(45deg);transform-origin:50px 50%}.inputForm{margin-right:170px}}.inputForm{margin-bottom:10px}.inputForm input,.inputForm textarea{-webkit-appearance:none;background-color:#fff;background-image:none;border:1px solid #dcdfe6;border-radius:4px;box-sizing:border-box;color:#333;display:block;font-size:16px;margin:0 10px 10px 0;outline:none;padding:0 8px;width:100%}.inputForm input{height:30px;line-height:28px}.inputForm input ::-ms-clear{display:none;height:0;width:0}.inputForm textarea{height:130px;line-height:20px;overflow-x:hidden;overflow-y:auto;padding:4px 8px;resize:vertical}.inputForm button{background-color:#409eff;border:none;border-radius:15px;color:#fff;cursor:pointer;font-size:16px;height:30px;line-height:30px;margin:0 10px 5px 0;min-width:60px;outline:none;padding:0 14px}.inputForm button:hover{background-color:#66b1ff}.inputForm button:disabled{background-color:#a0cfff;cursor:not-allowed}.inputForm button:disabled:hover{background-color:#a0cfff}.calcResult{display:inline-block;line-height:20px;padding:5px 0;vertical-align:top}.calcResult.error{color:#f56c6c}</style><script src="./dist/exprcalc.js"></script></head><body><ribbon><a href="https://github.com/frank-deng/linefold/">Fork me on GitHub</a></ribbon><form class="inputForm" method="post" action="#" id="inputForm"><input placeholder="Input expression" name="exprInput"/> <textarea placeholder="Input variables" name="varInput"></textarea> <button disabled="disabled" type="submit" translate="Calculate">Calculate</button><div class="calcResult" id="calcResult"></div></form></body><script>!function(){"use strict";function t(t,r){var n="undefined"!=typeof Symbol&&t[Symbol.iterator]||t["@@iterator"];if(!n){if(Array.isArray(t)||(n=function(t,r){if(t){if("string"==typeof t)return e(t,r);var n=Object.prototype.toString.call(t).slice(8,-1);return"Object"===n&&t.constructor&&(n=t.constructor.name),"Map"===n||"Set"===n?Array.from(t):"Arguments"===n||/^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)?e(t,r):void 0}}(t))||r&&t&&"number"==typeof t.length){n&&(t=n);var a=0,o=function(){};return{s:o,n:function(){return a>=t.length?{done:!0}:{done:!1,value:t[a++]}},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 l,i=!0,u=!1;return{s:function(){n=n.call(t)},n:function(){var t=n.next();return i=t.done,t},e:function(t){u=!0,l=t},f:function(){try{i||null==n.return||n.return()}finally{if(u)throw l}}}}function e(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 r(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 n(t,e,r){return e in t?Object.defineProperty(t,e,{value:r,enumerable:!0,configurable:!0,writable:!0}):t[e]=r,t}var a=function(){function e(t){!function(t,e){if(!(t instanceof e))throw new TypeError("Cannot call a class as a function")}(this,e),n(this,"__locale",null),n(this,"__fallback",null),this.__data=t,this.__locale=this.__fallback=Object.keys(this.__data)[0],this.setLocale()}var a,o;return a=e,(o=[{key:"setLocale",value:function(){var e=arguments.length>0&&void 0!==arguments[0]?arguments[0]:null;Array.isArray(e)||(e=window.navigator.languages||[window.navigator.language]);var r,n=t(e);try{for(n.s();!(r=n.n()).done;){var a=r.value;for(var o in this.__data)if(a.toLowerCase()==o.toLowerCase())return this.__locale=o,!0;for(var l in this.__data)if(0==a.toLowerCase().indexOf(l.toLowerCase()))return this.__locale=l,!0}}catch(t){n.e(t)}finally{n.f()}return!1}},{key:"get",value:function(t){try{var e=this.__data[this.__locale];if(e&&e[t])return e[t];if((e=this.__data[this.__fallback])[t])return e[t]}catch(t){console.error(t)}return t}}])&&r(a.prototype,o),e}();function o(t,e){return function(t){if(Array.isArray(t))return t}(t)||function(t,e){var r=null==t?null:"undefined"!=typeof Symbol&&t[Symbol.iterator]||t["@@iterator"];if(null!=r){var n,a,o=[],l=!0,i=!1;try{for(r=r.call(t);!(l=(n=r.next()).done)&&(o.push(n.value),!e||o.length!==e);l=!0);}catch(t){i=!0,a=t}finally{try{l||null==r.return||r.return()}finally{if(i)throw a}}return o}}(t,e)||l(t,e)||function(){throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}()}function l(t,e){if(t){if("string"==typeof t)return i(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)?i(t,e):void 0}}function i(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}var u=null;try{u=new a({en:{title:"Expression Calculator Test Page","Input expression":"Input expression","Input variables":'Input variables here, format: "varName=value"',Calculate:"Calculate",calc_ans:"Calculation Answer: "},zh:{title:"表达式计算器测试页面","Input expression":"请输入表达式","Input variables":"每行指定一个变量的值,格式:“变量名=变量值”",Calculate:"计算",calc_ans:"计算结果:"}}),document.title=u.get("title");for(var c=document.querySelectorAll("[placeholder]"),s=0;s<c.length;s++){var f=c[s];f.setAttribute("placeholder",u.get(f.getAttribute("placeholder")))}for(var v=document.querySelectorAll("[translate]"),y=0;y<v.length;y++){var d=v[y];d.innerHTML=u.get(d.getAttribute("translate"))}}catch(t){console.error("Failed to load translation",t)}var h=document.getElementById("inputForm"),b=h.querySelector('[name="exprInput"]'),p=h.querySelector('[type="submit"]'),m=document.getElementById("calcResult");function g(t){t instanceof Error?(m.classList.add("error"),m.innerHTML=t.message):(m.classList.remove("error"),m.innerHTML=u.get("calc_ans")+t)}function _(){var t=h[0].value;p.disabled=!t}b.addEventListener("input",_),b.addEventListener("change",_),b.addEventListener("blur",_),h.addEventListener("submit",(function(t){t.preventDefault();try{var e=new Calc(t.target[0].value),r=function(t){if(!t)return{};var e,r={},n=function(t,e){var r="undefined"!=typeof Symbol&&t[Symbol.iterator]||t["@@iterator"];if(!r){if(Array.isArray(t)||(r=l(t))){r&&(t=r);var n=0,a=function(){};return{s:a,n:function(){return n>=t.length?{done:!0}:{done:!1,value:t[n++]}},e:function(t){throw t},f:a}}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 o,i=!0,u=!1;return{s:function(){r=r.call(t)},n:function(){var t=r.next();return i=t.done,t},e:function(t){u=!0,o=t},f:function(){try{i||null==r.return||r.return()}finally{if(u)throw o}}}}(t.split("\n"));try{for(n.s();!(e=n.n()).done;){var a=e.value;if(a&&-1!=a.indexOf("=")){var i=o(a.split("="),2),u=i[0],c=i[1];r[u]=c}}}catch(t){n.e(t)}finally{n.f()}return r}(t.target[1].value);return g(e.calc(r)),!1}catch(t){console.error(t),g(t)}return!1}))}();</script></html>