-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: ✨ added simple router and test
- Loading branch information
Showing
8 changed files
with
1,779 additions
and
63 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"@rabelgm/route-matcher": patch | ||
--- | ||
|
||
added simple router functionality and tests |
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 |
---|---|---|
@@ -1,2 +1,3 @@ | ||
node_modules | ||
dist | ||
dist | ||
coverage |
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,14 @@ | ||
/** @type {import('ts-jest').JestConfigWithTsJest} */ | ||
module.exports = { | ||
preset: "ts-jest", | ||
testEnvironment: "node", | ||
rootDir: "./", | ||
coverageDirectory: "<rootDir>/coverage", | ||
collectCoverageFrom: [ | ||
"<rootDir>/lib/**/*.ts", | ||
"!<rootDir>/lib/**/constant.ts", | ||
], | ||
testPathIgnorePatterns: ["<rootDir>/node_modules"], | ||
coverageReporters: ["json", "html"], | ||
testMatch: ["<rootDir>/test/**/*.test.ts"], | ||
}; |
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 @@ | ||
export { Router } from "./router"; |
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,69 @@ | ||
const optionalParam = /\((.*?)\)/g; | ||
const namedParam = /(\(\?)?:\w+/g; | ||
const splatParam = /\*\w+/g; | ||
const escapeRegExp = /[\-{}\[\]+?.,\\\^$|#\s]/g; | ||
|
||
interface Route { | ||
path: string; | ||
regex: RegExp; | ||
callback: (params: (string | null)[]) => void; | ||
} | ||
|
||
export class Router { | ||
private routes: Route[]; | ||
|
||
constructor() { | ||
this.routes = []; | ||
} | ||
|
||
exec(path: string) { | ||
for (const route of this.routes) { | ||
const isMatch = route.regex.test(path); | ||
|
||
if (isMatch) { | ||
const params = this.extractParameters(route.regex, path); | ||
route.callback(params); | ||
break; | ||
} | ||
} | ||
} | ||
|
||
add(segment: string, callback: (params: (string | null)[]) => void) { | ||
const regex = this.routeToRegExp(segment); | ||
|
||
this.routes.push({ path: segment, regex, callback }); | ||
} | ||
|
||
/** | ||
* Convert a route string into a regular expression, suitable for matching. | ||
*/ | ||
routeToRegExp(route: string): RegExp { | ||
route = route | ||
.replace(escapeRegExp, "\\$&") | ||
.replace(optionalParam, "(?:$1)?") | ||
.replace(namedParam, function (match, optional) { | ||
return optional ? match : "([^/?]+)"; | ||
}) | ||
.replace(splatParam, "([^?]*?)"); | ||
return new RegExp("^" + route + "(?:\\?([\\s\\S]*))?$"); | ||
} | ||
|
||
/** | ||
* Given a route, and a URL fragment that it matches, return the array of | ||
* extracted decoded parameters. Empty or unmatched parameters will be treated as `null` | ||
* to normalize cross-browser behavior. | ||
*/ | ||
extractParameters(route: RegExp, fragment: string): Array<string | null> { | ||
const result = route.exec(fragment); | ||
if (!result) return []; | ||
|
||
// Removes the first item from the result array because its the input text executed by the regex. | ||
const params = result.slice(1); | ||
|
||
return params.map((param, i) => { | ||
// Don't decode the search params. | ||
if (i === params.length - 1) return param || null; | ||
return param ? decodeURIComponent(param) : null; | ||
}); | ||
} | ||
} |
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
Oops, something went wrong.