hash-brown-router
Advanced tools
Comparing version 1.2.0 to 1.3.0
37
index.js
var pathToRegexp = require('path-to-regexp-with-reversible-keys') | ||
var qs = require('querystring') | ||
var xtend = require('xtend') | ||
var browserHashLocation = require('./hash-location.js') | ||
module.exports = function Router() { | ||
module.exports = function Router(hashLocation) { | ||
if (!hashLocation) { | ||
hashLocation = browserHashLocation() | ||
} | ||
var routes = [] | ||
var onHashChange = evaluateCurrentPath.bind(null, routes) | ||
var onHashChange = evaluateCurrentPath.bind(null, routes, hashLocation) | ||
window.addEventListener('hashchange', onHashChange) | ||
hashLocation.on('hashchange', onHashChange) | ||
function stop() { | ||
window.removeEventListener('hashchange', onHashChange) | ||
hashLocation.removeListener('hashchange', onHashChange) | ||
} | ||
@@ -19,16 +24,13 @@ | ||
stop: stop, | ||
evaluate: go.bind(null, routes), | ||
evaluateCurrent: evaluateCurrentPathOrGoToDefault.bind(null, routes, hashLocation), | ||
setDefault: setDefault.bind(null, routes), | ||
replace: replace | ||
replace: hashLocation.replace, | ||
go: hashLocation.go | ||
} | ||
} | ||
function evaluateCurrentPath(routes) { | ||
evaluatePath(routes, removeHashFromPath(location.hash)) | ||
function evaluateCurrentPath(routes, hashLocation) { | ||
evaluatePath(routes, hashLocation.get()) | ||
} | ||
function removeHashFromPath(path) { | ||
return (path && path[0] === '#') ? path.substr(1) : path | ||
} | ||
function getPathParts(path) { | ||
@@ -87,7 +89,7 @@ var chunks = path.split('?') | ||
function go(routes, defaultPath) { | ||
if (removeHashFromPath(location.hash)) { | ||
evaluateCurrentPath(routes) | ||
function evaluateCurrentPathOrGoToDefault(routes, hashLocation, defaultPath) { | ||
if (hashLocation.get()) { | ||
evaluateCurrentPath(routes, hashLocation) | ||
} else { | ||
location.hash = defaultPath | ||
hashLocation.go(defaultPath) | ||
} | ||
@@ -100,4 +102,1 @@ } | ||
function replace(newPath) { | ||
location.replace(location.origin + location.pathname + '#' + newPath) | ||
} |
{ | ||
"name": "hash-brown-router", | ||
"version": "1.2.0", | ||
"version": "1.3.0", | ||
"description": "A client-side router that only cares about the bits after the #", | ||
"main": "index.js", | ||
"scripts": { | ||
"test": "browserify test/test.js -o test/build.js -d && echo 'now open test/index.html in a browser'", | ||
"watch": "watchify test/test.js -o test/build.js -d" | ||
"test": "node test/automated-test.js", | ||
"browserwatch": "watchify test/browser-test.js -o test/build.js -d" | ||
}, | ||
@@ -10,0 +10,0 @@ "repository": { |
@@ -38,7 +38,7 @@ | ||
## `evaluate(defaultPath)` - evaluate the current url | ||
## `evaluateCurrent(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). | ||
router.evaluate('/home') | ||
router.evaluateCurrent('/home') | ||
@@ -45,0 +45,0 @@ If location.hash is currently empty, it changes the path to the default path value you pass in. |
var domready = require('domready') | ||
var tapBrowserColor = require('tap-browser-color') | ||
var browserHashLocation = require('../hash-location.js') | ||
var test = require('tape').test | ||
@@ -36,2 +37,3 @@ | ||
function start() { | ||
var hashLocation = browserHashLocation() | ||
allTests.forEach(function(next) { | ||
@@ -41,5 +43,5 @@ test(next.description, function(t) { | ||
queuedUp.push(function() { | ||
location.hash = '' | ||
hashLocation.go('') | ||
process.nextTick(function() { | ||
next.fn(t, function() { | ||
next.fn(t, hashLocation, function() { | ||
t.end() | ||
@@ -46,0 +48,0 @@ runNext() |
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
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
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
14397
13
370
1