sheet-router
Advanced tools
Comparing version 4.0.1 to 4.0.2
@@ -32,6 +32,9 @@ const window = require('global/window') | ||
e.preventDefault() | ||
const href = node.href.replace(/#$/, '') | ||
cb(href) | ||
window.history.pushState({}, null, href) | ||
cb({ | ||
pathname: node.pathname, | ||
search: node.search, | ||
href: node.href, | ||
hash: node.hash | ||
}) | ||
} | ||
} |
27
index.js
@@ -1,5 +0,14 @@ | ||
const pathname = require('pathname-match') | ||
const wayfarer = require('wayfarer') | ||
const assert = require('assert') | ||
/* eslint-disable no-useless-escape */ | ||
const protocol = '^(http(s)?(:\/\/))?(www\.)?' | ||
const domain = '[a-zA-Z0-9-_\.]+' | ||
const qs = '[\?].*$' | ||
/* eslint-enable no-useless-escape */ | ||
const prefix = new RegExp(protocol + domain) | ||
const normalize = new RegExp('#') | ||
const suffix = new RegExp(qs) | ||
module.exports = sheetRouter | ||
@@ -36,3 +45,3 @@ | ||
if (typeof tree[0] === 'string') { | ||
var route = tree[0].replace(/^\//, '') | ||
var route = tree[0].replace(/^[#\/]/, '') | ||
} else { | ||
@@ -88,3 +97,3 @@ var rootArr = tree[0] | ||
} else { | ||
prevRoute = pathname(pathname(route)) | ||
prevRoute = pathname(route) | ||
prevCallback = router(prevRoute) | ||
@@ -97,3 +106,3 @@ return prevCallback(arg1, arg2, arg3, arg4, arg5) | ||
// wrap a function in a function so it can be called at a later time | ||
// fn -> null -> fn | ||
// fn -> obj -> (any, any, any, any, any) | ||
function thunkify (cb) { | ||
@@ -106,1 +115,11 @@ return function (params) { | ||
} | ||
// replace everything in a route but the pathname and hash | ||
// TODO(yw): ditch 'suffix' and allow qs routing | ||
// str -> str | ||
function pathname (route) { | ||
return route | ||
.replace(prefix, '') | ||
.replace(suffix, '') | ||
.replace(normalize, '/') | ||
} |
{ | ||
"name": "sheet-router", | ||
"version": "4.0.1", | ||
"version": "4.0.2", | ||
"description": "Fast, modular client router", | ||
@@ -25,3 +25,2 @@ "main": "index.js", | ||
"global": "^4.3.0", | ||
"pathname-match": "^1.1.2", | ||
"wayfarer": "^6.2.1", | ||
@@ -28,0 +27,0 @@ "xtend": "^4.0.1" |
@@ -42,8 +42,10 @@ const noop = require('noop2') | ||
t.test('should clean urls before matching', function (t) { | ||
t.plan(1) | ||
t.plan(2) | ||
const router = sheetRouter([ | ||
['/foo', (params) => t.pass('called')] | ||
['/foo', (params) => t.pass('cleaned qs')], | ||
['#hello-world', (params) => t.pass('matched hash')] | ||
]) | ||
router('https://foobar.com/foo#hello-world?bar=baz') | ||
router('https://foobar.com/foo?bar=baz') | ||
router('https://foobar.com#hello-world?bar=baz') | ||
}) | ||
@@ -169,1 +171,26 @@ | ||
}) | ||
test('hash routes', (t) => { | ||
t.test('should allow for hash routes', (t) => { | ||
t.plan(2) | ||
const router = sheetRouter([ | ||
['/foo', () => t.pass('foo called'), [ | ||
['#bar', () => t.pass('bar called')] | ||
]] | ||
]) | ||
router('/foo') | ||
router('/foo#bar') | ||
}) | ||
t.test('should allow for hash partials', (t) => { | ||
t.plan(1) | ||
const router = sheetRouter([ | ||
['/foo', () => t.pass('foo called'), [ | ||
['#:bar', (params) => t.equal(params.bar, 'bar', 'params match')] | ||
]] | ||
]) | ||
router('/foo#bar') | ||
}) | ||
}) |
27604
3
547
- Removedpathname-match@^1.1.2
- Removedpathname-match@1.2.0(transitive)