Skip to content

Commit

Permalink
fix: router sort (#1009)
Browse files Browse the repository at this point in the history
  • Loading branch information
czy88840616 authored Apr 24, 2021
1 parent fcd0932 commit e9bf8ed
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 0 deletions.
21 changes: 21 additions & 0 deletions packages/core/src/util/webRouterCollector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
getPropertyDataFromClass,
getPropertyMetadata,
getProviderId,
isRegExp,
listModule,
PRIORITY_KEY,
RouterOption,
Expand Down Expand Up @@ -381,6 +382,19 @@ export class WebRouterCollector {
return urlMatchList
.map(item => {
const urlString = item.url.toString();
const weightArr = isRegExp(item.url)
? urlString.split('/')
: urlString.split('/');
let weight = 0;
// 权重,比如通配的不加权,非通配加权,防止通配出现在最前面
for (const fragment of weightArr) {
if (fragment.includes(':') || fragment.includes('*')) {
weight += 0;
} else {
weight += 1;
}
}

let category = 2;
const paramString = urlString.includes(':')
? urlString.replace(/:.+$/, '')
Expand All @@ -397,14 +411,21 @@ export class WebRouterCollector {
_level: urlString.split('/').length - 1,
_paramString: paramString,
_category: category,
_weight: weight,
};
})
.sort((handlerA, handlerB) => {
// 不同一层级的对比
if (handlerA._category !== handlerB._category) {
return handlerB._category - handlerA._category;
}
// 不同长度
if (handlerA._level === handlerB._level) {
// 不同权重
if (handlerA._weight !== handlerB._weight) {
return handlerB._weight - handlerA._weight;
}

if (handlerB._pureRouter === handlerA._pureRouter) {
return (
handlerA.url.toString().length - handlerB.url.toString().length
Expand Down
31 changes: 31 additions & 0 deletions packages/core/test/router.js
Original file line number Diff line number Diff line change
Expand Up @@ -158,3 +158,34 @@ exports.routerList2 = [
'_level': 1
}
];

exports.routerList3 = [
{
prefix: '/',
url: '/:page/page'
},
{
prefix: '/',
url: '/page/:page'
},
{
prefix: '/',
url: '/:category/:slug'
}
];

exports.routerList4 = [
{
prefix: '/',
url: '/page/:page'
},
{
prefix: '/',
url: '/:page/page'
},
{
prefix: '/',
url: '/:category/:slug'
}
];

13 changes: 13 additions & 0 deletions packages/core/test/webRouterCollector.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,4 +98,17 @@ describe('/test/webRouterCollector.test.ts', function () {
expect(result[1].url).toEqual('/');
expect(result[2].url).toEqual('/*');
});

it('fix issue 1008', function () {
const collector = new WebRouterCollector();
const result1 = collector.sortRouter(require('./router').routerList3);
expect(result1[0].url).toEqual('/:page/page');
expect(result1[1].url).toEqual('/page/:page');
expect(result1[2].url).toEqual('/:category/:slug');

const result2 = collector.sortRouter(require('./router').routerList4);
expect(result2[0].url).toEqual('/page/:page');
expect(result2[1].url).toEqual('/:page/page');
expect(result2[2].url).toEqual('/:category/:slug');
});
});

0 comments on commit e9bf8ed

Please sign in to comment.