mirror of
https://github.com/sub-store-org/Sub-Store.git
synced 2026-11-27 06:43:08 +00:00
feat: 优化正则排序效率
This commit is contained in:
3 files changed
+55
-5
No files matched your search
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "sub-store",
|
||||
"version": "2.44.21",
|
||||
"version": "2.44.22",
|
||||
"description": "Advanced Subscription Manager for QX, Loon, Surge, Stash and Shadowrocket.",
|
||||
"main": "src/main.js",
|
||||
"packageManager": "pnpm@11.0.9",
|
||||
|
||||
@@ -346,10 +346,11 @@ function RegexSortOperator(input) {
|
||||
return {
|
||||
name: 'Regex Sort Operator',
|
||||
func: (proxies) => {
|
||||
expressions = expressions.map((expr) => buildRegex(expr));
|
||||
const regexes = expressions.map((expr) => buildRegex(expr));
|
||||
const ranks = new Map();
|
||||
return proxies.sort((a, b) => {
|
||||
const oA = getRegexOrder(expressions, a.name);
|
||||
const oB = getRegexOrder(expressions, b.name);
|
||||
const oA = getRegexOrder(regexes, a.name, ranks);
|
||||
const oB = getRegexOrder(regexes, b.name, ranks);
|
||||
if (oA && !oB) return -1;
|
||||
if (oB && !oA) return 1;
|
||||
if (oA && oB) return oA < oB ? -1 : 1;
|
||||
@@ -365,7 +366,11 @@ function RegexSortOperator(input) {
|
||||
};
|
||||
}
|
||||
|
||||
function getRegexOrder(expressions, str) {
|
||||
function getRegexOrder(expressions, str, ranks) {
|
||||
if (expressions.length === 0) return null;
|
||||
const cached = ranks.get(str);
|
||||
if (cached !== undefined) return cached;
|
||||
|
||||
let order = null;
|
||||
for (let i = 0; i < expressions.length; i++) {
|
||||
if (expressions[i].test(str)) {
|
||||
@@ -373,6 +378,7 @@ function getRegexOrder(expressions, str) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (typeof str === 'string') ranks.set(str, order);
|
||||
return order;
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,44 @@
|
||||
import { expect } from 'chai';
|
||||
import { describe, it } from 'mocha';
|
||||
|
||||
import { ProxyUtils } from '@/core/proxy-utils';
|
||||
|
||||
describe('Regex Sort Operator', function () {
|
||||
it('preserves regex priority and fallback ordering with duplicate names', async function () {
|
||||
const zuluNodes = [
|
||||
{ name: 'zulu', server: 'b.example' },
|
||||
{ name: 'zulu', server: 'a.example' },
|
||||
];
|
||||
const proxies = [
|
||||
zuluNodes[0],
|
||||
{ name: 'JP' },
|
||||
{ name: 'hk' },
|
||||
{ name: 'alpha' },
|
||||
{ name: 'HK' },
|
||||
{ name: 'JP' },
|
||||
zuluNodes[1],
|
||||
];
|
||||
const expressions = ['(?i)^hk', '^(JP|HK)'];
|
||||
for (const [order, unmatched] of [
|
||||
['asc', ['alpha', 'zulu', 'zulu']],
|
||||
['desc', ['zulu', 'zulu', 'alpha']],
|
||||
['original', ['zulu', 'alpha', 'zulu']],
|
||||
]) {
|
||||
const output = await ProxyUtils.process(proxies, [
|
||||
{ type: 'Regex Sort Operator', args: { order, expressions } },
|
||||
]);
|
||||
expect(output.map(({ name }) => name)).to.deep.equal([
|
||||
'hk',
|
||||
'HK',
|
||||
'JP',
|
||||
'JP',
|
||||
...unmatched,
|
||||
]);
|
||||
if (order === 'original') {
|
||||
expect(
|
||||
output.filter(({ name }) => name === 'zulu'),
|
||||
).to.deep.equal(zuluNodes);
|
||||
}
|
||||
}
|
||||
});
|
||||
});
|
||||
Reference in new issue
Block a user