|
2 | 2 | * Represents a transformation expression. |
3 | 3 | * @param {string} expressionStr - An expression in string format. |
4 | 4 | * @class Expression |
5 | | - * |
| 5 | + * Normally this class is not instantiated directly |
6 | 6 | */ |
7 | 7 | class Expression { |
8 | 8 | constructor(expressionStr) { |
@@ -31,22 +31,22 @@ class Expression { |
31 | 31 | * @return {string} the normalized form of the value expression, e.g. "w_gt_100" |
32 | 32 | */ |
33 | 33 | static normalize(expression) { |
34 | | - var operators, operatorsPattern, operatorsReplaceRE, predefinedVarsPattern, predefinedVarsReplaceRE; |
35 | 34 | if (expression == null) { |
36 | 35 | return expression; |
37 | 36 | } |
38 | 37 | expression = String(expression); |
39 | | - operators = "\\|\\||>=|<=|&&|!=|>|=|<|/|-|\\+|\\*|\\^"; |
| 38 | + const operators = "\\|\\||>=|<=|&&|!=|>|=|<|/|-|\\+|\\*|\\^"; |
40 | 39 |
|
41 | 40 | // operators |
42 | | - operatorsPattern = "((" + operators + ")(?=[ _]))"; |
43 | | - operatorsReplaceRE = new RegExp(operatorsPattern, "g"); |
| 41 | + const operatorsPattern = "((" + operators + ")(?=[ _]))"; |
| 42 | + const operatorsReplaceRE = new RegExp(operatorsPattern, "g"); |
44 | 43 | expression = expression.replace(operatorsReplaceRE, match => Expression.OPERATORS[match]); |
45 | 44 |
|
46 | 45 | // predefined variables |
47 | | - predefinedVarsPattern = "(" + Object.keys(Expression.PREDEFINED_VARS).join("|") + ")"; |
48 | | - predefinedVarsReplaceRE = new RegExp(predefinedVarsPattern, "g"); |
49 | | - expression = expression.replace(predefinedVarsReplaceRE, (match, p1, offset) => (expression[offset - 1] === '$' ? match : Expression.PREDEFINED_VARS[match])); |
| 46 | + const predefinedVarsPattern = "(" + Object.keys(Expression.PREDEFINED_VARS).join("|") + ")"; |
| 47 | + const userVariablePattern = '(\\$_*[^_ ]+)'; |
| 48 | + const variablesReplaceRE = new RegExp(`${userVariablePattern}|${predefinedVarsPattern}`, "g"); |
| 49 | + expression = expression.replace(variablesReplaceRE, (match) => (Expression.PREDEFINED_VARS[match] || match)); |
50 | 50 |
|
51 | 51 | return expression.replace(/[ _]+/g, '_'); |
52 | 52 | } |
|
0 commit comments