Skip to content

Commit 95d954e

Browse files
committed
refactor: enhance transformation string construction and add tests for transformation behavior
1 parent 63b01cd commit 95d954e

File tree

2 files changed

+108
-5
lines changed

2 files changed

+108
-5
lines changed

src/url/builder.ts

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -92,9 +92,19 @@ function constructTransformationString(transformation: Transformation[] | undefi
9292
transformKey = key;
9393
}
9494

95-
if (value === "-") value = true;
96-
97-
if (["e-grayscale", "e-contrast", "e-removedotbg", "e-bgremove", "e-upscale", "e-retouch", "e-genvar"].includes(transformKey) && value === true) {
95+
if (
96+
["e-grayscale", "e-contrast", "e-removedotbg", "e-bgremove", "e-upscale", "e-retouch", "e-genvar"].includes(transformKey)
97+
) {
98+
if (value === true || value === "-" || value === "true") {
99+
parsedTransformStep.push(transformKey);
100+
} else {
101+
// Any other value means that the effect should be applied
102+
continue;
103+
}
104+
} else if (
105+
["e-sharpen", "e-shadow", "e-gradient", "e-usm", "e-dropshadow"].includes(transformKey) &&
106+
value.toString().trim() === ""
107+
) {
98108
parsedTransformStep.push(transformKey);
99109
} else if (key === "raw") {
100110
parsedTransformStep.push(transformation[i][key]);

test/url-generation.js

Lines changed: 95 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ describe("URL generation", function () {
4949
});
5050

5151
it('should generate the url without sdk-version', function () {
52-
const ik = new ImageKit({...initializationParams, sdkVersion: ""})
52+
const ik = new ImageKit({ ...initializationParams, sdkVersion: "" })
5353

5454
const url = ik.url({
5555
path: "/test_path.jpg",
@@ -231,7 +231,7 @@ describe("URL generation", function () {
231231

232232
expect(url).equal(`https://ik.imagekit.io/test_url_endpoint/tr:-/test_path.jpg`);
233233
});
234-
234+
235235
/**
236236
* Provided to provide support to a new transform without sdk update
237237
*/
@@ -281,6 +281,99 @@ describe("URL generation", function () {
281281
expect(url).equal(`https://ik.imagekit.io/test_url_endpoint/tr:di-test_path.jpg/test_path1.jpg`);
282282
});
283283

284+
it('skip transformation if it is false', function () {
285+
const url = imagekit.url({
286+
path: "/test_path1.jpg",
287+
transformation: [{
288+
defaultImage: "/test_path.jpg",
289+
effectContrast: false
290+
}]
291+
})
292+
293+
expect(url).equal(`https://ik.imagekit.io/test_url_endpoint/tr:di-test_path.jpg/test_path1.jpg`);
294+
});
295+
296+
it('include just key if value is empty string', function () {
297+
const url = imagekit.url({
298+
path: "/test_path1.jpg",
299+
transformation: [{
300+
defaultImage: "/test_path.jpg",
301+
shadow: ""
302+
}]
303+
})
304+
305+
expect(url).equal(`https://ik.imagekit.io/test_url_endpoint/tr:di-test_path.jpg,e-shadow/test_path1.jpg`);
306+
});
307+
308+
it('include value if set', function () {
309+
const url = imagekit.url({
310+
path: "/test_path1.jpg",
311+
transformation: [{
312+
defaultImage: "/test_path.jpg",
313+
shadow: "bl-15_st-40_x-10_y-N5"
314+
}]
315+
})
316+
317+
expect(url).equal(`https://ik.imagekit.io/test_url_endpoint/tr:di-test_path.jpg,e-shadow-bl-15_st-40_x-10_y-N5/test_path1.jpg`);
318+
});
319+
320+
it('trim with true as boolean', function () {
321+
const url = imagekit.url({
322+
path: "/test_path1.jpg",
323+
transformation: [{
324+
defaultImage: "/test_path.jpg",
325+
trim: true
326+
}]
327+
})
328+
329+
expect(url).equal(`https://ik.imagekit.io/test_url_endpoint/tr:di-test_path.jpg,t-true/test_path1.jpg`);
330+
});
331+
332+
it('trim with true as string', function () {
333+
const url = imagekit.url({
334+
path: "/test_path1.jpg",
335+
transformation: [{
336+
defaultImage: "/test_path.jpg",
337+
trim: "true"
338+
}]
339+
})
340+
341+
expect(url).equal(`https://ik.imagekit.io/test_url_endpoint/tr:di-test_path.jpg,t-true/test_path1.jpg`);
342+
});
343+
344+
it('ai remove background', function () {
345+
const url = imagekit.url({
346+
path: "/test_path1.jpg",
347+
transformation: [{
348+
aiRemoveBackground: true
349+
}]
350+
})
351+
352+
expect(url).equal(`https://ik.imagekit.io/test_url_endpoint/tr:e-bgremove/test_path1.jpg`);
353+
});
354+
355+
it('ai remove background true as string', function () {
356+
const url = imagekit.url({
357+
path: "/test_path1.jpg",
358+
transformation: [{
359+
aiRemoveBackground: "true"
360+
}]
361+
})
362+
363+
expect(url).equal(`https://ik.imagekit.io/test_url_endpoint/tr:e-bgremove/test_path1.jpg`);
364+
});
365+
366+
it('ai remove background other than true', function () {
367+
const url = imagekit.url({
368+
path: "/test_path1.jpg",
369+
transformation: [{
370+
aiRemoveBackground: "false"
371+
}]
372+
})
373+
374+
expect(url).equal(`https://ik.imagekit.io/test_url_endpoint/test_path1.jpg`);
375+
});
376+
284377
it('All combined', function () {
285378
const url = imagekit.url({
286379
path: "/test_path.jpg",

0 commit comments

Comments
 (0)