From 1b3618bb65c46653e855a1561b441a1bd08ca4f8 Mon Sep 17 00:00:00 2001 From: Benjamin Rokseth Date: Tue, 27 Feb 2018 21:50:48 +0100 Subject: [PATCH 1/4] Move default route and pathRegex to options param --- README.md | 4 ++++ example/app.html | 8 +++++--- src/utils/create-router.js | 40 +++++++++++++++++++------------------- 3 files changed, 29 insertions(+), 23 deletions(-) diff --git a/README.md b/README.md index 66cf407..9158d7e 100644 --- a/README.md +++ b/README.md @@ -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 @@ -53,6 +54,9 @@ yarn add svelte-router } }) } + },{ + pathRegex: '[a-zA-Z]+', + defaultRoute: NotFound }) export default { diff --git a/example/app.html b/example/app.html index a6ffe44..d7ed084 100644 --- a/example/app.html +++ b/example/app.html @@ -36,9 +36,11 @@ data: { salutation: 'Hello' } - }, - }, - default: NotFound + } + } + },{ + pathRegex: '[a-zA-Z]+', + defaultRoute: NotFound }) createRouter.listen(() => { console.log('router changed') diff --git a/src/utils/create-router.js b/src/utils/create-router.js index 84b54c8..4205434 100644 --- a/src/utils/create-router.js +++ b/src/utils/create-router.js @@ -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) => { @@ -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 @@ -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-Z]+' + 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, {}) } } From 26e0d7da8207547bb65140d6b368a6b514b5442c Mon Sep 17 00:00:00 2001 From: Benjamin Rokseth Date: Thu, 1 Mar 2018 13:08:28 +0100 Subject: [PATCH 2/4] Re-insert default path route match --- src/utils/create-router.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/utils/create-router.js b/src/utils/create-router.js index 4205434..4b1aaf7 100644 --- a/src/utils/create-router.js +++ b/src/utils/create-router.js @@ -3,7 +3,7 @@ import history from './history' const getPathRegex = (sections, pathRegex) => { return sections.map((value) => { if (value.match(new RegExp(`:${pathRegex}`)) !== null) { - return `(${pathRegex})` + return `([a-zA-Z0-9]+)` } return value }).join('\\/') From 835e40129ee490f33f14e4851eac11712a464ecf Mon Sep 17 00:00:00 2001 From: Benjamin Rokseth Date: Thu, 1 Mar 2018 15:25:54 +0100 Subject: [PATCH 3/4] Fix previous commit, change default path regex in fallback --- src/utils/create-router.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/utils/create-router.js b/src/utils/create-router.js index 4b1aaf7..2898df8 100644 --- a/src/utils/create-router.js +++ b/src/utils/create-router.js @@ -3,7 +3,7 @@ import history from './history' const getPathRegex = (sections, pathRegex) => { return sections.map((value) => { if (value.match(new RegExp(`:${pathRegex}`)) !== null) { - return `([a-zA-Z0-9]+)` + return `(${pathRegex})` } return value }).join('\\/') @@ -48,7 +48,7 @@ const createRouter = (paths, options = {}) => { let _unlisten // history listener let _content // route instance - const pathRegex = options['pathRegex'] ? options['pathRegex'] : '[a-zA-Z]+' + const pathRegex = options['pathRegex'] ? options['pathRegex'] : '[a-zA-Z0-9]+' const defaultRoute = options['defaultRoute'] ? options['defaultRoute'] : 'default' const handleRouteChange = location => { From d1fbca82e6e288bb5f40e50e97065da24e638121 Mon Sep 17 00:00:00 2001 From: Benjamin Rokseth Date: Mon, 5 Mar 2018 10:29:15 +0100 Subject: [PATCH 4/4] Update rollup and node-resolve plugin --- package.json | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/package.json b/package.json index 7bdf54a..ba02f74 100644 --- a/package.json +++ b/package.json @@ -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",