Skip to content
This repository was archived by the owner on Aug 18, 2020. It is now read-only.

Move default route and pathRegex to options param #8

Open
wants to merge 4 commits into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ yarn add svelte-router
import Home from './Home.html'
import Welcome from './Welcome.html'
import Animal from './Animal.html'
import NotFound from './NotFound.html'

const { createRouter, RouterLink } = SvelteRouter

Expand All @@ -53,6 +54,9 @@ yarn add svelte-router
}
})
}
},{
pathRegex: '[a-zA-Z]+',
defaultRoute: NotFound
})

export default {
Expand Down
8 changes: 5 additions & 3 deletions example/app.html
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,11 @@
data: {
salutation: 'Hello'
}
},
},
default: NotFound
}
}
},{
pathRegex: '[a-zA-Z]+',
defaultRoute: NotFound
})
createRouter.listen(() => {
console.log('router changed')
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,11 @@
"koa-router": "^7.4.0",
"nodemon": "^1.14.11",
"rimraf": "^2.6.2",
"rollup": "^0.55.1",
"rollup": "^0.56.3",
"rollup-plugin-babel": "^3.0.3",
"rollup-plugin-commonjs": "^8.3.0",
"rollup-plugin-eslint": "^4.0.0",
"rollup-plugin-node-resolve": "^3.0.2",
"rollup-plugin-node-resolve": "^3.0.3",
"rollup-plugin-replace": "^2.0.0",
"rollup-plugin-svelte": "^4.0.0",
"rollup-plugin-uglify": "^3.0.0",
Expand Down
40 changes: 20 additions & 20 deletions src/utils/create-router.js
Original file line number Diff line number Diff line change
@@ -1,22 +1,19 @@
import history from './history'

const DYNAMIC_PATH_REGEX = '[a-zA-Z]+'
const DEFAULT_ROUTE = 'default'

const getPathRegex = (sections) => {
const getPathRegex = (sections, pathRegex) => {
return sections.map((value) => {
if (value.match(new RegExp(`:${DYNAMIC_PATH_REGEX}`)) !== null) {
return `([a-zA-Z0-9]+)`
if (value.match(new RegExp(`:${pathRegex}`)) !== null) {
return `(${pathRegex})`
}
return value
}).join('\\/')
}

const getPathVariables = (sections, matches) => {
const getPathVariables = (sections, matches, pathRegex) => {
const keys = sections.filter((value) => {
return value.match(new RegExp(`:${DYNAMIC_PATH_REGEX}`)) !== null
return value.match(new RegExp(`:${pathRegex}`)) !== null
}).map((value) => {
return value.match(new RegExp(`:(${DYNAMIC_PATH_REGEX})`))[1]
return value.match(new RegExp(`:(${pathRegex})`))[1]
})
const pathVariables = {}
keys.forEach((value, index) => {
Expand All @@ -26,9 +23,9 @@ const getPathVariables = (sections, matches) => {
return pathVariables
}

const getContent = (options, path, target, pathVariables) => {
let { Component, props } = options[path]
if (!Component) Component = options[path]
const getContent = (paths, path, target, pathVariables) => {
let { Component, props } = paths[path]
if (!Component) Component = paths[path]
let extraData = {
data: {
path: pathVariables
Expand All @@ -46,30 +43,33 @@ const getContent = (options, path, target, pathVariables) => {
})
}

const createRouter = options => {
const createRouter = (paths, options = {}) => {
let _target // target DOM
let _unlisten // history listener
let _content // route instance

const pathRegex = options['pathRegex'] ? options['pathRegex'] : '[a-zA-Z0-9]+'
const defaultRoute = options['defaultRoute'] ? options['defaultRoute'] : 'default'

const handleRouteChange = location => {
let found = false
if (_content) _content.destroy()

for (let path in options) {
if (Object.prototype.hasOwnProperty.call(options, path)) {
for (let path in paths) {
if (Object.prototype.hasOwnProperty.call(paths, path)) {
const sections = path.split('/')
const regexPath = getPathRegex(sections)
const regexPath = getPathRegex(sections, pathRegex)
const matches = location.pathname.match(new RegExp(`^${regexPath}$`))
if (matches !== null) {
const pathVariables = getPathVariables(sections, matches)
_content = getContent(options, path, _target, pathVariables)
const pathVariables = getPathVariables(sections, matches, pathRegex)
_content = getContent(paths, path, _target, pathVariables)
found = true
break
}
}
}
if (!found && options[DEFAULT_ROUTE]) {
_content = getContent(options, DEFAULT_ROUTE, _target, {})
if (!found && paths[defaultRoute]) {
_content = getContent(paths, defaultRoute, _target, {})
}
}

Expand Down