Skip to content

Commit

Permalink
reworked based on code review
Browse files Browse the repository at this point in the history
  • Loading branch information
c00 committed Oct 3, 2023
1 parent 1366ff7 commit 19eee79
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 35 deletions.
20 changes: 8 additions & 12 deletions src/param-builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ export type BuildOptions = {
/**
* Whether to append the path in plain.
*
* Defaults to false. If true, encodes the path to a base64url
* Defaults to false. If false, encodes the path to a base64url
*/
plain?: boolean;

Expand Down Expand Up @@ -167,7 +167,10 @@ class ParamBuilder {
if (path && plain) mods.push('plain', path);
else mods.push(encodeFilePath(path));

const extension = options?.addExtension ? this.getExtention(path) : '';
let extension = '';
if (!plain && options?.addExtension) {
extension = this.getExtension();
}

const res = mods.join('/') + extension;

Expand All @@ -191,18 +194,11 @@ class ParamBuilder {
* @param path The path to the target image, e.g. `https://example.com/foo.png`
* @returns An extension if found, or an empty string
*/
private getExtention(path: string): string {
let extension = '';

private getExtension(): string {
const formatString = this.modifiers.get('format');
if (formatString) {
extension = `.${formatString.split(':').pop()}`;
} else {
const parts = path.split('.');
if (parts.length) extension = `.${parts.pop()}`;
}
if (!formatString) return '';

return extension;
return `.${formatString.split(':').pop()}`;
}

/**
Expand Down
36 changes: 13 additions & 23 deletions test/chain.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,19 +58,6 @@ describe('Chain', () => {
);
});

test('Add Extension from path', () => {
expect(
pb().build({
path: 'test.png',
addExtension: true,
signature: {
key: '73757065722d7365637265742d6b6579', // super-secret-key
salt: '73757065722d7365637265742d73616c74', // super-secret-salt
},
}),
).toEqual('/zxikHGnHjfY5KF1eksnwFQvqNtQa0bAqDRDJBwPoMNY/dGVzdC5wbmc.png');
});

test('Add Extension from format', () => {
expect(
pb()
Expand All @@ -88,18 +75,21 @@ describe('Chain', () => {
);
});

test('Add Extension fallback', () => {
test('Do not Add Extension if plain', () => {
expect(
pb().build({
path: 'testwithoutextension',
addExtension: true,
signature: {
key: '73757065722d7365637265742d6b6579', // super-secret-key
salt: '73757065722d7365637265742d73616c74', // super-secret-salt
},
}),
pb()
.format('webp')
.build({
path: 'test.png',
addExtension: true,
plain: true,
signature: {
key: '73757065722d7365637265742d6b6579', // super-secret-key
salt: '73757065722d7365637265742d73616c74', // super-secret-salt
},
}),
).toEqual(
'/NZqsjnAakEL_WYW3m0jRJmfzbVojINSNJD_ygU9VzFY/dGVzdHdpdGhvdXRleHRlbnNpb24.testwithoutextension',
'/pJaBmZSqwN__JanbIGi_nT57o0sA4YPkhkfn-SKZTEk/f:webp/plain/test.png',
);
});
});

0 comments on commit 19eee79

Please sign in to comment.