|
|
|
'use strict'
|
|
|
|
var url = require('url')
|
|
|
|
var RouteNode = require('./route-node')
|
|
|
|
var utils = require('./utils')
|
|
|
|
|
|
|
|
var assign = utils.assign
|
|
|
|
var isArray = utils.isArray
|
|
|
|
var isString = utils.isString
|
|
|
|
var noop = utils.noop
|
|
|
|
|
|
|
|
function Router(options) {
|
|
|
|
options = options || {}
|
|
|
|
this.routes = new RouteNode()
|
|
|
|
}
|
|
|
|
|
|
|
|
assign(Router.prototype, {
|
|
|
|
add: function add(path, handler) {
|
|
|
|
return this.routes.add(this._uriToParts(path), handler)
|
|
|
|
},
|
|
|
|
|
|
|
|
route: function route(path, done) {
|
|
|
|
var routeArray = this._uriToParts(path)
|
|
|
|
done = done || noop
|
|
|
|
return this.routes.get(routeArray, [], function (err, func, context, args) {
|
|
|
|
if (err) {
|
|
|
|
return done(err)
|
|
|
|
} else {
|
|
|
|
return done(null, func.apply(context, args))
|
|
|
|
}
|
|
|
|
})
|
|
|
|
},
|
|
|
|
|
|
|
|
_uriToParts: function _uriToParts(uri) {
|
|
|
|
if (isString(uri)) {
|
|
|
|
return url.parse(uri)
|
|
|
|
.pathname
|
|
|
|
.split('/')
|
|
|
|
.filter(function (str) { return str !== ''})
|
|
|
|
} else if (isArray(uri)) {
|
|
|
|
return uri.reduce(this._uriToPartsReducer.bind(this), [])
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
_uriToPartsReducer: function _uriToPartsReducer(memo, item) {
|
|
|
|
if (isArray(item)) {
|
|
|
|
return memo.concat(item)
|
|
|
|
} else if (isString(item)) {
|
|
|
|
return memo.concat(this._uriToParts(item))
|
|
|
|
} else {
|
|
|
|
return memo.concat(item)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
Router.isNotFound = function isNotFound(err) {
|
|
|
|
return err instanceof Error && /not found/.exec(err.message) !== null
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = Router
|