Skip to content

Commit

Permalink
first commit
Browse files Browse the repository at this point in the history
  • Loading branch information
markgarrigan committed Dec 30, 2022
1 parent 3b0dd7a commit 8c4d348
Show file tree
Hide file tree
Showing 4 changed files with 147 additions and 0 deletions.
File renamed without changes.
92 changes: 92 additions & 0 deletions index.js
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
28 changes: 28 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

27 changes: 27 additions & 0 deletions package.json
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"
}
}

0 comments on commit 8c4d348

Please sign in to comment.