hash-brown-router
Advanced tools
Comparing version 1.1.0 to 1.2.0
12
index.js
@@ -19,4 +19,5 @@ var pathToRegexp = require('path-to-regexp-with-reversible-keys') | ||
stop: stop, | ||
go: go.bind(null, routes), | ||
setDefault: setDefault.bind(null, routes) | ||
evaluate: go.bind(null, routes), | ||
setDefault: setDefault.bind(null, routes), | ||
replace: replace | ||
} | ||
@@ -30,3 +31,3 @@ } | ||
function removeHashFromPath(path) { | ||
return path && path.substr(1) | ||
return (path && path[0] === '#') ? path.substr(1) : path | ||
} | ||
@@ -88,3 +89,3 @@ | ||
function go(routes, defaultPath) { | ||
if (location.hash) { | ||
if (removeHashFromPath(location.hash)) { | ||
evaluateCurrentPath(routes) | ||
@@ -100,1 +101,4 @@ } else { | ||
function replace(newPath) { | ||
location.replace(location.origin + location.pathname + '#' + newPath) | ||
} |
{ | ||
"name": "hash-brown-router", | ||
"version": "1.1.0", | ||
"version": "1.2.0", | ||
"description": "A client-side router that only cares about the bits after the #", | ||
@@ -36,3 +36,16 @@ "main": "index.js", | ||
"path-to-regexp-with-reversible-keys": "^1.0.1" | ||
}, | ||
"testling": { | ||
"files": "test/test.js", | ||
"browsers": [ | ||
"ie/6..latest", | ||
"chrome/22..latest", | ||
"firefox/16..latest", | ||
"safari/latest", | ||
"opera/11.0..latest", | ||
"iphone/6..latest", | ||
"ipad/6..latest", | ||
"android-browser/latest" | ||
] | ||
} | ||
} |
A router that is only concerned with single-page apps that want to change state based on the bits of the url after the hash. | ||
[![browser support](https://ci.testling.com/TehShrike/hash-brown-router.png)](https://ci.testling.com/TehShrike/hash-brown-router) | ||
## To use | ||
@@ -10,3 +12,3 @@ | ||
## To add routes | ||
## `add(routeString, cb)` - add routes | ||
@@ -19,19 +21,31 @@ router.add('/page/:pageName', function(parameters) { | ||
## To evaluate the first route | ||
## `setDefault(cb)` - set a default/404 route | ||
router.go('/home') | ||
router.setDefault(function(path, parameters) { | ||
console.log("you went to", path, "but that doesn't go anywhere, I guess you just end up here") | ||
}) | ||
Called whenever the hash route changes, but no other matching route is found. | ||
## `replace(newPath)` - replace the current route in the browser history | ||
router.add('/page/:pageName', function(parameters) { | ||
if (doesNotExistInTheDatabase(parameters.pageName)) { | ||
router.replace('/pageNotFound') | ||
} | ||
}) | ||
Convenience method for `location.replace(location.origin + location.pathname + '#' + newPath)`. | ||
## `evaluate(defaultPath)` - evaluate the current url | ||
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. | ||
router.evaluate('/home') | ||
## Default/404 route | ||
If location.hash is currently empty, it changes the path to the default path value you pass in. | ||
router.setDefault(function(path, parameters) { | ||
console.log("you went to", path, "but that doesn't go anywhere, I guess you just end up here") | ||
}) | ||
## Other | ||
### stop() | ||
### `stop()` | ||
@@ -38,0 +52,0 @@ If for some reason you want the router to start ignoring hash change events. you can call `route.stop()`. |
@@ -73,3 +73,3 @@ var router = require('../') | ||
// may not always want the route to fire in the same tick? | ||
route.go() | ||
route.evaluate() | ||
@@ -103,3 +103,3 @@ route.stop() | ||
tester.add('route.go calls the default route when the current path is empty', function(t, done) { | ||
tester.add('route.evaluate calls the default route when the current path is empty', function(t, done) { | ||
var route = router() | ||
@@ -112,6 +112,6 @@ | ||
route.go('/default') | ||
route.evaluate('/default') | ||
setTimeout(function() { | ||
t.equal(location.hash, '#/default', 'the hash was set to the default from the route.go call') | ||
t.equal(location.hash, '#/default', 'the hash was set to the default from the route.evaluate call') | ||
route.stop() | ||
@@ -122,3 +122,3 @@ done() | ||
tester.add('route.go does not call the default route when the current path is not empty', function(t, done) { | ||
tester.add('route.evaluate does not call the default route when the current path is not empty', function(t, done) { | ||
location.hash = '/starting-path' | ||
@@ -134,3 +134,3 @@ | ||
route.go('/default') | ||
route.evaluate('/default') | ||
@@ -196,3 +196,3 @@ setTimeout(function() { | ||
route.go('/default?lol=wut') | ||
route.evaluate('/default?lol=wut') | ||
@@ -205,2 +205,41 @@ setTimeout(function() { | ||
tester.add('replacing a url', function(t) { | ||
t.plan(4) | ||
var route = router() | ||
var startingHistoryLength = history.length | ||
function shouldHappenOnce(name, cb) { | ||
var happened = false | ||
return function() { | ||
if (happened) { | ||
t.fail(name + ' already happened once') | ||
} else { | ||
t.pass(name + ' happened') | ||
happened = true | ||
} | ||
cb && cb.apply(null, arguments) | ||
} | ||
} | ||
route.add('/initial', shouldHappenOnce('initial route', function() { | ||
location.hash = '/redirect' // history length++ | ||
})) | ||
route.add('/redirect', shouldHappenOnce('redirect route', function() { | ||
route.replace('/destination') | ||
})) | ||
route.add('/destination', shouldHappenOnce('destination route', function() { | ||
var historyDepth = history.length - startingHistoryLength | ||
t.equal(historyDepth, 2, 'should be two more items in the history then there were before (was ' + historyDepth + ')') | ||
t.end() | ||
})) | ||
route.setDefault(t.fail.bind(t, 'default route called')) | ||
location.hash = '/initial' // history length++ | ||
}) | ||
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
12235
308
56