Skip to content

Commit

Permalink
golf: shave 7 bytes~
Browse files Browse the repository at this point in the history
  • Loading branch information
lukeed committed Apr 25, 2019
1 parent b4561f9 commit 618cd53
Showing 1 changed file with 11 additions and 10 deletions.
21 changes: 11 additions & 10 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ export default function Navaid(base, on404) {
return rgx ? rgx.test(uri) && (uri.replace(rgx, '') || '/') : uri;
}

if ((base=fmt(base)) === '/') base = '';
base = fmt(base);
if (base === '/') base = '';
if (base) rgx = new RegExp('^/?' + base.substring(1) + '(?=/|$)', 'i');

$.route = function (uri, replace) {
Expand All @@ -18,17 +19,17 @@ export default function Navaid(base, on404) {

$.on = function (pat, fn) {
handlers[pat] = fn;
var o = convert(pat);
o.route = pat;
routes.push(o);
fn = convert(pat);
fn.route = pat;
routes.push(fn);
return $;
}

$.run = function (uri) {
var i=0, params={}, arr, obj;
uri = fmt(uri || location.pathname);
for (; i < routes.length; i++) {
if (arr = (obj = routes[i]).pattern.exec(uri)) {
for (; i < routes.length;) {
obj = routes[i++];
if (arr = obj.pattern.exec(fmt(uri || location.pathname))) {
for (i=0; i < obj.keys.length;) params[obj.keys[i]]=arr[++i] || null;
handlers[obj.route](params); // todo loop?
return $;
Expand Down Expand Up @@ -67,17 +68,17 @@ export default function Navaid(base, on404) {
off('replacestate', run);
off('pushstate', run);
off('click', click);
};
}

return $.run();
}

return $;
}

function wrap(type) {
function wrap(type, fn) {
type += 'State';
var fn = history[type];
fn = history[type];
history[type] = function (uri) {
var ev = new Event(type.toLowerCase());
ev.uri = uri;
Expand Down

0 comments on commit 618cd53

Please sign in to comment.