From 0ef84fedf18cbb4b8b97d256105c636fd2e87193 Mon Sep 17 00:00:00 2001 From: Jason Miller Date: Tue, 22 Sep 2020 19:56:49 -0400 Subject: [PATCH 1/3] Proposal: unmatched optional path segments should be `undefined` in props Fixes #381. --- src/util.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/util.js b/src/util.js index 8bd989c9..d4bfbec2 100644 --- a/src/util.js +++ b/src/util.js @@ -30,12 +30,12 @@ export function exec(url, route, opts) { flags = (route[i].match(/[+*?]+$/) || EMPTY)[0] || '', plus = ~flags.indexOf('+'), star = ~flags.indexOf('*'), - val = url[i] || ''; + val = url[i]; if (!val && !star && (flags.indexOf('?')<0 || plus)) { ret = false; break; } - matches[param] = decodeURIComponent(val); + matches[param] = val && decodeURIComponent(val); if (plus || star) { matches[param] = url.slice(i).map(decodeURIComponent).join('/'); break; From bf1e4f7213d3de30b527ba11deb63aad53e971fd Mon Sep 17 00:00:00 2001 From: Jason Miller Date: Tue, 22 Sep 2020 20:05:55 -0400 Subject: [PATCH 2/3] Update tests --- test/util.test.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/test/util.test.js b/test/util.test.js index 04eab7d7..6743c8c6 100644 --- a/test/util.test.js +++ b/test/util.test.js @@ -85,10 +85,10 @@ describe('util', () => { }); it('should match optional param segments', () => { - expect(exec('/', '/:foo?', {})).toEqual({ foo:'' }); + expect(exec('/', '/:foo?', {})).toEqual({ foo:undefined }); expect(exec('/bar', '/:foo?', {})).toEqual({ foo:'bar' }); - expect(exec('/', '/:foo?/:bar?', {})).toEqual({ foo:'', bar:'' }); - expect(exec('/bar', '/:foo?/:bar?', {})).toEqual({ foo:'bar', bar:'' }); + expect(exec('/', '/:foo?/:bar?', {})).toEqual({ foo:undefined, bar:undefined }); + expect(exec('/bar', '/:foo?/:bar?', {})).toEqual({ foo:'bar', bar:undefined }); expect(exec('/bar', '/:foo?/bar', {})).toEqual(false); expect(exec('/foo/bar', '/:foo?/bar', {})).toEqual({ foo:'foo' }); }); From 3804b14a47b786c1f2c20a563d04f4974b61fb2d Mon Sep 17 00:00:00 2001 From: Jason Miller Date: Tue, 22 Sep 2020 20:14:51 -0400 Subject: [PATCH 3/3] Update util.js --- src/util.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/util.js b/src/util.js index d4bfbec2..3f4f8663 100644 --- a/src/util.js +++ b/src/util.js @@ -35,7 +35,7 @@ export function exec(url, route, opts) { ret = false; break; } - matches[param] = val && decodeURIComponent(val); + matches[param] = decodeURIComponent(val) || undefined; if (plus || star) { matches[param] = url.slice(i).map(decodeURIComponent).join('/'); break;