Comparing version 1.0.2 to 2.0.0
38
index.js
@@ -0,22 +1,24 @@ | ||
var url = require('url') | ||
var Routes = require('routes') | ||
, extend = require('deep-extend') | ||
var extend = require('deep-extend') | ||
module.exports = createRouter | ||
function createRouter(defaultRoute, _root, _verbs) { | ||
function createRouter (defaultRoute, _root, _verbs) { | ||
var routers = {} | ||
, root = _root || '' | ||
, verbs = _verbs || ['get', 'post', 'put', 'patch', 'delete'] | ||
, routeFn | ||
var root = _root || '' | ||
var verbs = _verbs || ['get', 'post', 'put', 'patch', 'delete'] | ||
var routeFn | ||
verbs.push('any') | ||
routeFn = function() { | ||
routeFn = function () { | ||
var args = [].slice.call(arguments) | ||
, 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 | ||
var req = args[0] | ||
var route = req.splats && req.splats.length ? | ||
req.splats[req.splats.length - 1] : | ||
url.parse(req.url).pathname | ||
var method = req.method ? req.method.toLowerCase() : 'any' | ||
var result | ||
@@ -27,15 +29,15 @@ if (root.length && route.indexOf(root) === 0) { | ||
if(routers[method]) { | ||
if (routers[method]) { | ||
result = routers[method].match(route) | ||
} | ||
if(!result) { | ||
if (!result) { | ||
result = routers.any.match(route) | ||
} | ||
if(result) { | ||
if (result) { | ||
args[0] = extend(req, result) | ||
result.fn.apply(null, args) | ||
} else { | ||
if(typeof defaultRoute === 'function') { | ||
if (typeof defaultRoute === 'function') { | ||
defaultRoute.apply(null, args) | ||
@@ -50,5 +52,5 @@ | ||
verbs.forEach(function(verb) { | ||
verbs.forEach(function (verb) { | ||
routers[verb] = Routes() | ||
routeFn[verb] = function(pattern, handler) { | ||
routeFn[verb] = function (pattern, handler) { | ||
routers[verb].addRoute(pattern, handler) | ||
@@ -55,0 +57,0 @@ } |
{ | ||
"name": "commuter", | ||
"version": "1.0.2", | ||
"version": "2.0.0", | ||
"description": "A minimal, composable router that supports sub-routes.", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -5,4 +5,5 @@ # commuter | ||
[![Build Status](http://img.shields.io/travis/fardog/commuter/master.svg?style=flat)](https://travis-ci.org/fardog/commuter) | ||
[![npm install](http://img.shields.io/npm/dm/commuter.svg?style=flat)](https://www.npmjs.org/package/commuter) | ||
[![Build Status](http://img.shields.io/travis/fardog/commuter/master.svg?style=flat-square)](https://travis-ci.org/fardog/commuter) | ||
[![npm install](http://img.shields.io/npm/dm/commuter.svg?style=flat-square)](https://www.npmjs.org/package/commuter) | ||
[![js-standard-style](https://img.shields.io/badge/code%20style-standard-brightgreen.svg?style=flat-square)](https://github.com/feross/standard) | ||
@@ -34,3 +35,3 @@ ## Example | ||
var router = commuter() | ||
, subrouter = commuter() | ||
var subrouter = commuter() | ||
@@ -37,0 +38,0 @@ router.get('/post*', subrouter) |
128
test.js
var test = require('tape') | ||
, commuter = require('./index') | ||
var commuter = require('./index') | ||
test('routes on a request-like object', function(t) { | ||
test('routes on a request-like object', function (t) { | ||
t.plan(1) | ||
var router = commuter() | ||
, req = {url: '/path', method: 'GET'} | ||
var req = {url: '/path', method: 'GET'} | ||
@@ -14,3 +14,3 @@ router.get(req.url, onRoute) | ||
function onRoute() { | ||
function onRoute () { | ||
t.ok(true) | ||
@@ -22,9 +22,26 @@ | ||
test('passes through any additional parameters provided', function(t) { | ||
test('routes on a querystring path', function (t) { | ||
t.plan(1) | ||
var router = commuter() | ||
var req = {url: '/path?something=true', method: 'GET'} | ||
router.get('/path', onRoute) | ||
router(req) | ||
function onRoute () { | ||
t.ok(true) | ||
t.end() | ||
} | ||
}) | ||
test('passes through any additional parameters provided', function (t) { | ||
t.plan(2) | ||
var router = commuter() | ||
, req = {url: '/path', method: 'GET'} | ||
, res = {ok: function() {}} | ||
, superflous = {} | ||
var req = {url: '/path', method: 'GET'} | ||
var res = {ok: function () {}} | ||
var superflous = {} | ||
@@ -35,3 +52,3 @@ router.get(req.url, onRoute) | ||
function onRoute(request, response, unnecessary) { | ||
function onRoute (request, response, unnecessary) { | ||
t.strictEqual(res, response) | ||
@@ -44,8 +61,8 @@ t.strictEqual(superflous, unnecessary) | ||
test('calls error function on no match', function(t) { | ||
test('calls error function on no match', function (t) { | ||
t.plan(1) | ||
var router = commuter(onError) | ||
, url = '/path' | ||
, req = {url: '/boop', method: 'GET'} | ||
var url = '/path' | ||
var req = {url: '/boop', method: 'GET'} | ||
@@ -56,3 +73,3 @@ router.get(url, noop) | ||
function onError(r) { | ||
function onError (r) { | ||
t.strictEqual(r, req) | ||
@@ -62,8 +79,8 @@ } | ||
test('throws error if no error function provided', function(t) { | ||
test('throws error if no error function provided', function (t) { | ||
t.plan(1) | ||
var router = commuter() | ||
, url = '/path' | ||
, req = {url: '/boop', method: 'GET'} | ||
var url = '/path' | ||
var req = {url: '/boop', method: 'GET'} | ||
@@ -81,8 +98,8 @@ router.get(url, noop) | ||
test('places parameters on the request object', function(t) { | ||
test('places parameters on the request object', function (t) { | ||
t.plan(1) | ||
var router = commuter() | ||
, url = '/admin/:panel' | ||
, req = {url: '/admin/home', method: 'GET'} | ||
var url = '/admin/:panel' | ||
var req = {url: '/admin/home', method: 'GET'} | ||
@@ -93,3 +110,3 @@ router.get(url, onRoute) | ||
function onRoute(r) { | ||
function onRoute (r) { | ||
t.equal('home', r.params.panel) | ||
@@ -101,9 +118,9 @@ | ||
test('routes on expected sub-routes', function(t) { | ||
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() {}} | ||
var subRouter = commuter() | ||
var req = {url: '/admin/section/home', method: 'GET'} | ||
var res = {ok: function () {}} | ||
@@ -115,3 +132,3 @@ router.get('/admin*', subRouter) | ||
function onRoute(request, response) { | ||
function onRoute (request, response) { | ||
t.equal(request.params.panel, 'home') | ||
@@ -124,7 +141,28 @@ t.strictEqual(response, res) | ||
test('strips root from url when provided', function(t) { | ||
test('routes on expected sub-routes with query params', function (t) { | ||
t.plan(2) | ||
var router = commuter() | ||
var subRouter = commuter() | ||
var req = {url: '/admin/section/home?something=true', method: 'GET'} | ||
var 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() | ||
} | ||
}) | ||
test('strips root from url when provided', function (t) { | ||
t.plan(1) | ||
var router = commuter(null, '/strip') | ||
, req = {url: '/strip/admin/section/home', method: 'GET'} | ||
var req = {url: '/strip/admin/section/home', method: 'GET'} | ||
@@ -135,3 +173,3 @@ router.get('/admin*', onRoute) | ||
function onRoute(request) { | ||
function onRoute (request) { | ||
t.ok(request) | ||
@@ -143,9 +181,9 @@ | ||
test('root url does not affect sub-routes', function(t) { | ||
test('root url does not affect sub-routes', function (t) { | ||
t.plan(2) | ||
var router = commuter(null, '/strip') | ||
, subRouter = commuter() | ||
, req = {url: '/strip/admin/section/home', method: 'GET'} | ||
, res = {ok: function() {}} | ||
var subRouter = commuter() | ||
var req = {url: '/strip/admin/section/home', method: 'GET'} | ||
var res = {ok: function () {}} | ||
@@ -157,3 +195,3 @@ router.get('/admin*', subRouter) | ||
function onRoute(request, response) { | ||
function onRoute (request, response) { | ||
t.equal(request.params.panel, 'home') | ||
@@ -166,7 +204,7 @@ t.strictEqual(response, res) | ||
test('arbitrary verbs can be defined', function(t) { | ||
test('arbitrary verbs can be defined', function (t) { | ||
t.plan(1) | ||
var router = commuter(null, null, ['leap']) | ||
, req = {url: '/path', method: 'leap'} | ||
var req = {url: '/path', method: 'leap'} | ||
@@ -177,3 +215,3 @@ router.leap(req.url, onRoute) | ||
function onRoute() { | ||
function onRoute () { | ||
t.ok(true) | ||
@@ -185,7 +223,7 @@ | ||
test('"any" route is always present', function(t) { | ||
test('"any" route is always present', function (t) { | ||
t.plan(1) | ||
var router = commuter(null, null, ['leap']) | ||
, req = {url: '/path', method: 'wut'} | ||
var req = {url: '/path', method: 'wut'} | ||
@@ -196,3 +234,3 @@ router.any(req.url, onRoute) | ||
function onRoute() { | ||
function onRoute () { | ||
t.ok(true) | ||
@@ -204,9 +242,9 @@ | ||
test('latest defined parameter takes precedence', function(t) { | ||
test('latest defined parameter takes precedence', function (t) { | ||
t.plan(2) | ||
var router = commuter() | ||
, subRouter = commuter() | ||
, req = {url: '/admin/item/go/section/home', method: 'GET'} | ||
, res = {ok: function() {}} | ||
var subRouter = commuter() | ||
var req = {url: '/admin/item/go/section/home', method: 'GET'} | ||
var res = {ok: function () {}} | ||
@@ -218,3 +256,3 @@ router.get('/admin/:panel/:section/*', subRouter) | ||
function onRoute(request, response) { | ||
function onRoute (request, response) { | ||
t.equal(request.params.panel, 'item') | ||
@@ -227,4 +265,4 @@ t.equal(request.params.section, 'home') | ||
function noop() { | ||
function noop () { | ||
// | ||
} |
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
11999
9
216
104