diff --git a/src/files/get_file_extension.test.ts b/src/files/get_file_extension.test.ts index 822e412..d4f347c 100644 --- a/src/files/get_file_extension.test.ts +++ b/src/files/get_file_extension.test.ts @@ -5,21 +5,39 @@ Deno.test( 'Get the file extension from a specific path.', async (test) => { await test.step({ - name: 'txt', + name: 'Simple path with extension: txt', fn: () => { - assertEquals(getFileExtension('path/to/file.txt'), 'txt'); + assertEquals( + getFileExtension('path/to/file.txt'), + 'txt' + ); } }); await test.step({ - name: 'tar.gz (gz)', + name: 'Simple path with extension: tar.gz (gz)', fn: () => { - assertEquals(getFileExtension('path/to/file.tar.gz'), 'gz'); + assertEquals( + getFileExtension('path/to/file.tar.gz'), + 'gz' + ); } }); await test.step({ - name: 'tar.gz (gz)', + name: 'Complex path with special characters and extension: tar.gz (gz)', fn: () => { - assertEquals(getFileExtension('pa-th/t.o/file/wi.th/char$acte4rs.gz'), 'gz'); + assertEquals( + getFileExtension('pa-th/t.o/file/wi.th/char$acte4rs.gz'), + 'gz' + ); + } + }); + await test.step({ + name: 'Path without file extension.', + fn: () => { + assertEquals( + getFileExtension('path/without/file/'), + undefined + ); } }); } diff --git a/src/files/get_file_extension.ts b/src/files/get_file_extension.ts index 52e7921..1a1a205 100644 --- a/src/files/get_file_extension.ts +++ b/src/files/get_file_extension.ts @@ -1,8 +1,9 @@ /** * This function takes in a path to a file and returns the file extension (without the `.`). + * * @param path The path to the file - * @returns The file extension + * @returns The file extension or undefined if no file extension was found */ -export function getFileExtension(path : string) : string { +export function getFileExtension(path : string) : string | undefined { return path.replace(/.+\.([^/.]+$)/, (_, extension) => extension); } diff --git a/src/mime_types/get_mime_type.test.ts b/src/mime_types/get_mime_type.test.ts index c903994..ae30cef 100644 --- a/src/mime_types/get_mime_type.test.ts +++ b/src/mime_types/get_mime_type.test.ts @@ -5,7 +5,7 @@ Deno.test( 'Get MIME type for specific file extension.', async (test) => { const defaultMIMETypes = { - default: 'text/html', + default: 'text/plain', types: { 'text/html': [ 'html'