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
4 changes: 2 additions & 2 deletions __TESTS_BUNDLE_SIZE__/bundleSizeTestCases.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,14 +67,14 @@ const bundleSizeTestCases:ITestCase[] = [
},
{
name: 'Import backwards comaptiblity function',
sizeLimitInKB: 26,
sizeLimitInKB: 57,
importsArray: [
importFromBase('createCloudinaryLegacyURL')
]
},
{
name: 'Import all of the SDK',
sizeLimitInKB: 110,
sizeLimitInKB: 118,
importsArray: [
importFromBase('CloudinaryBaseSDK')
]
Expand Down
69 changes: 69 additions & 0 deletions __TESTS__/backwardsComaptibility/createLegacyURL.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import {createTestURL} from "./transformationLegacyTests/utils/createTestURL";
import {createCloudinaryLegacyURL} from "../../src";
import Transformation from "../../src/backwards/transformation";

describe('Create legacy urls', () => {
it('Should throw without cloudName', () => {
Expand Down Expand Up @@ -987,4 +988,72 @@ describe('Create legacy urls', () => {
expect(urlFirstRun).toEqual("http://res.cloudinary.com/test123/image/upload/c_fill,h_120,l_somepid,w_80/sample");
expect(urlFirstRun).toEqual(urlSecondRun);
});
it("Transformation object: User Define Variables", function () {
const options = {
if: "face_count > 2",
variables: [["$z", 5], ["$foo", "$z * 2"]],
crop: "scale",
width: "$foo * 200"
};
const result = createTestURL("sample",
{
transformation: new Transformation(options)
}
);
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');
});

it("Transformation object: should sort variables", function () {
const result = createTestURL("sample",
{
transformation: new Transformation({
$second: 1,
$first: 2
})
}
);
expect(result).toBe('http://res.cloudinary.com/demo/image/upload/$first_2,$second_1/sample');
});

it("Transformation object: string overlay", function () {
const result = createTestURL("sample",
{
transformation: new Transformation().overlay("text:hello").width(100).height(100)
}
);
expect(result).toBe('http://res.cloudinary.com/demo/image/upload/h_100,l_text:hello,w_100/sample');
});

it("Transformation object: object overlay", function () {
const options = {
text: "Cloudinary for the win!",
fontFamily: "Arial",
fontSize: 18,
fontAntialiasing: "fast"
};
const result = createTestURL("sample",
{
transformation: new Transformation().overlay(options).width(100).height(100)
}
);
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');
});

it("Transformation object: should support fetch:URL literal", function () {
const result = createTestURL("sample",
{
transformation: new Transformation().overlay("fetch:http://cloudinary.com/images/old_logo.png")
}
);
expect(result).toBe('http://res.cloudinary.com/demo/image/upload/l_fetch:aHR0cDovL2Nsb3VkaW5hcnkuY29tL2ltYWdlcy9vbGRfbG9nby5wbmc=/sample');
});

it("Transformation object: should support chained transformation", function () {
const result = createTestURL("sample",
{
transformation: new Transformation().width(100).crop("scale").chain().crop("crop").width(200)
}
);
expect(result).toBe('http://res.cloudinary.com/demo/image/upload/c_scale,w_100/c_crop,w_200/sample');
});
});
72 changes: 72 additions & 0 deletions __TESTS__/backwardsComaptibility/legacyLayerURL.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
import {createTestURL} from "./transformationLegacyTests/utils/createTestURL";
import Transformation from "../../src/backwards/transformation";
import FetchLayer from "../../src/backwards/legacyLayer/fetchlayer";
import TextLayer from "../../src/backwards/legacyLayer/textlayer";
import Layer from "../../src/backwards/legacyLayer/layer";

describe('Create legacy layer urls', () => {
it("Should support Layer as overlay input", function () {
const result = createTestURL("sample",
{overlay: new Layer().resourceType("video").publicId("cat")}
);
expect(result).toBe('http://res.cloudinary.com/demo/image/upload/l_video:cat/sample');
});

it("Should support TextLayer as overlay input", function () {
const result = createTestURL("sample",
{overlay: new TextLayer().fontFamily("Arial").fontSize(80).text("Flowers")}
);
expect(result).toBe('http://res.cloudinary.com/demo/image/upload/l_text:Arial_80:Flowers/sample');
});

it("Should support TextLayer object", function () {
const options = {
text: "Cloudinary for the win!",
fontFamily: "Arial",
fontSize: 18,
fontHinting: "full"
};
const result = createTestURL("sample",
{overlay: new TextLayer(options)}
);
expect(result).toBe('http://res.cloudinary.com/demo/image/upload/l_text:Arial_18_hinting_full:Cloudinary%20for%20the%20win%21/sample');
});

it("Should support FetchLayer: string", function () {
const result = createTestURL("sample",
{
transformation: new Transformation({
overlay: new FetchLayer("http://cloudinary.com/images/logo.png")
})
}
);
expect(result).toBe('http://res.cloudinary.com/demo/image/upload/l_fetch:aHR0cDovL2Nsb3VkaW5hcnkuY29tL2ltYWdlcy9sb2dvLnBuZw==/sample');
});

it("Should support FetchLayer: url object", function () {
const result = createTestURL("sample",
{
transformation: new Transformation({
overlay: new FetchLayer({
url: 'http://res.cloudinary.com/demo/sample.jpg'
})
})
}
);
expect(result).toBe('http://res.cloudinary.com/demo/image/upload/l_fetch:aHR0cDovL3Jlcy5jbG91ZGluYXJ5LmNvbS9kZW1vL3NhbXBsZS5qcGc=/sample');
});

it("Should change dpr to float", function () {
const result = createTestURL("sample",
{dpr: 1}
);
expect(result).toBe('http://res.cloudinary.com/demo/image/upload/dpr_1.0/sample');
});

it("Should change dpr to float on transformation input", function () {
const result = createTestURL("sample",
{transformation: new Transformation().dpr(1)}
);
expect(result).toBe('http://res.cloudinary.com/demo/image/upload/dpr_1.0/sample');
});
});
3 changes: 2 additions & 1 deletion jest.config.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
"<rootDir>/src/**/*.ts",
"<rootDir>/scripts/**/*.ts",
"!<rootDir>/scripts/createEntrypoints.ts",
"!<rootDir>/scripts/copyPackageJsonToSrc.ts.ts"
"!<rootDir>/scripts/copyPackageJsonToSrc.ts.ts",
"!<rootDir>/src/backwards/**/*.ts"
],
"modulePaths": [
"<rootDir>/src"
Expand Down
102 changes: 102 additions & 0 deletions src/backwards/condition.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
import Expression from './expression';

/**
* Represents a transformation condition.
* @param {string} conditionStr - a condition in string format
* @class Condition
* @example
* // normally this class is not instantiated directly
* var tr = cloudinary.Transformation.new()
* .if().width( ">", 1000).and().aspectRatio("<", "3:4").then()
* .width(1000)
* .crop("scale")
* .else()
* .width(500)
* .crop("scale")
*
* var tr = cloudinary.Transformation.new()
* .if("w > 1000 and aspectRatio < 3:4")
* .width(1000)
* .crop("scale")
* .else()
* .width(500)
* .crop("scale")
*
*/
class Condition extends Expression {
constructor(conditionStr:string) {
super(conditionStr);
}

/**
* @function Condition#height
* @param {string} operator the comparison operator (e.g. "<", "lt")
* @param {string|number} value the right hand side value
* @return {Condition} this condition
*/
height(operator: string, value: string|number) {
return this.predicate("h", operator, value);
}

/**
* @function Condition#width
* @param {string} operator the comparison operator (e.g. "<", "lt")
* @param {string|number} value the right hand side value
* @return {Condition} this condition
*/
width(operator: string, value: string|number) {
return this.predicate("w", operator, value);
}

/**
* @function Condition#aspectRatio
* @param {string} operator the comparison operator (e.g. "<", "lt")
* @param {string|number} value the right hand side value
* @return {Condition} this condition
*/
aspectRatio(operator: string, value: string|number) {
return this.predicate("ar", operator, value);
}

/**
* @function Condition#pages
* @param {string} operator the comparison operator (e.g. "<", "lt")
* @param {string|number} value the right hand side value
* @return {Condition} this condition
*/
pageCount(operator: string, value: string|number) {
return this.predicate("pc", operator, value);
}

/**
* @function Condition#faces
* @param {string} operator the comparison operator (e.g. "<", "lt")
* @param {string|number} value the right hand side value
* @return {Condition} this condition
*/
faceCount(operator: string, value: string|number) {
return this.predicate("fc", operator, value);
}

/**
* @function Condition#duration
* @param {string} operator the comparison operator (e.g. "<", "lt")
* @param {string|number} value the right hand side value
* @return {Condition} this condition
*/
duration(operator: string, value: string|number) {
return this.predicate("du", operator, value);
}

/**
* @function Condition#initialDuration
* @param {string} operator the comparison operator (e.g. "<", "lt")
* @param {string|number} value the right hand side value
* @return {Condition} this condition
*/
initialDuration(operator: string, value: string|number) {
return this.predicate("idu", operator, value);
}
}

export default Condition;
Loading