Skip to content

Commit

Permalink
feat: improve matching by sorting routes by path length
Browse files Browse the repository at this point in the history
Issue router/router5#8
  • Loading branch information
troch committed Jul 23, 2015
1 parent 53a5bcc commit 37165bd
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 4 deletions.
2 changes: 1 addition & 1 deletion dist/amd/route-node.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ define(['exports', 'module', 'path-parser'], function (exports, module, _pathPar
if (!b.parser.hasSpatParam && a.parser.hasSpatParam) return 1;
if (!a.parser.hasUrlParams && b.parser.hasUrlParams) return -1;
if (!b.parser.hasUrlParams && a.parser.hasUrlParams) return 1;
return 0;
return a.path.length < b.path.length;
});
} else {
// Locate parent node
Expand Down
2 changes: 1 addition & 1 deletion dist/commonjs/route-node.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ var RouteNode = (function () {
if (!b.parser.hasSpatParam && a.parser.hasSpatParam) return 1;
if (!a.parser.hasUrlParams && b.parser.hasUrlParams) return -1;
if (!b.parser.hasUrlParams && a.parser.hasUrlParams) return 1;
return 0;
return a.path.length < b.path.length;
});
} else {
// Locate parent node
Expand Down
2 changes: 1 addition & 1 deletion dist/umd/route-node.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@
if (!b.parser.hasSpatParam && a.parser.hasSpatParam) return 1;
if (!a.parser.hasUrlParams && b.parser.hasUrlParams) return -1;
if (!b.parser.hasUrlParams && a.parser.hasUrlParams) return 1;
return 0;
return a.path.length < b.path.length;
});
} else {
// Locate parent node
Expand Down
2 changes: 1 addition & 1 deletion modules/RouteNode.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ export default class RouteNode {
if (!b.parser.hasSpatParam && a.parser.hasSpatParam) return 1
if (!a.parser.hasUrlParams && b.parser.hasUrlParams) return -1
if (!b.parser.hasUrlParams && a.parser.hasUrlParams) return 1
return 0;
return a.path && b.path ? a.path.length < b.path.length : 0;
})
} else {
// Locate parent node
Expand Down
11 changes: 11 additions & 0 deletions test/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,17 @@ describe('RouteNode', function () {
rootNode.buildPath('users.view', {id: 1}).should.equal('/users/view/1');
rootNode.buildPath('users.list', {id: 1}).should.equal('/users/list');
});

it('should sort paths by length', function () {
var rootNode = new RouteNode('', '')
.addNode('index', '/')
.addNode('abo', '/abo')
.addNode('about', '/about');

rootNode.matchPath('/').should.eql({name: 'index', params: {}});
rootNode.matchPath('/abo').should.eql({name: 'abo', params: {}});
rootNode.matchPath('/about').should.eql({name: 'about', params: {}});
});
});


Expand Down

0 comments on commit 37165bd

Please sign in to comment.