From 37165bd8ab1720cbd3c52f2a254d6bd39dcb95ac Mon Sep 17 00:00:00 2001 From: Thomas Roch Date: Thu, 23 Jul 2015 10:27:31 +0100 Subject: [PATCH] feat: improve matching by sorting routes by path length Issue router/router5#8 --- dist/amd/route-node.js | 2 +- dist/commonjs/route-node.js | 2 +- dist/umd/route-node.js | 2 +- modules/RouteNode.js | 2 +- test/main.js | 11 +++++++++++ 5 files changed, 15 insertions(+), 4 deletions(-) diff --git a/dist/amd/route-node.js b/dist/amd/route-node.js index 0106508..1dea25a 100644 --- a/dist/amd/route-node.js +++ b/dist/amd/route-node.js @@ -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 diff --git a/dist/commonjs/route-node.js b/dist/commonjs/route-node.js index e9988b0..b845834 100644 --- a/dist/commonjs/route-node.js +++ b/dist/commonjs/route-node.js @@ -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 diff --git a/dist/umd/route-node.js b/dist/umd/route-node.js index 2b60219..ef1db2d 100644 --- a/dist/umd/route-node.js +++ b/dist/umd/route-node.js @@ -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 diff --git a/modules/RouteNode.js b/modules/RouteNode.js index 83c4fb3..5551ed9 100644 --- a/modules/RouteNode.js +++ b/modules/RouteNode.js @@ -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 diff --git a/test/main.js b/test/main.js index 78e2b12..a0dff32 100644 --- a/test/main.js +++ b/test/main.js @@ -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: {}}); + }); });