Skip to content

Commit

Permalink
Merge pull request #120 from mergebandit/master
Browse files Browse the repository at this point in the history
Do not encode null values for params
  • Loading branch information
fridays authored Dec 8, 2017
2 parents 9443978 + dce003b commit 2472ac4
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 10 deletions.
23 changes: 13 additions & 10 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -169,13 +169,16 @@ class Route {
}
}

const toQuerystring = obj => Object.keys(obj).map(key => {
let value = obj[key]
if (Array.isArray(value)) {
value = value.join('/')
}
return [
encodeURIComponent(key),
encodeURIComponent(value)
].join('=')
}).join('&')
const toQuerystring = obj => Object.keys(obj)
.filter(key => obj[key] !== null && obj[key] !== undefined)
.map(key => {
let value = obj[key]

if (Array.isArray(value)) {
value = value.join('/')
}
return [
encodeURIComponent(key),
encodeURIComponent(value)
].join('=')
}).join('&')
8 changes: 8 additions & 0 deletions test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,14 @@ describe('Routes', () => {
expect(setup('a').route.getUrls()).toEqual({as: '/a', href: '/a?'})
})

test('do not pass "null" for params that have null values', () => {
const {route} = setup('a', '/a/:b/:c?')
const params = {b: 'b', c: null, d: undefined}
const expected = {as: '/a/b?', href: '/a?b=b'}
expect(route.getUrls(params)).toEqual(expected)
expect(setup('a').route.getUrls()).toEqual({as: '/a', href: '/a?'})
})

test('ensure "as" when path match is empty', () => {
expect(setup('a', '/:a?').route.getAs()).toEqual('/')
})
Expand Down

0 comments on commit 2472ac4

Please sign in to comment.