Skip to content

Commit 5ed8fda

Browse files
authored
Merge pull request #420 from cloudinary/feature/add-backwards-compatibility
added backwards compatibility object processing
2 parents c106071 + 8df1adb commit 5ed8fda

File tree

22 files changed

+2713
-6
lines changed

22 files changed

+2713
-6
lines changed

__TESTS_BUNDLE_SIZE__/bundleSizeTestCases.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,14 +67,14 @@ const bundleSizeTestCases:ITestCase[] = [
6767
},
6868
{
6969
name: 'Import backwards comaptiblity function',
70-
sizeLimitInKB: 26,
70+
sizeLimitInKB: 57,
7171
importsArray: [
7272
importFromBase('createCloudinaryLegacyURL')
7373
]
7474
},
7575
{
7676
name: 'Import all of the SDK',
77-
sizeLimitInKB: 110,
77+
sizeLimitInKB: 118,
7878
importsArray: [
7979
importFromBase('CloudinaryBaseSDK')
8080
]

__TESTS__/backwardsComaptibility/createLegacyURL.test.ts

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import {createTestURL} from "./transformationLegacyTests/utils/createTestURL";
22
import {createCloudinaryLegacyURL} from "../../src";
3+
import Transformation from "../../src/backwards/transformation";
34

45
describe('Create legacy urls', () => {
56
it('Should throw without cloudName', () => {
@@ -987,4 +988,72 @@ describe('Create legacy urls', () => {
987988
expect(urlFirstRun).toEqual("http://res.cloudinary.com/test123/image/upload/c_fill,h_120,l_somepid,w_80/sample");
988989
expect(urlFirstRun).toEqual(urlSecondRun);
989990
});
991+
it("Transformation object: User Define Variables", function () {
992+
const options = {
993+
if: "face_count > 2",
994+
variables: [["$z", 5], ["$foo", "$z * 2"]],
995+
crop: "scale",
996+
width: "$foo * 200"
997+
};
998+
const result = createTestURL("sample",
999+
{
1000+
transformation: new Transformation(options)
1001+
}
1002+
);
1003+
expect(result).toBe('http://res.cloudinary.com/demo/image/upload/if_fc_gt_2,$z_5,$foo_$z_mul_2,c_scale,w_$foo_mul_200/sample');
1004+
});
1005+
1006+
it("Transformation object: should sort variables", function () {
1007+
const result = createTestURL("sample",
1008+
{
1009+
transformation: new Transformation({
1010+
$second: 1,
1011+
$first: 2
1012+
})
1013+
}
1014+
);
1015+
expect(result).toBe('http://res.cloudinary.com/demo/image/upload/$first_2,$second_1/sample');
1016+
});
1017+
1018+
it("Transformation object: string overlay", function () {
1019+
const result = createTestURL("sample",
1020+
{
1021+
transformation: new Transformation().overlay("text:hello").width(100).height(100)
1022+
}
1023+
);
1024+
expect(result).toBe('http://res.cloudinary.com/demo/image/upload/h_100,l_text:hello,w_100/sample');
1025+
});
1026+
1027+
it("Transformation object: object overlay", function () {
1028+
const options = {
1029+
text: "Cloudinary for the win!",
1030+
fontFamily: "Arial",
1031+
fontSize: 18,
1032+
fontAntialiasing: "fast"
1033+
};
1034+
const result = createTestURL("sample",
1035+
{
1036+
transformation: new Transformation().overlay(options).width(100).height(100)
1037+
}
1038+
);
1039+
expect(result).toBe('http://res.cloudinary.com/demo/image/upload/h_100,l_text:Arial_18_antialias_fast:Cloudinary%20for%20the%20win%21,w_100/sample');
1040+
});
1041+
1042+
it("Transformation object: should support fetch:URL literal", function () {
1043+
const result = createTestURL("sample",
1044+
{
1045+
transformation: new Transformation().overlay("fetch:http://cloudinary.com/images/old_logo.png")
1046+
}
1047+
);
1048+
expect(result).toBe('http://res.cloudinary.com/demo/image/upload/l_fetch:aHR0cDovL2Nsb3VkaW5hcnkuY29tL2ltYWdlcy9vbGRfbG9nby5wbmc=/sample');
1049+
});
1050+
1051+
it("Transformation object: should support chained transformation", function () {
1052+
const result = createTestURL("sample",
1053+
{
1054+
transformation: new Transformation().width(100).crop("scale").chain().crop("crop").width(200)
1055+
}
1056+
);
1057+
expect(result).toBe('http://res.cloudinary.com/demo/image/upload/c_scale,w_100/c_crop,w_200/sample');
1058+
});
9901059
});
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
import {createTestURL} from "./transformationLegacyTests/utils/createTestURL";
2+
import Transformation from "../../src/backwards/transformation";
3+
import FetchLayer from "../../src/backwards/legacyLayer/fetchlayer";
4+
import TextLayer from "../../src/backwards/legacyLayer/textlayer";
5+
import Layer from "../../src/backwards/legacyLayer/layer";
6+
7+
describe('Create legacy layer urls', () => {
8+
it("Should support Layer as overlay input", function () {
9+
const result = createTestURL("sample",
10+
{overlay: new Layer().resourceType("video").publicId("cat")}
11+
);
12+
expect(result).toBe('http://res.cloudinary.com/demo/image/upload/l_video:cat/sample');
13+
});
14+
15+
it("Should support TextLayer as overlay input", function () {
16+
const result = createTestURL("sample",
17+
{overlay: new TextLayer().fontFamily("Arial").fontSize(80).text("Flowers")}
18+
);
19+
expect(result).toBe('http://res.cloudinary.com/demo/image/upload/l_text:Arial_80:Flowers/sample');
20+
});
21+
22+
it("Should support TextLayer object", function () {
23+
const options = {
24+
text: "Cloudinary for the win!",
25+
fontFamily: "Arial",
26+
fontSize: 18,
27+
fontHinting: "full"
28+
};
29+
const result = createTestURL("sample",
30+
{overlay: new TextLayer(options)}
31+
);
32+
expect(result).toBe('http://res.cloudinary.com/demo/image/upload/l_text:Arial_18_hinting_full:Cloudinary%20for%20the%20win%21/sample');
33+
});
34+
35+
it("Should support FetchLayer: string", function () {
36+
const result = createTestURL("sample",
37+
{
38+
transformation: new Transformation({
39+
overlay: new FetchLayer("http://cloudinary.com/images/logo.png")
40+
})
41+
}
42+
);
43+
expect(result).toBe('http://res.cloudinary.com/demo/image/upload/l_fetch:aHR0cDovL2Nsb3VkaW5hcnkuY29tL2ltYWdlcy9sb2dvLnBuZw==/sample');
44+
});
45+
46+
it("Should support FetchLayer: url object", function () {
47+
const result = createTestURL("sample",
48+
{
49+
transformation: new Transformation({
50+
overlay: new FetchLayer({
51+
url: 'http://res.cloudinary.com/demo/sample.jpg'
52+
})
53+
})
54+
}
55+
);
56+
expect(result).toBe('http://res.cloudinary.com/demo/image/upload/l_fetch:aHR0cDovL3Jlcy5jbG91ZGluYXJ5LmNvbS9kZW1vL3NhbXBsZS5qcGc=/sample');
57+
});
58+
59+
it("Should change dpr to float", function () {
60+
const result = createTestURL("sample",
61+
{dpr: 1}
62+
);
63+
expect(result).toBe('http://res.cloudinary.com/demo/image/upload/dpr_1.0/sample');
64+
});
65+
66+
it("Should change dpr to float on transformation input", function () {
67+
const result = createTestURL("sample",
68+
{transformation: new Transformation().dpr(1)}
69+
);
70+
expect(result).toBe('http://res.cloudinary.com/demo/image/upload/dpr_1.0/sample');
71+
});
72+
});

jest.config.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@
66
"<rootDir>/src/**/*.ts",
77
"<rootDir>/scripts/**/*.ts",
88
"!<rootDir>/scripts/createEntrypoints.ts",
9-
"!<rootDir>/scripts/copyPackageJsonToSrc.ts.ts"
9+
"!<rootDir>/scripts/copyPackageJsonToSrc.ts.ts",
10+
"!<rootDir>/src/backwards/**/*.ts"
1011
],
1112
"modulePaths": [
1213
"<rootDir>/src"

src/backwards/condition.ts

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
import Expression from './expression';
2+
3+
/**
4+
* Represents a transformation condition.
5+
* @param {string} conditionStr - a condition in string format
6+
* @class Condition
7+
* @example
8+
* // normally this class is not instantiated directly
9+
* var tr = cloudinary.Transformation.new()
10+
* .if().width( ">", 1000).and().aspectRatio("<", "3:4").then()
11+
* .width(1000)
12+
* .crop("scale")
13+
* .else()
14+
* .width(500)
15+
* .crop("scale")
16+
*
17+
* var tr = cloudinary.Transformation.new()
18+
* .if("w > 1000 and aspectRatio < 3:4")
19+
* .width(1000)
20+
* .crop("scale")
21+
* .else()
22+
* .width(500)
23+
* .crop("scale")
24+
*
25+
*/
26+
class Condition extends Expression {
27+
constructor(conditionStr:string) {
28+
super(conditionStr);
29+
}
30+
31+
/**
32+
* @function Condition#height
33+
* @param {string} operator the comparison operator (e.g. "<", "lt")
34+
* @param {string|number} value the right hand side value
35+
* @return {Condition} this condition
36+
*/
37+
height(operator: string, value: string|number) {
38+
return this.predicate("h", operator, value);
39+
}
40+
41+
/**
42+
* @function Condition#width
43+
* @param {string} operator the comparison operator (e.g. "<", "lt")
44+
* @param {string|number} value the right hand side value
45+
* @return {Condition} this condition
46+
*/
47+
width(operator: string, value: string|number) {
48+
return this.predicate("w", operator, value);
49+
}
50+
51+
/**
52+
* @function Condition#aspectRatio
53+
* @param {string} operator the comparison operator (e.g. "<", "lt")
54+
* @param {string|number} value the right hand side value
55+
* @return {Condition} this condition
56+
*/
57+
aspectRatio(operator: string, value: string|number) {
58+
return this.predicate("ar", operator, value);
59+
}
60+
61+
/**
62+
* @function Condition#pages
63+
* @param {string} operator the comparison operator (e.g. "<", "lt")
64+
* @param {string|number} value the right hand side value
65+
* @return {Condition} this condition
66+
*/
67+
pageCount(operator: string, value: string|number) {
68+
return this.predicate("pc", operator, value);
69+
}
70+
71+
/**
72+
* @function Condition#faces
73+
* @param {string} operator the comparison operator (e.g. "<", "lt")
74+
* @param {string|number} value the right hand side value
75+
* @return {Condition} this condition
76+
*/
77+
faceCount(operator: string, value: string|number) {
78+
return this.predicate("fc", operator, value);
79+
}
80+
81+
/**
82+
* @function Condition#duration
83+
* @param {string} operator the comparison operator (e.g. "<", "lt")
84+
* @param {string|number} value the right hand side value
85+
* @return {Condition} this condition
86+
*/
87+
duration(operator: string, value: string|number) {
88+
return this.predicate("du", operator, value);
89+
}
90+
91+
/**
92+
* @function Condition#initialDuration
93+
* @param {string} operator the comparison operator (e.g. "<", "lt")
94+
* @param {string|number} value the right hand side value
95+
* @return {Condition} this condition
96+
*/
97+
initialDuration(operator: string, value: string|number) {
98+
return this.predicate("idu", operator, value);
99+
}
100+
}
101+
102+
export default Condition;

0 commit comments

Comments
 (0)