@fw-components/formula-editor 2.4.0 → 2.4.1-handle-unary-prefixes.1

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.
@@ -19,6 +19,20 @@ export class Parser {
19
19
  return false;
20
20
  return !Number.isNaN(Number(value));
21
21
  }
22
+ /**
23
+ * Checks whether a token is a valid operand: a known variable, a number, or a unary-prefixed variable
24
+ */
25
+ isOperand(token) {
26
+ if (!token.trim().length)
27
+ return false;
28
+ if (!Number.isNaN(Number(token)) || this.variables.has(token))
29
+ return true;
30
+ if (token.length > 1 && unaryOperators.includes(token[0])) {
31
+ const inner = token.substring(1);
32
+ return !Number.isNaN(Number(inner)) || this.variables.has(inner);
33
+ }
34
+ return false;
35
+ }
22
36
  formatFormulaToken(token) {
23
37
  for (const existingKey of this.variables.keys()) {
24
38
  if (existingKey.toLowerCase() === token.toLowerCase()) {
@@ -215,7 +229,7 @@ export class Parser {
215
229
  }
216
230
  operatorStack.push(token);
217
231
  }
218
- else if ((!Number.isNaN(Number(token)) || this.variables.has(token)) && token.trim().length) {
232
+ else if (this.isOperand(token)) {
219
233
  outputQueue.enqueue(token);
220
234
  }
221
235
  }
@@ -238,9 +252,7 @@ export class Parser {
238
252
  let parsedLeftExpression;
239
253
  let parsedRightExpression;
240
254
  // check if the symbol is a number or variable or unaryOperatorPreceded Variable
241
- if ((unaryOperators.includes(symbol[0]) && this.variables.has(symbol.substring(1))) ||
242
- this.variables.has(symbol) ||
243
- (!Number.isNaN(parseFloat(symbol)) && Number.isFinite(parseFloat(symbol)))) {
255
+ if (this.isOperand(symbol)) {
244
256
  resultStack.push(symbol);
245
257
  operatorStack.push(null);
246
258
  }
@@ -280,8 +292,8 @@ export class Parser {
280
292
  const calcStack = new Stack();
281
293
  while (!formulaRPN.isEmpty()) {
282
294
  const frontItem = formulaRPN.dequeue();
283
- if (!this.allowedOperators.has(frontItem)) {
284
- const [sign, variableKey] = /^[+-]/.test(frontItem) ? [frontItem[0], frontItem.slice(1)] : ["", frontItem];
295
+ if (this.isOperand(frontItem)) {
296
+ const [sign, variableKey] = /^[+-]/.test(frontItem) && frontItem.length > 1 ? [frontItem[0], frontItem.slice(1)] : ["", frontItem];
285
297
  const operandValue = Number.parseFloat(this.variables.get(variableKey)?.toString() ?? variableKey);
286
298
  const number = Number.parseFloat(`${sign}1`) * operandValue;
287
299
  calcStack.push(Big(number));
package/package.json CHANGED
@@ -1,13 +1,11 @@
1
1
  {
2
2
  "name": "@fw-components/formula-editor",
3
- "version": "2.4.0",
3
+ "version": "2.4.1-handle-unary-prefixes.1",
4
4
  "description": "A WYSIWYG type formula editor",
5
- "main": "dist/formula-editor/src/formula-editor.js",
5
+ "main": "dist/formula-editor.js",
6
6
  "exports": {
7
- ".": "./dist/formula-editor/src/formula-editor.js",
8
- "./types": "./dist/formula-editor/src/types",
9
- "./types/*.js": "./dist/formula-editor/src/types/*.js",
10
- "./utils/*.js": "./dist/formula-editor/src/utils/*.js"
7
+ ".": "./dist/formula-editor.js",
8
+ "./utils/*.js": "./dist/utils/*.js"
11
9
  },
12
10
  "publishConfig": {
13
11
  "access": "public"
@@ -15,6 +13,8 @@
15
13
  "scripts": {
16
14
  "build": "tsc --composite false",
17
15
  "dev": "tsc -w --composite false",
16
+ "test": "vitest run",
17
+ "test:watch": "vitest",
18
18
  "prepublishOnly": "npm run build"
19
19
  },
20
20
  "dependencies": {
@@ -32,7 +32,8 @@
32
32
  "devDependencies": {
33
33
  "@types/big.js": "6.2.2",
34
34
  "es-dev-server": "2.1.0",
35
- "typescript": "5.9.3"
35
+ "typescript": "5.9.3",
36
+ "vitest": "2.1.9"
36
37
  },
37
- "gitHead": "1aabc46174564e7c9f3046218744244a77568b54"
38
+ "gitHead": "59988abdbc80a4f8de513dbbd2e558a2d447bfe6"
38
39
  }