Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions src/lib/utilities/path-matches.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,32 @@ describe(pathMatches, () => {
it('should return false if the second path is not a subdirectory of the first', () => {
expect(pathMatches('/workflows', '/queries/abc')).toBe(false);
});

it('should ignore query params if missing from the first path and the paths otherwise match', () => {
expect(pathMatches('/workflows', '/workflows?query=param')).toBe(true);
});

it('should ignore query params if missing from the second path and the paths otherwise match', () => {
expect(pathMatches('/workflows?query=param', '/workflows')).toBe(true);
});

it('should ignore query params if missing from the first path and the paths do not match', () => {
expect(pathMatches('/queries', '/workflows?query=param')).toBe(false);
});

it('should ignore query params if missing from the second path and the paths do not match', () => {
expect(pathMatches('/workflows?query=param', '/queries')).toBe(false);
});

it('should ignore query params if the paths match but the parameters do not', () => {
expect(pathMatches('/workflows?foo=bar', '/workflows?query=param')).toBe(
true,
);
});

it('should ignore query params if both the paths and parameters do not match', () => {
expect(pathMatches('/workflows?query=param', '/queries?foo=bar')).toBe(
false,
);
});
});
10 changes: 8 additions & 2 deletions src/lib/utilities/path-matches.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
const dropQueryParameters = (url: string) => {
const queryParameterIndex = url.indexOf('?');
if (queryParameterIndex > -1) return url.slice(0, queryParameterIndex);
return url;
};

export const pathMatches = (first: string, second: string): boolean => {
const firstSegments = first.split('/');
const secondSegments = second.split('/');
const firstSegments = dropQueryParameters(first).split('/');
const secondSegments = dropQueryParameters(second).split('/');

for (let index = 0; index < firstSegments.length; index++) {
const firstSegment = firstSegments[index];
Expand Down