Add support for regular expressions with capture groups
Router • add can take either a string or array • An array will check each item, for a string will be converted to an array RouteNode • rename this._exact → this._children • exact matches will not have a '/' in a key because we split on the '/' character and RegExp will have a '/' in it • If an exact match can't be found first, check every regex on a node • Exact match will have preference over RegExp utils • add forEach helper • add isArray and isRegExp checkermaster
parent
10371b7b1e
commit
a317aa86a8
@ -1,12 +1,39 @@
|
||||
'use strict'
|
||||
module.exports = {
|
||||
|
||||
var toString = ({}).toString
|
||||
var hasOwnProp = ({}).hasOwnProperty
|
||||
|
||||
var utils = {
|
||||
noop: function noop() {},
|
||||
|
||||
assign: function assign (dest, adding) {
|
||||
for (var key in adding) {
|
||||
if (adding.hasOwnProperty(key)) {
|
||||
dest[key] = adding[key]
|
||||
isString: function isString(str) {
|
||||
return typeof str === 'string'
|
||||
},
|
||||
|
||||
forEach: function forEach(obj, fn) {
|
||||
for (var key in obj) {
|
||||
if (hasOwnProp.call(obj, key)) {
|
||||
fn(key, obj[key])
|
||||
}
|
||||
}
|
||||
},
|
||||
|
||||
assign: function assign (dest, methods) {
|
||||
utils.forEach(methods, function (key, val) {
|
||||
dest[key] = val
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
function checkType(type) {
|
||||
return function _checkType(val) {
|
||||
return toString.call(val) === '[object ' + type + ']'
|
||||
}
|
||||
}
|
||||
|
||||
// add isRegExp, isArray
|
||||
['RegExp', 'Array'].forEach(function addIsChecks(type) {
|
||||
utils['is' + type] = checkType(type)
|
||||
})
|
||||
|
||||
module.exports = utils
|
||||
|
Loading…
Reference in New Issue