♻️ refactor(substore): 重构 regex 缓存机制并升级公共脚本版本

This commit is contained in:
2026-06-08 10:23:12 +08:00
parent afc8c70b95
commit 9c9b78ded9
8 changed files with 84 additions and 47 deletions
+30 -21
View File
@@ -5,7 +5,7 @@
* - 基于归一化后的完整名称做精准去重
*/
// noinspection JSUnusedGlobalSymbols
const COMMON_SCRIPT_VERSION = '20260603-7';
const COMMON_SCRIPT_VERSION = '20260608-1';
const COMMON_SCRIPT_URL = `https://git.orionc.me/orion/script/raw/branch/main/substore/common.js?v=${COMMON_SCRIPT_VERSION}`;
const hasCurrentRenameUtils = utils => Boolean(
utils && utils.version === COMMON_SCRIPT_VERSION
@@ -608,6 +608,18 @@ async function operator(proxies) {
['ISIF', 'ISIF'],
['RFC', 'RFC']
];
const providerAliasMatchers = providerAliases.map(([alias, provider]) => [
provider,
/^[A-Za-z0-9.]+$/.test(alias)
? new RegExp(`(^|[^A-Za-z0-9])${escapeRegex(alias)}([^A-Za-z0-9]|$)`, 'i')
: new RegExp(escapeRegex(alias), 'i')
]);
const embeddedRegionAliasMatchers = sortedRegionAliases.map(([alias, replacement]) => [
replacement,
/^[A-Za-z0-9 ]+$/.test(alias)
? new RegExp(`(^|[^A-Za-z])${escapeRegex(alias)}(?=$|[^A-Za-z])`, 'i')
: new RegExp(escapeRegex(alias), 'i')
]);
const stripExistingSuffix = (name, subName) => {
if (!subName) return name.trim();
@@ -670,45 +682,42 @@ async function operator(proxies) {
const providerOf = name => {
const cleanName = String(name);
for (const [alias, provider] of providerAliases) {
const aliasRegex = /^[A-Za-z0-9.]+$/.test(alias)
? new RegExp(`(^|[^A-Za-z0-9])${escapeRegex(alias)}([^A-Za-z0-9]|$)`, 'i')
: new RegExp(escapeRegex(alias), 'i');
for (const [provider, aliasRegex] of providerAliasMatchers) {
if (aliasRegex.test(cleanName)) return provider;
}
return '';
};
const embeddedRegionPrefixOf = (name, fallbackRegionCode) => {
const cleanName = String(name);
const candidates = [];
if (fallbackRegionCode) {
candidates.push({ index: -1, region: fallbackRegionCode });
}
if (fallbackRegionCode) return fallbackRegionCode;
for (const [alias, replacement, aliasLower] of sortedRegionAliases) {
const aliasRegex = /^[A-Za-z0-9 ]+$/.test(alias)
? new RegExp(`(^|[^A-Za-z])${escapeRegex(alias)}(?=$|[^A-Za-z])`, 'i')
: new RegExp(escapeRegex(alias), 'i');
const cleanName = String(name);
let bestIndex = Number.POSITIVE_INFINITY;
let bestRegion = '';
for (const [replacement, aliasRegex] of embeddedRegionAliasMatchers) {
const match = cleanName.match(aliasRegex);
if (!match) continue;
const matchIndex = match.index + (match[1] ? match[1].length : 0);
candidates.push({ index: matchIndex, region: replacement });
if (matchIndex < bestIndex) {
bestIndex = matchIndex;
bestRegion = replacement;
}
}
for (const match of cleanName.matchAll(/(^|[^A-Za-z])([A-Z]{2})(?:\d{1,2})?(?=$|[^A-Za-z])/g)) {
const regionCode = match[2].toUpperCase();
if (!knownRegionCodes.has(regionCode)) continue;
candidates.push({
index: match.index + (match[1] ? match[1].length : 0),
region: regionCode
});
const matchIndex = match.index + (match[1] ? match[1].length : 0);
if (matchIndex < bestIndex) {
bestIndex = matchIndex;
bestRegion = regionCode;
}
}
candidates.sort((firstCandidate, secondCandidate) => firstCandidate.index - secondCandidate.index);
return candidates.length ? candidates[0].region : '';
return bestRegion;
};
const normalizeProviderRegionName = (name, fallbackRegionCode) => {