Skip to content

Commit

Permalink
Merge pull request #20 from yoshuawuyts/on-path
Browse files Browse the repository at this point in the history
On path
  • Loading branch information
yoshuawuyts committed Jun 14, 2015
2 parents 0d88ad8 + 44f8476 commit d00350a
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 18 deletions.
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,14 @@ const wayfarer = require('wayfarer')
const router = wayfarer('/404')

router.on('/', () => console.log('/'))
router.on('/404', uri => console.log('404 %s not found', uri))
router.on('/:user', (uri, param) => console.log('user is %s', param.user))
router.on('/404', () => console.log('404 not found'))
router.on('/:user', params => console.log('user is %s', params.user))

router('/tobi')
// => 'user is tobi'

router('/uh/oh')
// => '404 /uh/oh not found'
// => '404 not found'
```

## Subrouting
Expand All @@ -48,7 +48,7 @@ r1('/dada/child')
### router = wayfarer(default)
Initialize a router with a default route. Doesn't ignore querystrings and hashes.

### router.on(route, cb)
### router.on(route, cb(params))
Register a new route. The order in which routes are registered does not matter.
See [`routington.define()`](https://github.com/pillarjs/routington#nodes-node--routerdefineroute)
for all route options.
Expand Down
9 changes: 5 additions & 4 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,9 @@ function wayfarer (dft) {

// match and call a route
// str -> null
function emit (path, param) {
function emit (path, params) {
path = sanitizeUri(path) || ''
param = param || {}
params = params || {}

const mountPath = mountMatch(path)
if (mountPath) {
Expand All @@ -49,8 +49,9 @@ function wayfarer (dft) {

const mch = mountPath ? mountPath : router.match(path) || router.match(dft)
assert.ok(mch, 'path ' + path + ' did not match')
param = xtend(param, mch.param)
mch.node.cb('/' + path, param)
params = xtend(params, mch.param)
// only nested routers need a path
mountPath ? mch.node.cb(path, params) : mch.node.cb(params)
}

// mount a subrouter
Expand Down
18 changes: 8 additions & 10 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,9 @@ test('.on() should catch type errors', function (t) {
})

test('.emit() should match partials', function (t) {
t.plan(2)
t.plan(1)
const r = wayfarer()
r.on('/:user', function (uri, param) {
t.equal(uri, '/tobi')
r.on('/:user', function (param) {
t.equal(param.user, 'tobi')
})
r('/tobi')
Expand All @@ -43,33 +42,32 @@ test('.emit() should throw if no matches are found', function (t) {
})

test('.emit() should allow nesting', function (t) {
t.plan(5)
t.plan(4)

const r1 = wayfarer()
const r2 = wayfarer()
r1.on('/home', r2)
r2.on('/', function (uri) {
t.equal(uri, '/')
r2.on('/', function () {
t.pass('/')
})

r1('/home')

const r3 = wayfarer()
const r4 = wayfarer()
r3.on('/parent', r4)
r4.on('/child', function (uri) {
t.equal(uri, '/child')
r4.on('/child', function () {
t.pass('/child')
})

r3('/parent/child')

const r5 = wayfarer()
const r6 = wayfarer()
r5.on('/:parent', r6)
r6.on('/child', function (uri, param) {
r6.on('/child', function (param) {
t.equal(typeof param, 'object')
t.equal(param.parent, 'hello')
t.equal(uri, '/child')
})

r5('/hello/child')
Expand Down

0 comments on commit d00350a

Please sign in to comment.