Skip to content

Commit

Permalink
formatApiPath / formatAssetPath - Implement smart functionality to de…
Browse files Browse the repository at this point in the history
…tect double subpath (#2777)

Today we have two functions that wrap our paths with the subpath. Since
all of our customers are hosted on a subpath, such as /eubb1001, we need
to account for this path when building API paths and asset paths.

These functions could be smarter, we could, for example detect if we
have already added the base path and ignore it if it already exists.

**This PR implements this and adds 2 capabilities:**

1. If there is list of paths that need to be joined and one is subset of
another, it is removed.
2. All duplicate paths in the list are removed
  • Loading branch information
sjaanus committed Jan 2, 2023
1 parent 340bcf1 commit d5e47ac
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 1 deletion.
2 changes: 2 additions & 0 deletions frontend/src/utils/formatPath.test.ts
Expand Up @@ -28,6 +28,7 @@ test('formatAssetPath', () => {
expect(formatAssetPath('/a', '/x')).toEqual('/x/a');
expect(formatAssetPath('/a/', '/x/')).toEqual('/x/a');
expect(formatAssetPath('a/b/', 'x/y/')).toEqual('/x/y/a/b');
expect(formatAssetPath('x/y/', 'x/y/')).toEqual('/x/y');
expect(formatAssetPath('//a//b//', '//x//y//')).toEqual('/x/y/a/b');
});

Expand All @@ -42,6 +43,7 @@ test('formatApiPath', () => {
expect(formatApiPath('/', '/')).toEqual('');
expect(formatApiPath('a', 'x')).toEqual('/x/a');
expect(formatApiPath('/a', '/x')).toEqual('/x/a');
expect(formatApiPath('/a', '/x/a')).toEqual('/x/a');
expect(formatApiPath('/a/', '/x/')).toEqual('/x/a');
expect(formatApiPath('a/b/', 'x/y/')).toEqual('/x/y/a/b');
expect(formatApiPath('//a//b//', '//x//y//')).toEqual('/x/y/a/b');
Expand Down
9 changes: 8 additions & 1 deletion frontend/src/utils/formatPath.ts
Expand Up @@ -23,7 +23,14 @@ export const parseBasePath = (value = basePathMetaTagContent()): string => {

// Join paths with a leading separator and without a trailing separator.
const joinPaths = (...paths: string[]): string => {
return ['', ...paths]
const filteredPaths = paths.filter(path => {
return !paths.some(
currentPath => currentPath !== path && currentPath.includes(path)
);
});
const uniquePaths = [...new Set(filteredPaths)];

return ['', ...uniquePaths]
.join('/')
.replace(/\/+$/g, '') // Remove trailing separators.
.replace(/\/+/g, '/'); // Collapse repeated separators.
Expand Down

0 comments on commit d5e47ac

Please sign in to comment.