diff --git a/src/index.js b/src/index.js index b02f3fb1..473ee379 100644 --- a/src/index.js +++ b/src/index.js @@ -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('&') diff --git a/test/index.test.js b/test/index.test.js index fc69187d..84d2cb4e 100644 --- a/test/index.test.js +++ b/test/index.test.js @@ -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('/') })