@fw-components/formula-editor 2.0.7-formula-editor.36 → 2.0.7-formula-editor.37
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
|
@@ -37,7 +37,7 @@ let FormulaEditor = class FormulaEditor extends LitElement {
|
|
|
37
37
|
this._adjustTextAreaHeight();
|
|
38
38
|
}
|
|
39
39
|
if (_changedProperties.has("variables")) {
|
|
40
|
-
this._parser = new Parser(this.variables, this.formulaRegex, this.allowedNumbers, this.allowedOperators, this.minSuggestionLen);
|
|
40
|
+
this._parser = new Parser(this.variables, this.formulaRegex, this.allowedNumbers, this.allowedOperators, this.variableType, this.minSuggestionLen);
|
|
41
41
|
this.recommendations = Array.from(this.variables.keys());
|
|
42
42
|
}
|
|
43
43
|
}
|
|
@@ -202,6 +202,9 @@ __decorate([
|
|
|
202
202
|
__decorate([
|
|
203
203
|
property()
|
|
204
204
|
], FormulaEditor.prototype, "allowedOperators", void 0);
|
|
205
|
+
__decorate([
|
|
206
|
+
property()
|
|
207
|
+
], FormulaEditor.prototype, "variableType", void 0);
|
|
205
208
|
__decorate([
|
|
206
209
|
query("#wysiwyg-editor")
|
|
207
210
|
], FormulaEditor.prototype, "editor", void 0);
|
|
@@ -6,12 +6,13 @@ import { Expectation } from "../types";
|
|
|
6
6
|
import { operatorPrecedence, unaryOperators } from "./constants.js";
|
|
7
7
|
import { getFormulaTokens } from "./get-formula-tokens.js";
|
|
8
8
|
export class Parser {
|
|
9
|
-
constructor(variables, formulaRegex, allowedNumbers, allowedOperators, minSuggestionLen) {
|
|
9
|
+
constructor(variables, formulaRegex, allowedNumbers, allowedOperators, variableType, minSuggestionLen) {
|
|
10
10
|
this.variables = variables;
|
|
11
11
|
this.formulaRegex = formulaRegex;
|
|
12
12
|
this._recommender = new Recommender(Array.from(this.variables.keys()), minSuggestionLen);
|
|
13
13
|
this.allowedNumbers = allowedNumbers;
|
|
14
14
|
this.allowedOperators = allowedOperators;
|
|
15
|
+
this.variableType = variableType;
|
|
15
16
|
}
|
|
16
17
|
isNumber(value) {
|
|
17
18
|
if (!this.allowedNumbers || value.trim() === "")
|
|
@@ -74,14 +75,21 @@ export class Parser {
|
|
|
74
75
|
/**
|
|
75
76
|
* Error checks
|
|
76
77
|
* skip error check if there is one already
|
|
77
|
-
|
|
78
|
+
*/
|
|
78
79
|
if (expectation != Expectation.UNDEFINED) {
|
|
79
|
-
|
|
80
|
-
|
|
80
|
+
/**
|
|
81
|
+
* Unknown symbol/variable/word
|
|
82
|
+
*/
|
|
83
|
+
if (!(isNumber || isOperator || isBracket || isSpace)) {
|
|
84
|
+
parseOutput.errorString = `${this.variableType} : ${token} does not exist`;
|
|
85
|
+
expectation = Expectation.UNDEFINED;
|
|
86
|
+
}
|
|
87
|
+
else if (this.allowedOperators.has(previousToken) && isOperator) {
|
|
88
|
+
parseOutput.errorString = `Use ${this.variableType}${this.allowedNumbers ? " or numbers" : ""} after ${previousToken}. Pls do not use consecutive two mathametical operators (+,-,*,/,^)`;
|
|
81
89
|
expectation = Expectation.UNDEFINED;
|
|
82
90
|
}
|
|
83
91
|
else if (parentheses.isEmpty() && token === ")") {
|
|
84
|
-
parseOutput.errorString =
|
|
92
|
+
parseOutput.errorString = "Unexpected closing bracket. Make sure all opening brackets '(' have matching closing brackets ')'.";
|
|
85
93
|
expectation = Expectation.UNDEFINED;
|
|
86
94
|
}
|
|
87
95
|
/**
|
|
@@ -90,35 +98,28 @@ export class Parser {
|
|
|
90
98
|
*/
|
|
91
99
|
else if (expectation === Expectation.VARIABLE && !isNumber && !isSpace && token != "("
|
|
92
100
|
&& !((unaryOperators.includes(token)) && (!parsedString.trim() || previousToken === "(" || this.allowedOperators.has(previousToken)))) {
|
|
93
|
-
parseOutput.errorString = `
|
|
101
|
+
parseOutput.errorString = `Use ${this.variableType}${this.allowedNumbers ? " or numbers" : ""} after ${previousToken}.`;
|
|
94
102
|
expectation = Expectation.UNDEFINED;
|
|
95
103
|
}
|
|
96
104
|
/**
|
|
97
105
|
* Multiple number/variable together without operator
|
|
98
106
|
*/
|
|
99
107
|
else if (expectation === Expectation.OPERATOR && !isOperator && !isSpace && token != ")") {
|
|
100
|
-
parseOutput.errorString = `
|
|
101
|
-
expectation = Expectation.UNDEFINED;
|
|
102
|
-
}
|
|
103
|
-
/**
|
|
104
|
-
* Unknown symbol/variable/word
|
|
105
|
-
*/
|
|
106
|
-
else if (!(isNumber || isOperator || isBracket || isSpace)) {
|
|
107
|
-
parseOutput.errorString = `Unknown word at position ${currentPosition}`;
|
|
108
|
+
parseOutput.errorString = `Use mathametical operators (${Array.from(this.allowedOperators).join(",")}) after ${previousToken}.`;
|
|
108
109
|
expectation = Expectation.UNDEFINED;
|
|
109
110
|
}
|
|
110
111
|
/**
|
|
111
112
|
* division by zero
|
|
112
113
|
*/
|
|
113
114
|
else if (isNumber && previousToken === "/" && (this.variables.get(token) === 0 || Number(token) === 0)) {
|
|
114
|
-
parseOutput.errorString = `Division by zero
|
|
115
|
+
parseOutput.errorString = `Division by zero is not possible`;
|
|
115
116
|
expectation = Expectation.UNDEFINED;
|
|
116
117
|
}
|
|
117
118
|
/**
|
|
118
119
|
* Empty brackets
|
|
119
120
|
*/
|
|
120
121
|
else if (previousToken === "(" && token === ")") {
|
|
121
|
-
parseOutput.errorString = `
|
|
122
|
+
parseOutput.errorString = `Pls do not use empty brackets ().`;
|
|
122
123
|
expectation = Expectation.UNDEFINED;
|
|
123
124
|
}
|
|
124
125
|
}
|
|
@@ -151,10 +152,10 @@ export class Parser {
|
|
|
151
152
|
parseOutput.recommendations = !parseOutput.errorString?.length ? Array.from(this.variables.keys()) : [];
|
|
152
153
|
}
|
|
153
154
|
if (this.allowedOperators.has(previousToken)) {
|
|
154
|
-
parseOutput.errorString = `
|
|
155
|
+
parseOutput.errorString = `Pls do not use mathametical operators (${Array.from(this.allowedOperators).join(",")}) at the end.`;
|
|
155
156
|
}
|
|
156
157
|
if (!parentheses.isEmpty()) {
|
|
157
|
-
parseOutput.errorString =
|
|
158
|
+
parseOutput.errorString = "Unexpected opening bracket. Make sure all closing brackets ')' have matching opening brackets '('.";
|
|
158
159
|
}
|
|
159
160
|
return parseOutput;
|
|
160
161
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@fw-components/formula-editor",
|
|
3
|
-
"version": "2.0.7-formula-editor.
|
|
3
|
+
"version": "2.0.7-formula-editor.37",
|
|
4
4
|
"description": "A WYSIWYG type formula editor",
|
|
5
5
|
"main": "dist/formula-editor/src/formula-editor.js",
|
|
6
6
|
"exports": {
|
|
@@ -31,5 +31,5 @@
|
|
|
31
31
|
"@types/big.js": "^6.1.6",
|
|
32
32
|
"es-dev-server": "^2.1.0"
|
|
33
33
|
},
|
|
34
|
-
"gitHead": "
|
|
34
|
+
"gitHead": "a9c51fbd40797afbd0fc3b65ee780656edb1114f"
|
|
35
35
|
}
|