diff --git a/src/index.js b/src/index.js index d4d3845..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) { @@ -70,6 +70,9 @@ function validate({sourcePath, matchedPath, matchedValue, routes}) { if (!path) { path = routes[`*`] ? sourcePath : null value = path ? routes[`*`] : null + } else if (routes[`*`] && betterMatch(sourcePath, path)) { + path = sourcePath + value = routes[`*`] } return {path, value} } diff --git a/test/index.js b/test/index.js index 0d5b1ef..01296a5 100644 --- a/test/index.js +++ b/test/index.js @@ -135,6 +135,88 @@ 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 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,