routes
Advanced tools
Comparing version 1.0.0 to 1.1.0
@@ -15,8 +15,8 @@ !function(e){if("object"==typeof exports)module.exports=e();else if("function"==typeof define&&define.amd)define(e);else{var f;"undefined"!=typeof window?f=window:"undefined"!=typeof global?f=global:"undefined"!=typeof self&&(f=self),f.routes=e()}}(function(){var define,module,exports;return (function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a=typeof require=="function"&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);throw new Error("Cannot find module '"+o+"'")}var f=n[o]={exports:{}};t[o][0].call(f.exports,function(e){var n=t[o][1][e];return s(n?n:e)},f,f.exports,e,t,n,r)}return n[o].exports}var i=typeof require=="function"&&require;for(var o=0;o<r.length;o++)s(r[o]);return s})({1:[function(_dereq_,module,exports){ | ||
*/ | ||
var Route = function(path){ | ||
//using 'new' is optional | ||
var src, re, keys = []; | ||
if(path instanceof RegExp){ | ||
@@ -78,4 +78,4 @@ re = path; | ||
*/ | ||
var match = function (routes, uri) { | ||
var captures, i = 0; | ||
var match = function (routes, uri, startAt) { | ||
var captures, i = startAt || 0; | ||
@@ -104,3 +104,4 @@ for (var len = routes.length; i < len; ++i) { | ||
splats: splats, | ||
route: route.src | ||
route: route.src, | ||
next: i + 1 | ||
}; | ||
@@ -119,3 +120,3 @@ } | ||
*/ | ||
var Router = function(){ | ||
@@ -137,6 +138,7 @@ //using 'new' is optional | ||
match: function(pathname){ | ||
var route = match(this.routes, pathname); | ||
match: function(pathname, startAt){ | ||
var route = match(this.routes, pathname, startAt); | ||
if(route){ | ||
route.fn = this.routeMap[route.route]; | ||
route.next = this.match.bind(this, pathname, route.next) | ||
} | ||
@@ -143,0 +145,0 @@ return route; |
20
index.js
@@ -14,8 +14,8 @@ | ||
*/ | ||
var Route = function(path){ | ||
//using 'new' is optional | ||
var src, re, keys = []; | ||
if(path instanceof RegExp){ | ||
@@ -77,4 +77,4 @@ re = path; | ||
*/ | ||
var match = function (routes, uri) { | ||
var captures, i = 0; | ||
var match = function (routes, uri, startAt) { | ||
var captures, i = startAt || 0; | ||
@@ -103,3 +103,4 @@ for (var len = routes.length; i < len; ++i) { | ||
splats: splats, | ||
route: route.src | ||
route: route.src, | ||
next: i + 1 | ||
}; | ||
@@ -118,3 +119,3 @@ } | ||
*/ | ||
var Router = function(){ | ||
@@ -136,6 +137,7 @@ //using 'new' is optional | ||
match: function(pathname){ | ||
var route = match(this.routes, pathname); | ||
match: function(pathname, startAt){ | ||
var route = match(this.routes, pathname, startAt); | ||
if(route){ | ||
route.fn = this.routeMap[route.route]; | ||
route.next = this.match.bind(this, pathname, route.next) | ||
} | ||
@@ -142,0 +144,0 @@ return route; |
{ | ||
"name": "routes", | ||
"description": "Minimalist route matching for javascript", | ||
"version": "1.0.0", | ||
"version": "1.1.0", | ||
"homepage": "https://github.com/aaronblohowiak/routes.js", | ||
@@ -6,0 +6,0 @@ "repository": "https://github.com/aaronblohowiak/routes.js.git", |
@@ -42,6 +42,7 @@ # Routes.js | ||
route: '/:controller/:action/:id.:format?', | ||
fn: [Function] | ||
fn: [Function], | ||
next: [Function] | ||
} | ||
``` | ||
In the example above, `fn` would be the function that was passed into the router. | ||
@@ -57,2 +58,39 @@ | ||
## Match Continuation | ||
The object returned by `router.match` includes a `next` function you can use to continue matching against subsequent routes. Routes are evaluated in the order they are added to the router, so generally, you would add your most specific routes first and most ambiguous routes last. Using the `next` function allows you evaluate more ambiguous routes first. | ||
```js | ||
var Router = require('routes'); | ||
var router = new Router(); | ||
router.addRoute('/admin/*?', auth); | ||
router.addRoute('/admin/users', adminUsers); | ||
http.createServer(function (req, res) { | ||
var path = url.parse(req.url).pathname; | ||
var match = router.match(path); | ||
match.fn(req, res, match); | ||
}).listen(1337) | ||
// authenticate the user and pass them on to | ||
// the next route, or respond with 403. | ||
function auth(req, res, match) { | ||
if (checkUser(req)) { | ||
match = match.next(); | ||
if (match) match.fn(req, res, match); | ||
return; | ||
} | ||
res.statusCode = 403; | ||
res.end() | ||
} | ||
// render the admin.users page | ||
function adminUsers(req, res, match) { | ||
// send user list | ||
res.statusCode = 200; | ||
res.end(); | ||
} | ||
``` | ||
## Installation | ||
@@ -83,3 +121,3 @@ | ||
"/assets/*" will match "/assets/blah/blah/blah.png" and "/assets/". | ||
"/assets/*.*" will match "/assets/1/2/3.js" as splats: ["1/2/3", "js"] | ||
@@ -107,3 +145,3 @@ | ||
`match`: takes a `String` or `RegExp` and returns an object that contains the named `params`, `splats`, `route` (string that was matched against), and the `fn` handler you passed in with `addRoute` | ||
`match`: takes a `String` or `RegExp` and returns an object that contains the named `params`, `splats`, `route` (string that was matched against), the `fn` handler you passed in with `addRoute`, and a `next` function which will run `match` against subsequent routes. | ||
@@ -110,0 +148,0 @@ ## Library API |
@@ -9,3 +9,3 @@ var assert = require("assert"), | ||
this[k] = assert[k]; | ||
} | ||
} | ||
})(); | ||
@@ -38,3 +38,3 @@ | ||
fn: noop, | ||
params: { | ||
params: { | ||
"lang":"de" | ||
@@ -48,3 +48,3 @@ }, | ||
{ | ||
path: "/normal/:id", | ||
path: "/normal/:id", | ||
testMatch: { | ||
@@ -62,3 +62,3 @@ "/normal/1":{ | ||
{ | ||
path: "/optional/:id?", | ||
path: "/optional/:id?", | ||
testMatch: { | ||
@@ -104,3 +104,3 @@ "/optional/1":{ | ||
}, | ||
{ | ||
{ | ||
path: "/files/*.*", | ||
@@ -126,4 +126,4 @@ testMatch: { | ||
fn: noop, | ||
params: { | ||
"kind":"users", | ||
params: { | ||
"kind":"users", | ||
"id":"ekjnekjnfkej", | ||
@@ -136,17 +136,17 @@ "method": undefined, | ||
fn: noop, | ||
params: { | ||
"kind":"users", | ||
params: { | ||
"kind":"users", | ||
"id":"ekjnekjnfkej", | ||
"method": "update", | ||
"format": undefined }, | ||
splats:[], | ||
splats:[], | ||
}, | ||
"/transitive/users/ekjnekjnfkej/update.json": { | ||
fn: noop, | ||
params: { | ||
"kind":"users", | ||
params: { | ||
"kind":"users", | ||
"id":"ekjnekjnfkej", | ||
"method": "update", | ||
"format": "json" }, | ||
splats:[], | ||
splats:[], | ||
} | ||
@@ -193,9 +193,10 @@ }, | ||
fixture = test.testMatch[path]; | ||
//save typing in fixtures | ||
fixture.route = test.path.toString(); // match gets string, so ensure same type | ||
delete match.next; // next shouldn't be compared | ||
deepEqual(match, fixture); | ||
assertCount++; | ||
} | ||
for(noMatchIdx in test.testNoMatch){ | ||
@@ -229,2 +230,13 @@ match = router.match(test.testNoMatch[noMatchIdx]); | ||
// test next | ||
router.addRoute("/*?", noop); | ||
router.addRoute("/next/x", noop); | ||
var match = router.match("/next/x"); | ||
equal(typeof match.next, "function") | ||
strictEqual(match.route, "/*?"); | ||
assertCount++; | ||
var next = match.next(); | ||
strictEqual(next.route, "/next/x"); | ||
assertCount++; | ||
console.log(assertCount.toString()+ " assertions made succesfully"); |
Sorry, the diff of this file is not supported yet
Network access
Supply chain riskThis module accesses the network.
Found 1 instance in 1 package
20783
8
585
164
1