diff --git a/lib/Router.js b/lib/Router.js index 46c6b6b..76931f6 100644 --- a/lib/Router.js +++ b/lib/Router.js @@ -48,7 +48,7 @@ function getQueryParams(query) { }; function createRoute(name, path, handler) { - var matcher = new RegExp(path.replace(parametersPattern, '([^\/]+)')); + var matcher = new RegExp(path.replace(parametersPattern, '([^\/]+)') + '$'); var params = (path.match(parametersPattern) || []).map(function (x) { return x.substring(1); }); @@ -56,15 +56,15 @@ function createRoute(name, path, handler) { return { name: name, path: path, handler: handler, matcher: matcher, params: params }; }; -function findRouteParams(routes, path) { +var findRouteParams = function findRouteParams(routes, path) { var params = void 0; var route = routes.find(function (r) { return params = getMatchedParams(r, path); }); - return route && { route: route, params: params }; -} + return { route: route, params: params }; +}; -function parseUrl(url) { +var parseUrl = function parseUrl(url) { var _url$split = url.split('?'); var _url$split2 = _slicedToArray(_url$split, 2); @@ -73,7 +73,11 @@ function parseUrl(url) { var queryString = _url$split2[1]; return { path: path, queryString: queryString }; -} +}; + +var stripPrefix = function stripPrefix(url, prefix) { + return url.replace(new RegExp('^' + prefix), ''); +}; // The actual Router as the default export of the module @@ -107,9 +111,7 @@ var Router = function () { }, { key: 'dispatch', value: function dispatch(url) { - var urlWithoutPrefix = url.replace(new RegExp('^' + this.prefix), ''); - - var _parseUrl = parseUrl(urlWithoutPrefix); + var _parseUrl = parseUrl(stripPrefix(url, this.prefix)); var path = _parseUrl.path; var queryString = _parseUrl.queryString; @@ -132,9 +134,7 @@ var Router = function () { }, { key: 'getCurrentRoute', value: function getCurrentRoute(url) { - var urlWithoutPrefix = url.replace(new RegExp('^' + this.prefix), ''); - - var _parseUrl2 = parseUrl(urlWithoutPrefix); + var _parseUrl2 = parseUrl(stripPrefix(url, this.prefix)); var path = _parseUrl2.path; var queryString = _parseUrl2.queryString; diff --git a/package.json b/package.json index 9269315..3128461 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "minimal-router", - "version": "1.0.3", + "version": "1.0.5", "description": "A minimalist router for client-side javascript applications", "main": "lib/index.js", "scripts": { @@ -8,7 +8,7 @@ "build": "babel src -d lib", "build:browser": "browserify src/index.js -t [ babelify ] --standalone Router | uglifyjs > dist/minimal-router.min.js", "dev": "mocha test --watch --compilers js:babel-register", - "prepublish": "npm test && npm build && npm run build:browser" + "prepublish": "npm test && npm run build && npm run build:browser" }, "repository": { "type": "git",