Skip to content

Commit 4283cef

Browse files
authored
Merge pull request #402 from cloudinary/fix/clone-transformation-options
Fix/clone transformation options
2 parents f90a52e + 294e4e8 commit 4283cef

File tree

7 files changed

+72
-8
lines changed

7 files changed

+72
-8
lines changed

__TESTS_BUNDLE_SIZE__/bundleSizeTestCases.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ const bundleSizeTestCases:ITestCase[] = [
6767
},
6868
{
6969
name: 'Import backwards comaptiblity function',
70-
sizeLimitInKB: 12,
70+
sizeLimitInKB: 26,
7171
importsArray: [
7272
importFromBase('createCloudinaryLegacyURL')
7373
]

__TESTS__/backwardsComaptibility/createLegacyURL.test.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -970,4 +970,21 @@ describe('Create legacy urls', () => {
970970
});
971971
expect(result).toBe('http://res.cloudinary.com/demo/image/upload/bo_5px_solid_rgb:ffaabbdd/sample.jpg');
972972
});
973+
974+
it("should not modify options objects when chaining transformations ", function () {
975+
const optionObj = {
976+
cloud_name: 'test123',
977+
transformation: [{
978+
overlay: 'somepid',
979+
crop: "fill",
980+
height: 120,
981+
width: 80
982+
}]
983+
};
984+
const urlFirstRun = createTestURL("sample", optionObj);
985+
const urlSecondRun = createTestURL("sample", optionObj);
986+
987+
expect(urlFirstRun).toEqual("http://res.cloudinary.com/test123/image/upload/c_fill,h_120,l_somepid,w_80/sample");
988+
expect(urlFirstRun).toEqual(urlSecondRun);
989+
});
973990
});

__TESTS__/backwardsComaptibility/generateTransformationString.test.ts

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,4 +135,50 @@ describe('it works?', () => {
135135
const t = generateTransformationString(options);
136136
expect(t).toEqual("$big_$small_pow_1.5,$small_150");
137137
});
138+
139+
it("border can be presented as objects", function () {
140+
const optionsAsObject = {
141+
transformation: [
142+
{
143+
border: { width: 4, color: 'white' }
144+
}
145+
]
146+
};
147+
const optionsAsString = {
148+
transformation: [
149+
{
150+
border: '4px_solid_white'
151+
}
152+
]
153+
};
154+
const tObj = generateTransformationString(optionsAsObject);
155+
const tStr = generateTransformationString(optionsAsString);
156+
expect(tStr).toEqual("bo_4px_solid_white");
157+
expect(tStr).toEqual(tObj);
158+
});
159+
160+
it("radius as an array is parsed", function () {
161+
const options = {
162+
transformation: [
163+
{
164+
radius: [10, 20, 30, 40]
165+
}
166+
]
167+
};
168+
const t = generateTransformationString(options);
169+
expect(t).toEqual("r_10:20:30:40");
170+
});
171+
172+
it("radius accepts a string value", function () {
173+
const options = {
174+
transformation: [
175+
{
176+
radius: 'max'
177+
}
178+
]
179+
};
180+
const t = generateTransformationString(options);
181+
expect(t).toEqual("r_max");
182+
});
183+
138184
});

package.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
"@rollup/plugin-node-resolve": "^7.1.1",
3838
"@types/jest": "^25.2.3",
3939
"@types/mock-fs": "^4.10.0",
40+
"@types/node": "^15.14.2",
4041
"@types/webpack": "^4.41.21",
4142
"@typescript-eslint/eslint-plugin": "^3.0.2",
4243
"@typescript-eslint/parser": "^3.0.2",
@@ -71,6 +72,7 @@
7172
"rimraf": "^3.0.2",
7273
"rollup": "^2.3.2",
7374
"rollup-plugin-babel": "^4.4.0",
75+
"rollup-plugin-commonjs": "^10.1.0",
7476
"rollup-plugin-serve": "^1.0.1",
7577
"rollup-plugin-typescript": "^1.0.1",
7678
"sanitize-html": "^1.18.2",
@@ -79,8 +81,7 @@
7981
"tslib": "^2.0.0",
8082
"typedoc": "^0.17.8",
8183
"typescript": "^3.9.3",
82-
"webpack": "^4.44.1",
83-
"@types/node": "^15.14.2"
84+
"webpack": "^4.44.1"
8485
},
8586
"dependencies": {
8687
"@types/lodash.clonedeep": "^4.5.6",

rollup.config.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import resolve from '@rollup/plugin-node-resolve';
22
import typescript from 'rollup-plugin-typescript';
33
import json from '@rollup/plugin-json';
4+
import commonjs from 'rollup-plugin-commonjs';
45

56
export default [{
67
input: 'src/index.ts',
@@ -14,7 +15,8 @@ export default [{
1415
plugins: [
1516
json(),
1617
resolve(),
17-
typescript({ target: "es5"})
18+
typescript({ target: "es5"}),
19+
commonjs()
1820
]
1921
}];
2022

src/backwards/createCloudinaryLegacyURL.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import {generateTransformationString} from "./generateTransformationString";
33
import {finalize_resource_type} from "./utils/finalizeResourceType";
44
import {finalize_source} from "./utils/finalize_source";
55
import {unsigned_url_prefix} from "./utils/unsigned_url_prefix";
6+
import cloneDeep from 'lodash/cloneDeep';
67

78
export function createCloudinaryLegacyURL(public_id: string, transformationOptions: LegacyITransforamtionOptions) {
89
// Path format
@@ -69,8 +70,7 @@ export function createCloudinaryLegacyURL(public_id: string, transformationOptio
6970
version = null;
7071
}
7172

72-
const transformation = generateTransformationString(transformationOptions).replace(/([^:])\/\//g, '$1/');;
73-
73+
const transformation = generateTransformationString(cloneDeep(transformationOptions)).replace(/([^:])\/\//g, '$1/');;
7474
if (sign_url && !auth_token) {
7575
let to_sign = [transformation, source_to_sign].filter(function (part) {
7676
return (part != null) && part !== '';

src/backwards/generateTransformationString.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@ export function generateTransformationString(transformationOptions: LegacyITrans
3333

3434
if (Array.isArray(transformationOptions)) {
3535
return transformationOptions
36-
// TODO we might need to clone the childTransformation
3736
.map((singleTransformation) => {
3837
return generateTransformationString(singleTransformation);
3938
}).filter((a) => { console.log(a); return a;}).join('/');
@@ -96,7 +95,6 @@ export function generateTransformationString(transformationOptions: LegacyITrans
9695
if (isAnyChildAnObject) {
9796
childTransformations = childTransformations.map((transformation: LegacyITransforamtionOptions) => {
9897
if (isObject(transformation)) {
99-
// TODO We might need to clone here
10098
return generateTransformationString(transformation);
10199
} else {
102100
return generateTransformationString({transformation});

0 commit comments

Comments
 (0)