Skip to content

Commit 3099c62

Browse files
authored
Add condiotional Action from/to Json (#524)
1 parent 8612e37 commit 3099c62

File tree

5 files changed

+107
-1
lines changed

5 files changed

+107
-1
lines changed
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import {fromJson} from "../../../src/internal/fromJson";
2+
import {Transformation} from "../../../src";
3+
import {Resize} from "../../../src/actions/resize";
4+
5+
describe('condition.fromJson', () => {
6+
it('should generate a transformation string from condition action', function () {
7+
const widthTx = new Transformation().addAction('w_100');
8+
const transformation = fromJson({
9+
actions: [
10+
{
11+
actionType: 'ifCondition',
12+
expression: 'ar >= 1.0',
13+
transformation: widthTx
14+
}
15+
]
16+
});
17+
18+
expect(transformation.toString()).toStrictEqual('if_ar_gte_1.0/w_100/if_end');
19+
});
20+
21+
it('should generate a transformation string from condition action with otherwise', function () {
22+
const widthTx = new Transformation().addAction('w_100');
23+
const otherwiseTx = new Transformation().resize(Resize.scale(50));
24+
const transformation = fromJson({
25+
actions: [
26+
{
27+
actionType: 'ifCondition',
28+
expression: 'ar >= 1.0',
29+
transformation: widthTx,
30+
otherwise: otherwiseTx
31+
}
32+
]
33+
});
34+
35+
expect(transformation.toString()).toStrictEqual('if_ar_gte_1.0/w_100/if_else/c_scale,w_50/if_end');
36+
});
37+
});
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
import {Conditional} from "../../../src/actions/conditional";
2+
import {Transformation} from "../../../src";
3+
import {Resize} from "../../../src/actions/resize";
4+
5+
6+
describe('Conditional.toJson', ()=>{
7+
it('Conditional with transformation', () => {
8+
const conditionalTx = new Transformation().addAction(
9+
Conditional.ifCondition('ar >= 1.0', new Transformation().addAction('w_100')));
10+
const transformation = new Transformation().addAction('w_100');
11+
expect(conditionalTx.toJson()).toStrictEqual({
12+
actions: [
13+
{
14+
actionType: 'ifCondition',
15+
expression: 'ar >= 1.0',
16+
transformation: transformation
17+
}
18+
]
19+
});
20+
});
21+
22+
it('Conditional with if/else combination', () => {
23+
const conditionalTx = new Transformation().addAction(
24+
Conditional.ifCondition('ar >= 1.0', new Transformation().resize(Resize.scale(50)))
25+
.otherwise(new Transformation().resize(Resize.scale(200))));
26+
const transformation = new Transformation().resize(Resize.scale(50));
27+
const otherwise = new Transformation().resize(Resize.scale(200));
28+
29+
expect(conditionalTx.toJson()).toStrictEqual({
30+
actions: [
31+
{
32+
actionType: 'ifCondition',
33+
expression: 'ar >= 1.0',
34+
transformation: transformation,
35+
otherwise: otherwise
36+
}
37+
]
38+
});
39+
});
40+
});

src/actions/conditional.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import {Action} from "../internal/Action.js";
22
import {expression} from "../qualifiers/expression.js";
33
import {Transformation} from "../transformation/Transformation.js";
4+
import {IConditionalActionModel} from "../internal/models/IConditionalActionModel.js";
5+
import {IActionModel} from "../internal/models/IActionModel.js";
46

57
/**
68
* Sets up a conditional transformation.
@@ -35,6 +37,7 @@ import {Transformation} from "../transformation/Transformation.js";
3537
* // Transformation will contain `if_ar_gte_1.0/w_100/if_end`
3638
*/
3739
class ConditionalAction extends Action{
40+
protected _actionModel: IConditionalActionModel = {actionType: "ifCondition"};
3841
private ifTx: Transformation;
3942
private elseTx: Transformation;
4043
private exp: string;
@@ -48,6 +51,8 @@ class ConditionalAction extends Action{
4851
super();
4952
this.exp = exp;
5053
this.ifTx = ifTx;
54+
this._actionModel.expression = exp;
55+
this._actionModel.transformation = ifTx;
5156
}
5257

5358
/**
@@ -57,6 +62,7 @@ class ConditionalAction extends Action{
5762
*/
5863
otherwise(elseTx: Transformation): this {
5964
this.elseTx = elseTx;
65+
this._actionModel.otherwise = elseTx;
6066
return this;
6167
}
6268

@@ -68,6 +74,17 @@ class ConditionalAction extends Action{
6874
`if_end`
6975
].filter((a) => a).join('/');
7076
}
77+
78+
static fromJson(actionModel: IActionModel): ConditionalAction {
79+
const {expression, transformation, otherwise} = (actionModel as IConditionalActionModel);
80+
81+
// We are using this() to allow inheriting classes to use super.fromJson.apply(this, [actionModel])
82+
// This allows the inheriting classes to determine the class to be created
83+
const result = new this(expression, transformation);
84+
otherwise && result.otherwise(otherwise);
85+
86+
return result;
87+
}
7188
}
7289

7390

src/internal/fromJson.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ import ToAnimatedAction from "../actions/transcode/ToAnimatedAction.js";
5656
import {FadeInEffectAction} from "../actions/effect/leveled/FadeIn.js";
5757
import {FadeOutEffectAction} from "../actions/effect/leveled/FadeOut.js";
5858
import {VideoCodecAction} from "../actions/transcode/VideoCodecAction.js";
59+
import {ConditionalAction} from "../actions/conditional.js";
5960

6061
const ActionModelMap: Record<string, IHasFromJson> = {
6162
scale: ResizeScaleAction,
@@ -122,7 +123,8 @@ const ActionModelMap: Record<string, IHasFromJson> = {
122123
toAnimated: ToAnimatedAction,
123124
fadeIn: FadeInEffectAction,
124125
fadeOut: FadeOutEffectAction,
125-
videoCodec: VideoCodecAction
126+
videoCodec: VideoCodecAction,
127+
ifCondition: ConditionalAction
126128
};
127129

128130
/**
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import {IActionModel} from "./IActionModel.js";
2+
import {Transformation} from "../../transformation/Transformation.js";
3+
4+
interface IConditionalActionModel extends IActionModel {
5+
expression?: string;
6+
transformation?: Transformation;
7+
otherwise?: Transformation;
8+
}
9+
10+
export {IConditionalActionModel};

0 commit comments

Comments
 (0)