Skip to content

Commit 812f60e

Browse files
jochenbergerryanflorence
authored andcommitted
Don't pass 'undefined' for missing optional parameters (#4064)
* Don't try do decode `undefined` decodeURIComponent(undefined) is "undefined" * add a test * do not pass missing optional parameters at all
1 parent 0aaac61 commit 812f60e

File tree

2 files changed

+19
-1
lines changed

2 files changed

+19
-1
lines changed

modules/__tests__/Match-test.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,24 @@ describe('Match', () => {
7171
})
7272
})
7373
})
74+
it('does not pass optinal parameters if they do not occur in the URL', () => {
75+
renderToString(
76+
<Match
77+
pattern="/:foo/:bar?"
78+
location={{ pathname: '/foo' }}
79+
component={(props) => {
80+
expect(props).toEqual({
81+
params: { foo: 'foo' },
82+
isExact: true,
83+
pathname: '/foo',
84+
location: { pathname: '/foo' },
85+
pattern: '/:foo/:bar?'
86+
})
87+
return <div />
88+
}}
89+
/>
90+
)
91+
})
7492
})
7593

7694
describe('when matched partially', () => {

modules/matchPattern.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ const getMatcher = (pattern, exactly) => {
2222
}
2323

2424
const parseParams = (pattern, match, keys) =>
25-
match.slice(1).reduce((params, value, index) => {
25+
match.slice(1).filter(value => value !== undefined).reduce((params, value, index) => {
2626
params[keys[index].name] = decodeURIComponent(value)
2727
return params
2828
}, {})

0 commit comments

Comments
 (0)