From a7967040f4a25ec1b43859c036be9906f63f9c97 Mon Sep 17 00:00:00 2001 From: Patrik Oldsberg Date: Sun, 7 Mar 2021 23:16:11 +0100 Subject: [PATCH] core-api: simplify joinPaths Signed-off-by: Patrik Oldsberg --- .../core-api/src/routing/RouteResolver.ts | 20 ++++--------------- 1 file changed, 4 insertions(+), 16 deletions(-) diff --git a/packages/core-api/src/routing/RouteResolver.ts b/packages/core-api/src/routing/RouteResolver.ts index e22b6259be..fb44b8c486 100644 --- a/packages/core-api/src/routing/RouteResolver.ts +++ b/packages/core-api/src/routing/RouteResolver.ts @@ -31,23 +31,11 @@ import { isExternalRouteRef } from './ExternalRouteRef'; // Joins a list of paths together, avoiding trailing and duplicate slashes function joinPaths(...paths: string[]): string { - const joined = paths - .map(path => { - let ret = path; - if (path.endsWith('/')) { - ret = path.slice(0, -1); - } - if (!path.startsWith('/')) { - ret = `/${ret}`; - } - return ret; - }) - .join(''); - - if (joined !== '/' && joined.endsWith('/')) { - return joined.slice(0, -1); + const normalized = paths.join('/').replace(/\/\/+/g, '/'); + if (normalized !== '/' && normalized.endsWith('/')) { + return normalized.slice(0, -1); } - return joined; + return normalized; } /**