Skip to content

Commit

Permalink
Updated get file extension logic
Browse files Browse the repository at this point in the history
  • Loading branch information
Mqxx committed Oct 20, 2023
1 parent 078ad2a commit 6407548
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 4 deletions.
11 changes: 10 additions & 1 deletion src/files/get_file_extension.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,19 @@ Deno.test(
});
await test.step({
name: 'Path without file extension.',
fn: () => {
assertEquals(
getFileExtension('path/without/file'),
null
);
}
});
await test.step({
name: 'Path without file extension, but trailing slash (/).',
fn: () => {
assertEquals(
getFileExtension('path/without/file/'),
undefined
null
);
}
});
Expand Down
13 changes: 10 additions & 3 deletions src/files/get_file_extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,15 @@
* 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 or undefined if no file extension was found
* @returns The file extension or null if no file extension was found
*/
export function getFileExtension(path : string) : string | undefined {
return path.replace(/.+\.([^/.]+$)/, (_, extension) => extension);
export function getFileExtension(path : string) : string | null {
const matchFileExtension = /\.([^/.]+$)/;
const fileExtension = path.match(matchFileExtension)

if (fileExtension === null) {
return null
}

return fileExtension[1]
}

0 comments on commit 6407548

Please sign in to comment.