UNPKG

smartcal

Version:

SmartCal is a lightweight and powerful library for evaluating complex mathematical expressions dynamically. It supports tokenization, parsing, and interpreting mathematical formulas seamlessly, offering flexibility to integrate custom data objects for dyn

2 lines 17.6 kB
!function(e,r){"object"==typeof exports&&"object"==typeof module?module.exports=r():"function"==typeof define&&define.amd?define([],r):"object"==typeof exports?exports.smartcal=r():e.smartcal=r()}(this,(()=>(()=>{"use strict";var e={819:(e,r,t)=>{Object.defineProperty(r,"__esModule",{value:!0}),r.compile=function(e){return new o.CompiledFormulaExpression(e)};const o=t(439)},643:(e,r,t)=>{Object.defineProperty(r,"__esModule",{value:!0}),r.default=function(e,r){const t=new n.FormulaTokenizer,s=new i.FormulaParser;return(new o.FormulaInterpreter).execute(s.execute(t.execute(e)),r||{})};const o=t(258),i=t(400),n=t(464)},998:function(e,r,t){var o=this&&this.__createBinding||(Object.create?function(e,r,t,o){void 0===o&&(o=t);var i=Object.getOwnPropertyDescriptor(r,t);i&&!("get"in i?!r.__esModule:i.writable||i.configurable)||(i={enumerable:!0,get:function(){return r[t]}}),Object.defineProperty(e,o,i)}:function(e,r,t,o){void 0===o&&(o=t),e[o]=r[t]}),i=this&&this.__exportStar||function(e,r){for(var t in e)"default"===t||Object.prototype.hasOwnProperty.call(r,t)||o(r,e,t)},n=this&&this.__importDefault||function(e){return e&&e.__esModule?e:{default:e}};Object.defineProperty(r,"__esModule",{value:!0}),r.default=void 0;var s=t(643);Object.defineProperty(r,"default",{enumerable:!0,get:function(){return n(s).default}}),i(t(250),r),i(t(819),r)},250:(e,r,t)=>{Object.defineProperty(r,"__esModule",{value:!0}),r.isValidExpression=function(e){try{const r=new i.FormulaTokenizer,t=new o.FormulaParser,n=r.execute(e);return t.isValidFormula(n)}catch(e){return!1}};const o=t(400),i=t(464)},641:(e,r)=>{Object.defineProperty(r,"__esModule",{value:!0}),r.Priority_4_Operator=r.Priority_3_Operator=r.Priority_2_Operator=r.Priority_1_Operator=r.AllOperators=r.Operators=r.ArithmeticOperator=r.ComparisonOperator=r.BackslashOperator=r.QuestionMarkOperator=r.ColonOperator=r.ParenthesisCloseOperator=r.ParenthesisOpenOperator=r.AssignmentOperator=r.NotEqualOperator=r.EqualOperator=r.LessThanOrEqualOperator=r.GreaterThanOrEqualOperator=r.LessThanOperator=r.GreaterThanOperator=r.LogicalOrOperator=r.LogicalAndOperator=r.ModuloOperator=r.ExponentialOperator=r.MultiplicationOperator=r.DivisionOperator=r.SubtractionOperator=r.AdditionOperator=r.ConditionResult=r.REGEX=void 0,r.REGEX={formulaOperatorG:/(<=|\^|%|>=|==|\|\||&&|!=|[+/\-*=()<>?:])/g,formulaOperator:/(<=|>=|\^|%|==|\|\||&&|!=|[+/\-*=()<>?!:])/,formulaFieldName:/f_[\w]/},r.ConditionResult={True:1,False:0},r.AdditionOperator="+",r.SubtractionOperator="-",r.DivisionOperator="/",r.MultiplicationOperator="*",r.ExponentialOperator="^",r.ModuloOperator="%",r.LogicalAndOperator="&&",r.LogicalOrOperator="||",r.GreaterThanOperator=">",r.LessThanOperator="<",r.GreaterThanOrEqualOperator=">=",r.LessThanOrEqualOperator="<=",r.EqualOperator="==",r.NotEqualOperator="!=",r.AssignmentOperator="=",r.ParenthesisOpenOperator="(",r.ParenthesisCloseOperator=")",r.ColonOperator=":",r.QuestionMarkOperator="?",r.BackslashOperator="\\",r.ComparisonOperator=[r.GreaterThanOperator,r.LessThanOperator,r.LogicalOrOperator,r.LogicalAndOperator,r.GreaterThanOrEqualOperator,r.LessThanOrEqualOperator,r.EqualOperator,r.NotEqualOperator],r.ArithmeticOperator=[r.AdditionOperator,r.SubtractionOperator,r.DivisionOperator,r.MultiplicationOperator,r.ExponentialOperator,r.ModuloOperator],r.Operators=[...r.ArithmeticOperator,...r.ComparisonOperator,r.QuestionMarkOperator],r.AllOperators=[...r.Operators,r.ColonOperator,r.ParenthesisCloseOperator,r.ParenthesisOpenOperator],r.Priority_1_Operator=[r.AdditionOperator,r.SubtractionOperator],r.Priority_2_Operator=[r.DivisionOperator,r.MultiplicationOperator,r.ModuloOperator],r.Priority_3_Operator=[r.ExponentialOperator],r.Priority_4_Operator=[...r.ComparisonOperator,r.QuestionMarkOperator]},311:(e,r)=>{Object.defineProperty(r,"__esModule",{value:!0}),r.FormulaInterpreterError=void 0;r.FormulaInterpreterError=class{constructor(e,r){this.message=e,this.name="FormulaInterpreterError",this.stack=r}}},476:(e,r)=>{Object.defineProperty(r,"__esModule",{value:!0}),r.FormulaVariableNotFoundError=void 0;class t extends Error{constructor(e,r,t){super(e),this.name="FormulaVariableNotFound",this.data={variableName:r,container:t}}getData(){return this.data}}r.FormulaVariableNotFoundError=t},717:(e,r)=>{Object.defineProperty(r,"__esModule",{value:!0}),r.IncorrectSyntaxError=void 0;class t extends Error{constructor(e,r){super(e),this.name="IncorrectSyntaxError",this.data={exp:r}}getData(){return this.data}}r.IncorrectSyntaxError=t},590:(e,r)=>{Object.defineProperty(r,"__esModule",{value:!0}),r.InvalidFormulaError=void 0;class t extends Error{constructor(e,r){super(e),this.name="Invalid formula",this.data={exp:r}}getData(){return this.data}}r.InvalidFormulaError=t},201:function(e,r,t){var o=this&&this.__createBinding||(Object.create?function(e,r,t,o){void 0===o&&(o=t);var i=Object.getOwnPropertyDescriptor(r,t);i&&!("get"in i?!r.__esModule:i.writable||i.configurable)||(i={enumerable:!0,get:function(){return r[t]}}),Object.defineProperty(e,o,i)}:function(e,r,t,o){void 0===o&&(o=t),e[o]=r[t]}),i=this&&this.__exportStar||function(e,r){for(var t in e)"default"===t||Object.prototype.hasOwnProperty.call(r,t)||o(r,e,t)};Object.defineProperty(r,"__esModule",{value:!0}),i(t(311),r),i(t(476),r),i(t(717),r),i(t(590),r)},114:(e,r,t)=>{Object.defineProperty(r,"__esModule",{value:!0}),r.BinaryOperation=void 0;const o=t(780);class i extends o.Expression{constructor(e,r,t){super(),this.left=e,this.right=r,this.operator=t}execute(e){return this.operator(this.left.execute(e),this.right.execute(e))}}r.BinaryOperation=i},439:(e,r,t)=>{Object.defineProperty(r,"__esModule",{value:!0}),r.CompiledFormulaExpression=void 0;const o=t(258),i=t(400),n=t(464);r.CompiledFormulaExpression=class{constructor(e){this.expression=e,this.type="CompiledExpression",this._ast=(new i.FormulaParser).execute((new n.FormulaTokenizer).execute(e))}evaluate(e){return(new o.FormulaInterpreter).execute(this._ast,e)}toString(){return this.expression}}},790:(e,r,t)=>{Object.defineProperty(r,"__esModule",{value:!0}),r.ConditionalExpression=void 0;const o=t(780);class i extends o.Expression{constructor(e,r,t){super(),this.condition=e,this.isTrue=r,this.isFalse=t}execute(e){return 0!=this.condition.execute(e)?this.isTrue.execute(e):this.isFalse.execute(e)}}r.ConditionalExpression=i},780:(e,r)=>{Object.defineProperty(r,"__esModule",{value:!0}),r.Expression=void 0;r.Expression=class{}},70:(e,r,t)=>{Object.defineProperty(r,"__esModule",{value:!0}),r.ExpressionConstructor=void 0;const o=t(538),i=t(803),n=t(114),s=t(790);r.ExpressionConstructor=class{static literalValue(e){return new o.LiteralValue(e)}static fieldReference(e){return new i.FieldReference(e)}static addition(e,r){return new n.BinaryOperation(e,r,((e,r)=>e+r))}static subtraction(e,r){return new n.BinaryOperation(e,r,((e,r)=>e-r))}static multiplication(e,r){return new n.BinaryOperation(e,r,((e,r)=>e*r))}static division(e,r){return new n.BinaryOperation(e,r,((e,r)=>{if(0===r)throw new Error("Division by zero");return e/r}))}static condition(e,r,t){return new s.ConditionalExpression(e,r,t)}static equality(e,r){return new n.BinaryOperation(e,r,((e,r)=>Number(e===r)))}static superior(e,r){return new n.BinaryOperation(e,r,((e,r)=>Number(e>r)))}static inferior(e,r){return new n.BinaryOperation(e,r,((e,r)=>Number(e<r)))}static different(e,r){return new n.BinaryOperation(e,r,((e,r)=>Number(e!=r)))}static or(e,r){return new n.BinaryOperation(e,r,((e,r)=>Number(e||r)))}static and(e,r){return new n.BinaryOperation(e,r,((e,r)=>Number(e&&r)))}static pow(e,r){return new n.BinaryOperation(e,r,((e,r)=>Math.pow(Number(e),Number(r))))}static modulo(e,r){return new n.BinaryOperation(e,r,((e,r)=>e%r))}}},803:(e,r,t)=>{Object.defineProperty(r,"__esModule",{value:!0}),r.FieldReference=void 0;const o=t(780),i=t(641),n=t(400),s=t(464),a=t(258),u=t(476);class p extends o.Expression{constructor(e){super(),this.fieldName=e}execute(e){if(null!=e&&null!=e&&null!=e[this.fieldName])return this.isFormulaRef()&&"string"==typeof e[this.fieldName]?this.executeFormulaRef(e):this.isCompiledExpression(e)?this.evaluateCompiledExpression(e):e[this.fieldName];throw new u.FormulaVariableNotFoundError(`The fieldName ${this.fieldName} does not exist or is undefined on object ${e}`,this.fieldName,e)}isFormulaRef(){return i.REGEX.formulaFieldName.test(this.fieldName)}isCompiledExpression(e){var r;return"CompiledExpression"===(null===(r=e[this.fieldName])||void 0===r?void 0:r.type)}executeFormulaRef(e){const r=new s.FormulaTokenizer,t=new n.FormulaParser,o=new a.FormulaInterpreter,i=t.execute(r.execute(e[this.fieldName]));return o.execute(i,e)}evaluateCompiledExpression(e){return e[this.fieldName].evaluate(e)}}r.FieldReference=p},538:(e,r,t)=>{Object.defineProperty(r,"__esModule",{value:!0}),r.LiteralValue=void 0;const o=t(780);class i extends o.Expression{constructor(e){super(),this._value=e}execute(e){return this._value}}r.LiteralValue=i},73:function(e,r,t){var o=this&&this.__createBinding||(Object.create?function(e,r,t,o){void 0===o&&(o=t);var i=Object.getOwnPropertyDescriptor(r,t);i&&!("get"in i?!r.__esModule:i.writable||i.configurable)||(i={enumerable:!0,get:function(){return r[t]}}),Object.defineProperty(e,o,i)}:function(e,r,t,o){void 0===o&&(o=t),e[o]=r[t]}),i=this&&this.__exportStar||function(e,r){for(var t in e)"default"===t||Object.prototype.hasOwnProperty.call(r,t)||o(r,e,t)},n=this&&this.__importDefault||function(e){return e&&e.__esModule?e:{default:e}};Object.defineProperty(r,"__esModule",{value:!0}),r.ConditionResult=r.isValidExpression=r.compile=r.default=void 0;const s=t(641);Object.defineProperty(r,"ConditionResult",{enumerable:!0,get:function(){return s.ConditionResult}});var a=t(998);Object.defineProperty(r,"default",{enumerable:!0,get:function(){return n(a).default}}),Object.defineProperty(r,"compile",{enumerable:!0,get:function(){return a.compile}}),Object.defineProperty(r,"isValidExpression",{enumerable:!0,get:function(){return a.isValidExpression}}),i(t(235),r),i(t(201),r)},258:(e,r,t)=>{Object.defineProperty(r,"__esModule",{value:!0}),r.FormulaInterpreter=void 0;const o=t(70),i=t(641),n=t(311);r.FormulaInterpreter=class{execute(e,r){return this.interpret(e,r).execute(r)}interpret(e,r){try{if(e.isNode()){const t=e.operator,n=this.interpret(e.right,r),s=this.interpret(e.left,r);switch(t){case i.AdditionOperator:return o.ExpressionConstructor.addition(s,n);case i.SubtractionOperator:return o.ExpressionConstructor.subtraction(s,n);case i.MultiplicationOperator:return o.ExpressionConstructor.multiplication(s,n);case i.DivisionOperator:return o.ExpressionConstructor.division(s,n);case i.ModuloOperator:return o.ExpressionConstructor.modulo(s,n);case i.ExponentialOperator:return o.ExpressionConstructor.pow(s,n);default:throw new Error(`This operator ${t} is not supported.`)}}else{if(e.isValue()){const r=e.value;if("number"==typeof r)return o.ExpressionConstructor.literalValue(Number(e.value));{const e=/["']([\w]+)["']/,t=r.match(e)[1];return o.ExpressionConstructor.literalValue(t)}}if(e.isField()){const t=r[String(e.fieldName)];if(void 0===t)throw new n.FormulaInterpreterError(`The variable ${e.fieldName} not defined.`);return o.ExpressionConstructor.fieldReference(e.fieldName)}if(!e.isComparison()){if(e.isConditional()){const t=this.interpret(e.condition,r),i=this.interpret(e.isTrue,r),n=this.interpret(e.isFalse,r);return o.ExpressionConstructor.condition(t,i,n)}throw new n.FormulaInterpreterError(`This Expression is not Correct. Please verify Your expression [Interpreter]:${e}`)}{const t=e.operator,s=this.interpret(e.left,r),a=this.interpret(e.right,r);switch(t){case i.GreaterThanOperator:return o.ExpressionConstructor.superior(s,a);case i.LessThanOperator:return o.ExpressionConstructor.inferior(s,a);case i.EqualOperator:return o.ExpressionConstructor.equality(s,a);case i.GreaterThanOrEqualOperator:return o.ExpressionConstructor.or(o.ExpressionConstructor.superior(s,a),o.ExpressionConstructor.equality(s,a));case i.LessThanOrEqualOperator:return o.ExpressionConstructor.or(o.ExpressionConstructor.inferior(s,a),o.ExpressionConstructor.equality(s,a));case i.LogicalOrOperator:return o.ExpressionConstructor.or(s,a);case i.LogicalAndOperator:return o.ExpressionConstructor.and(s,a);case i.NotEqualOperator:return o.ExpressionConstructor.different(s,a);default:throw new n.FormulaInterpreterError(`This comparison ${t} method is not supported`)}}}}catch(e){throw e}}}},909:(e,r,t)=>{Object.defineProperty(r,"__esModule",{value:!0}),r.AstNode=void 0;const o=t(641);r.AstNode=class{isConditional(){return!!this.condition&&!!this.isFalse&&!!this.isTrue}isValue(){return null!=this.value}isComparison(){return!!this.isComparisonOperator()}isField(){return!!this.fieldName}isNode(){return!(this.isValue()||this.isField()||this.isComparison()||this.isConditional())}isComparisonOperator(){return!!o.ComparisonOperator.includes(this.operator)}}},400:(e,r,t)=>{Object.defineProperty(r,"__esModule",{value:!0}),r.FormulaParser=void 0;const o=t(641),i=t(201),n=t(909);r.FormulaParser=class{isFormula(e){let r=1,t=1;const i=o.REGEX.formulaOperator;for(let o=0;o<e.length;o++){const n=e[o],s=o-1;if(i.test(String(n)))t=o;else{if(r==s&&t!=s)return!1;r=o}}return!0}checkSyntax(e){this.checkParenthesisSyntax(e),this.checkOperatorSyntax(e),this.checkTernaryConditionSyntax(e)}checkParenthesisSyntax(e){const r=[];if(e.forEach((t=>{if(t===o.ParenthesisOpenOperator&&r.push(o.ParenthesisOpenOperator),t===o.ParenthesisCloseOperator){if(0===r.length)throw new i.IncorrectSyntaxError("Parenthesis mismatch",e.join(""));r.pop()}})),0!==r.length)throw new i.IncorrectSyntaxError("Incorrect parenthesis disposition.",e.join(""))}checkOperatorSyntax(e){const r=e.map((e=>"string"==typeof e?e:Math.abs(e))).join("");if(/[+-\/*]{2,}/.test(r))throw new i.IncorrectSyntaxError("Incorrect Operator error",r);if(1==e.length&&/\w/.test(r))return;if(!/>=|<=|==|!=|&&|\|\||[+-\/*<>%\^][\w\(]/.test(r))throw new i.IncorrectSyntaxError("Incorrect Operator position for Operand",r)}checkTernaryConditionSyntax(e){let r=0,t=0;if(e.forEach((e=>{e===o.QuestionMarkOperator&&r++,e===o.ColonOperator&&t++})),r!==t)throw new i.IncorrectSyntaxError("Incorrect Ternary syntax: unmatched ? and :",e.join(""));const n=/[?:]/;let s=!0;e.forEach((r=>{if(n.test(String(r))){if(s&&r===o.ColonOperator)throw new i.IncorrectSyntaxError("Ternary syntax error: found ':' before '?'",e.join(""));s=!s}}))}isValidFormula(e){try{return!!this.isFormula(e)&&(this.checkSyntax(e),!0)}catch(e){return!1}}execute(e){if(0!=e.length&&this.isFormula(e))return this.checkSyntax(e),this.parser(e);throw new i.InvalidFormulaError("Invalid formula error. Please check is formula. ",e.join(""))}parser(e){const r=this.infixToPostFix(e);return this.generateAST(r)}generateAST(e){return this._generateAST(e,0,[])}_generateAST(e,r,t=[]){const o=e[r];if(null==o)return t[0];if(this.isOperatorFirstAndParenthesis(o)){const e=new n.AstNode;e.operator=o,this.isArithmeticOperator(o)||this.isComparisonOperator(o)?(e.right=t.pop(),e.left=t.pop()):this.isTernaryOperator(o)&&(e.isFalse=t.pop(),e.isTrue=t.pop(),e.condition=t.pop()),t.push(e)}else{const e=new n.AstNode;this.isValue(o)?e.value=o:e.fieldName=o,t.push(e)}return this._generateAST(e,r+1,t)}infixToPostFix(e){const r=[],t=[];for(e.forEach((e=>{if(this.isOperatorFirstAndParenthesis(e)){const i=String(e),n=this.priority(i);if(i===o.ParenthesisOpenOperator)t.push(i);else if(i===o.ParenthesisCloseOperator){for(;t.length>0&&t[t.length-1]!==o.ParenthesisOpenOperator;){const e=t.pop();e.trim()!==o.ParenthesisOpenOperator&&r.push(e)}t.pop()}else if(i===o.ColonOperator)for(;t.length>0&&t[t.length-1]!==o.QuestionMarkOperator;)r.push(t.pop());else if(o.Operators.includes(i)){for(;t.length>0&&this.priority(t[t.length-1])>=n;)r.push(t.pop());t.push(i)}}else r.push(e)}));t.length>0;)r.push(t.pop());return r}priority(e){return o.Priority_1_Operator.includes(e)?1:o.Priority_2_Operator.includes(e)?2:o.Priority_3_Operator.includes(e)?3:o.Priority_4_Operator.includes(e)?4:0}isOperatorFirstAndParenthesis(e){return!!o.AllOperators.includes(String(e).trim())}isArithmeticOperator(e){return!!o.ArithmeticOperator.includes(e)}isComparisonOperator(e){return!!o.ComparisonOperator.includes(e)}isTernaryOperator(e){return o.QuestionMarkOperator===e}isValue(e){return!("number"!=typeof e&&!/["'][\w]+["']/.test(e))}}},464:(e,r,t)=>{Object.defineProperty(r,"__esModule",{value:!0}),r.FormulaTokenizer=void 0;const o=t(641);r.FormulaTokenizer=class{formatInput(e){return e.replace(o.REGEX.formulaOperatorG," $1 ").replace(/\s+/g," ").trim()}filterTokens(e){const r=[];let t=!1;return e.forEach((e=>{const i=r[r.length-1];if(/^\d+(\.\d+)?$/.test(e)){const i=r.pop(),n=r.pop();if(void 0!==i){const s=o.Priority_1_Operator.includes(i),a=void 0!==n&&"string"==typeof n&&o.Operators.includes(n),u=n===o.ParenthesisOpenOperator,p=void 0===n;s&&u?(r.push(Number(i+e)),t=!0):s&&(a||p)?(void 0!==n&&r.push(n),r.push(Number(i+e))):(void 0!==n&&r.push(n),r.push(i,Number(e)))}else r.push(Number(e))}else/-\d+/.test(i)&&e===o.ParenthesisCloseOperator&&t?t=!1:r.push(e)})),r}execute(e){const r=this.formatInput(e);if(""===r.trim())return[];const t=r.split(" ");return this.filterTokens(t)}}},235:(e,r,t)=>{Object.defineProperty(r,"__esModule",{value:!0});const o=t(641);o.ArithmeticOperator,o.ComparisonOperator}},r={};var t=function t(o){var i=r[o];if(void 0!==i)return i.exports;var n=r[o]={exports:{}};return e[o].call(n.exports,n,n.exports,t),n.exports}(73);return t})())); //# sourceMappingURL=index.js.map