-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
3b0dd7a
commit 8c4d348
Showing
4 changed files
with
147 additions
and
0 deletions.
There are no files selected for viewing
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
if (typeof window === 'undefined') { | ||
throw new Error('This module only works in the browser.') | ||
} | ||
|
||
const { addEventListener, history, dispatchEvent, location } = window | ||
import { pathToRegexp, match } from 'path-to-regexp' | ||
|
||
const routes = [] | ||
|
||
function handle(path) { | ||
const matched = routes.filter(r => r.regexp.exec(path)) | ||
if (matched.length === 0) { | ||
console.log('No route match.', path) | ||
return false | ||
} | ||
for (let route of matched) { | ||
const { controller, match, name } = route | ||
const data = match(path) | ||
controller.apply({}, [{ name, ...data }]) | ||
return true | ||
} | ||
} | ||
|
||
const Router = { | ||
config({ | ||
base = '/', | ||
controller = () => { | ||
console.log('Default Controller'); | ||
} | ||
}) { | ||
this.controller = controller | ||
if (base[0] !== '/') { | ||
console.error('Base must start with a /'); | ||
return | ||
} | ||
if (base !== '/' && [base.length - 1] === '/') { | ||
console.error('Base can not end with a /'); | ||
return | ||
} | ||
this.base = base | ||
return this | ||
}, | ||
add({ path, controller = this.controller, name } = {}) { | ||
let thePath = path | ||
const pathType = Object.prototype.toString.call(path) | ||
const { base } = this | ||
if (path[0] !== '/' && path !== '*') { | ||
console.error('Path must start with a /'); | ||
return | ||
} | ||
if (path === '*') { | ||
thePath = /.*/ | ||
} | ||
if (base !== '/') { | ||
if (pathType === '[object RegExp]') { | ||
// Add base to the beginning of the path | ||
} | ||
if (pathType === '[object String]') { | ||
thePath = path === '/' ? base : base + path | ||
} | ||
} | ||
routes.push({ name, thePath, controller, regexp: pathToRegexp(thePath), match: match(thePath) }) | ||
return this | ||
}, | ||
start() { | ||
const { pathname, hash } = location | ||
const path = pathname + hash | ||
handle(path) | ||
} | ||
} | ||
|
||
addEventListener('click', function (event) { | ||
const { target } = event | ||
if (target.tagName === 'A') { | ||
const { href } = target | ||
const { origin, pathname, hash, search } = new URL(href) | ||
if (origin !== location.origin) return | ||
if (href.match(/^mailto:/)) return | ||
if (href.match(/^tel:/)) return | ||
if (href.match(/^javascript:/)) return | ||
if (pathname.match(/^#/)) return | ||
event.preventDefault() | ||
history.pushState({}, '', pathname + hash + search) | ||
dispatchEvent(new PopStateEvent('popstate', { state: {} })) | ||
} | ||
}) | ||
|
||
addEventListener('popstate', () => { | ||
Router.start() | ||
}) | ||
|
||
export default Router |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
{ | ||
"name": "@markgarrigan/router", | ||
"version": "0.0.1", | ||
"description": "Because I need a router to exactly what I want it to do.", | ||
"main": "index.js", | ||
"type": "module", | ||
"scripts": { | ||
"test": "echo \"Error: no test specified\" && exit 1" | ||
}, | ||
"repository": { | ||
"type": "git", | ||
"url": "git+https://github.com/markgarrigan/router.git" | ||
}, | ||
"keywords": [ | ||
"javascript", | ||
"router" | ||
], | ||
"author": "Mark Garrigan", | ||
"license": "Unlicense", | ||
"bugs": { | ||
"url": "https://github.com/markgarrigan/router/issues" | ||
}, | ||
"homepage": "https://github.com/markgarrigan/router#readme", | ||
"dependencies": { | ||
"path-to-regexp": "^6.2.1" | ||
} | ||
} |