Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 8 additions & 8 deletions src/expression.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* Represents a transformation expression.
* @param {string} expressionStr - An expression in string format.
* @class Expression
*
* Normally this class is not instantiated directly
*/
class Expression {
constructor(expressionStr) {
Expand Down Expand Up @@ -31,22 +31,22 @@ class Expression {
* @return {string} the normalized form of the value expression, e.g. "w_gt_100"
*/
static normalize(expression) {
var operators, operatorsPattern, operatorsReplaceRE, predefinedVarsPattern, predefinedVarsReplaceRE;
if (expression == null) {
return expression;
}
expression = String(expression);
operators = "\\|\\||>=|<=|&&|!=|>|=|<|/|-|\\+|\\*|\\^";
const operators = "\\|\\||>=|<=|&&|!=|>|=|<|/|-|\\+|\\*|\\^";

// operators
operatorsPattern = "((" + operators + ")(?=[ _]))";
operatorsReplaceRE = new RegExp(operatorsPattern, "g");
const operatorsPattern = "((" + operators + ")(?=[ _]))";
const operatorsReplaceRE = new RegExp(operatorsPattern, "g");
expression = expression.replace(operatorsReplaceRE, match => Expression.OPERATORS[match]);

// predefined variables
predefinedVarsPattern = "(" + Object.keys(Expression.PREDEFINED_VARS).join("|") + ")";
predefinedVarsReplaceRE = new RegExp(predefinedVarsPattern, "g");
expression = expression.replace(predefinedVarsReplaceRE, (match, p1, offset) => (expression[offset - 1] === '$' ? match : Expression.PREDEFINED_VARS[match]));
const predefinedVarsPattern = "(" + Object.keys(Expression.PREDEFINED_VARS).join("|") + ")";
const userVariablePattern = '(\\$_*[^_ ]+)';
const variablesReplaceRE = new RegExp(`${userVariablePattern}|${predefinedVarsPattern}`, "g");
expression = expression.replace(variablesReplaceRE, (match) => (Expression.PREDEFINED_VARS[match] || match));

return expression.replace(/[ _]+/g, '_');
}
Expand Down
3 changes: 3 additions & 0 deletions src/namespace/cloudinary-core.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import * as Util from '../util';
import Transformation from '../transformation';
import Condition from '../condition';
import Configuration from '../configuration';
import Expression from "../expression";
import HtmlTag from '../tags/htmltag';
import ImageTag from '../tags/imagetag';
import PictureTag from '../tags/picturetag';
Expand All @@ -24,6 +25,7 @@ export default {
Condition,
Configuration,
crc32,
Expression,
FetchLayer,
HtmlTag,
ImageTag,
Expand All @@ -43,6 +45,7 @@ export {
Condition,
Configuration,
crc32,
Expression,
FetchLayer,
HtmlTag,
ImageTag,
Expand Down
60 changes: 60 additions & 0 deletions test/spec/automatic/transformation-spec.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.