Skip to content

Commit

Permalink
fix(url-utils): joinRoutes now accounts for routes that normalize to …
Browse files Browse the repository at this point in the history
…an empty string

joinRoutes now accounts for routes that normalize to an empty string

COMUI-1131
  • Loading branch information
daragh-king-genesys committed Aug 8, 2022
1 parent ab6e45b commit 3df97a5
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 1 deletion.
28 changes: 28 additions & 0 deletions src/specs/urlUtils.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import {
joinRoutes,
normalizeRoute,
stripLeadingSlash,
stripLeadingSlashAndHashTag,
stripTrailingSlash
} from '../urlUtils';

describe('urlUtils', () => {
describe('joinRoutes', () => {
[
{ input: ['', '', ''], expectedOutput: '' },
{ input: ['test', 'test', 'test'], expectedOutput: 'test/test/test' },
{
input: ['/test/', '/test/', '/test/'],
expectedOutput: 'test/test/test'
},
{ input: ['test', '', 'test'], expectedOutput: 'test/test' },
{ input: ['/test/', '', '/test/'], expectedOutput: 'test/test' },
{ input: ['test', '/', 'test'], expectedOutput: 'test/test' },
{ input: ['/test/', '/', '/test/'], expectedOutput: 'test/test' }
].forEach(({ input, expectedOutput }, index) => {
it(`should work as expected (${index + 1})`, () => {
expect(joinRoutes(...input)).toBe(expectedOutput);
});
});
});
});
12 changes: 11 additions & 1 deletion src/urlUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,5 +36,15 @@ export function stripLeadingSlashAndHashTag(str: string): string {
*
*/
export function joinRoutes(...routes: string[]): string {
return routes.map(route => normalizeRoute(route)).join('/');
return routes
.reduce((acc, route) => {
const normalizedRoute = normalizeRoute(route);

if (normalizedRoute) {
return acc.concat([normalizedRoute]);
}

return acc;
}, [] as string[])
.join('/');
}

0 comments on commit 3df97a5

Please sign in to comment.