Skip to content

Commit

Permalink
Updated file extension functions
Browse files Browse the repository at this point in the history
  • Loading branch information
Mqxx committed Oct 19, 2023
1 parent 7bb29d4 commit 036972e
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 9 deletions.
30 changes: 24 additions & 6 deletions src/files/get_file_extension.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
);
}
});
}
Expand Down
5 changes: 3 additions & 2 deletions src/files/get_file_extension.ts
Original file line number Diff line number Diff line change
@@ -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);
}
2 changes: 1 addition & 1 deletion src/mime_types/get_mime_type.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down

0 comments on commit 036972e

Please sign in to comment.