From 6407548c096bc8f1dd7d20c0518ee8106638127e Mon Sep 17 00:00:00 2001 From: Mqxx <62719703+Mqxx@users.noreply.github.com> Date: Fri, 20 Oct 2023 11:10:34 +0200 Subject: [PATCH] Updated get file extension logic --- src/files/get_file_extension.test.ts | 11 ++++++++++- src/files/get_file_extension.ts | 13 ++++++++++--- 2 files changed, 20 insertions(+), 4 deletions(-) diff --git a/src/files/get_file_extension.test.ts b/src/files/get_file_extension.test.ts index d4f347c..255487f 100644 --- a/src/files/get_file_extension.test.ts +++ b/src/files/get_file_extension.test.ts @@ -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 ); } }); diff --git a/src/files/get_file_extension.ts b/src/files/get_file_extension.ts index 1a1a205..6aec795 100644 --- a/src/files/get_file_extension.ts +++ b/src/files/get_file_extension.ts @@ -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] }