From b4c7b84f923baf89245e53ab7999ee39bceb30f5 Mon Sep 17 00:00:00 2001 From: Carlos Valente Date: Sun, 19 Jan 2025 11:10:32 +0100 Subject: [PATCH] refactor: handle trailing slash in URL preset --- apps/client/src/common/utils/urlPresets.ts | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/apps/client/src/common/utils/urlPresets.ts b/apps/client/src/common/utils/urlPresets.ts index 18f17efe8..098f0391a 100644 --- a/apps/client/src/common/utils/urlPresets.ts +++ b/apps/client/src/common/utils/urlPresets.ts @@ -28,6 +28,13 @@ export const validateUrlPresetPath = (preset: string): { message: string; isVali return { isValid: true, message: 'ok' }; }; +/** + * Utility removes trailing slash from a string + */ +function removeTrailingSlash(text: string): string { + return text.replace(/\/$/, ''); +} + /** * Gets the URL to send a preset to * @param location @@ -38,7 +45,7 @@ export const getRouteFromPreset = (location: Location, data: URLPreset[], search const currentURL = location.pathname.substring(1); // we need to check if the whole url here is an alias, so we can redirect - const foundPreset = data.filter((d) => d.alias === currentURL && d.enabled)[0]; + const foundPreset = data.find((preset) => preset.alias === removeTrailingSlash(currentURL) && preset.enabled); if (foundPreset) { return generateUrlFromPreset(foundPreset); }