Skip to content

Commit 52e020a

Browse files
author
Nir Maoz
authored
Feature/format from json (#470)
1 parent e25f1e4 commit 52e020a

File tree

7 files changed

+63
-12
lines changed

7 files changed

+63
-12
lines changed

__TESTS_BUNDLE_SIZE__/bundleSizeTestCases.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ const bundleSizeTestCases:ITestCase[] = [
2424
},
2525
{
2626
name: 'Tests CloudinaryImage with Resize and Adjust',
27-
sizeLimitInKB: 22,
27+
sizeLimitInKB: 23,
2828
importsArray: [
2929
importFromDist('assets/CloudinaryImage', 'CloudinaryImage'),
3030
importFromDist('instance/Cloudinary', 'Cloudinary'),

__TESTS__/unit/fromJson/delivery.fromJson.test.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,28 @@ describe('delivery.fromJson', () => {
1515

1616
expect(transformation.toString()).toStrictEqual('cs_icc:sample');
1717
});
18+
19+
it('jpg.progressive.semi()', () => {
20+
const transformation = fromJson([
21+
{
22+
progressive: { mode: 'semi' },
23+
actionType: 'format',
24+
formatType: 'jpg'
25+
}
26+
]);
27+
28+
expect(transformation.toString()).toStrictEqual('f_jpg,fl_progressive:semi');
29+
});
30+
31+
it('gif.lossy()', () => {
32+
const transformation = fromJson([
33+
{
34+
actionType: 'format',
35+
formatType: 'gif',
36+
lossy: true
37+
}
38+
]);
39+
40+
expect(transformation.toString()).toStrictEqual('f_gif,fl_lossy');
41+
});
1842
});

__TESTS__/unit/toJson/delivery.toJson.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ describe('Delivery.toJson()', () => {
6262
]);
6363
});
6464

65-
it('jpg.progressive.semi()', () => {
65+
it('jpg.progressive("semi")', () => {
6666
const transformation = new Transformation()
6767
.addAction(Delivery.format(Format.jpg()).progressive('semi'));
6868
expect(transformation.toJson()).toStrictEqual([

src/actions/delivery.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
* See the examples under every method
77
*/
88

9-
import {DeliveryFormat} from "./delivery/DeliveryFormat.js";
9+
import {DeliveryFormatAction} from "./delivery/DeliveryFormatAction.js";
1010
import {DeliveryQualityAction} from "./delivery/DeliveryQuality.js";
1111
import {FormatQualifier} from "../qualifiers/format/FormatQualifier.js";
1212
import {toFloatAsString} from "../internal/utils/toFloatAsString.js";
@@ -42,8 +42,8 @@ export type IDeliveryAction = DeliveryAction | DeliveryColorSpaceAction | Delive
4242
* );
4343
*
4444
*/
45-
function format(format:FormatQualifier | ImageFormatType | VideoFormatType | string) :DeliveryFormat {
46-
return new DeliveryFormat('f', format);
45+
function format(format:FormatQualifier | ImageFormatType | VideoFormatType | string) :DeliveryFormatAction {
46+
return new DeliveryFormatAction('f', format);
4747
}
4848

4949
/**

src/actions/delivery/DeliveryFormat.ts renamed to src/actions/delivery/DeliveryFormatAction.ts

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,15 @@ import {DeliveryAction} from "./DeliveryAction.js";
33
import {ProgressiveQualifier} from "../../qualifiers/progressive.js";
44
import {FormatQualifier} from "../../qualifiers/format/FormatQualifier.js";
55
import {ProgressiveType} from "../../types/types.js";
6+
import {IActionModel} from "../../internal/models/IActionModel.js";
7+
import {IDeliveryFormatModel} from "../../internal/models/IDeliveryActionModel.js";
68

79
/**
810
* @memberOf Actions.Delivery
911
* @extends {Actions.Delivery.DeliveryAction}
1012
* @see Visit {@link Actions.Delivery|Delivery} for an example
1113
*/
12-
class DeliveryFormat extends DeliveryAction {
14+
class DeliveryFormatAction extends DeliveryAction {
1315
constructor(deliveryKey?: string, deliveryType?: FormatQualifier|string|number) {
1416
super(deliveryKey, deliveryType, 'formatType');
1517
}
@@ -46,6 +48,30 @@ class DeliveryFormat extends DeliveryAction {
4648
this.addFlag(preserveTransparency());
4749
return this;
4850
}
51+
52+
static fromJson(actionModel: IActionModel): DeliveryFormatAction {
53+
const {formatType, lossy, progressive, preserveTransparency} = (actionModel as IDeliveryFormatModel);
54+
let result: DeliveryFormatAction;
55+
56+
if (formatType) {
57+
result = new this('f', formatType);
58+
} else{
59+
result = new this('f');
60+
}
61+
62+
if (progressive){
63+
if (progressive.mode){
64+
result.progressive(progressive.mode as unknown as ProgressiveQualifier);
65+
} else{
66+
result.progressive();
67+
}
68+
}
69+
70+
lossy && result.lossy();
71+
preserveTransparency && result.preserveTransparency();
72+
73+
return result;
74+
}
4975
}
5076

51-
export {DeliveryFormat};
77+
export {DeliveryFormatAction};

src/assets/CloudinaryTransformable.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,9 @@ import IURLConfig from "../config/interfaces/Config/IURLConfig.js";
2222
import ICloudConfig from "../config/interfaces/Config/ICloudConfig.js";
2323
import {IDeliveryAction} from "../actions/delivery.js";
2424
import {IAdjustAction} from "../actions/adjust.js";
25-
import {DeliveryQualityAction} from "../actions/delivery/DeliveryQuality.js";
2625
import {ITrackedPropertiesThroughAnalytics} from "../sdkAnalytics/interfaces/ITrackedPropertiesThroughAnalytics.js";
2726
import {AnimatedAction} from "../actions/animated.js";
28-
import {DeliveryFormat} from "../actions/delivery/DeliveryFormat.js";
27+
import {DeliveryFormatAction} from "../actions/delivery/DeliveryFormatAction.js";
2928

3029
/**
3130
* @desc Cloudinary Transformable interface, extended by any class that needs a Transformation Interface
@@ -86,7 +85,7 @@ class CloudinaryTransformable extends CloudinaryFile {
8685
* @return {this}
8786
*/
8887
quality(quality: string|number): this {
89-
this.addAction(new DeliveryFormat('q', quality));
88+
this.addAction(new DeliveryFormatAction('q', quality));
9089
return this;
9190
}
9291

@@ -96,7 +95,7 @@ class CloudinaryTransformable extends CloudinaryFile {
9695
* @return {this}
9796
*/
9897
format(format: string): this {
99-
this.addAction(new DeliveryFormat('f', format));
98+
this.addAction(new DeliveryFormatAction('f', format));
10099
return this;
101100
}
102101

src/internal/fromJson.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import {ResizeLimitPadAction} from "../actions/resize/ResizeLimitPadAction.js";
1717
import {ResizeMinimumPadAction} from "../actions/resize/ResizeMinimumPadAction.js";
1818
import {DeliveryColorSpaceAction} from "../actions/delivery/DeliveryColorSpaceAction.js";
1919
import {DeliveryColorSpaceFromICCAction} from "../actions/delivery/DeliveryColorSpaceFromICCAction.js";
20+
import {DeliveryFormatAction} from "../actions/delivery/DeliveryFormatAction.js";
2021

2122
const ActionModelMap: Record<string, IHasFromJson> = {
2223
scale: ResizeScaleAction,
@@ -31,7 +32,8 @@ const ActionModelMap: Record<string, IHasFromJson> = {
3132
limitPad: ResizeLimitPadAction,
3233
minimumPad: ResizeMinimumPadAction,
3334
colorSpace: DeliveryColorSpaceAction,
34-
colorSpaceFromICC: DeliveryColorSpaceFromICCAction
35+
colorSpaceFromICC: DeliveryColorSpaceFromICCAction,
36+
format: DeliveryFormatAction
3537
};
3638

3739
/**

0 commit comments

Comments
 (0)