From cb6e810411d8a304a28353885874a25f690869a2 Mon Sep 17 00:00:00 2001 From: David Fraser Date: Tue, 31 Jan 2017 22:08:45 +0200 Subject: [PATCH 1/6] Allow routes to specify `patternOptions` to pass through to `url-pattern` --- src/create-matcher.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/create-matcher.js b/src/create-matcher.js index f67c7fbc..a2d080ec 100644 --- a/src/create-matcher.js +++ b/src/create-matcher.js @@ -71,7 +71,7 @@ export default (routes: Object, wildcard: bool = false) => { pattern: new UrlPattern( // Prepend with wildcards if requested `${route}${wildcard && '*' || ''}` - ), + , routes[route].patternOptions || {}), result: routes[route] })); From 9a7e2e1978fffd4f63a269b0df70d97435715dcc Mon Sep 17 00:00:00 2001 From: David Fraser Date: Tue, 31 Jan 2017 22:09:02 +0200 Subject: [PATCH 2/6] Add a note and example for how `url-pattern` options can be set --- README.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) 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_'}, } } } From 3732ee1711c231cd9b3c7c14ba1c419701d6db58 Mon Sep 17 00:00:00 2001 From: David Fraser Date: Tue, 31 Jan 2017 22:23:59 +0200 Subject: [PATCH 3/6] Add test for `patternOptions` Note that this reveals the unintended side-effect that the `patternOptions` are included in the matching `route` --- test/create-matcher.spec.js | 11 +++++++++++ test/fixtures/routes.js | 4 ++++ 2 files changed, 15 insertions(+) diff --git a/test/create-matcher.spec.js b/test/create-matcher.spec.js index a90c3a0e..fa4efb26 100644 --- a/test/create-matcher.spec.js +++ b/test/create-matcher.spec.js @@ -52,5 +52,16 @@ 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', + patternOptions: {segmentValueCharset: 'a-zA-Z0-9-_~ %@.'} + } + }); }); }); diff --git a/test/fixtures/routes.js b/test/fixtures/routes.js index 95d158a5..ddcd6a28 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', From 7a581eb820b2385f88bffa534e17bfd36e747f8a Mon Sep 17 00:00:00 2001 From: David Fraser Date: Tue, 31 Jan 2017 22:29:58 +0200 Subject: [PATCH 4/6] Add `lodash.omit` so that we can remove `patternOptions` from route matches --- package.json | 1 + yarn.lock | 4 ++++ 2 files changed, 5 insertions(+) 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/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" From 8e56589d74faaa4ed68c01d73852b07a897bbe25 Mon Sep 17 00:00:00 2001 From: David Fraser Date: Tue, 31 Jan 2017 22:30:16 +0200 Subject: [PATCH 5/6] if `patternOptions` is defined on the route, omit it from the result --- src/create-matcher.js | 4 +++- test/create-matcher.spec.js | 1 - 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/src/create-matcher.js b/src/create-matcher.js index a2d080ec..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, @@ -72,7 +73,8 @@ export default (routes: Object, wildcard: bool = false) => { // Prepend with wildcards if requested `${route}${wildcard && '*' || ''}` , routes[route].patternOptions || {}), - result: routes[route] + 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 fa4efb26..9b72dbe1 100644 --- a/test/create-matcher.spec.js +++ b/test/create-matcher.spec.js @@ -60,7 +60,6 @@ describe('createMatcher', () => { }, result: { name: 'custom', - patternOptions: {segmentValueCharset: 'a-zA-Z0-9-_~ %@.'} } }); }); From 9b80e1d578871f59a4086d2ff40ae2e528b6ac1c Mon Sep 17 00:00:00 2001 From: David Fraser Date: Tue, 31 Jan 2017 22:41:48 +0200 Subject: [PATCH 6/6] eslint fixes --- test/create-matcher.spec.js | 2 +- test/fixtures/routes.js | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/test/create-matcher.spec.js b/test/create-matcher.spec.js index 9b72dbe1..cfe35def 100644 --- a/test/create-matcher.spec.js +++ b/test/create-matcher.spec.js @@ -59,7 +59,7 @@ describe('createMatcher', () => { customparam: 'doot@dootmail.com' }, result: { - name: 'custom', + name: 'custom' } }); }); diff --git a/test/fixtures/routes.js b/test/fixtures/routes.js index ddcd6a28..756e0b3d 100644 --- a/test/fixtures/routes.js +++ b/test/fixtures/routes.js @@ -18,7 +18,7 @@ export default flattenRoutes({ }, '/home/email/:customparam': { name: 'custom', - patternOptions: {segmentValueCharset: 'a-zA-Z0-9-_~ %@.'} + patternOptions: { segmentValueCharset: 'a-zA-Z0-9-_~ %@.' } }, '/': { '/play': {