generated from honeycombio/.github
-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adds an option to infer route from the pathname. Any path segment that matches a permissive UUID will be replaced by `{paramN}`, as an example: `/entity/bba1b7c5-3ce1-42cd-984a-9d95988b08e7` becomes `/entity/{param1}`
- Loading branch information
Showing
6 changed files
with
47 additions
and
5 deletions.
There are no files selected for viewing
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
/** a permissive regex for UUIDs, any sequence of hex characters in the uuid format will do */ | ||
const uuidRe = /[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}/i; | ||
|
||
/** | ||
* takes a URL pathname, replaces uuid segments with paramN | ||
*/ | ||
export function pathnameToRoute(pathname: string): string { | ||
const segments = pathname.split('/'); | ||
|
||
let count = 0; | ||
const routeSegments = segments.map((segment) => { | ||
if (uuidRe.test(segment)) { | ||
return `{param${++count}}`; | ||
} | ||
return segment; | ||
}); | ||
|
||
return routeSegments.join('/'); | ||
} |
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
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import { pathnameToRoute } from '../src/pathname-to-route'; | ||
|
||
describe('pathnameToRoute', () => { | ||
it('infers the route from the pathname', () => { | ||
const cases = [ | ||
['/entity/BBA1B7C5-3CE1-42CD-984A-9D95988B08E7', '/entity/{param1}'], | ||
['/entity/abcde', '/entity/abcde'], | ||
['/entity/bba1b7c5-3ce1-42cd-984a-9d95988b08e7', '/entity/{param1}'], | ||
[ | ||
'/entity/bba1b7c5-3ce1-42cd-984a-9d95988b08e7/sub/BBA1B7C5-3CE1-42CD-984A-9D95988B08E8', | ||
'/entity/{param1}/sub/{param2}', | ||
], | ||
]; | ||
for (const [pathname, route] of cases) { | ||
expect(pathnameToRoute(pathname)).toEqual(route); | ||
} | ||
}); | ||
}); |