From 4f7d1a67cf1a3048d89bc6396f6cffb58d6216f8 Mon Sep 17 00:00:00 2001 From: Shivam Mishra Date: Wed, 5 Jun 2024 09:09:52 +0530 Subject: [PATCH] refactor: move filter fn as separate isValidPath --- app/javascript/shared/helpers/joinUrl.js | 16 +++++++--------- 1 file changed, 7 insertions(+), 9 deletions(-) diff --git a/app/javascript/shared/helpers/joinUrl.js b/app/javascript/shared/helpers/joinUrl.js index ef512e34c..80426efc2 100644 --- a/app/javascript/shared/helpers/joinUrl.js +++ b/app/javascript/shared/helpers/joinUrl.js @@ -1,12 +1,10 @@ -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; +const isValidPath = 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; - }); + return true; }; /** @@ -20,7 +18,7 @@ const sanitizePaths = paths => { 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 = sanitizePaths(paths); + const sanitizedPaths = paths.filter(isValidPath); const fullUrl = sanitizedPaths.reduce( (acc, path) => {