Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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}
}
Expand Down
82 changes: 82 additions & 0 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down