Skip to content

Commit

Permalink
Merge pull request #49 from cloudinary/rotate-flip-json-conversion
Browse files Browse the repository at this point in the history
Add json conversion for rotate by mode
  • Loading branch information
mckomo-cl authored Mar 6, 2024
2 parents 36f0fe1 + 3181f5c commit 2aff70c
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 12 deletions.
21 changes: 13 additions & 8 deletions __TESTS__/unit/fromJson/rotate.fromJson.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,20 @@ import {fromJson} from "../../../src/internal/fromJson";

describe('rotate.fromJson', () => {
it('should generate a url with rotate actions from array of models', function () {
const transformation = fromJson({actions:[
{
actionType: 'rotateByAngle',
angle: 4
}
]}
);
const transformation = fromJson({
actions: [
{
actionType: 'rotateByAngle',
angle: 4
},
{
actionType: 'rotateByMode',
mode: 'vflip'
}
]
});
expect(transformation.toString()).toStrictEqual(
'a_4'
'a_4/a_vflip'
);
});
});
Expand Down
12 changes: 12 additions & 0 deletions __TESTS__/unit/toJson/rotate.toJson.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,16 @@ describe('Rotate toJson()', () => {
]
});
});
it('rotateByMode', () => {
const transformation = new Transformation()
.addAction(Rotate.mode('hflip'));
expect(transformation.toJson()).toStrictEqual({
actions: [
{
actionType: 'rotateByMode',
mode: 'hflip'
}
]
});
});
});
9 changes: 6 additions & 3 deletions src/actions/rotate/RotateAction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@ class RotateAction extends Action {
constructor(angle?: number) {
super();
this.addQualifier(new Qualifier(QUALIFIER_KEY, angle));
this._actionModel.actionType = 'rotateByAngle';

if (angle) {
this._actionModel.actionType = 'rotateByAngle';
this._actionModel.angle = angle;
}
}
Expand All @@ -37,6 +37,8 @@ class RotateAction extends Action {
* @return {this}
*/
mode(rotationMode: RotationModeQualifierValue | RotationModeType | string):this {
this._actionModel.actionType = 'rotateByMode';
this._actionModel.mode = rotationMode;
return this.addValueToQualifier(QUALIFIER_KEY, rotationMode);
}

Expand All @@ -46,16 +48,17 @@ class RotateAction extends Action {
* @return {this}
*/
angle(degrees: number): this {
this._actionModel.actionType = 'rotateByAngle';
this._actionModel.angle = degrees;
return this.addValueToQualifier(QUALIFIER_KEY, degrees);
}

static fromJson(actionModel: IActionModel): RotateAction {
const {angle} = (actionModel as IRotateByAngleActionModel);
const {angle, mode} = (actionModel as IRotateByAngleActionModel);

// We are using this() to allow inheriting classes to use super.fromJson.apply(this, [actionModel])
// This allows the inheriting classes to determine the class to be created
const result = new this(angle);
const result = mode ? new this().mode(mode) : new this(angle);
return result;
}
}
Expand Down
1 change: 1 addition & 0 deletions src/internal/fromJson.ts
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,7 @@ const ActionModelMap: Record<string, IHasFromJson> = {
videoCodec: VideoCodecAction,
ifCondition: ConditionalAction,
rotateByAngle: RotateAction,
rotateByMode: RotateAction,
backgroundRemoval: BackgroundRemoval,
dropshadow: DropShadow,
roundCorners: RoundCornersAction,
Expand Down
5 changes: 4 additions & 1 deletion src/internal/models/IRotateActionModel.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
import {IActionModel} from "./IActionModel.js";
import {RotationModeType} from "../../types/types.js";
import {RotationModeQualifierValue} from "../../qualifiers/rotate/RotationModeQualifierValue.js";

interface IRotateByAngleActionModel extends IActionModel {
angle?: number
angle?: number;
mode?: RotationModeQualifierValue | RotationModeType | string;
}

export {IRotateByAngleActionModel};

0 comments on commit 2aff70c

Please sign in to comment.