url-router
Advanced tools
Comparing version 12.0.0 to 13.0.0
@@ -1,2 +0,1 @@ | ||
declare type Route<T> = [string, T]; | ||
declare type Result<T> = { | ||
@@ -8,3 +7,3 @@ handler: T; | ||
private root; | ||
constructor(routes?: Route<T>[]); | ||
constructor(routes?: Record<string, T>); | ||
private createNode; | ||
@@ -11,0 +10,0 @@ add(pattern: string, handler: T): this; |
@@ -9,3 +9,3 @@ const REGEX_PARAM_DEFAULT = /^[^/]+/; | ||
if (routes) { | ||
routes.forEach(route => this.add(...route)); | ||
Object.entries(routes).forEach(route => this.add(...route)); | ||
} | ||
@@ -12,0 +12,0 @@ } |
{ | ||
"name": "url-router", | ||
"version": "12.0.0", | ||
"version": "13.0.0", | ||
"description": "A Trie-based router", | ||
@@ -33,5 +33,5 @@ "main": "./dist/index.cjs", | ||
"devDependencies": { | ||
"@typescript-eslint/eslint-plugin": "^4.10.0", | ||
"@typescript-eslint/parser": "^4.10.0", | ||
"eslint": "^7.15.0", | ||
"@typescript-eslint/eslint-plugin": "^4.14.1", | ||
"@typescript-eslint/parser": "^4.14.1", | ||
"eslint": "^7.18.0", | ||
"eslint-config-enough": "^0.7.0", | ||
@@ -38,0 +38,0 @@ "typescript": "^4.1.3" |
# url-router | ||
A Trie-based router. | ||
## V13 Breaking Change | ||
The constructor's `routes` parameter changed to key-value object that key is pattern and value is handler. | ||
## Installation | ||
@@ -9,4 +12,4 @@ ``` | ||
NOTE: This package is written in ES2020 syntax and not transpiled. It is only tested on Node.js v14 LTS. | ||
To use it in old browsers, you need to transpile the code using tool such as Babel. | ||
NOTE: This package is written in ES2020 syntax and not transpiled. It is tested only on Node.js v14 LTS. | ||
To use it in old browsers, you should transpile the code using tools such as Babel. | ||
@@ -19,11 +22,11 @@ ## Examples | ||
const router = new Router([ | ||
['/foo', 1], | ||
['/foo/bar', 2], | ||
['/user/:id', 3], | ||
['/user/:id/:page', 4], | ||
['/people/:name(\\w+)', 5], | ||
['(.*)', 6], | ||
['/:year(\\d+)-:month(\\d+)', 7] | ||
]); | ||
const router = new Router({ | ||
'/foo': 1, | ||
'/foo/bar': 2, | ||
'/user/:id': 3, | ||
'/user/:id/:page': 4, | ||
'/people/:name(\\w+)': 5, | ||
'(.*)': 6, | ||
'/:year(\\d+)-:month(\\d+)': 7 | ||
}); | ||
@@ -108,7 +111,9 @@ assert.deepStrictEqual( | ||
```js | ||
new Router([ | ||
[pattern1, handler1], | ||
[pattern2, handler2], | ||
const routes = { | ||
pattern_1: handler_1, | ||
pattern_2: handler_2, | ||
... | ||
]) | ||
}; | ||
router = new Router(routes); | ||
``` | ||
@@ -118,3 +123,6 @@ | ||
If the routes array is provided, `router.add` will be applied on each route. | ||
#### Params | ||
##### routes | ||
Optional. A key-value object that key is pattern and value is handler. | ||
See `router.add()` below for how to define pattern and handler. | ||
@@ -126,3 +134,3 @@ ### router.add | ||
Adds a route definition. | ||
Adds a route entry. | ||
@@ -129,0 +137,0 @@ #### Params |
@@ -6,4 +6,2 @@ const REGEX_PARAM_DEFAULT = /^[^/]+/; | ||
type Route<T> = [string, T]; | ||
type Node<T> = { | ||
@@ -30,7 +28,7 @@ regex?: RegExp, | ||
constructor(routes?: Route<T>[]) { | ||
constructor(routes?: Record<string, T>) { | ||
this.root = this.createNode(); | ||
if (routes) { | ||
routes.forEach(route => this.add(...route)); | ||
Object.entries(routes).forEach(route => this.add(...route)); | ||
} | ||
@@ -62,3 +60,3 @@ } | ||
add(pattern: string, handler: T) { | ||
add(pattern: string, handler: T): this { | ||
this.parseOptim(pattern, handler, this.root); | ||
@@ -114,3 +112,3 @@ return this; | ||
find(path: string) { | ||
find(path: string): Result<T> | null { | ||
return this.findOptim(path, this.root, {}); | ||
@@ -117,0 +115,0 @@ } |
import assert from 'assert'; | ||
import Router from '../dist/index.js'; | ||
const router = new Router([ | ||
['/foo', 1], | ||
['/foo/bar', 2], | ||
['/user/:id', 3], | ||
['/user/:id/:page', 4], | ||
['/people/:name([\\w%]+)', 5], | ||
['(.*)', 6], | ||
['/:year(\\d+)-:month(\\d+)', 7] | ||
]); | ||
const router = new Router({ | ||
'/foo': 1, | ||
'/foo/bar': 2, | ||
'/user/:id': 3, | ||
'/user/:id/:page': 4, | ||
'/people/:name([\\w%]+)': 5, | ||
'(.*)': 6, | ||
'/:year(\\d+)-:month(\\d+)': 7 | ||
}); | ||
@@ -14,0 +14,0 @@ assert.deepStrictEqual( |
Sorry, the diff of this file is not supported yet
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
19190
208
0
483