From b31673451189f6a0aaa76171ea8c6ae25de5a232 Mon Sep 17 00:00:00 2001 From: Mylloon Date: Wed, 18 Sep 2024 17:15:13 +0200 Subject: [PATCH] splitFilenameExtensions --- src/events/loader.ts | 6 ++---- src/utils/misc.ts | 21 ++++++++++++++------- 2 files changed, 16 insertions(+), 11 deletions(-) diff --git a/src/events/loader.ts b/src/events/loader.ts index d84e63b..1a40e39 100644 --- a/src/events/loader.ts +++ b/src/events/loader.ts @@ -1,6 +1,7 @@ import { Player, PlayerEvents, useMainPlayer } from "discord-player"; import { Client } from "discord.js"; import { readdir } from "fs/promises"; +import { splitFilenameExtensions } from "../utils/misc"; /** Load all the events */ export default async (client: Client) => { @@ -20,13 +21,10 @@ export default async (client: Client) => { ); // Remove extension - // TODO: use utils functions - const event_type_ext = event_file.split("."); - const ext = event_type_ext.pop(); + const { file: event_type, ext } = splitFilenameExtensions(event_file); if (!(ext === "js" || ext === "ts")) { throw `Unknown file in ${event_category}: ${event_file}`; } - const event_type = event_type_ext.join("."); if (event_category == "player") { const player = useMainPlayer() as Player; diff --git a/src/utils/misc.ts b/src/utils/misc.ts index 4e863f8..b89db42 100644 --- a/src/utils/misc.ts +++ b/src/utils/misc.ts @@ -28,16 +28,25 @@ export const getFilename = (path: string) => { return removeExtension(filename_with_ext); }; +/** + * Split a filename and his extension + * @param filename string of the filename + * @returns Object with filename and extension splitted + */ +export const splitFilenameExtensions = (filename: string) => { + const array = filename.split("."); + const ext = array.pop(); + + return { file: array.join("."), ext }; +}; + /** * Remove extension from a filename * @param filename string of the filename with an extension * @returns string of the filename without an extension */ export const removeExtension = (filename: string) => { - const array = filename.split("."); - array.pop(); - - return array.join("."); + return splitFilenameExtensions(filename).file; }; /** @@ -46,9 +55,7 @@ export const removeExtension = (filename: string) => { * @returns string of the extension if it exists */ export const getExtension = (filename: string) => { - const array = filename.split("."); - - return array.pop(); + return splitFilenameExtensions(filename).ext; }; /**