Comparing version 0.0.0 to 0.1.0
35
index.js
var Routes = require('routes') | ||
, url = require('url') | ||
, extend = require('xtend') | ||
module.exports = createRouter | ||
function createRouter(noMatch) { | ||
var router = Routes() | ||
function createRouter(noMatch, _verbs) { | ||
var routers = {} | ||
, verbs = _verbs || ['get', 'post', 'put', 'patch', 'delete'] | ||
, routeFn | ||
verbs.push('any') | ||
routeFn = function() { | ||
var args = [].slice.call(arguments) | ||
, route = args[0].url || args[0] | ||
, req = args[0] | ||
, route = req.splats && req.splats.length ? | ||
req.splats[req.splats.length - 1] : | ||
req.url | ||
, method = req.method ? req.method.toLowerCase() : 'any' | ||
, result | ||
result = router.match(route) | ||
result = routers[method].match(route) | ||
if(!result) { | ||
result = routers.any.match(route) | ||
} | ||
if(result) { | ||
result.fn.apply(result, args) | ||
args[0] = extend(req, result) | ||
result.fn.apply(null, args) | ||
} else { | ||
if(typeof noMatch === 'function') { | ||
noMatch(route) | ||
noMatch.apply(null, args) | ||
@@ -30,7 +42,10 @@ return | ||
routeFn.add = function(pattern, handler) { | ||
router.addRoute(pattern, handler) | ||
} | ||
verbs.forEach(function(verb) { | ||
routers[verb] = Routes() | ||
routeFn[verb] = function(pattern, handler) { | ||
routers[verb].addRoute(pattern, handler) | ||
} | ||
}) | ||
return routeFn | ||
} |
{ | ||
"name": "commuter", | ||
"version": "0.0.0", | ||
"description": "Possibly a router", | ||
"version": "0.1.0", | ||
"description": "A minimal, composable router with sub-routes.", | ||
"main": "index.js", | ||
@@ -24,3 +24,4 @@ "scripts": { | ||
"dependencies": { | ||
"routes": "^2.0.0" | ||
"routes": "^2.0.0", | ||
"xtend": "^4.0.0" | ||
}, | ||
@@ -27,0 +28,0 @@ "devDependencies": { |
# commuter | ||
A router that does the right thing. | ||
A minimal, composable router with sub-routes. | ||
**Note:** Nothing is solid here; this is a work in progress. | ||
## Example | ||
## API | ||
```javascript | ||
var commuter = require('commuter') | ||
// with a URL string | ||
var router = commuter() | ||
, url = '/path/:title' | ||
router.add(url, onRoute) | ||
router.get('/post/:slug', onRoute) | ||
router('/path/some-title') | ||
//later, a GET request is made with the url '/post/some-title' | ||
router(req, res) | ||
function onRoute(u) { | ||
console.log(this.params.title) // 'some-title' | ||
function onRoute(req, res) { | ||
console.log(req.params.title) // 'some-title' | ||
// handle route... | ||
} | ||
``` | ||
// with a request object | ||
Subroutes work exactly as you'd expect: | ||
```javascript | ||
var commuter = require('commuter') | ||
var router = commuter() | ||
, url = '/path/:title' | ||
, subrouter = commuter() | ||
router.add(url, onRoute) | ||
router.get('/post*', subrouter) | ||
subrouter.get('/view/:title', onRoute) | ||
// ... assuming req.url is '/path/some-title' | ||
// later, a GET request with url '/post/view/some-title' | ||
router(req, res) | ||
function onRoute(req, res) { | ||
console.log(this.params.title) // 'some-title' | ||
console.log(req.params.title) // 'some-title' | ||
console.log(req.url) // '/post/view/some-title' | ||
// handle route... | ||
} | ||
@@ -35,0 +44,0 @@ ``` |
91
test.js
var test = require('tape') | ||
, commuter = require('./index') | ||
test('routes on strings', function(t) { | ||
test('routes on a request-like object', function(t) { | ||
t.plan(1) | ||
var router = commuter() | ||
, url = '/path' | ||
, req = {url: '/path', method: 'GET'} | ||
router.add(url, onRoute) | ||
router.get(req.url, onRoute) | ||
router(url) | ||
router(req) | ||
function onRoute(u) { | ||
t.equal(u, url) | ||
function onRoute() { | ||
t.ok(true) | ||
@@ -21,10 +21,10 @@ t.end() | ||
test('routes on a request-like object', function(t) { | ||
t.plan(2) | ||
test('passes through any additional parameters provided', function(t) { | ||
t.plan(1) | ||
var router = commuter() | ||
, req = {url: '/path'} | ||
, req = {url: '/path', method: 'GET'} | ||
, res = {ok: function() {}} | ||
router.add(req.url, onRoute) | ||
router.get(req.url, onRoute) | ||
@@ -34,3 +34,2 @@ router(req, res) | ||
function onRoute(request, response) { | ||
t.strictEqual(req, request) | ||
t.strictEqual(res, response) | ||
@@ -42,53 +41,73 @@ | ||
test('result object should be on `this`', function(t) { | ||
t.plan(2) | ||
test('calls error function on no match', function(t) { | ||
t.plan(1) | ||
var router = commuter() | ||
var router = commuter(onError) | ||
, url = '/path' | ||
, req = {url: '/boop', method: 'GET'} | ||
router.add(url, onRoute) | ||
router.get(url, noop) | ||
router(url) | ||
router(req) | ||
function onRoute(u) { | ||
t.equal(u, url) | ||
t.equal(this.route, url) | ||
t.end() | ||
function onError(r) { | ||
t.strictEqual(r, req) | ||
} | ||
}) | ||
test('calls error function on no match', function(t) { | ||
test('throws error if no error function provided', function(t) { | ||
t.plan(1) | ||
var router = commuter(onError) | ||
var router = commuter() | ||
, url = '/path' | ||
, badUrl = '/boop' | ||
, req = {url: '/boop', method: 'GET'} | ||
router.add(url, noop) | ||
router.get(url, noop) | ||
router(badUrl) | ||
try { | ||
router(req) | ||
} catch(e) { | ||
t.ok(e) | ||
function onError(u) { | ||
t.equal(u, badUrl) | ||
t.end() | ||
} | ||
}) | ||
test('throws error if no error function provided', function(t) { | ||
test('places parameters on the request object', function(t) { | ||
t.plan(1) | ||
var router = commuter() | ||
, url = '/path' | ||
, badUrl = '/boop' | ||
, url = '/admin/:panel' | ||
, req = {url: '/admin/home', method: 'GET'} | ||
router.add(url, noop) | ||
router.get(url, onRoute) | ||
try { | ||
router(badUrl) | ||
} catch(e) { | ||
t.ok(e) | ||
router(req) | ||
function onRoute(r) { | ||
t.equal('home', r.params.panel) | ||
t.end() | ||
} | ||
}) | ||
test('routes on expected sub-routes', function(t) { | ||
t.plan(2) | ||
var router = commuter() | ||
, subRouter = commuter() | ||
, req = {url:'/admin/section/home', method: 'GET'} | ||
, res = {ok: function() {}} | ||
router.get('/admin*', subRouter) | ||
subRouter.get('/section/:panel', onRoute) | ||
router(req, res) | ||
function onRoute(request, response) { | ||
t.equal(request.params.panel, 'home') | ||
t.strictEqual(response, res) | ||
t.end() | ||
} | ||
}) | ||
@@ -95,0 +114,0 @@ |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
6024
118
49
2
+ Addedxtend@^4.0.0
+ Addedxtend@4.0.2(transitive)