Skip to content

Commit

Permalink
router: ditch route() arg
Browse files Browse the repository at this point in the history
  • Loading branch information
yoshuawuyts committed Jul 22, 2016
1 parent 12c06f7 commit 52a02eb
Show file tree
Hide file tree
Showing 3 changed files with 113 additions and 176 deletions.
50 changes: 21 additions & 29 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,18 +26,16 @@ efficient data structure. Here each route takes either an array of children or
a callback, which are then translated to paths that take callbacks
```js
const sheetRouter = require('sheet-router')
const yo = require('yo-yo')
const html = require('bel')

// default to `/404` if no path matches
const router = sheetRouter('/404', function (route) {
return [
route('/', (params) => yo`<div>Welcome to router land!</div>`),
route('/:username', (params) => yo`<div>${params.username}</div>`, [
route('/orgs', (params) => yo`<div>${params.username}'s orgs!</div>`)
]),
route('/404', (params) => yo`<div>Oh no, path not found!</div>`),
]
})
const router = sheetRouter('/404', [
['/', (params) => html`<div>Welcome to router land!</div>`],
['/:username', (params) => html`<div>${params.username}</div>`, [
['/orgs', (params) => html`<div>${params.username}'s orgs!</div>`]
]],
['/404', (params) => html`<div>Oh no, path not found!</div>`],
])

router('/hughsk/orgs')
```
Expand Down Expand Up @@ -84,14 +82,13 @@ href(function (href) {
const render = require('virtual-dom/create-element')
const sheetRouter = require('sheet-router')
const h = require('virtual-dom/h')
const hyperx = require('hyperx')

const router = sheetRouter(function (r, t) {
return [
r('/foo/bar', function (params, h, state) {
return h('div', null, 'hello world')
})
]
})
const html = hyperx(h)

const router = sheetRouter([
['/foo/bar', (params, h, state) => html`<div>hello world!</div>`]
])

const node = render(router('/foo/bar', h, { name: 'Jane' }))
document.body.appendChild(node)
Expand All @@ -106,15 +103,14 @@ document.body.appendChild(node)
```js
const sheetRouter = require('sheet-router')
const render = require('react-dom')
const hyperx = require('hyperx')
const react = require('react')

const router = sheetRouter(function (r, t) {
return [
r('/foo/bar', function (params, h, state) {
h('div', null, 'hello world')
}
]
})
const html = hyperx(react.createElement)

const router = sheetRouter([
['/foo/bar', (params, h, state) => html`<div>hello world!</div>`]
])

render(router('/foo', react.createElement, { name: 'Jane' }), document.body)
```
Expand All @@ -125,14 +121,10 @@ render(router('/foo', react.createElement, { name: 'Jane' }), document.body)
```

## API
### router = sheetRouter(dft?, createTree(route), createRoute?)
### router = sheetRouter(dft?, [routes])
Create a new router from a nested array. Takes an optional default path as the
first argument.

If `createRoute(route)` is passed as the third argument, the `route()` function
passed to `createTree()` can be manipulated. This is useful for things like
changing argument order and the like.
### router(route, [,...])
Match a route on the router. Takes a path and an arbitrary list of arguments
that are then passed to the matched routes. Cleans urls to only match the
Expand Down
77 changes: 36 additions & 41 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,45 +5,52 @@ const assert = require('assert')
module.exports = sheetRouter

// Fast, modular client router
// fn(str, any[..], fn?) -> fn(str, any[..])
function sheetRouter (dft, createTree, createRoute) {
createRoute = (createRoute ? createRoute(_createRoute) : _createRoute)

if (!createTree) {
createTree = dft
// fn(str, any[..]) -> fn(str, any[..])
function sheetRouter (dft, tree) {
if (!tree) {
tree = dft
dft = ''
}

assert.equal(typeof dft, 'string', 'sheet-router: dft must be a string')
assert.equal(typeof createTree, 'function', 'sheet-router: createTree must be a function')
assert.equal(typeof createRoute, 'function', 'sheet-router: createRoute must be a function')
assert.ok(Array.isArray(tree), 'sheet-router: tree must be an array')

const router = wayfarer(dft)
const tree = createTree(createRoute)

// register tree in router
;(function walk (tree, route) {
if (Array.isArray(tree[0])) {
// walk over all routes at the root of the tree
// tree[0] is a string, thus a route
// tree[0] is an array, thus not a route
// tree[1] is a function
// tree[2] is an array
// tree[2] is not an array
// tree[1] is an array
;(function walk (tree, fullRoute) {
if (typeof tree[0] === 'string') {
var route = tree[0].replace(/^\//, '')
} else {
var rootArr = tree[0]
}

const cb = (typeof tree[1] === 'function') ? tree[1] : null
const children = (Array.isArray(tree[1]))
? tree[1]
: Array.isArray(tree[2]) ? tree[2] : null

if (rootArr) {
tree.forEach(function (node) {
walk(node, route)
walk(node, fullRoute)
})
} else if (tree[1]) {
// handle inline functions as args
const innerRoute = tree[0]
? route.concat(tree[0]).join('/')
: route.length ? route.join('/') : tree[0]
router.on(innerRoute, tree[1])
walk(tree[2], route.concat(tree[0]))
} else if (Array.isArray(tree[2])) {
// traverse and append route
walk(tree[2], route.concat(tree[0]))
} else {
// register path in router
const nwRoute = tree[0]
? route.concat(tree[0]).join('/')
: route.length ? route.join('/') : tree[0]
router.on(nwRoute, tree[2])
}

if (cb) {
const innerRoute = route
? fullRoute.concat(route).join('/')
: fullRoute.length ? fullRoute.join('/') : route
router.on(innerRoute, cb)
}

if (Array.isArray(children)) {
walk(children, fullRoute.concat(route))
}
})(tree, [])

Expand All @@ -55,15 +62,3 @@ function sheetRouter (dft, createTree, createRoute) {
return router.apply(null, args)
}
}

// register regular route
function _createRoute (route, inline, child) {
if (!child) {
child = inline
inline = null
}
assert.equal(typeof route, 'string', 'route must be a string')
assert.ok(child, 'child exists')
route = route.replace(/^\//, '')
return [ route, inline, child ]
}
Loading

0 comments on commit 52a02eb

Please sign in to comment.