feat: add joinURL

This commit is contained in:
Shivam Mishra
2024-05-30 13:42:41 +05:30
parent bece33792e
commit bd7e1fba3f
2 changed files with 89 additions and 0 deletions
+30
View File
@@ -0,0 +1,30 @@
/**
* Join multiple paths together with a base URL
* NOTE: This function is not designed to handle query strings or fragments
*
* @param {string} baseUrl - The base URL to join the paths to
* @param {...string} paths - The paths to join
* @returns {string} - The full URL
*/
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 fullUrl = sanitizedPaths.reduce(
(acc, path) => {
const sanitizedPath = path.replace(/^\/+|\/+$/g, ''); // Remove leading and trailing slashes from each path segment
return `${acc}/${sanitizedPath}`; // Concatenate with a single slash
},
baseUrl.replace(/\/+$/, '')
);
return fullUrl;
}