Skip to content

Commit

Permalink
Add support for routing to an index file inside of a named folder
Browse files Browse the repository at this point in the history
  • Loading branch information
jesseditson committed Feb 10, 2017
1 parent 2b6be4b commit f22ae79
Show file tree
Hide file tree
Showing 7 changed files with 51 additions and 2 deletions.
16 changes: 14 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,14 @@ function addMatch(route) {
routePath = routePath.replace(paramPattern, '([^\?\/]+)')
paramNames.push(matched[1])
}
// if a route ends with `index`, allow matching that route without matching the `index` part
if (path.basename(routePath) === 'index') {
route.isIndex = true
routePath = routePath.replace(/\/index$/, '\/?(:?index)?')
}
// create a regex with our path
let pattern = new RegExp(`^${routePath}\\??(.*)`, 'i')
let pattern = new RegExp(`^${routePath}(\\?(.*)|$)`, 'i')
route.pattern = pattern
route.match = url => {
let m = url.match(pattern)
if (m) {
Expand Down Expand Up @@ -51,9 +57,15 @@ module.exports = function router(routesDir) {
}
return route
})
// add a match function
.map(addMatch)
// sort named files ahead of subfolder index files
.map(route => {
if (!route.priority && route.isIndex) route.priority = -1
return route
})
// if a route exposes a `priority` property, sort the route on it.
.sort((a, b) => val(a.priority) < val(b.priority) ? 1 : -1)
.map(addMatch)

// generated match method - call with a req object to get a route.
return function match(req) {
Expand Down
1 change: 1 addition & 0 deletions test/fixtures/index/conflict.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = () => 'conflict'
1 change: 1 addition & 0 deletions test/fixtures/index/conflict/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = () => 'conflict/index'
1 change: 1 addition & 0 deletions test/fixtures/index/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = () => 'index'
1 change: 1 addition & 0 deletions test/fixtures/index/test/:param/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = () => 'test/:param'
1 change: 1 addition & 0 deletions test/fixtures/index/thing/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = () => 'thing'
32 changes: 32 additions & 0 deletions test/index-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
const test = require('tape')
const router = require('..')

let match = router(__dirname + '/fixtures/index')

test('uses index.js when the root is requested', t => {
t.plan(2)
let req = { url: '/', method: 'GET' }
t.equal(typeof match(req), 'function', 'finds a route')
t.equal(match(req)(), 'index', 'finds the index route')
})

test('uses index.js of a subfolder', t => {
t.plan(2)
let req = { url: '/thing', method: 'GET' }
t.equal(typeof match(req), 'function', 'finds a route')
t.equal(match(req)(), 'thing', 'finds the right route')
})

test('uses index.js of a parameter', t => {
t.plan(2)
let req = { url: '/test/foo', method: 'GET' }
t.equal(typeof match(req), 'function', 'finds a route')
t.equal(match(req)(), 'test/:param', 'finds the right route')
})

test('prefers a .js file to a nested index file when there is a conflict', t => {
t.plan(2)
let req = { url: '/conflict', method: 'GET' }
t.equal(typeof match(req), 'function', 'finds a route')
t.equal(match(req)(), 'conflict', 'finds the right route')
})

0 comments on commit f22ae79

Please sign in to comment.