hash-brown-router
Advanced tools
Comparing version 1.0.0 to 1.1.0
92
index.js
var pathToRegexp = require('path-to-regexp-with-reversible-keys') | ||
var qs = require('querystring') | ||
var xtend = require('xtend') | ||
module.exports = function Router() { | ||
var routes = [] | ||
var onHashChange = evaluateCurrentPath.bind(null, routes) | ||
window.addEventListener('hashchange', onHashChange) | ||
function stop() { | ||
window.removeEventListener('hashchange', onHashChange) | ||
} | ||
return { | ||
add: add.bind(null, routes), | ||
stop: stop, | ||
go: go.bind(null, routes), | ||
setDefault: setDefault.bind(null, routes) | ||
} | ||
} | ||
function evaluateCurrentPath(routes) { | ||
evaluatePath(routes, removeHashFromPath(location.hash)) | ||
} | ||
function removeHashFromPath(path) { | ||
@@ -7,20 +32,14 @@ return path && path.substr(1) | ||
function add(routes, routeString, routeFunction) { | ||
if (typeof routeFunction !== 'function') { | ||
throw new Error('The router add function must be passed a callback function') | ||
function getPathParts(path) { | ||
var chunks = path.split('?') | ||
return { | ||
path: chunks.shift(), | ||
queryString: qs.parse(chunks.join('')) | ||
} | ||
var newRoute = pathToRegexp(routeString) | ||
newRoute.fn = routeFunction | ||
routes.push(newRoute) | ||
} | ||
function makeParametersObject(keys, regexResult) { | ||
return keys.reduce(function(memo, urlKey, index) { | ||
memo[urlKey.name] = regexResult[index + 1] | ||
return memo | ||
}, {}) | ||
} | ||
function evaluatePath(routes, path) { | ||
var pathParts = getPathParts(path) | ||
path = pathParts.path | ||
function onHashChange(routes) { | ||
var path = removeHashFromPath(location.hash) | ||
var matchingRoute = routes.reduce(function(found, route) { | ||
@@ -40,31 +59,40 @@ if (found) { | ||
var queryStringParameters = pathParts.queryString | ||
if (matchingRoute) { | ||
var params = makeParametersObject(matchingRoute.route.keys, matchingRoute.regexResult) | ||
var routeParameters = makeParametersObjectFromRegexResult(matchingRoute.route.keys, matchingRoute.regexResult) | ||
var params = xtend(queryStringParameters, routeParameters) | ||
matchingRoute.route.fn(params) | ||
} else if (routes.defaultFn) { | ||
routes.defaultFn(path) | ||
routes.defaultFn(path, queryStringParameters) | ||
} | ||
} | ||
function setDefault(routes, defaultFn) { | ||
routes.defaultFn = defaultFn | ||
function makeParametersObjectFromRegexResult(keys, regexResult) { | ||
return keys.reduce(function(memo, urlKey, index) { | ||
memo[urlKey.name] = regexResult[index + 1] | ||
return memo | ||
}, {}) | ||
} | ||
module.exports = function Router() { | ||
var routes = [] | ||
var listener = onHashChange.bind(null, routes) | ||
window.addEventListener('hashchange', listener) | ||
function stop() { | ||
window.removeEventListener('hashchange', listener) | ||
function add(routes, routeString, routeFunction) { | ||
if (typeof routeFunction !== 'function') { | ||
throw new Error('The router add function must be passed a callback function') | ||
} | ||
var newRoute = pathToRegexp(routeString) | ||
newRoute.fn = routeFunction | ||
routes.push(newRoute) | ||
} | ||
return { | ||
add: add.bind(null, routes), | ||
stop: stop, | ||
go: listener, | ||
setDefault: setDefault.bind(null, routes) | ||
function go(routes, defaultPath) { | ||
if (location.hash) { | ||
evaluateCurrentPath(routes) | ||
} else { | ||
location.hash = defaultPath | ||
} | ||
} | ||
function setDefault(routes, defaultFn) { | ||
routes.defaultFn = defaultFn | ||
} | ||
{ | ||
"name": "hash-brown-router", | ||
"version": "1.0.0", | ||
"version": "1.1.0", | ||
"description": "A client-side router that only cares about the bits after the #", | ||
@@ -31,3 +31,4 @@ "main": "index.js", | ||
"tap-browser-color": "^0.1.2", | ||
"tape": "^3.0.1" | ||
"tape": "^3.0.1", | ||
"xtend": "^4.0.0" | ||
}, | ||
@@ -34,0 +35,0 @@ "dependencies": { |
@@ -18,6 +18,14 @@ | ||
## Default route | ||
## To evaluate the first route | ||
router.setDefault(function(path) { | ||
console.log("you went to", path, "but that doesn't go anywhere, I guess I'll send you somewhere else") | ||
router.go('/home') | ||
Forces the library to evaluate the current route from location.hash. Probably best do do once the [dom is ready](https://www.npmjs.org/package/domready). | ||
If location.hash is currently empty, it sets it to the value you pass in. | ||
## Default/404 route | ||
router.setDefault(function(path, parameters) { | ||
console.log("you went to", path, "but that doesn't go anywhere, I guess you just end up here") | ||
}) | ||
@@ -27,5 +35,5 @@ | ||
To force the routing library to evaluate the current route once your JavaScript is done loading (probably once the [dom is ready](https://www.npmjs.org/package/domready)), call `router.go()`. | ||
### stop() | ||
If for some reason you want the router to start ignoring hash change events. you can call `route.stop()`. | ||
If for some reason you want the router to start ignoring hash change events. you can call `route.stop()`. | ||
@@ -32,0 +40,0 @@ License |
108
test/test.js
@@ -26,3 +26,3 @@ var router = require('../') | ||
done() | ||
}, 500) | ||
}, 200) | ||
}) | ||
@@ -50,3 +50,3 @@ | ||
done() | ||
}, 500) | ||
}, 200) | ||
}) | ||
@@ -80,3 +80,3 @@ | ||
done() | ||
}, 500) | ||
}, 200) | ||
}) | ||
@@ -87,3 +87,3 @@ | ||
t.plan(2) | ||
t.plan(3) | ||
@@ -94,2 +94,3 @@ route.add('/no/way', t.fail.bind(t, 'the wrong route was called')) | ||
t.equal(typeof parameters, 'object', 'parameters object is an object') | ||
t.equal(Object.keys(parameters).length, 1, 'parameters object has one property') | ||
t.equal(parameters.special, 'input') | ||
@@ -103,5 +104,102 @@ }) | ||
done() | ||
}, 500) | ||
}, 200) | ||
}) | ||
tester.add('route.go calls the default route when the current path is empty', function(t, done) { | ||
var route = router() | ||
t.plan(2) | ||
route.add('/default', t.pass.bind(t, 'the default route was called')) | ||
route.add('/other', t.fail.bind(t, 'the wrong route was called')) | ||
route.go('/default') | ||
setTimeout(function() { | ||
t.equal(location.hash, '#/default', 'the hash was set to the default from the route.go call') | ||
route.stop() | ||
done() | ||
}, 200) | ||
}) | ||
tester.add('route.go does not call the default route when the current path is not empty', function(t, done) { | ||
location.hash = '/starting-path' | ||
t.plan(1) | ||
setTimeout(function() { | ||
var route = router() | ||
route.add('/default', t.fail.bind(t, 'the default route was called incorrectly')) | ||
route.add('/starting-path', t.pass.bind(t, 'the correct route was called')) | ||
route.go('/default') | ||
setTimeout(function() { | ||
route.stop() | ||
done() | ||
}, 200) | ||
}, 100) | ||
}) | ||
tester.add('parmeters include values from querystring', function(t, done) { | ||
t.plan(4) | ||
var route = router() | ||
route.add('myroute/:fromUrl', function(parameters) { | ||
t.equal(typeof parameters, 'object', 'parameters object is an object') | ||
t.equal(Object.keys(parameters).length, 2, 'parameters object has two properties') | ||
t.equal(parameters.fromUrl, 'value1', 'Value from the url parameter is correct') | ||
t.equal(parameters.fromQueryString, 'value2', 'Value from the query string is correct') | ||
}) | ||
location.hash = 'myroute/value1?fromQueryString=value2' | ||
setTimeout(function() { | ||
route.stop() | ||
done() | ||
}, 200) | ||
}) | ||
tester.add('parameters from route overwrite querystring parameters', function(t, done) { | ||
t.plan(3) | ||
var route = router() | ||
route.add('myroute/:fromUrl', function(parameters) { | ||
t.equal(typeof parameters, 'object', 'parameters object is an object') | ||
t.equal(Object.keys(parameters).length, 1, 'parameters object has one property') | ||
t.equal(parameters.fromUrl, 'value1', 'Value is from the route parameter') | ||
}) | ||
location.hash = 'myroute/value1?fromUrl=value2' | ||
setTimeout(function() { | ||
route.stop() | ||
done() | ||
}, 200) | ||
}) | ||
tester.add('querystring parameters passed to the default route', function(t, done) { | ||
var route = router() | ||
t.plan(3) | ||
route.setDefault(function(path, parameters) { | ||
t.equal(typeof parameters, 'object', 'parameters object is an object') | ||
t.equal(parameters.lol, 'wut', 'value from the querystring was passed in') | ||
t.equal(path, '/default', 'the /default path was correctly passed in') | ||
}) | ||
route.go('/default?lol=wut') | ||
setTimeout(function() { | ||
route.stop() | ||
done() | ||
}, 200) | ||
}) | ||
tester.start() |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
10097
274
42
5