From 3980765613cc12336bf251da288f3dfaa4023c0b Mon Sep 17 00:00:00 2001 From: Scott Silvi Date: Wed, 29 Nov 2017 11:07:38 -0600 Subject: [PATCH 1/2] Do not encode null values for params --- src/index.js | 3 +++ test/index.test.js | 8 ++++++++ 2 files changed, 11 insertions(+) diff --git a/src/index.js b/src/index.js index b02f3fb1..3f8d29e3 100644 --- a/src/index.js +++ b/src/index.js @@ -171,6 +171,9 @@ class Route { const toQuerystring = obj => Object.keys(obj).map(key => { let value = obj[key] + if (value === null) { + value = '' + } if (Array.isArray(value)) { value = value.join('/') } diff --git a/test/index.test.js b/test/index.test.js index fc69187d..eaf32a18 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} + const expected = {as: '/a/b', href: '/a?b=b&c='} + 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('/') }) From dce003b2e08a4f5b3ec104812e42feae46dfe5b4 Mon Sep 17 00:00:00 2001 From: Scott Silvi Date: Tue, 5 Dec 2017 22:54:03 -0600 Subject: [PATCH 2/2] Filtering query string for values that are null or undefined --- src/index.js | 26 +++++++++++++------------- test/index.test.js | 4 ++-- 2 files changed, 15 insertions(+), 15 deletions(-) diff --git a/src/index.js b/src/index.js index 3f8d29e3..473ee379 100644 --- a/src/index.js +++ b/src/index.js @@ -169,16 +169,16 @@ class Route { } } -const toQuerystring = obj => Object.keys(obj).map(key => { - let value = obj[key] - if (value === null) { - value = '' - } - 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 eaf32a18..84d2cb4e 100644 --- a/test/index.test.js +++ b/test/index.test.js @@ -76,8 +76,8 @@ describe('Routes', () => { test('do not pass "null" for params that have null values', () => { const {route} = setup('a', '/a/:b/:c?') - const params = {b: 'b', c: null} - const expected = {as: '/a/b', href: '/a?b=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?'}) })