Skip to content

Commit 0b220d6

Browse files
Change addTransformation() to accept a transformation instance as well as a string (#309)
1 parent 4ba8671 commit 0b220d6

File tree

3 files changed

+28
-4
lines changed

3 files changed

+28
-4
lines changed

__TESTS__/unit/transformation/transformation.test.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import {Transformation} from "../../../src/transformation/Transformation";
2+
import {scale} from "../../../src/actions/resize";
23

34
describe('Tests for ImageTransformation', () => {
45
let tx: Transformation = null;
@@ -15,6 +16,15 @@ describe('Tests for ImageTransformation', () => {
1516
expect(tx.toString()).toContain('w_100/w_200/w_300');
1617
});
1718

19+
it('Accepts a transforamtion instance to addTransformation', () => {
20+
const tx1 = new Transformation().resize(scale().width(100));
21+
const tx2 = new Transformation().resize(scale().width(500));
22+
23+
tx2.addTransformation(tx1);
24+
tx2.resize(scale().width(200));
25+
expect(tx2.toString()).toContain('c_scale,w_500/c_scale,w_100/c_scale,w_200');
26+
});
27+
1828
it('Throws when passing a slash to an action', () => {
1929
expect(() => {
2030
tx.addAction('w_100/w_200/w_300');

src/assets/CloudinaryTransformable.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,15 @@ class CloudinaryTransformable extends CloudinaryFile {
223223
return this;
224224
}
225225

226+
/**
227+
* @description Extend your transformation with another transformation
228+
* @param { string | SDK.Transformation } tx
229+
*/
230+
addTransformation(tx: string | Transformation): this {
231+
this.transformation.addTransformation(tx);
232+
return this;
233+
}
234+
226235
/**
227236
* @desc A proxy to {@link SDK.Transformation| Transformation} - Calls the same method contained in this.transformation
228237
* @return {string}

src/transformation/Transformation.ts

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,13 +47,18 @@ class Transformation {
4747
}
4848

4949
/**
50-
* @description Allows the injection of a raw transformation as a string into the transformation
51-
* @param {string} stringTransformation
50+
* @description Allows the injection of a raw transformation as a string into the transformation, or a Transformation instance that was previously created
51+
* @param {string | SDK.Transformation} tx
5252
* @example transformation.addTransformation('w_100/w_200/w_300');
5353
* @return {this}
5454
*/
55-
addTransformation(stringTransformation: string): this {
56-
this.actions.push(stringTransformation);
55+
addTransformation(tx: string | Transformation): this {
56+
if (tx instanceof Transformation) {
57+
// Concat the new actions into the existing actions
58+
this.actions = this.actions.concat(tx.actions);
59+
} else {
60+
this.actions.push(tx);
61+
}
5762
return this;
5863
}
5964

0 commit comments

Comments
 (0)