Skip to content

Commit ef6f5ad

Browse files
Merge pull request #578 from cloudinary/start_end_offset_normalized
feat: start and end offset normalized in a transformation string
2 parents 1153e75 + 4d93d7c commit ef6f5ad

File tree

4 files changed

+32
-5
lines changed

4 files changed

+32
-5
lines changed

lib-es5/utils/consts.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,4 +85,4 @@ module.exports = {
8585
UPLOAD_PREFIX,
8686
SUPPORTED_SIGNATURE_ALGORITHMS,
8787
DEFAULT_SIGNATURE_ALGORITHM
88-
};
88+
};

lib-es5/utils/index.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -567,6 +567,12 @@ function generate_transformation_string(options) {
567567
options.start_offset = _split_range2[0];
568568
options.end_offset = _split_range2[1];
569569
}
570+
if (options.start_offset) {
571+
options.start_offset = normalize_expression(options.start_offset);
572+
}
573+
if (options.end_offset) {
574+
options.end_offset = normalize_expression(options.end_offset);
575+
}
570576
var overlay = process_layer(consumeOption(options, "overlay"));
571577
var radius = process_radius(consumeOption(options, "radius"));
572578
var underlay = process_layer(consumeOption(options, "underlay"));

lib/utils/index.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -510,6 +510,12 @@ function generate_transformation_string(options) {
510510
if (options.offset != null) {
511511
[options.start_offset, options.end_offset] = split_range(consumeOption(options, "offset"));
512512
}
513+
if (options.start_offset) {
514+
options.start_offset = normalize_expression(options.start_offset);
515+
}
516+
if (options.end_offset) {
517+
options.end_offset = normalize_expression(options.end_offset);
518+
}
513519
let overlay = process_layer(consumeOption(options, "overlay"));
514520
let radius = process_radius(consumeOption(options, "radius"));
515521
let underlay = process_layer(consumeOption(options, "underlay"));

test/unit/normalize_expression_spec.js

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
const cloudinary = require("../../cloudinary");
22
const createTestConfig = require('../testUtils/createTestConfig');
33
const helper = require("../spechelper");
4-
const { SIMPLE_PARAMS } = require(`../../${helper.libPath}/utils/consts`);
4+
const {SIMPLE_PARAMS} = require(`../../${helper.libPath}/utils/consts`);
5+
const {generate_transformation_string} = require("../../lib/utils");
56

67
describe("normalize_expression tests", function () {
78
beforeEach(function () {
@@ -12,7 +13,21 @@ describe("normalize_expression tests", function () {
1213
}));
1314
});
1415

15-
it("Expression normalization", function() {
16+
it('should normalize start_offset', function () {
17+
const result = generate_transformation_string({
18+
"start_offset": "idu - 5"
19+
});
20+
expect(result).to.equal("so_idu_sub_5");
21+
});
22+
23+
it('should normalize end_offset', function () {
24+
const result = generate_transformation_string({
25+
"end_offset": "idu - 5"
26+
});
27+
expect(result).to.equal("eo_idu_sub_5");
28+
});
29+
30+
it("Expression normalization", function () {
1631
const cases = {
1732
'null is not affected': [null, null],
1833
'None is not affected': ['None', 'None'],
@@ -61,8 +76,8 @@ describe("normalize_expression tests", function () {
6176
const value = "width * 2";
6277
const normalizedValue = "w_mul_2";
6378
const normalizedParams = ["angle", "aspect_ratio", "dpr", "effect", "height", "opacity", "quality", "radius",
64-
"width", "x", "y", "zoom"];
65-
const nonNormalizedParams = simpleTransformationParams.concat('overlay', 'underlay')
79+
"width", "x", "y", "zoom", "end_offset", "start_offset"];
80+
const nonNormalizedParams = simpleTransformationParams.concat('overlay', 'underlay').filter(param => !normalizedParams.includes(param));
6681
normalizedParams.forEach((param) => {
6782
it(`should normalize value in ${param}`, () => {
6883
// c_scale needed to test h_ and w_ parameters that are ignored without crop mode

0 commit comments

Comments
 (0)