Skip to content

Commit 5f44e0f

Browse files
Remove flag concatenation (#297)
- Add support for addFlag('foo') directly, without a flagQualifier instance - Remove support for flag concatenation addFlag('foo').addFlag('bar') -> fl_foo,fl_bar (instead of fl_foo.bar)
1 parent 84a757f commit 5f44e0f

File tree

9 files changed

+55
-24
lines changed

9 files changed

+55
-24
lines changed

__TESTS__/unit/Action/Action.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ describe('Tests for Transformation Action', () => {
7878
.addAction(action)
7979
.toURL();
8080

81-
expect(url).toBe('https://res.cloudinary.com/demo/image/upload/fl_first_flag.second_flag,l_sample/sample');
81+
expect(url).toBe('https://res.cloudinary.com/demo/image/upload/fl_first_flag,fl_second_flag,l_sample/sample');
8282
});
8383

8484
it('Correctly sorts qualifiers', () => {
@@ -89,7 +89,7 @@ describe('Tests for Transformation Action', () => {
8989
.addFlag(new FlagQualifier('b'))
9090
.addQualifier(new Qualifier('c', '3'));
9191

92-
expect(action.toString()).toBe('a_1,b_2,c_3,fl_a.b');
92+
expect(action.toString()).toBe('a_1,b_2,c_3,fl_a,fl_b');
9393
});
9494

9595
it('Add and read actions tags', () => {

__TESTS__/unit/Action/Flag.test.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11
import {FlagQualifier} from "../../../src/values/flag/FlagQualifier";
2+
import {Action} from "../../../src/internal/Action";
23

34
describe('Tests for Flag', () => {
45
it('Creates a Flag', () => {
56
const flag = new FlagQualifier("single_flag");
67
expect(flag.toString()).toBe('fl_single_flag');
78
});
8-
it('Creates a Flag with multiple values', () => {
9-
const flag = new FlagQualifier(["first_flag", "second_flag"]);
109

11-
expect(flag.toString()).toBe('fl_first_flag.second_flag');
10+
it ('Creates an action with multiple flags', () => {
11+
const str = new Action().addFlag('foo').addFlag('bar').toString();
12+
13+
expect(str).toEqual('fl_bar,fl_foo');
1214
});
1315
});

__TESTS__/unit/actions/Flag.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,6 @@ describe('Tests for Transformation Action -- Flag', () => {
100100
.setPublicID('sample')
101101
.toURL();
102102

103-
expect(url).toBe('https://res.cloudinary.com/demo/image/upload/ar_1.0,c_fill,fl_keep_iptc.keep_attribution,w_400/sample');
103+
expect(url).toBe('https://res.cloudinary.com/demo/image/upload/ar_1.0,c_fill,fl_keep_attribution,fl_keep_iptc,w_400/sample');
104104
});
105105
});

__TESTS__/unit/actions/Transcode.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ describe('Tests for Transformation Action -- Transcode', () => {
111111
.setPublicID('sample')
112112
.toURL();
113113

114-
expect(url).toBe('https://res.cloudinary.com/demo/video/upload/f_webp,fl_awebp.animated/sample');
114+
expect(url).toBe('https://res.cloudinary.com/demo/video/upload/f_webp,fl_animated,fl_awebp/sample');
115115
});
116116

117117
it('Creates a cloudinaryURL with toAnimated and delay', () => {

__TESTS__/unit/types/Position.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ describe('Position Qualifier', () => {
2121
.offsetY(10)
2222
.toString();
2323

24-
expect(posString).toBe('fl_no_overflow.tiled,g_north,x_10,y_10');
24+
expect(posString).toBe('fl_no_overflow,fl_tiled,g_north,x_10,y_10');
2525
});
2626

2727
it('Tests the toString() method of Position (FocusOn Gravity)', () => {
@@ -33,6 +33,6 @@ describe('Position Qualifier', () => {
3333
.offsetY(10)
3434
.toString();
3535

36-
expect(posString).toBe('fl_no_overflow.tiled,g_cat,x_10,y_10');
36+
expect(posString).toBe('fl_no_overflow,fl_tiled,g_cat,x_10,y_10');
3737
});
3838
});

__TESTS__/unit/utils/dataStructureUtils/sortMapByKey.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,6 @@ describe('Tests for sortMapByKey', () => {
77
map.set('a', 'a');
88
map.set('b', 'b');
99

10-
expect(mapToSortedArray(map).join(',')).toBe('a,b,c');
10+
expect(mapToSortedArray(map, []).join(',')).toBe('a,b,c');
1111
});
1212
});

src/internal/Action.ts

Lines changed: 29 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,11 @@ class Action {
1010
// We're using map, to overwrite existing keys. for example:
1111
// addParam(w_100).addQualifier(w_200) should result in w_200. and not w_100,w_200
1212
qualifiers: Map<string, Qualifier> = new Map();
13+
14+
// Unlike regular qualifiers, there can be multiple flags in each url component /fl_1,fl_2/
15+
// If the falgs are added to the qualifiers map, only a single flag could exist in a component (it's a map)
16+
// So flags are stored separately until the very end because of that reason
17+
flags: FlagQualifier[] = [];
1318
private delimiter = ','; // {qualifier}{delimiter}{qualifier} for example: `${'w_100'}${','}${'c_fill'}`
1419
protected prepareQualifiers():void {}
1520
private actionTag = ''; // A custom name tag to identify this action in the future
@@ -37,16 +42,33 @@ class Action {
3742
*/
3843
toString(): string {
3944
this.prepareQualifiers();
40-
return mapToSortedArray(this.qualifiers).join(this.delimiter);
45+
return mapToSortedArray(this.qualifiers, this.flags).join(this.delimiter);
4146
}
4247

4348
/**
4449
* @description Adds the parameter to the action.
4550
* @param {SDK.Qualifier} qualifier
4651
* @return {this}
4752
*/
48-
addQualifier(qualifier: Qualifier): this {
49-
this.qualifiers.set(qualifier.key, qualifier);
53+
addQualifier(qualifier: Qualifier | string): this {
54+
// if string, find the key and value
55+
if (typeof qualifier === 'string') {
56+
const [key, value] = qualifier.toLowerCase().split('_');
57+
58+
59+
if (key === 'fl') {
60+
// if string qualifier is a flag, store it in the flags arrays
61+
this.flags.push(new FlagQualifier(value));
62+
} else {
63+
// if the string qualifier is not a flag, create a new qualifier from it
64+
this.qualifiers.set(key, new Qualifier(key, value));
65+
}
66+
67+
} else {
68+
// if a qualifier object, insert to the qualifiers map
69+
this.qualifiers.set(qualifier.key, qualifier);
70+
}
71+
5072
return this;
5173
}
5274

@@ -55,14 +77,11 @@ class Action {
5577
* @param {Values.Flag} flag
5678
* @return {this}
5779
*/
58-
addFlag(flag: FlagQualifier): this {
59-
const existingFlag = this.qualifiers.get('fl_');
60-
flag.qualifierValue.setDelimiter('.');
61-
62-
if (existingFlag){
63-
existingFlag.addValue(flag.qualifierValue);
80+
addFlag(flag: FlagQualifier | string): this {
81+
if (typeof flag === 'string') {
82+
this.flags.push(new FlagQualifier(flag));
6483
} else {
65-
this.qualifiers.set('fl_', flag);
84+
this.flags.push(flag);
6685
}
6786

6887
return this;

src/internal/qualifier/Qualifier.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ class Qualifier {
1414
this.qualifierValue = qualifierValue;
1515
} else {
1616
this.qualifierValue = new QualifierValue();
17-
this.qualifierValue.addValue(qualifierValue).setDelimiter('.');
17+
this.qualifierValue.addValue(qualifierValue);
1818
}
1919
}
2020

src/internal/utils/dataStructureUtils.ts

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,23 @@
1+
import {FlagQualifier} from "../../values/flag/FlagQualifier";
2+
13
/**
24
* Sort a map by key
35
* @private
46
* @param map <string, unknown>
57
* @Return array of map's values sorted by key
68
*/
7-
function mapToSortedArray<T>(map: Map<string, T>): T[] {
8-
const array = Array.from(map.entries()).sort();
9+
function mapToSortedArray<T>(map: Map<string, T | FlagQualifier>, flags: FlagQualifier[]): (T | FlagQualifier)[] {
10+
const array = Array.from(map.entries());
11+
12+
// objects from the Array.from() method above are stored in array of arrays:
13+
// [[qualifierKey, QualifierObj], [qualifierKey, QualifierObj]]
14+
// Flags is an array of FlagQualifierObj
15+
// We need to convert it to the same form: [flagQualifier] => ['fl', flagQualifier]
16+
flags.forEach((flag) => {
17+
array.push(['fl', flag]); // push ['fl', flagQualifier]
18+
});
919

10-
return array.map((v) => v[1]);
20+
return array.sort().map((v) => v[1]);
1121
}
1222

1323
/**

0 commit comments

Comments
 (0)