From 9614a8bf2e32dadaa1896217aa2082d5af99cb51 Mon Sep 17 00:00:00 2001 From: Vincent Orr Date: Sun, 8 May 2016 22:42:09 +0100 Subject: [PATCH 1/3] fix() issue #16 --- src/index.js | 9 ++++++++- test/index.js | 28 ++++++++++++++++++++++++++-- 2 files changed, 34 insertions(+), 3 deletions(-) diff --git a/src/index.js b/src/index.js index 358076c..f4443f5 100644 --- a/src/index.js +++ b/src/index.js @@ -77,6 +77,14 @@ function switchPath(sourcePath, routes) { let matchedValue = null traverseRoutes(routes, function matchPattern(pattern) { + if (pattern === `/`) { + // TODO: check if `/` & `*` are present at the same time in sourcePath + console.log(sourcePath) + console.log(routes) + matchedPath = `/` + matchedValue = routes[pattern] + } + if (sourcePath.search(pattern) === 0 && betterMatch(pattern, matchedPath)) { matchedPath = pattern matchedValue = routes[pattern] @@ -105,7 +113,6 @@ function switchPath(sourcePath, routes) { } } }) - return validate({sourcePath, matchedPath, matchedValue, routes}) } diff --git a/test/index.js b/test/index.js index 7c81e71..f0d117b 100644 --- a/test/index.js +++ b/test/index.js @@ -109,8 +109,32 @@ describe('switchPath basic usage', () => { expect(value).to.be.equal(null); }); + // TODO: fix the test to work as expected + it('should match a root base path when using `/` and `*`', () => { + const {path, value} = switchPath('/abc', { + '/': 123, + '/home': 456, + '/bar': 789, + '*': 'Route not defined' + }); + expect(path).to.be.equal('/abc'); + expect(value).to.be.equal('Route not defined'); + }); + + // TODO: fix the test to work as expected + it('should still match a notFound pattern when using `/` and `*`', () => { + const {path, value} = switchPath('/', { + '/': 123, + '/home': 456, + '/bar': 789, + '*': 'Route not defined' + }); + expect(path).to.be.equal('/'); + expect(value).to.be.equal(123); + }); + it('should return match to a notFound pattern if provided', () => { - const {path, value} = switchPath('/home/33/books/10', { + const {path, value} = switchPath('/123', { '/': 123, '/authors': 234, '/books': { @@ -119,7 +143,7 @@ describe('switchPath basic usage', () => { }, '*': 'Route not defined' }); - expect(path).to.be.equal('/home/33/books/10'); + expect(path).to.be.equal('/123'); expect(value).to.be.equal('Route not defined'); }); From 4d1d678b3d2e72fbb03e1344b37234f789b55398 Mon Sep 17 00:00:00 2001 From: Vincent Orr Date: Mon, 9 May 2016 08:37:59 +0100 Subject: [PATCH 2/3] fix() edge case on $ --- src/index.js | 28 +++++++++++++++++--------- test/index.js | 56 +++++++++++++++++++++++++++++---------------------- 2 files changed, 51 insertions(+), 33 deletions(-) diff --git a/src/index.js b/src/index.js index f4443f5..fdcfcb6 100644 --- a/src/index.js +++ b/src/index.js @@ -65,8 +65,13 @@ function validate({sourcePath, matchedPath, matchedValue, routes}) { let path = matchedPath ? validatePath(sourcePath, matchedPath) : null let value = matchedValue if (!path) { - path = routes[`*`] ? sourcePath : null - value = path ? routes[`*`] : null + if (sourcePath === `/`) { + path = routes[`*`] ? sourcePath : null + value = path ? routes[`/$`] : null + } else { + path = routes[`*`] ? sourcePath : null + value = path ? routes[`*`] : null + } } return {path, value} } @@ -76,13 +81,18 @@ function switchPath(sourcePath, routes) { let matchedPath = null let matchedValue = null - traverseRoutes(routes, function matchPattern(pattern) { - if (pattern === `/`) { - // TODO: check if `/` & `*` are present at the same time in sourcePath - console.log(sourcePath) - console.log(routes) - matchedPath = `/` - matchedValue = routes[pattern] + traverseRoutes(routes, function matchPattern(pattern) { // eslint-disable-line complexity, max-len + if (pattern[pattern.length - 1] === `$`) { + const realPattern = pattern.split(`/$`).join(``) + if (sourcePath.search(realPattern) === 0 && + betterMatch(pattern, matchedPath) || + sourcePath.search(realPattern + `/`) && + betterMatch(pattern, matchedPath)) + { + matchedPath = realPattern + matchedValue = routes[pattern] + } + return } if (sourcePath.search(pattern) === 0 && betterMatch(pattern, matchedPath)) { diff --git a/test/index.js b/test/index.js index f0d117b..a921a84 100644 --- a/test/index.js +++ b/test/index.js @@ -109,30 +109,6 @@ describe('switchPath basic usage', () => { expect(value).to.be.equal(null); }); - // TODO: fix the test to work as expected - it('should match a root base path when using `/` and `*`', () => { - const {path, value} = switchPath('/abc', { - '/': 123, - '/home': 456, - '/bar': 789, - '*': 'Route not defined' - }); - expect(path).to.be.equal('/abc'); - expect(value).to.be.equal('Route not defined'); - }); - - // TODO: fix the test to work as expected - it('should still match a notFound pattern when using `/` and `*`', () => { - const {path, value} = switchPath('/', { - '/': 123, - '/home': 456, - '/bar': 789, - '*': 'Route not defined' - }); - expect(path).to.be.equal('/'); - expect(value).to.be.equal(123); - }); - it('should return match to a notFound pattern if provided', () => { const {path, value} = switchPath('/123', { '/': 123, @@ -199,6 +175,38 @@ describe('switchPath basic usage', () => { expect(path).to.be.equal('/1736'); expect(value).to.be.equal('id is 1736'); }); + + it('should match routes explicitly using `$`', () => { + const {path, value} = switchPath('/', { + '/$': 123, + '/other': 456, + '*': 'not found route' + }) + + expect(path).to.be.equal('/'); + expect(value).to.be.equal(123); + }) + + it('should match routes explicitly using `$`', () => { + const {path, value} = switchPath('/other', { + '/': 123, + '/other/$': 456 + }) + + expect(path).to.be.equal('/other'); + expect(value).to.be.equal(456); + }) + + it('should allow opting out of partial matching for routes using `$`', () => { + const {path, value} = switchPath('/random', { + '/$': 123, + '/other': 456, + '*': 'not found route' + }); + + expect(path).to.be.equal('/random'); + expect(value).to.be.equal('not found route'); + }); }); describe('switchPath corner cases', () => { From cff5525ae84e831721479a3361cb1901bcff396f Mon Sep 17 00:00:00 2001 From: Vincent Orr Date: Mon, 9 May 2016 08:43:31 +0100 Subject: [PATCH 3/3] chore() put test back to original value --- test/index.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/index.js b/test/index.js index a921a84..46c4a11 100644 --- a/test/index.js +++ b/test/index.js @@ -110,7 +110,7 @@ describe('switchPath basic usage', () => { }); it('should return match to a notFound pattern if provided', () => { - const {path, value} = switchPath('/123', { + const {path, value} = switchPath('/home/33/books/10', { '/': 123, '/authors': 234, '/books': { @@ -119,7 +119,7 @@ describe('switchPath basic usage', () => { }, '*': 'Route not defined' }); - expect(path).to.be.equal('/123'); + expect(path).to.be.equal('/home/33/books/10'); expect(value).to.be.equal('Route not defined'); });