sheet-router
Advanced tools
Comparing version 4.0.3 to 4.1.0
26
index.js
@@ -0,14 +1,8 @@ | ||
const window = require('global/window') | ||
const pathname = require('./_pathname') | ||
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 isElectron = (window.process && window.process.type) | ||
const prefix = new RegExp(protocol + domain) | ||
const normalize = new RegExp('#') | ||
const suffix = new RegExp(qs) | ||
module.exports = sheetRouter | ||
@@ -92,7 +86,7 @@ | ||
if (opts.thunk === false) { | ||
return router(pathname(route), arg1, arg2, arg3, arg4, arg5) | ||
return router(pathname(route, isElectron), arg1, arg2, arg3, arg4, arg5) | ||
} else if (route === prevRoute) { | ||
return prevCallback(arg1, arg2, arg3, arg4, arg5) | ||
} else { | ||
prevRoute = pathname(route) | ||
prevRoute = pathname(route, isElectron) | ||
prevCallback = router(prevRoute) | ||
@@ -113,11 +107,1 @@ return prevCallback(arg1, arg2, arg3, arg4, arg5) | ||
} | ||
// 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.3", | ||
"version": "4.1.0", | ||
"description": "Fast, modular client router", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -9,3 +9,3 @@ const tape = require('tape') | ||
const goodLink = { localName: 'a', href: 'someUrl#', hasAttribute: () => {} } | ||
const goodLink = { localName: 'a', href: 'someUrl#', pathname: 'someUrl', hasAttribute: () => {} } | ||
@@ -41,3 +41,3 @@ const nonCatchEvents = { | ||
t.test('should capture link click', (t) => { | ||
t.plan(5) | ||
t.plan(3) | ||
const event = { | ||
@@ -53,3 +53,2 @@ target: { | ||
} | ||
const previousPushCount = window.history.pushState.callCount | ||
const cb = sinon.spy() | ||
@@ -61,5 +60,3 @@ href(cb) | ||
t.equal(cb.callCount, 1) | ||
t.equal(cb.lastCall.args[0], 'someUrl') | ||
t.equal(window.history.pushState.callCount, previousPushCount + 1) | ||
t.deepEqual(window.history.pushState.lastCall.args, [{}, null, 'someUrl']) | ||
t.deepEqual(cb.lastCall.args[0], { hash: undefined, href: 'someUrl#', pathname: 'someUrl', search: undefined }) | ||
}) | ||
@@ -66,0 +63,0 @@ |
28442
18
564