From 95c2efb83be06055bf314610ede23979a5ee3aaf Mon Sep 17 00:00:00 2001 From: davelopez <46503462+davelopez@users.noreply.github.com> Date: Fri, 15 Jul 2022 13:22:35 +0200 Subject: [PATCH] Fix prefix duplication in `absPath` function Also enhance code documentation a bit --- client/src/utils/redirect.js | 22 ++++++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-) diff --git a/client/src/utils/redirect.js b/client/src/utils/redirect.js index a5dd4adcc18..92bd34d39c4 100644 --- a/client/src/utils/redirect.js +++ b/client/src/utils/redirect.js @@ -10,15 +10,33 @@ export function reloadPage() { window.location.reload(); } -// Prepends configured appRoot to given url const slashCleanup = /(\/)+/g; +/** + * Prepends the configured app root to given url + * @param {String} path + * @returns The relative URL path with the configured appRoot. + */ export function prependPath(path) { const root = getAppRoot(); return `${root}/${path}`.replace(slashCleanup, "/"); } +/** + * Returns the absolute URL path for this server given a relative path. + * @param {String} path + * @returns The absolute URL path. + */ export function absPath(path) { - const relativePath = prependPath(path); + const relativePath = hasRoot(path) ? path : prependPath(path); const server = window.location.origin; return `${server}/${relativePath}`.replace(slashCleanup, "/"); } + +/** + * Checks if the path already has the app root. + * @param {String} path + * @returns true if the given path starts with the app root. + */ +function hasRoot(path) { + return path.startsWith(getAppRoot()); +}