Skip to content

Commit 9703313

Browse files
authored
Fix normalize_expression for complex cases (#265)
1 parent 5c51d5b commit 9703313

3 files changed

Lines changed: 71 additions & 8 deletions

File tree

src/expression.js

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
* Represents a transformation expression.
33
* @param {string} expressionStr - An expression in string format.
44
* @class Expression
5-
*
5+
* Normally this class is not instantiated directly
66
*/
77
class Expression {
88
constructor(expressionStr) {
@@ -31,22 +31,22 @@ class Expression {
3131
* @return {string} the normalized form of the value expression, e.g. "w_gt_100"
3232
*/
3333
static normalize(expression) {
34-
var operators, operatorsPattern, operatorsReplaceRE, predefinedVarsPattern, predefinedVarsReplaceRE;
3534
if (expression == null) {
3635
return expression;
3736
}
3837
expression = String(expression);
39-
operators = "\\|\\||>=|<=|&&|!=|>|=|<|/|-|\\+|\\*|\\^";
38+
const operators = "\\|\\||>=|<=|&&|!=|>|=|<|/|-|\\+|\\*|\\^";
4039

4140
// operators
42-
operatorsPattern = "((" + operators + ")(?=[ _]))";
43-
operatorsReplaceRE = new RegExp(operatorsPattern, "g");
41+
const operatorsPattern = "((" + operators + ")(?=[ _]))";
42+
const operatorsReplaceRE = new RegExp(operatorsPattern, "g");
4443
expression = expression.replace(operatorsReplaceRE, match => Expression.OPERATORS[match]);
4544

4645
// 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));
5050

5151
return expression.replace(/[ _]+/g, '_');
5252
}

src/namespace/cloudinary-core.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import * as Util from '../util';
77
import Transformation from '../transformation';
88
import Condition from '../condition';
99
import Configuration from '../configuration';
10+
import Expression from "../expression";
1011
import HtmlTag from '../tags/htmltag';
1112
import ImageTag from '../tags/imagetag';
1213
import PictureTag from '../tags/picturetag';
@@ -24,6 +25,7 @@ export default {
2425
Condition,
2526
Configuration,
2627
crc32,
28+
Expression,
2729
FetchLayer,
2830
HtmlTag,
2931
ImageTag,
@@ -43,6 +45,7 @@ export {
4345
Condition,
4446
Configuration,
4547
crc32,
48+
Expression,
4649
FetchLayer,
4750
HtmlTag,
4851
ImageTag,

test/spec/automatic/transformation-spec.js

Lines changed: 60 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)