♻️ refactor(substore): 提取重构公共工具 common.js 并新增 liangxin 脚本

This commit is contained in:
Orion
2026-06-03 12:56:18 +08:00
parent d60d1f2709
commit ce9fac0504
8 changed files with 460 additions and 192 deletions
+26 -55
View File
@@ -5,7 +5,27 @@
* - 基于归一化后的完整名称做精准去重
*/
// noinspection JSUnusedGlobalSymbols
function operator(proxies) {
const COMMON_SCRIPT_URL = 'https://git.orionc.me/orion/script/raw/branch/main/substore/common.js';
async function loadRenameUtils() {
if (typeof globalThis !== 'undefined' && globalThis.SubStoreRenameUtils) {
return globalThis.SubStoreRenameUtils;
}
const cache = typeof scriptResourceCache !== 'undefined' ? scriptResourceCache : null;
let script = cache && cache.get(COMMON_SCRIPT_URL);
if (!script) {
const response = await $substore.http.get({ url: COMMON_SCRIPT_URL });
script = response && (response.body || response.data || response);
if (cache) cache.set(COMMON_SCRIPT_URL, script);
}
new Function(String(script))();
return globalThis.SubStoreRenameUtils;
}
async function operator(proxies) {
const utils = await loadRenameUtils();
const globalCount = Object.create(null);
const currentCount = Object.create(null);
const baseNames = new Array(proxies.length);
@@ -557,29 +577,11 @@ function operator(proxies) {
);
Object.values(flagCodeOverrides).forEach(code => knownRegionCodes.add(code));
const regexSpecialChars = new Set(['.', '*', '+', '?', '^', '$', '{', '}', '(', ')', '|', '[', ']', '\\']);
const escapeRegex = value => [...value]
.map(char => regexSpecialChars.has(char) ? `\\${char}` : char)
.join('');
const escapeRegex = utils.escapeRegex;
const leadingFlagRegex = /^([\u{1F1E6}-\u{1F1FF}]{2}|🏴‍☠️)\s*/u;
const inlineFlagRegex = /^(.{1,24}?)([\u{1F1E6}-\u{1F1FF}]{2}|🏴‍☠️)\s*(.*)$/u;
const flagToRegionCode = flag => {
if (!flag) return '';
if (flagCodeOverrides[flag]) return flagCodeOverrides[flag];
const codePoints = [...flag].map(char => char.codePointAt(0));
if (
codePoints.length !== 2 ||
codePoints.some(codePoint => codePoint < 0x1F1E6 || codePoint > 0x1F1FF)
) {
return '';
}
return codePoints
.map(codePoint => String.fromCharCode(codePoint - 0x1F1E6 + 65))
.join('');
};
const flagToRegionCode = flag => utils.flagToRegionCode(flag, flagCodeOverrides);
const stripExistingSuffix = (name, subName) => {
if (!subName) return name.trim();
@@ -589,14 +591,7 @@ function operator(proxies) {
return name.replace(suffixRegex, '').trim();
};
const joinAlias = (replacement, rest) => {
const cleanRest = rest.trim();
if (!cleanRest) return replacement;
if (/^[|/\-]/.test(cleanRest)) {
return `${replacement}${cleanRest}`;
}
return `${replacement} ${cleanRest}`;
};
const joinAlias = utils.joinAlias;
const normalizeLeadingRegion = name => {
const cleanName = name.trim();
@@ -665,33 +660,9 @@ function operator(proxies) {
.trim();
};
const moveMultiplierToEnd = name => {
const multipliers = [];
const nameWithoutMultiplier = name
.replace(/\b(\d+(?:\.\d+)?X)\b/gi, multiplier => {
multipliers.push(multiplier.toUpperCase());
return ' ';
})
.replace(/\s+\[/g, ' [')
.replace(/\s{2,}/g, ' ')
.trim();
const moveMultiplierToEnd = utils.moveMultiplierToEnd;
if (!multipliers.length) return name;
return `${nameWithoutMultiplier} ${multipliers.join(' ')}`.trim();
};
const normalizeRegionSequence = name => name
.replace(/(^|\s)([A-Z]{2}(?:\s+[A-Z]{3})?)[\s|\-]*(\d{1,2})(?=$|[\s|\-[\]])/g, (
match,
prefix,
regionCode,
sequenceNumber
) => {
return `${prefix}${regionCode} ${sequenceNumber.padStart(2, '0')}`;
})
.replace(/(\b[A-Z]{2}(?:\s+[A-Z]{3})? \d{2})-\s*(?=\d+(?:\.\d+)?X\b)/g, '$1 ')
.replace(/\s{2,}/g, ' ')
.trim();
const normalizeRegionSequence = name => utils.normalizeRegionSequence(name, { allowCityCode: true });
const regionSequenceOf = name => {
const sequenceMatch = name.match(/^((?:[\u{1F1E6}-\u{1F1FF}]{2}|🏴‍☠️)\s*)?([A-Z]{2}(?:\s+[A-Z]{3})?)(?:\s+(\d{2}))?(\s.*)?$/u);