diff --git a/README.md b/README.md index 44adc52d..5f9caa39 100644 --- a/README.md +++ b/README.md @@ -46,7 +46,7 @@ import yourReducer from './your-app'; // Useful for page titles and other route-specific data. // Uses https://github.com/snd/url-pattern for URL matching -// and parameter extraction. +// and parameter extraction. options can be set in patternOptions const routes = { '/messages': { title: 'Message' @@ -62,6 +62,7 @@ const routes = { title: 'Biographies', '/:name': { title: 'Biography for:' + patternOptions: {segmentValueCharset: 'a-zA-Z_'}, } } } diff --git a/package.json b/package.json index 4376bac0..967aab42 100644 --- a/package.json +++ b/package.json @@ -46,6 +46,7 @@ "dependencies": { "history": "^4.4.1", "lodash.assign": "^4.2.0", + "lodash.omit": "^4.2.0", "query-string": "^4.2.3", "url-pattern": "^1.0.1" }, diff --git a/src/create-matcher.js b/src/create-matcher.js index f67c7fbc..31eda5b0 100644 --- a/src/create-matcher.js +++ b/src/create-matcher.js @@ -1,5 +1,6 @@ // @flow import UrlPattern from 'url-pattern'; +import omit from 'lodash.omit'; type RouteCache = { route: string, @@ -71,8 +72,9 @@ export default (routes: Object, wildcard: bool = false) => { pattern: new UrlPattern( // Prepend with wildcards if requested `${route}${wildcard && '*' || ''}` - ), - result: routes[route] + , routes[route].patternOptions || {}), + result: 'patternOptions' in routes[route] ? + omit(routes[route], 'patternOptions') : routes[route] })); return wildcard diff --git a/test/create-matcher.spec.js b/test/create-matcher.spec.js index a90c3a0e..cfe35def 100644 --- a/test/create-matcher.spec.js +++ b/test/create-matcher.spec.js @@ -52,5 +52,15 @@ describe('createMatcher', () => { name: '3spooky5me' } }); + + expect(matchRoute('/home/email/doot@dootmail.com')).to.deep.equal({ + route: '/home/email/:customparam', + params: { + customparam: 'doot@dootmail.com' + }, + result: { + name: 'custom' + } + }); }); }); diff --git a/test/fixtures/routes.js b/test/fixtures/routes.js index 95d158a5..756e0b3d 100644 --- a/test/fixtures/routes.js +++ b/test/fixtures/routes.js @@ -16,6 +16,10 @@ export default flattenRoutes({ '/home/:spookyparam': { name: '3spooky5me' }, + '/home/email/:customparam': { + name: 'custom', + patternOptions: { segmentValueCharset: 'a-zA-Z0-9-_~ %@.' } + }, '/': { '/play': { name: 'play', diff --git a/yarn.lock b/yarn.lock index 0ee60e6b..bb20d87c 100644 --- a/yarn.lock +++ b/yarn.lock @@ -3704,6 +3704,10 @@ lodash.memoize@^4.1.0: version "4.1.2" resolved "https://registry.yarnpkg.com/lodash.memoize/-/lodash.memoize-4.1.2.tgz#bcc6c49a42a2840ed997f323eada5ecd182e0bfe" +lodash.omit@^4.2.0: + version "4.5.0" + resolved "https://registry.yarnpkg.com/lodash.omit/-/lodash.omit-4.5.0.tgz#6eb19ae5a1ee1dd9df0b969e66ce0b7fa30b5e60" + lodash.pick@^4.2.1: version "4.4.0" resolved "https://registry.yarnpkg.com/lodash.pick/-/lodash.pick-4.4.0.tgz#52f05610fff9ded422611441ed1fc123a03001b3"