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: 12 additions & 4 deletions lib-es5/utils/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -137,12 +137,20 @@ function normalize_expression(expression) {
if (!isString(expression) || expression.length === 0 || expression.match(/^!.+!$/)) {
return expression;
}

var operators = "\\|\\||>=|<=|&&|!=|>|=|<|/|-|\\+|\\*";
var pattern = "((" + operators + ")(?=[ _])|" + Object.keys(PREDEFINED_VARS).join("|") + ")";
var replaceRE = new RegExp(pattern, "g");
expression = expression.replace(replaceRE, function (match) {
return CONDITIONAL_OPERATORS[match] || PREDEFINED_VARS[match];
var operatorsPattern = "((" + operators + ")(?=[ _]))";
var operatorsReplaceRE = new RegExp(operatorsPattern, "g");
expression = expression.replace(operatorsReplaceRE, function (match) {
return CONDITIONAL_OPERATORS[match];
});

var predefinedVarsPattern = "(" + Object.keys(PREDEFINED_VARS).join("|") + ")";
var predefinedVarsReplaceRE = new RegExp(predefinedVarsPattern, "g");
expression = expression.replace(predefinedVarsReplaceRE, function (match, p1, offset) {
return expression[offset - 1] === '$' ? match : PREDEFINED_VARS[match];
});

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

Expand Down
14 changes: 9 additions & 5 deletions lib/utils/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -127,12 +127,16 @@ function normalize_expression(expression) {
if (!isString(expression) || expression.length === 0 || expression.match(/^!.+!$/)) {
return expression;
}

const operators = "\\|\\||>=|<=|&&|!=|>|=|<|/|-|\\+|\\*";
const pattern = "((" + operators + ")(?=[ _])|" + Object.keys(PREDEFINED_VARS).join("|") + ")";
const replaceRE = new RegExp(pattern, "g");
expression = expression.replace(replaceRE, function (match) {
return CONDITIONAL_OPERATORS[match] || PREDEFINED_VARS[match];
});
const operatorsPattern = "((" + operators + ")(?=[ _]))";
const operatorsReplaceRE = new RegExp(operatorsPattern, "g");
expression = expression.replace(operatorsReplaceRE, match => CONDITIONAL_OPERATORS[match]);

const predefinedVarsPattern = "(" + Object.keys(PREDEFINED_VARS).join("|") + ")";
const predefinedVarsReplaceRE = new RegExp(predefinedVarsPattern, "g");
expression = expression.replace(predefinedVarsReplaceRE, (match, p1, offset) => (expression[offset - 1] === '$' ? match : PREDEFINED_VARS[match]));

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

Expand Down
15 changes: 15 additions & 0 deletions test/utils_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -1052,6 +1052,21 @@ describe("utils", function () {
t = cloudinary.utils.generate_transformation_string(options);
expect(t).to.eql("$foo_10/if_fc_gt_2/c_scale,w_$foo_mul_200_div_fc/if_end");
});
it("should not change variable names even if they look like keywords", function () {
var options, t;
options = {
transformation: [
{
$width: 10,
},
{
width: "$width + 10 + width",
},
],
};
t = cloudinary.utils.generate_transformation_string(options);
expect(t).to.eql("$width_10/w_$width_add_10_add_w");
});
it("should support text values", function () {
test_cloudinary_url("sample", {
effect: "$efname:100",
Expand Down