Skip to content
This repository has been archived by the owner on Dec 15, 2018. It is now read-only.

Allow url-pattern options to be specified on routes #136

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand All @@ -62,6 +62,7 @@ const routes = {
title: 'Biographies',
'/:name': {
title: 'Biography for:'
patternOptions: {segmentValueCharset: 'a-zA-Z_'},
}
}
}
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
},
Expand Down
6 changes: 4 additions & 2 deletions src/create-matcher.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// @flow
import UrlPattern from 'url-pattern';
import omit from 'lodash.omit';

type RouteCache = {
route: string,
Expand Down Expand Up @@ -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
Expand Down
10 changes: 10 additions & 0 deletions test/create-matcher.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,5 +52,15 @@ describe('createMatcher', () => {
name: '3spooky5me'
}
});

expect(matchRoute('/home/email/[email protected]')).to.deep.equal({
route: '/home/email/:customparam',
params: {
customparam: '[email protected]'
},
result: {
name: 'custom'
}
});
});
});
4 changes: 4 additions & 0 deletions test/fixtures/routes.js
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
4 changes: 4 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down