Skip to content

Commit c1144e2

Browse files
committed
Combine filter and map with reduce
1 parent 04add0c commit c1144e2

File tree

1 file changed

+4
-13
lines changed

1 file changed

+4
-13
lines changed

packages/react/src/reactrouterv3.ts

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -150,17 +150,8 @@ function getRouteStringFromRoutes(routes: Route[]): string {
150150
}
151151
}
152152

153-
const pathParts = routesWithPaths
154-
.slice(index)
155-
.filter(({ path }) => !!path)
156-
.map(({ path }) => path);
157-
158-
// Join all parts with '/', then replace multiple slashes with a single one.
159-
let fullPath = pathParts.join('/');
160-
fullPath = fullPath.replace(/\/+/g, '/');
161-
162-
// Edge case: If the path started with multiple slashes and routes,
163-
// like `//foo`, it might become `/foo`. This is generally acceptable.
164-
165-
return fullPath;
153+
return routesWithPaths.slice(index).reduce((acc, { path }) => {
154+
const pathSegment = acc === '/' || acc === '' ? path : `/${path}`;
155+
return `${acc}${pathSegment}`;
156+
}, '');
166157
}

0 commit comments

Comments
 (0)