Skip to content

Commit aa038fd

Browse files
authored
update dpr action from/to json (#494)
1 parent d6ad9b0 commit aa038fd

File tree

7 files changed

+72
-17
lines changed

7 files changed

+72
-17
lines changed

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

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,4 @@
11
import {fromJson} from "../../../src/internal/fromJson";
2-
import {Transformation} from "../../../src";
3-
import {Delivery} from "../../../src/actions/delivery";
4-
import {Quality} from "../../../src/qualifiers/quality";
5-
import {ChromaSubSampling} from "../../../src/qualifiers/chromaSubSampling";
62

73
describe('delivery.fromJson', () => {
84
it('should generate a transformation string from colorSpace action', function () {
@@ -167,4 +163,12 @@ describe('delivery.fromJson', () => {
167163

168164
expect(transformation.toString()).toStrictEqual('q_75:420');
169165
});
166+
167+
it('dpr', () => {
168+
const transformation = fromJson([
169+
{ actionType: 'dpr', dpr: 'auto'},
170+
{ actionType: 'dpr', dpr: 2},
171+
]);
172+
expect(transformation.toString()).toStrictEqual('dpr_auto/dpr_2.0');
173+
});
170174
});

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

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,24 @@ describe('Delivery.toJson()', () => {
1717
);
1818
});
1919

20-
it('delivery.dpr', () => {
20+
it('delivery.dpr auto', () => {
2121
const transformation = new Transformation()
2222
.addAction(Delivery.dpr('auto'));
2323
expect(transformation.toJson()).toStrictEqual([
2424
{
2525
actionType: 'dpr',
26-
type: 'auto'
26+
dpr: 'auto'
27+
}
28+
]);
29+
});
30+
31+
it('delivery.dpr number', () => {
32+
const transformation = new Transformation()
33+
.addAction(Delivery.dpr(2));
34+
expect(transformation.toJson()).toStrictEqual([
35+
{
36+
actionType: 'dpr',
37+
dpr: '2.0'
2738
}
2839
]);
2940
});

src/actions/delivery.ts

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,15 @@
99
import {DeliveryFormatAction} from "./delivery/DeliveryFormatAction.js";
1010
import {DeliveryQualityAction} from "./delivery/DeliveryQualityAction.js";
1111
import {FormatQualifier} from "../qualifiers/format/FormatQualifier.js";
12-
import {toFloatAsString} from "../internal/utils/toFloatAsString.js";
1312
import {DeliveryColorSpaceFromICCAction} from "./delivery/DeliveryColorSpaceFromICCAction.js";
1413
import {DeliveryAction} from "./delivery/DeliveryAction.js";
1514
import {ColorSpaceType} from "../types/types.js";
1615
import {QualityTypes} from "../types/types.js";
1716
import {ImageFormatType, VideoFormatType} from "../types/types.js";
1817
import {DeliveryColorSpaceAction} from "./delivery/DeliveryColorSpaceAction.js";
18+
import {DeliveryDPRAction} from "./delivery/DeliveryDPRAction.js";
1919

20-
export type IDeliveryAction = DeliveryAction | DeliveryColorSpaceAction | DeliveryColorSpaceFromICCAction;
20+
export type IDeliveryAction = DeliveryAction | DeliveryColorSpaceAction | DeliveryColorSpaceFromICCAction | DeliveryDPRAction;
2121

2222
/**
2323
* @summary action
@@ -62,9 +62,8 @@ function format(format:FormatQualifier | ImageFormatType | VideoFormatType | str
6262
* dpr('2.0'),
6363
* );
6464
*/
65-
function dpr(dpr: string|number):DeliveryAction {
66-
// toFloatAsString is used to ensure 1 turns into 1.0
67-
return new DeliveryAction('dpr', toFloatAsString(dpr), 'type');
65+
function dpr(dpr: string|number):DeliveryDPRAction {
66+
return new DeliveryDPRAction(dpr);
6867
}
6968

7069
/**

src/actions/delivery/DeliveryAction.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,25 +3,27 @@ import {FormatQualifier} from "../../qualifiers/format/FormatQualifier.js";
33
import {Qualifier} from "../../internal/qualifier/Qualifier.js";
44
import {
55
IDefaultImageModel, IDeliveryColorSpaceActionModel, IDeliveryColorSpaceFromICCActionModel,
6-
IDeliveryFormatModel, IDeliveryQualityModel, IDensityModel, IDprModel
6+
IDeliveryFormatModel, IDeliveryQualityModel, IDensityModel
77
} from "../../internal/models/IDeliveryActionModel.js";
8-
import {DELIVERY_MODE_TO_ACTION_TYPE_MAP} from "../../internal/internalConstants.js";
8+
import {
9+
DELIVERY_MODE_TO_ACTION_TYPE_MAP
10+
} from "../../internal/internalConstants.js";
911

1012
/**
1113
* @description Qualifies the delivery of an asset.
1214
* @memberOf Actions.Delivery
1315
* @extends SDK.Action
1416
*/
1517
class DeliveryAction extends Action {
16-
protected _actionModel: IDeliveryColorSpaceActionModel | IDeliveryColorSpaceFromICCActionModel | IDprModel | IDensityModel | IDefaultImageModel | IDeliveryFormatModel | IDeliveryQualityModel = {};
18+
protected _actionModel: IDeliveryColorSpaceActionModel | IDeliveryColorSpaceFromICCActionModel | IDensityModel | IDefaultImageModel | IDeliveryFormatModel | IDeliveryQualityModel = {};
1719

1820
/**
1921
* @param {string} deliveryKey A generic Delivery Action Key (such as q, f, dn, etc.)
2022
* @param {string} deliveryType A Format Qualifiers for the action, such as Quality.auto()
2123
* @param {string} modelProperty internal model property of the action, for example quality uses `level` while dpr uses `density`
2224
* @see Visit {@link Actions.Delivery|Delivery} for an example
2325
*/
24-
constructor(deliveryKey?: string, deliveryType?: FormatQualifier | string|number, modelProperty?: 'level' | 'density' | 'defaultImage' | 'colorSpaceType' | 'formatType' | 'type') {
26+
constructor(deliveryKey?: string, deliveryType?: FormatQualifier | string|number, modelProperty?: 'level' | 'density' | 'defaultImage' | 'colorSpaceType' | 'formatType') {
2527
super();
2628

2729
let deliveryTypeValue;
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import {Action} from "../../internal/Action.js";
2+
import {Qualifier} from "../../internal/qualifier/Qualifier.js";
3+
import {IActionModel} from "../../internal/models/IActionModel.js";
4+
import {IDprModel} from "../../internal/models/IDeliveryActionModel.js";
5+
import {toFloatAsString} from "../../internal/utils/toFloatAsString.js";
6+
7+
/**
8+
* @description Specifies the dpr.
9+
* @memberOf Actions.Delivery
10+
* @extends SDK.Action
11+
* @see Visit {@link Actions.Delivery|Delivery} for an example
12+
*/
13+
class DeliveryDPRAction extends Action {
14+
protected _actionModel: IDprModel = { actionType: 'dpr'};
15+
16+
/**
17+
* Create a new DeliveryDPRAction
18+
* @param dprValue
19+
*/
20+
constructor(dprValue: string | number) {
21+
super();
22+
// toFloatAsString is used to ensure 1 turns into 1.0
23+
const dprAsFloat = toFloatAsString(dprValue);
24+
this._actionModel.dpr = dprAsFloat;
25+
this.addQualifier(new Qualifier('dpr', dprAsFloat));
26+
}
27+
28+
29+
static fromJson(actionModel: IActionModel): DeliveryDPRAction {
30+
const {dpr} = (actionModel as IDprModel);
31+
// We are using this() to allow inheriting classes to use super.fromJson.apply(this, [actionModel])
32+
// This allows the inheriting classes to determine the class to be created
33+
return new this(dpr);
34+
}
35+
}
36+
37+
export {DeliveryDPRAction};

src/internal/fromJson.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ import {DeshakeEffectAction} from "../actions/effect/leveled/Deshake.js";
3939
import {Pixelate} from "../actions/effect/pixelate/Pixelate.js";
4040
import {BlurAction} from "../actions/effect/blur/Blur.js";
4141
import {ImproveAction} from "../actions/adjust/ImproveAction.js";
42+
import {DeliveryDPRAction} from "../actions/delivery/DeliveryDPRAction.js";
4243

4344
const ActionModelMap: Record<string, IHasFromJson> = {
4445
scale: ResizeScaleAction,
@@ -85,7 +86,8 @@ const ActionModelMap: Record<string, IHasFromJson> = {
8586
blur: BlurAction,
8687
improve: ImproveAction,
8788
unsharpMask: EffectActionWithStrength,
88-
saturation: EffectActionWithLevel
89+
saturation: EffectActionWithLevel,
90+
dpr: DeliveryDPRAction
8991
};
9092

9193
/**

src/internal/models/IDeliveryActionModel.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ interface IDeliveryColorSpaceActionModel extends IActionModel{
77
}
88

99
interface IDprModel extends IActionModel {
10-
"type": number | string;
10+
"dpr"?: number | string;
1111
}
1212

1313
interface IDensityModel extends IActionModel {

0 commit comments

Comments
 (0)