From e439d6f18f0514b6906daeea2ea1cc2135313a64 Mon Sep 17 00:00:00 2001 From: Scott Hardy Date: Fri, 2 Dec 2016 14:16:49 -0800 Subject: [PATCH 1/4] test(): add failing non-nested notFound test case --- test/index.js | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/test/index.js b/test/index.js index 0d5b1ef..a097e50 100644 --- a/test/index.js +++ b/test/index.js @@ -135,6 +135,18 @@ describe('switchPath basic usage', () => { expect(value).to.be.equal('Route not defined'); }); + it('should return match to a notFound pattern if provided non-nested configuration', () => { + const {path, value} = switchPath('/home/33/books/10', { + '/': 123, + '/authors': 234, + '/books': 345, + '/books/:id': 456, + '*': 'Route not defined' + }); + expect(path).to.be.equal('/home/33/books/10'); + expect(value).to.be.equal('Route not defined'); + }); + it('should not prematurely match a notFound pattern', () => { const {path, value} = switchPath('/home/foo', { '*': 0, From 361f8e374bf320fcda1bd0e2b7c5e22b87ac97c5 Mon Sep 17 00:00:00 2001 From: Scott Hardy Date: Fri, 2 Dec 2016 14:56:27 -0800 Subject: [PATCH 2/4] fix(): notFound value is returned correctly when using non-nested configuration --- src/index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/index.js b/src/index.js index d4d3845..58a0ee1 100644 --- a/src/index.js +++ b/src/index.js @@ -67,7 +67,7 @@ function getParamFnValue(paramFn, params) { function validate({sourcePath, matchedPath, matchedValue, routes}) { let path = matchedPath ? validatePath(sourcePath, matchedPath) : null let value = matchedValue - if (!path) { + if (!path || routes[`*`]) { path = routes[`*`] ? sourcePath : null value = path ? routes[`*`] : null } From 15b8e2e6e5ad5f6169f4b6ceaf9704994dfeb53c Mon Sep 17 00:00:00 2001 From: Scott Hardy Date: Fri, 2 Dec 2016 16:10:43 -0800 Subject: [PATCH 3/4] test(): add 6 failing tests against last attempt --- test/index.js | 70 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 70 insertions(+) diff --git a/test/index.js b/test/index.js index a097e50..01296a5 100644 --- a/test/index.js +++ b/test/index.js @@ -147,6 +147,76 @@ describe('switchPath basic usage', () => { expect(value).to.be.equal('Route not defined'); }); + it('should match a base root path when notFound is specified', () => { + const {path, value} = switchPath('/', { + '/': 123, + '*': 'Route not defined' + }); + expect(path).to.be.equal('/'); + expect(value).to.be.equal(123); + }); + + it('should match a base path when notFound is specified', () => { + const {path, value} = switchPath('/books', { + '/': 123, + '/books': 234, + '/books/:id': 345, + '*': 'Route not defined' + }); + expect(path).to.be.equal('/books'); + expect(value).to.be.equal(234); + }); + + it('should match a base parameter path when notFound is specified', () => { + const {path, value} = switchPath('/10', { + '/': 123, + '/:id': id => `id is ${id}`, + '*': 'Route not defined' + }); + expect(path).to.be.equal('/10'); + expect(value).to.be.equal('id is 10'); + }); + + it('should match a base nested paramter path when notFound is specified', () => { + const {path, value} = switchPath('/1736', { + '/': 123, + '/bar': 234, + '/:id': { + '/': id => `id is ${id}`, + '/home': 345 + }, + '*': 'Route not defined' + }); + expect(path).to.be.equal('/1736'); + expect(value).to.be.equal('id is 1736'); + }); + + it('should match a nested root path when notFound is specified', () => { + const {path, value} = switchPath('/books', { + '/': 123, + '/books': { + '/': 234, + '/:id': 345 + }, + '*': 'Route not defined' + }); + expect(path).to.be.equal('/books'); + expect(value).to.be.equal(234); + }); + + it('should match a nested parameter path when notFound is specified', () => { + const {path, value} = switchPath('/books/10', { + '/': 123, + '/books': { + '/': 234, + '/:id': id => `id is ${id}` + }, + '*': 'Route not defined' + }); + expect(path).to.be.equal('/books/10'); + expect(value).to.be.equal('id is 10'); + }); + it('should not prematurely match a notFound pattern', () => { const {path, value} = switchPath('/home/foo', { '*': 0, From ebfe91c43fe602e19dc8d389e56fa7c02c9d10e7 Mon Sep 17 00:00:00 2001 From: Scott Hardy Date: Fri, 2 Dec 2016 16:13:24 -0800 Subject: [PATCH 4/4] fix(): tests pass, and betterMatch() returns true only if candidate is strictly better --- src/index.js | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/src/index.js b/src/index.js index 58a0ee1..369919d 100644 --- a/src/index.js +++ b/src/index.js @@ -42,7 +42,7 @@ function betterMatch(candidate, reference) { if (!validatePath(candidate, reference)) { return false } - return candidate.length >= reference.length + return candidate.length > reference.length } function matchesWithParams(sourcePath, pattern) { @@ -67,9 +67,12 @@ function getParamFnValue(paramFn, params) { function validate({sourcePath, matchedPath, matchedValue, routes}) { let path = matchedPath ? validatePath(sourcePath, matchedPath) : null let value = matchedValue - if (!path || routes[`*`]) { + if (!path) { path = routes[`*`] ? sourcePath : null value = path ? routes[`*`] : null + } else if (routes[`*`] && betterMatch(sourcePath, path)) { + path = sourcePath + value = routes[`*`] } return {path, value} }