From 1336b0c750788ee15199b5d89c701be347b651a2 Mon Sep 17 00:00:00 2001 From: Shivam Mishra Date: Mon, 3 Jun 2024 23:07:19 +0530 Subject: [PATCH] refactor: separate sanitizePaths fn --- app/javascript/shared/helpers/joinUrl.js | 20 ++++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) diff --git a/app/javascript/shared/helpers/joinUrl.js b/app/javascript/shared/helpers/joinUrl.js index ea7e80a65..ef512e34c 100644 --- a/app/javascript/shared/helpers/joinUrl.js +++ b/app/javascript/shared/helpers/joinUrl.js @@ -1,3 +1,14 @@ +const sanitizePaths = paths => { + return paths.filter(path => { + if (!path) return false; + if (path === null && path === '') return false; + // if path is just a sequence of slashes + if (/^\/+$/.test(path)) return false; + + return true; + }); +}; + /** * Join multiple paths together with a base URL * NOTE: This function is not designed to handle query strings or fragments @@ -9,14 +20,7 @@ export function joinUrl(baseUrl, ...paths) { // remove empty undefined and null path items // also handle if the path is just a slash or just multiple slashes only - const sanitizedPaths = paths.filter(path => { - if (!path) return false; - if (path === null && path === '') return false; - // if path is just a sequence of slashes - if (/^\/+$/.test(path)) return false; - - return true; - }); + const sanitizedPaths = sanitizePaths(paths); const fullUrl = sanitizedPaths.reduce( (acc, path) => {