From 234dc04f83242563d9c2cdf6f2d8b8cac68efd40 Mon Sep 17 00:00:00 2001 From: andrewd Date: Tue, 27 Nov 2018 17:29:18 +0000 Subject: [PATCH] Allow for options to be passed to UrlPattern --- README.md | 5 +++-- src/util/create-matcher.js | 2 +- test/test-util/fixtures/routes.js | 4 ++++ test/util/create-matcher.spec.js | 10 ++++++++++ 4 files changed, 18 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index d552150b..9cc12ba0 100644 --- a/README.md +++ b/README.md @@ -49,7 +49,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 for 'url-pattern' can be set in patternOptions const routes = { '/messages': { title: 'Message' @@ -64,7 +64,8 @@ const routes = { '/bio': { title: 'Biographies', '/:name': { - title: 'Biography for:' + title: 'Biography for:', + patternOptions: {segmentValueCharset: 'a-zA-Z_'}, } } } diff --git a/src/util/create-matcher.js b/src/util/create-matcher.js index 120da921..3ccce4d6 100644 --- a/src/util/create-matcher.js +++ b/src/util/create-matcher.js @@ -41,7 +41,7 @@ export default (routes: Object) => { .reverse() .map(route => ({ route, - pattern: new UrlPattern(route), + pattern: new UrlPattern(route, routes[route].patternOptions || {}), result: routes[route] })); diff --git a/test/test-util/fixtures/routes.js b/test/test-util/fixtures/routes.js index d4b0943b..484b51c7 100644 --- a/test/test-util/fixtures/routes.js +++ b/test/test-util/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-_~ %@.' } + }, '/': { '/oh': { name: 'oh', diff --git a/test/util/create-matcher.spec.js b/test/util/create-matcher.spec.js index 51514b9b..da197832 100644 --- a/test/util/create-matcher.spec.js +++ b/test/util/create-matcher.spec.js @@ -70,5 +70,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' + } + }); }); });