From f1150572d9e5cba6a4a9fb7ae52ead2422f23512 Mon Sep 17 00:00:00 2001 From: Patrick Tolosa Date: Sun, 23 May 2021 18:20:44 +0300 Subject: [PATCH] Add support for complex variable names --- lib-es5/utils/index.js | 8 +++++--- lib/utils/index.js | 5 +++-- test/unit/cloudinary_spec.js | 18 ++++++++++++++++++ test/utils/utils_spec.js | 2 ++ 4 files changed, 28 insertions(+), 5 deletions(-) diff --git a/lib-es5/utils/index.js b/lib-es5/utils/index.js index 79c83afd..8a45c8c7 100644 --- a/lib-es5/utils/index.js +++ b/lib-es5/utils/index.js @@ -153,10 +153,12 @@ function normalize_expression(expression) { return CONDITIONAL_OPERATORS[match]; }); + // // The new solution 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]; + var userVariablePattern = '(\\$_*[^_ ]+)'; + var variablesReplaceRE = new RegExp(`${userVariablePattern}|${predefinedVarsPattern}`, "g"); + expression = expression.replace(variablesReplaceRE, function (match) { + return PREDEFINED_VARS[match] || match; }); return expression.replace(/[ _]+/g, '_'); diff --git a/lib/utils/index.js b/lib/utils/index.js index 8719daad..a782b569 100644 --- a/lib/utils/index.js +++ b/lib/utils/index.js @@ -139,8 +139,9 @@ function normalize_expression(expression) { 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])); + const userVariablePattern = '(\\$_*[^_ ]+)'; + const variablesReplaceRE = new RegExp(`${userVariablePattern}|${predefinedVarsPattern}`, "g"); + expression = expression.replace(variablesReplaceRE, (match) => (PREDEFINED_VARS[match] || match)); return expression.replace(/[ _]+/g, '_'); } diff --git a/test/unit/cloudinary_spec.js b/test/unit/cloudinary_spec.js index d920e295..d4b4506b 100644 --- a/test/unit/cloudinary_spec.js +++ b/test/unit/cloudinary_spec.js @@ -770,6 +770,7 @@ describe("cloudinary", function () { api_key: "1234" }); }); + it("should support preloaded identifier format", function () { var result = cloudinary.utils.url("raw/private/v123456/document.docx"); expect(result).to.eql("http://res.cloudinary.com/test123/raw/private/v123456/document.docx"); @@ -779,6 +780,7 @@ describe("cloudinary", function () { }); expect(result).to.eql("http://res.cloudinary.com/test123/image/private/c_scale,w_1.0/v123456/img.jpg"); }); + it("should add responsive width transformation", function () { var options, result; options = { @@ -827,4 +829,20 @@ describe("cloudinary", function () { result = cloudinary.utils.url("sample.jpg", options); expect(result).to.eql('http://res.cloudinary.com/test123/image/upload/s--v2fTPYTu--/sample.jpg'); }); + + it("should not affect user variable names containing predefined names", function() { + const options = { transformation: [ + { + $mywidth: "100", + $aheight: 300 + }, + { + width: "3 + $mywidth * 3 + 4 / 2 * initialWidth * $mywidth", + height: "3 * initialHeight + $aheight", + crop: 'scale' + } + ]}; + const result = cloudinary.utils.url("sample", options); + expect(result).to.contain("$aheight_300,$mywidth_100/c_scale,h_3_mul_ih_add_$aheight,w_3_add_$mywidth_mul_3_add_4_div_2_mul_iw_mul_$mywidth"); + }); }); diff --git a/test/utils/utils_spec.js b/test/utils/utils_spec.js index 36806165..4855b43c 100644 --- a/test/utils/utils_spec.js +++ b/test/utils/utils_spec.js @@ -1060,6 +1060,7 @@ 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 = { @@ -1075,6 +1076,7 @@ describe("utils", function () { 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",