trouter
Advanced tools
Comparing version 1.0.0 to 1.1.0
@@ -9,6 +9,5 @@ const { METHODS } = require('http'); | ||
this.handlers = {}; | ||
this.all = this.add.bind(this, '*'); | ||
METHODS.forEach(str => { | ||
this[str.toLowerCase()] = this.add.bind(this, str); | ||
this.handlers[str] = {}; | ||
this.routes[str] = []; | ||
}); | ||
@@ -19,4 +18,6 @@ } | ||
// Save decoded pattern info | ||
if (this.routes[method] === void 0) this.routes[method]=[]; | ||
this.routes[method].push(parse(pattern)); | ||
// Save route handler | ||
if (this.handlers[method] === void 0) this.handlers[method]={}; | ||
this.handlers[method][pattern] = handler; | ||
@@ -28,4 +29,7 @@ // Allow chainable | ||
find(method, url) { | ||
let arr = match(url, this.routes[method]); | ||
if (!arr.length) return false; | ||
let arr = match(url, this.routes[method] || []); | ||
if (arr.length === 0) { | ||
arr = match(url, this.routes[method='*'] || []); | ||
if (!arr.length) return false; | ||
} | ||
return { | ||
@@ -32,0 +36,0 @@ params: exec(url, arr), |
{ | ||
"name": "trouter", | ||
"version": "1.0.0", | ||
"version": "1.1.0", | ||
"description": "🐟 A fast, small-but-mighty, familiar ~fish~ router", | ||
@@ -5,0 +5,0 @@ "repository": "lukeed/trouter", |
@@ -81,18 +81,24 @@ # trouter [![Build Status](https://travis-ci.org/lukeed/trouter.svg?branch=master)](https://travis-ci.org/lukeed/trouter) | ||
### trouter.find(method, url) | ||
Returns: `Object|Boolean`<br> | ||
Searches within current instance for a `method` + `pattern` pairing that matches the current `method` + `url`. | ||
### trouter.all(pattern, handler) | ||
Returns: `self` | ||
This method will return `false` if no match is found. Otherwise it returns an Object with `params` and `handler` keys. | ||
This is an alias for [`trouter.add('*', pattern, handler)`](#trouteraddmethod-pattern-handler), matching **all** HTTP methods. | ||
#### method | ||
Type: `String` | ||
> **Important:** If the `pattern` used within `all()` exists for a specific `method` as well, then **only** the method-specific entry will be returned! | ||
Any valid HTTP method name. | ||
```js | ||
router.post('/hello', () => 'FROM POST'); | ||
router.add('GET', '/hello', () => 'FROM GET'); | ||
router.all('/hello', () => 'FROM ALL'); | ||
#### url | ||
Type: `String` | ||
router.find('GET', '/hello').handler(); | ||
//=> 'FROM GET' | ||
router.find('POST', '/hello').handler(); | ||
//=> 'FROM POST' | ||
router.find('DELETE', '/hello').handler(); | ||
//=> 'FROM ALL' | ||
router.find('PUT', '/hello').handler(); | ||
//=> 'FROM ALL' | ||
``` | ||
The URL used to match against pattern definitions. This is typically `req.url`. | ||
### trouter.METHOD(pattern, handler) | ||
@@ -116,22 +122,41 @@ | ||
### trouter.find(method, url) | ||
Returns: `Object|Boolean`<br> | ||
Searches within current instance for a `method` + `pattern` pairing that matches the current `method` + `url`. | ||
This method will return `false` if no match is found. Otherwise it returns an Object with `params` and `handler` keys. | ||
#### method | ||
Type: `String` | ||
Any valid HTTP method name. | ||
#### url | ||
Type: `String` | ||
The URL used to match against pattern definitions. This is typically `req.url`. | ||
## Benchmarks | ||
> Run on Node v6.11.1 | ||
> Run on Node v8.9.0 | ||
``` | ||
GET / ON / | ||
--> 6,621,618 ops/sec ±1.43% (91 runs sampled) | ||
--> 9,548,621 ops/sec ±0.65% (96 runs sampled) | ||
POST /users ON /users | ||
--> 2,180,156 ops/sec ±1.06% (91 runs sampled) | ||
--> 2,324,166 ops/sec ±0.52% (93 runs sampled) | ||
GET /users/123 ON /users/:id | ||
--> 1,126,468 ops/sec ±0.44% (93 runs sampled) | ||
--> 1,704,811 ops/sec ±0.50% (95 runs sampled) | ||
PUT /users/123/books ON /users/:id/books/:title? | ||
--> 1,003,157 ops/sec ±0.43% (94 runs sampled) | ||
--> 1,396,875 ops/sec ±0.14% (94 runs sampled) | ||
DELETE /users/123/books/foo ON /users/:id/books/:title | ||
--> 827,550 ops/sec ±0.53% (91 runs sampled) | ||
--> 1,266,708 ops/sec ±0.59% (95 runs sampled) | ||
HEAD /hello on /hello -- via all() | ||
--> 1,641,558 ops/sec ±0.14% (96 runs sampled) | ||
``` | ||
@@ -138,0 +163,0 @@ |
6775
35
165