Skip to content

Commit d19352d

Browse files
author
Maya Shavin
committed
add: If Else condition implementation #2
1 parent 27678f9 commit d19352d

File tree

7 files changed

+166
-40
lines changed

7 files changed

+166
-40
lines changed
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
import { computeConditionExpression, computeCondition, condition } from '../lib/transformers/condition'
2+
3+
describe('Condition modifier', () => {
4+
describe('condition()', () => {
5+
it('should return empty string if nothing passed', () => {
6+
expect(condition()).toBe('')
7+
});
8+
9+
it('should return empty string if condition.if does not exist', () => {
10+
expect(condition({
11+
if: {
12+
transformations: [{ rotate: 20 }]
13+
}
14+
})).toBe('')
15+
});
16+
17+
it('should return if condition string', () => {
18+
expect(condition({
19+
if: {
20+
expression: [{
21+
characteristic: "width",
22+
operator: 'equal',
23+
value: 200
24+
}],
25+
transformations: [{ rotate: 20 }]
26+
}
27+
})).toEqual(['if_w_eq_200/a_20/if_end'])
28+
});
29+
30+
it('should return if and else condition string', () => {
31+
expect(condition({
32+
if: {
33+
expression: [{
34+
characteristic: "width",
35+
operator: 'equal',
36+
value: 200
37+
}],
38+
transformations: [{ rotate: 20 }]
39+
},
40+
else: {
41+
transformations: [{
42+
resize: {
43+
width: 120,
44+
height: 120,
45+
type: 'fill'
46+
}
47+
}]
48+
}
49+
})).toEqual(['if_w_eq_200/a_20/if_else/c_fill,w_120,h_120/if_end'])
50+
});
51+
it('should return if value expression with mapped', () => {
52+
expect(condition({
53+
if: {
54+
expression: [{
55+
characteristic: "width",
56+
operator: 'equal',
57+
value: 'shoes'
58+
}],
59+
transformations: [{ rotate: 20 }]
60+
},
61+
})).toEqual(['if_w_eq_!shoes!/a_20/if_end'])
62+
});
63+
});
64+
65+
describe('computeConditionExpression()', () => {
66+
it('should map all characteristic together', () => {
67+
expect(computeConditionExpression({
68+
characteristic: ['width', 'height'],
69+
operator: 'include',
70+
value: 'shoes'
71+
})).toBe('w:h_in_!shoes!')
72+
});
73+
});
74+
75+
describe('computeCondition()', () => {
76+
it('should map multiple conditions together', () => {
77+
expect(computeCondition({
78+
expression: [{
79+
characteristic: 'aspectRatio',
80+
operator: 'greaterThan',
81+
value: 3
82+
}, {
83+
characteristic: 'width',
84+
operator: 'greaterOrEqual',
85+
value: 300
86+
}, {
87+
characteristic: 'height',
88+
operator: 'lessThan',
89+
value: 20
90+
}],
91+
transformations: [{
92+
resize: {
93+
width: 300,
94+
height: 200,
95+
type: 'crop'
96+
}
97+
}]
98+
})).toEqual({
99+
expression: 'ar_gt_3_and_w_gte_300_and_h_lt_20',
100+
transformations: 'c_crop,w_300,h_200'
101+
})
102+
});
103+
});
104+
});

packages/url/lib/constants/condition.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
1-
export const IMAGE_CONDITIONAL_OPERATORS = {
1+
export const ConditionOperators = {
22
equal: "eq",
33
notEqual: "ne",
44
lessThan: "lt",
55
greaterThan: "gt",
66
lessOrEqual: "lte",
77
greaterOrEqual: "gte",
88
include: "in",
9-
notInclude: "nin"
9+
notInclude: "nin",
1010
} as const
1111

12-
export const IMAGE_CONDITIONAL_FIELDS = {
12+
export const ConditionalParams = {
1313
width: 'w',
1414
initialWidth: 'iw',
1515
height: 'h',
@@ -28,4 +28,4 @@ export const IMAGE_CONDITIONAL_FIELDS = {
2828
initialDensity: 'idn',
2929
illustration: 'ils',
3030
pageNames: 'pgnames' // layers in TIFF
31-
} as const
31+
} as const

packages/url/lib/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ const Transformer:TransformerType = { transform, toString: toTransformationStr }
4444
export { RESOURCE_TYPES, extractPublicId, STORAGE_TYPES, ROTATION_MODES, Transformer }
4545

4646
export type { CldOptions, TransformerOption, TransformerVideoOption, CloudConfig } from './types/CldOptions'
47-
export type { Resize, ResizeType, Border, CustomFunction, Position, Effect, Rotation, Flag, Offset, Radius, Gravity } from './types/transformation'
47+
export type { Resize, ResizeType, Border, CustomFunction, Position, Effect, Rotation, Flag, Offset, Radius, Gravity, StringValue, Variable, Expression, Condition, ConditionExpression } from './types/transformation'
4848
export type { ResourceType } from './types/ResourceType'
4949
export type { StorageType } from './types/StorageType'
5050

Lines changed: 36 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,37 @@
1-
export const condition = ():string => {
2-
// if(value = "") {
3-
// var i, ifVal, j, ref, trIf, trRest;
4-
// switch (value) {
5-
// case "else":
6-
// this.chain();
7-
// return this.param(value, "if", "if");
8-
// case "end":
9-
// this.chain();
10-
// for (i = j = ref = this.chained.length - 1; j >= 0; i = j += -1) {
11-
// ifVal = this.chained[i].getValue("if");
12-
// if (ifVal === "end") {
13-
// break;
14-
// } else if (ifVal != null) {
15-
// trIf = Transformation.new().if(ifVal);
16-
// this.chained[i].remove("if");
17-
// trRest = this.chained[i];
18-
// this.chained[i] = Transformation.new().transformation([trIf, trRest]);
19-
// if (ifVal !== "else") {
20-
// break;
21-
// }
22-
// }
23-
// }
24-
// return this.param(value, "if", "if");
25-
// case "":
26-
// return Condition.new().setParent(this);
27-
// default:
28-
// return this.param(value, "if", "if", function (value) {
29-
// return Condition.new(value).toString();
30-
// });
31-
// }
32-
return ''
1+
import transform, { toTransformationStr } from ".";
2+
import { ConditionalParams, ConditionOperators } from "../constants/condition";
3+
import { Condition, ConditionExpression, Expression } from "../types/transformation/Condition";
4+
import { toString } from "../utils";
5+
6+
export const computeCondition = (conditionObj: ConditionExpression): { expression: string, transformations: string } => {
7+
const expression = conditionObj.expression ? toString(conditionObj.expression.map(exp => computeConditionExpression(exp)), '_and_') : ''
8+
9+
const transformations = conditionObj.transformations.map(transformation => toTransformationStr(transform(transformation)))
10+
11+
return {
12+
expression,
13+
transformations: toString(transformations, '/')
14+
}
15+
}
16+
17+
export const mapCharacteristic = (expression: string) => ConditionalParams[expression] || expression
18+
19+
export const computeConditionExpression = (expression: Expression) => {
20+
const characteristic = Array.isArray(expression.characteristic) ? toString((expression.characteristic as string[]).map(mapCharacteristic)) : mapCharacteristic(expression.characteristic as string)
21+
const operator = ConditionOperators[expression.operator]
22+
const value = isNaN(expression.value as number) ? `!${expression.value}!` : expression.value
23+
24+
return toString([characteristic, operator, value], '_')
25+
}
26+
27+
export const condition = (conditionObj?: Condition):string | string[] => {
28+
if (!conditionObj || !conditionObj.if || !conditionObj.if.expression) return ''
29+
30+
const ifCondition = computeCondition(conditionObj.if)
31+
const elseCondition = conditionObj.else ? computeCondition(conditionObj.else) : null
32+
33+
const formattedIf = toString([`if_${ifCondition.expression}`, ifCondition.transformations ], '/')
34+
const formattedElse = elseCondition ? toString(['if_else', elseCondition.transformations], '/') : ''
35+
36+
return [ toString([formattedIf, formattedElse, 'if_end'], '/') ]
3337
}

packages/url/lib/transformers/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ export const transform = (options):Transformation => {
7878
return transformations.filter(Boolean)
7979
}
8080

81-
export const toTransformationStr = (transformations: Transformation) => transformations.reduce((str: string, transformation: string | string[]):string => {
81+
export const toTransformationStr = (transformations: Transformation):string => transformations.reduce<string>((str: string, transformation: string | string[]):string => {
8282
const isChained = Array.isArray(transformation)
8383
const separation = isChained ? '/' : ','
8484

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import { ConditionOperators } from '../../constants/condition'
2+
import { TransformerOption } from '../CldOptions'
3+
4+
export type Expression = {
5+
characteristic: string | string[],
6+
operator: keyof typeof ConditionOperators,
7+
value: string | number,
8+
}
9+
10+
export type ConditionExpression = {
11+
expression?: Expression[],
12+
transformations: TransformerOption[]
13+
}
14+
15+
export type Condition = {
16+
if: ConditionExpression,
17+
else?: ConditionExpression
18+
}

packages/url/lib/types/transformation/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
export { Effect } from './Effect'
2-
export { Variable } from './Variable'
2+
export { Variable, StringValue } from './Variable'
33
export { Border } from './Border'
44
export { CustomFunction } from './CustomFunc'
5-
export { } from './Condition'
5+
export { Condition, ConditionExpression, Expression } from './Condition'
66
export { Flag } from './Flag'
77
export { Gravity } from './Gravity'
88
export { Offset } from './Offset'

0 commit comments

Comments
 (0)