Skip to content

Commit

Permalink
Merge pull request #16245 from Olegas/fix-router-match
Browse files Browse the repository at this point in the history
Fix router matching utility in case matching with "startsWith" and searched part is inside of current url
  • Loading branch information
ndelangen authored Jun 30, 2022
2 parents f0a4b55 + b894d48 commit 3d3eba4
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 2 deletions.
6 changes: 6 additions & 0 deletions lib/router/src/utils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,12 @@ describe('getMatch', () => {

expect(output).toBe(null);
});

it('returns null match if "startsWith" part is in the middle', () => {
const output = getMatch('/foo/bar', '/bar', true);

expect(output).toBe(null);
});
});

describe('parsePath', () => {
Expand Down
12 changes: 10 additions & 2 deletions lib/router/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -148,11 +148,19 @@ type Match = { path: string };

export const getMatch = memoize(1000)(
(current: string, target: string, startsWith = true): Match | null => {
const startsWithTarget = current && startsWith && current.startsWith(target);
if (startsWith) {
const startsWithTarget = current && current.startsWith(target);
if (startsWithTarget) {
return { path: current };
}

return null;
}

const currentIsTarget = typeof target === 'string' && current === target;
const matchTarget = current && target && current.match(target);

if (startsWithTarget || currentIsTarget || matchTarget) {
if (currentIsTarget || matchTarget) {
return { path: current };
}

Expand Down

0 comments on commit 3d3eba4

Please sign in to comment.