Last active
November 15, 2024 05:51
-
-
Save tomfa/f925366cd036bb0d4af5bbd8397c84ae to your computer and use it in GitHub Desktop.
NextJS route vs pathname matcher
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
describe('matchesPath', () => { | |
const matches = [ | |
['/cake', '/cake'], | |
['/cake', '/cake/'], | |
['/cake', '/cake?frige=warm'], | |
['/cake', '/cake?frige=warm&freezer=cold'], | |
['/[id]', '/cake'], | |
['/[anything-goes]', '/cake'], | |
['/c/[id]/practitioner/[pid]/[anything-goes]', '/c/1/practitioner/2/3'], | |
['/[...rest]', '/cake'], | |
['/[...rest]', '/cake/fake/snake?shake=true'], | |
['/shop/[[...rest]]', '/shop'], | |
['/shop/[[...rest]]', '/shop/'], | |
['/shop/[[...rest]]', '/shop/snake'], | |
['/[...rest]/fake/snake', '/cake/fake/snake?shake=true'], | |
[ | |
'/welcome', | |
'/welcome/?verifier=z3vvsSm', | |
], | |
] as Array<[string, string]>; | |
const nonMatches = [ | |
['/stake', '/snake'], | |
['/cake', '/cake/subpath-not-ok'], | |
['/cake/[oh-whats-this]', '/cake/'], | |
['/[...rest]/nope/snake', '/cake/fake/snake?shake=true'], | |
['/[...rest]', '/'], | |
] as Array<[string, string]>; | |
matches.forEach(([path, asPath]) => { | |
it(`matches ${asPath} to ${path}`, () => { | |
expect(matchesPathname(asPath, path)).toEqual(true); | |
}); | |
}); | |
nonMatches.forEach(([path, asPath]) => { | |
it(`does not match ${asPath} to ${path}`, () => { | |
expect(matchesPathname(asPath, path)).toEqual(false); | |
}); | |
}); | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export const removeTrailingSlash = (val: string) => | |
val.endsWith('/') ? val.substring(0, val.length - 1) : val; | |
export const matchesPathname = (asPath: string, pathname: string) => { | |
if (asPath === pathname) { | |
return true; | |
} | |
const baseAsPath = removeTrailingSlash(asPath.split('?')[0] as string); | |
const basePathname = removeTrailingSlash(pathname.split('?')[0] as string); | |
if (baseAsPath === basePathname) { | |
return true; | |
} | |
const basePathRegex = new RegExp( | |
`^${basePathname.replace(/(\[[a-zA-Z0-9-]+\])+/g, '[a-zA-Z0-9-]+')}$` | |
.replace(/\[\[\.\.\.[a-zA-Z0-9-]+\]\]/g, '?.*') | |
.replace(/\[\.\.\.[a-zA-Z0-9-]+\]/g, '.*'), | |
); | |
if (basePathRegex.test(baseAsPath)) { | |
return true; | |
} | |
return false; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Great Job!
I've simplyfied the if statement, since test always returns a boolean:
before:
after:
full implementation:
Test results