Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Clearer delegation of responsibilities #3

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 7 additions & 9 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,12 @@ function Enroute (routes) {
return function enroute (location, props) {
if (!location) throw new Error('enroute requires a location')
props = props || {}
var params = {}

for (var route in routes) {
var m = match(route, params, location)
var params = match(route, location)
var fn = routes[route]

if (m) {
if (params) {
if (typeof fn !== 'function') return fn
else return fn(params, props)
}
Expand All @@ -40,27 +39,26 @@ function Enroute (routes) {

/**
* Check if this route matches `path`, if so
* populate `params`.
* return a `params` object.
*
* @param {String} path
* @param {Object} params
* @return {Boolean}
* @return {Object}
* @api private
*/

function match(path, params, pathname) {
function match(path, pathname) {
var keys = [];
var regexp = Regexp(path, keys);
var m = regexp.exec(pathname);
var params = {};

if (!m) return false;
else if (!params) return true;

for (var i = 1, len = m.length; i < len; ++i) {
var key = keys[i - 1];
var val = 'string' == typeof m[i] ? decodeURIComponent(m[i]) : m[i];
if (key) params[key.name] = val;
}

return true;
return params;
}