♻️ refactor(core): 简化 Sub-Store 节点重命名逻辑
* 移除多个机场专用的独立重命名脚本 * 整合节点命名规范至 rename.js 统一处理 * 更新文档以反映全局统一命名规则与使用方式 * 统一输出格式为:国旗 国家缩写 序号 [倍率] - 机场名
This commit is contained in:
1 parent
f601a0155a
commit
66ac40a69e
10 files changed
+244
-1213
No files matched your search
+205
-149
@@ -1,17 +1,87 @@
|
||||
/**
|
||||
* Sub-Store 整合订阅后缀脚本
|
||||
* - 保留节点原名,只追加当前订阅的 `_subName`
|
||||
* - 按节点名里最先出现的国家/地区标识重置开头国旗
|
||||
* - 重复执行不会叠加相同的订阅后缀
|
||||
* - 同名节点追加两位编号,避免节点名重复
|
||||
* Sub-Store 整合订阅统一重命名脚本
|
||||
* - 输出:国旗 国家/地区缩写 序号 [倍率] - 机场名
|
||||
* - 同一国家/地区连续编号并集中排序
|
||||
* - 实验性节点固定为 0.2x
|
||||
*/
|
||||
// noinspection JSUnusedGlobalSymbols
|
||||
const COMMON_SCRIPT_VERSION = '20260702-1';
|
||||
const COMMON_SCRIPT_URL = `https://git.orionc.me/orion/script/raw/branch/main/substore/common.js?v=${COMMON_SCRIPT_VERSION}`;
|
||||
// 部分客户端无法渲染台湾地区旗帜,显示层回退到中国国旗;地区识别仍保留为 TW。
|
||||
|
||||
// 台湾节点仍用 TW 识别和编号,仅显示层沿用中国国旗,兼容不支持台湾旗帜的客户端。
|
||||
const REGION_FLAG_DISPLAY_OVERRIDES = {
|
||||
TW: 'CN'
|
||||
};
|
||||
|
||||
const ISO_REGION_CODES = `
|
||||
AD AE AF AG AI AL AM AO AQ AR AS AT AU AW AX AZ BA BB BD BE BF BG BH BI BJ BL BM BN BO BQ BR BS BT BV BW BY BZ
|
||||
CA CC CD CF CG CH CI CK CL CM CN CO CR CU CV CW CX CY CZ DE DJ DK DM DO DZ EC EE EG EH ER ES ET FI FJ FK FM FO
|
||||
FR GA GB GD GE GF GG GH GI GL GM GN GP GQ GR GS GT GU GW GY HK HM HN HR HT HU ID IE IL IM IN IO IQ IR IS IT JE
|
||||
JM JO JP KE KG KH KI KM KN KP KR KW KY KZ LA LB LC LI LK LR LS LT LU LV LY MA MC MD ME MF MG MH MK ML MM MN MO
|
||||
MP MQ MR MS MT MU MV MW MX MY MZ NA NC NE NF NG NI NL NO NP NR NU NZ OM PA PE PF PG PH PK PL PM PN PR PS PT PW
|
||||
PY QA RE RO RS RU RW SA SB SC SD SE SG SH SI SJ SK SL SM SN SO SR SS ST SV SX SY SZ TC TD TF TG TH TJ TK TL TM
|
||||
TN TO TR TT TV TW TZ UA UG UM US UY UZ VA VC VE VG VI VN VU WF WS YE YT ZA ZM ZW
|
||||
`.trim().split(/\s+/);
|
||||
|
||||
const REGION_ALIASES = [
|
||||
['AT&T', 'US'],
|
||||
['United States of America', 'US'], ['United States', 'US'], ['U.S.A.', 'US'], ['USA', 'US'],
|
||||
['United Arab Emirates', 'AE'], ['UAE', 'AE'],
|
||||
['United Kingdom', 'UK'], ['Great Britain', 'UK'], ['Britain', 'UK'], ['England', 'UK'], ['UK', 'UK'],
|
||||
['Russian Federation', 'RU'], ['Russia', 'RU'],
|
||||
['Republic of Korea', 'KR'], ['South Korea', 'KR'], ['Korea', 'KR'],
|
||||
['North Korea', 'KP'], ['DPRK', 'KP'],
|
||||
['Hong Kong', 'HK'], ['Hongkong', 'HK'], ['HK', 'HK'],
|
||||
['Macau', 'MO'], ['Macao', 'MO'],
|
||||
['Taiwan', 'TW'], ['TW', 'TW'],
|
||||
['Singapore', 'SG'], ['SG', 'SG'],
|
||||
['Japan', 'JP'], ['JP', 'JP'],
|
||||
['Australia', 'AU'], ['AU', 'AU'],
|
||||
['US', 'US'],
|
||||
['香港', 'HK'], ['澳门', 'MO'], ['澳門', 'MO'],
|
||||
['台湾', 'TW'], ['台灣', 'TW'], ['臺灣', 'TW'],
|
||||
['新加坡', 'SG'], ['狮城', 'SG'], ['獅城', 'SG'],
|
||||
['日本', 'JP'], ['美国', 'US'], ['美國', 'US'], ['美西', 'US'], ['美东', 'US'], ['美東', 'US'],
|
||||
['英国', 'UK'], ['英國', 'UK'], ['阿联酋', 'AE'], ['阿聯酋', 'AE'],
|
||||
['韩国', 'KR'], ['韓國', 'KR'], ['南韩', 'KR'], ['南韓', 'KR'],
|
||||
['朝鲜', 'KP'], ['朝鮮', 'KP'], ['俄罗斯', 'RU'], ['俄羅斯', 'RU'],
|
||||
['中国', 'CN'], ['中國', 'CN'], ['澳大利亚', 'AU'], ['澳大利亞', 'AU'], ['澳洲', 'AU'],
|
||||
['Netherlands', 'NL'], ['Holland', 'NL'], ['荷兰', 'NL'], ['荷蘭', 'NL'],
|
||||
['Switzerland', 'CH'], ['瑞士', 'CH'], ['France', 'FR'], ['法国', 'FR'], ['法國', 'FR'],
|
||||
['Germany', 'DE'], ['德国', 'DE'], ['德國', 'DE'], ['Sweden', 'SE'], ['瑞典', 'SE'],
|
||||
['Bulgaria', 'BG'], ['保加利亚', 'BG'], ['保加利亞', 'BG'],
|
||||
['Austria', 'AT'], ['奥地利', 'AT'], ['奧地利', 'AT'],
|
||||
['Ireland', 'IE'], ['爱尔兰', 'IE'], ['愛爾蘭', 'IE'],
|
||||
['Turkey', 'TR'], ['Türkiye', 'TR'], ['土耳其', 'TR'],
|
||||
['Hungary', 'HU'], ['匈牙利', 'HU'], ['Canada', 'CA'], ['加拿大', 'CA'],
|
||||
['Italy', 'IT'], ['意大利', 'IT'], ['義大利', 'IT'], ['Spain', 'ES'], ['西班牙', 'ES'],
|
||||
['New Zealand', 'NZ'], ['新西兰', 'NZ'], ['新西蘭', 'NZ'],
|
||||
['Thailand', 'TH'], ['泰国', 'TH'], ['泰國', 'TH'], ['Vietnam', 'VN'], ['越南', 'VN'],
|
||||
['Pakistan', 'PK'], ['巴基斯坦', 'PK'], ['Israel', 'IL'], ['以色列', 'IL'],
|
||||
['Philippines', 'PH'], ['菲律宾', 'PH'], ['菲律賓', 'PH'],
|
||||
['Malaysia', 'MY'], ['马来西亚', 'MY'], ['馬來西亞', 'MY'],
|
||||
['Egypt', 'EG'], ['埃及', 'EG'], ['Nigeria', 'NG'], ['尼日利亚', 'NG'], ['尼日利亞', 'NG'],
|
||||
['Moldova', 'MD'], ['摩尔多瓦', 'MD'], ['摩爾多瓦', 'MD'],
|
||||
['Ukraine', 'UA'], ['乌克兰', 'UA'], ['烏克蘭', 'UA'],
|
||||
['Brazil', 'BR'], ['巴西', 'BR'], ['Chile', 'CL'], ['智利', 'CL'],
|
||||
['Argentina', 'AR'], ['阿根廷', 'AR'], ['India', 'IN'], ['印度', 'IN'],
|
||||
['Indonesia', 'ID'], ['印度尼西亚', 'ID'], ['印度尼西亞', 'ID'], ['印尼', 'ID'],
|
||||
['Mexico', 'MX'], ['墨西哥', 'MX'],
|
||||
['North Macedonia', 'MK'], ['Macedonia', 'MK'], ['北马其顿', 'MK'], ['北馬其頓', 'MK'], ['马其顿', 'MK'], ['馬其頓', 'MK'],
|
||||
['Lithuania', 'LT'], ['立陶宛', 'LT'], ['Saudi Arabia', 'SA'], ['沙特阿拉伯', 'SA'], ['沙特', 'SA'],
|
||||
['Czechia', 'CZ'], ['Czech', 'CZ'], ['捷克', 'CZ'], ['South Africa', 'ZA'], ['南非', 'ZA'],
|
||||
['Denmark', 'DK'], ['丹麦', 'DK'], ['丹麥', 'DK'], ['Togo', 'TG'], ['多哥', 'TG'],
|
||||
['Norway', 'NO'], ['挪威', 'NO'], ['Morocco', 'MA'], ['摩洛哥', 'MA'], ['卡萨布兰卡', 'MA'], ['卡薩布蘭卡', 'MA'],
|
||||
['Azerbaijan', 'AZ'], ['阿塞拜疆', 'AZ'], ['Romania', 'RO'], ['罗马尼亚', 'RO'], ['羅馬尼亞', 'RO'],
|
||||
['Colombia', 'CO'], ['哥伦比亚', 'CO'], ['哥倫比亞', 'CO'],
|
||||
['Kazakhstan', 'KZ'], ['哈萨克斯坦', 'KZ'], ['哈薩克斯坦', 'KZ'],
|
||||
['Kyrgyzstan', 'KG'], ['吉尔吉斯斯坦', 'KG'], ['吉爾吉斯斯坦', 'KG'],
|
||||
['Nepal', 'NP'], ['尼泊尔', 'NP'], ['尼泊爾', 'NP'],
|
||||
['Bangladesh', 'BD'], ['孟加拉国', 'BD'], ['孟加拉國', 'BD'],
|
||||
['Myanmar', 'MM'], ['Burma', 'MM'], ['缅甸', 'MM'], ['緬甸', 'MM'],
|
||||
['Cambodia', 'KH'], ['柬埔寨', 'KH']
|
||||
];
|
||||
|
||||
const hasCurrentRenameUtils = utils => Boolean(
|
||||
utils && utils.version === COMMON_SCRIPT_VERSION
|
||||
);
|
||||
@@ -36,79 +106,31 @@ async function loadRenameUtils() {
|
||||
return globalThis.SubStoreRenameUtils;
|
||||
}
|
||||
|
||||
const ISO_REGION_CODES = `
|
||||
AD AE AF AG AI AL AM AO AQ AR AS AT AU AW AX AZ BA BB BD BE BF BG BH BI BJ BL BM BN BO BQ BR BS BT BV BW BY BZ
|
||||
CA CC CD CF CG CH CI CK CL CM CN CO CR CU CV CW CX CY CZ DE DJ DK DM DO DZ EC EE EG EH ER ES ET FI FJ FK FM FO
|
||||
FR GA GB GD GE GF GG GH GI GL GM GN GP GQ GR GS GT GU GW GY HK HM HN HR HT HU ID IE IL IM IN IO IQ IR IS IT JE
|
||||
JM JO JP KE KG KH KI KM KN KP KR KW KY KZ LA LB LC LI LK LR LS LT LU LV LY MA MC MD ME MF MG MH MK ML MM MN MO
|
||||
MP MQ MR MS MT MU MV MW MX MY MZ NA NC NE NF NG NI NL NO NP NR NU NZ OM PA PE PF PG PH PK PL PM PN PR PS PT PW
|
||||
PY QA RE RO RS RU RW SA SB SC SD SE SG SH SI SJ SK SL SM SN SO SR SS ST SV SX SY SZ TC TD TF TG TH TJ TK TL TM
|
||||
TN TO TR TT TV TW TZ UA UG UM US UY UZ VA VC VE VG VI VN VU WF WS YE YT ZA ZM ZW
|
||||
`.trim().split(/\s+/);
|
||||
const normalizeRegionCode = regionCode => regionCode === 'GB' ? 'UK' : regionCode;
|
||||
|
||||
const EXTRA_REGION_ALIASES = [
|
||||
['EU', 'EU'], ['Europe', 'EU'], ['European Union', 'EU'], ['欧洲', 'EU'], ['歐洲', 'EU'],
|
||||
['XK', 'XK'], ['Kosovo', 'XK'], ['科索沃', 'XK'],
|
||||
['UK', 'UK'], ['United Kingdom', 'UK'], ['Britain', 'UK'], ['Great Britain', 'UK'], ['England', 'UK'], ['英国', 'UK'], ['英國', 'UK'],
|
||||
['USA', 'US'], ['U.S.A.', 'US'], ['United States', 'US'], ['United States of America', 'US'], ['America', 'US'], ['美国', 'US'], ['美國', 'US'], ['美西', 'US'], ['美东', 'US'], ['美東', 'US'],
|
||||
['UAE', 'AE'], ['United Arab Emirates', 'AE'], ['阿联酋', 'AE'], ['阿聯酋', 'AE'],
|
||||
['Russia', 'RU'], ['Russian Federation', 'RU'], ['俄罗斯', 'RU'], ['俄羅斯', 'RU'],
|
||||
['South Korea', 'KR'], ['Korea', 'KR'], ['Republic of Korea', 'KR'], ['韩国', 'KR'], ['韓國', 'KR'], ['南韩', 'KR'], ['南韓', 'KR'],
|
||||
['North Korea', 'KP'], ['DPRK', 'KP'], ['朝鲜', 'KP'], ['北韩', 'KP'], ['北韓', 'KP'],
|
||||
['Hong Kong', 'HK'], ['香港', 'HK'],
|
||||
['Macau', 'MO'], ['Macao', 'MO'], ['澳门', 'MO'], ['澳門', 'MO'],
|
||||
['Taiwan', 'TW'], ['台湾', 'TW'], ['台灣', 'TW'], ['臺灣', 'TW'],
|
||||
['Singapore', 'SG'], ['新加坡', 'SG'], ['狮城', 'SG'], ['獅城', 'SG'],
|
||||
['Japan', 'JP'], ['日本', 'JP'],
|
||||
['China', 'CN'], ['中国', 'CN'], ['中國', 'CN'],
|
||||
['Australia', 'AU'], ['澳大利亚', 'AU'], ['澳大利亞', 'AU'], ['澳洲', 'AU'],
|
||||
['Canada', 'CA'], ['加拿大', 'CA'],
|
||||
['Germany', 'DE'], ['德国', 'DE'], ['德國', 'DE'],
|
||||
['France', 'FR'], ['法国', 'FR'], ['法國', 'FR'],
|
||||
['Netherlands', 'NL'], ['Holland', 'NL'], ['荷兰', 'NL'], ['荷蘭', 'NL'],
|
||||
['Switzerland', 'CH'], ['瑞士', 'CH'],
|
||||
['Sweden', 'SE'], ['瑞典', 'SE'],
|
||||
['Norway', 'NO'], ['挪威', 'NO'],
|
||||
['Finland', 'FI'], ['芬兰', 'FI'], ['芬蘭', 'FI'],
|
||||
['Denmark', 'DK'], ['丹麦', 'DK'], ['丹麥', 'DK'],
|
||||
['Ireland', 'IE'], ['爱尔兰', 'IE'], ['愛爾蘭', 'IE'],
|
||||
['Italy', 'IT'], ['意大利', 'IT'], ['義大利', 'IT'],
|
||||
['Spain', 'ES'], ['西班牙', 'ES'],
|
||||
['Portugal', 'PT'], ['葡萄牙', 'PT'],
|
||||
['Poland', 'PL'], ['波兰', 'PL'], ['波蘭', 'PL'],
|
||||
['Ukraine', 'UA'], ['乌克兰', 'UA'], ['烏克蘭', 'UA'],
|
||||
['Turkey', 'TR'], ['Türkiye', 'TR'], ['土耳其', 'TR'],
|
||||
['India', 'IN'], ['印度', 'IN'],
|
||||
['Indonesia', 'ID'], ['印度尼西亚', 'ID'], ['印尼', 'ID'],
|
||||
['Malaysia', 'MY'], ['马来西亚', 'MY'], ['馬來西亞', 'MY'],
|
||||
['Thailand', 'TH'], ['泰国', 'TH'], ['泰國', 'TH'],
|
||||
['Vietnam', 'VN'], ['Viet Nam', 'VN'], ['越南', 'VN'],
|
||||
['Philippines', 'PH'], ['菲律宾', 'PH'], ['菲律賓', 'PH'],
|
||||
['Brazil', 'BR'], ['巴西', 'BR'],
|
||||
['Mexico', 'MX'], ['墨西哥', 'MX'],
|
||||
['Argentina', 'AR'], ['阿根廷', 'AR'],
|
||||
['Chile', 'CL'], ['智利', 'CL'],
|
||||
['South Africa', 'ZA'], ['南非', 'ZA'],
|
||||
['Israel', 'IL'], ['以色列', 'IL'],
|
||||
['Saudi Arabia', 'SA'], ['沙特', 'SA'], ['沙特阿拉伯', 'SA']
|
||||
];
|
||||
|
||||
const createFirstCountryResolver = utils => {
|
||||
const createRegionResolver = utils => {
|
||||
const aliases = [];
|
||||
const seenAliases = Object.create(null);
|
||||
const addAlias = (alias, regionCode) => {
|
||||
const cleanAlias = String(alias || '').trim();
|
||||
const cleanRegionCode = String(regionCode || '').trim().toUpperCase();
|
||||
if (!cleanAlias || !cleanRegionCode) return;
|
||||
const cleanRegionCode = normalizeRegionCode(String(regionCode || '').trim().toUpperCase());
|
||||
if (!cleanAlias || !cleanRegionCode || cleanAlias.toUpperCase() === cleanRegionCode) return;
|
||||
|
||||
const seenKey = `${cleanAlias.toLowerCase()}\u0000${cleanRegionCode}`;
|
||||
if (seenAliases[seenKey]) return;
|
||||
seenAliases[seenKey] = true;
|
||||
const key = `${cleanAlias.toLowerCase()}\u0000${cleanRegionCode}`;
|
||||
if (seenAliases[key]) return;
|
||||
seenAliases[key] = true;
|
||||
aliases.push([cleanAlias, cleanRegionCode]);
|
||||
};
|
||||
|
||||
ISO_REGION_CODES.forEach(regionCode => addAlias(regionCode, regionCode));
|
||||
EXTRA_REGION_ALIASES.forEach(([alias, regionCode]) => addAlias(alias, regionCode));
|
||||
REGION_ALIASES.forEach(([alias, regionCode]) => {
|
||||
const cleanAlias = String(alias).trim();
|
||||
const cleanRegionCode = normalizeRegionCode(regionCode);
|
||||
const key = `${cleanAlias.toLowerCase()}\u0000${cleanRegionCode}`;
|
||||
if (!seenAliases[key]) {
|
||||
seenAliases[key] = true;
|
||||
aliases.push([cleanAlias, cleanRegionCode]);
|
||||
}
|
||||
});
|
||||
|
||||
if (typeof Intl !== 'undefined' && typeof Intl.DisplayNames === 'function') {
|
||||
['en', 'zh-CN', 'zh-Hans', 'zh-TW', 'zh-Hant'].forEach(locale => {
|
||||
@@ -116,26 +138,39 @@ const createFirstCountryResolver = utils => {
|
||||
const displayNames = new Intl.DisplayNames([locale], { type: 'region' });
|
||||
ISO_REGION_CODES.forEach(regionCode => addAlias(displayNames.of(regionCode), regionCode));
|
||||
} catch (error) {
|
||||
// 部分 Sub-Store 运行环境没有完整 Intl 数据,静态别名仍可覆盖国家码。
|
||||
// 静态别名和国旗回退仍可完成识别。
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
const compiledAliases = aliases.map(([alias, regionCode]) => {
|
||||
const escapedAlias = utils.escapeRegex(alias);
|
||||
const isLatinAlias = /^[A-Za-z][A-Za-z\s.'()-]*$/.test(alias);
|
||||
const usesLatinBoundary = /^[A-Za-z0-9].*[A-Za-z0-9]$/.test(alias);
|
||||
return {
|
||||
alias,
|
||||
aliasLength: alias.length,
|
||||
regionCode,
|
||||
regex: isLatinAlias
|
||||
regex: usesLatinBoundary
|
||||
? new RegExp(`(^|[^A-Za-z])(${escapedAlias})(?=$|[^A-Za-z])`, 'i')
|
||||
: new RegExp(`(${escapedAlias})`, 'i')
|
||||
};
|
||||
});
|
||||
const regionCodeRegex = new RegExp(
|
||||
`(^|[^A-Za-z])(${[...ISO_REGION_CODES, 'UK'].join('|')})(?=$|[^A-Za-z])`,
|
||||
'g'
|
||||
);
|
||||
|
||||
return name => {
|
||||
let firstMatch = null;
|
||||
return (name, leadingFlag) => {
|
||||
const cleanName = String(name || '');
|
||||
let bestMatch = null;
|
||||
const consider = (index, aliasLength, regionCode) => {
|
||||
if (
|
||||
!bestMatch ||
|
||||
index < bestMatch.index ||
|
||||
(index === bestMatch.index && aliasLength > bestMatch.aliasLength)
|
||||
) {
|
||||
bestMatch = { index, aliasLength, regionCode: normalizeRegionCode(regionCode) };
|
||||
}
|
||||
};
|
||||
|
||||
compiledAliases.forEach(item => {
|
||||
const match = cleanName.match(item.regex);
|
||||
@@ -143,93 +178,114 @@ const createFirstCountryResolver = utils => {
|
||||
|
||||
const aliasText = match[2] || match[1] || match[0];
|
||||
const prefixLength = match[2] ? match[0].length - aliasText.length : 0;
|
||||
const index = match.index + prefixLength;
|
||||
if (
|
||||
!firstMatch ||
|
||||
index < firstMatch.index ||
|
||||
(index === firstMatch.index && item.alias.length > firstMatch.aliasLength)
|
||||
) {
|
||||
firstMatch = {
|
||||
index,
|
||||
aliasLength: item.alias.length,
|
||||
regionCode: item.regionCode
|
||||
};
|
||||
}
|
||||
consider(match.index + prefixLength, item.aliasLength, item.regionCode);
|
||||
});
|
||||
|
||||
return firstMatch ? firstMatch.regionCode : '';
|
||||
regionCodeRegex.lastIndex = 0;
|
||||
for (const match of cleanName.matchAll(regionCodeRegex)) {
|
||||
const prefixLength = match[1] ? match[1].length : 0;
|
||||
const matchIndex = match.index + prefixLength;
|
||||
// 国家短码应出现在名称前部,避免把后面的线路商缩写误判为国家。
|
||||
if (matchIndex > 24) continue;
|
||||
consider(matchIndex, match[2].length, match[2]);
|
||||
}
|
||||
|
||||
if (bestMatch) return bestMatch.regionCode;
|
||||
return normalizeRegionCode(utils.flagToRegionCode(leadingFlag));
|
||||
};
|
||||
};
|
||||
|
||||
const multiplierOf = name => {
|
||||
if (/(?:实验性|實驗性|experimental)/i.test(name)) return '0.2x';
|
||||
|
||||
const numberBeforeUnit = String(name).match(/(^|[^\d.])(\d+(?:\.\d+)?)\s*(?:[xX]|倍)(?=$|[^A-Za-z])/i);
|
||||
const numberAfterUnit = String(name).match(/(^|[^A-Za-z])[xX]\s*(\d+(?:\.\d+)?)(?=$|[^\d.])/i);
|
||||
const rawMultiplier = numberAfterUnit ? numberAfterUnit[2] : numberBeforeUnit ? numberBeforeUnit[2] : '';
|
||||
if (!rawMultiplier) return '';
|
||||
|
||||
const multiplierValue = Number(rawMultiplier);
|
||||
return Number.isFinite(multiplierValue) ? `${multiplierValue}x` : '';
|
||||
};
|
||||
|
||||
const splitAirportSuffix = (name, declaredAirport, utils) => {
|
||||
const cleanName = utils.normalizeLeadingMojibakeFlag(String(name || '')).trim();
|
||||
const cleanAirport = String(declaredAirport || '').trim();
|
||||
if (cleanAirport) {
|
||||
const suffixRegex = new RegExp(`\\s*-\\s*${utils.escapeRegex(cleanAirport)}\\s*$`, 'i');
|
||||
return {
|
||||
airport: cleanAirport,
|
||||
coreName: cleanName.replace(suffixRegex, '').trim()
|
||||
};
|
||||
}
|
||||
|
||||
const separatorIndex = cleanName.lastIndexOf(' - ');
|
||||
if (separatorIndex === -1) return { airport: '', coreName: cleanName };
|
||||
return {
|
||||
airport: cleanName.slice(separatorIndex + 3).trim(),
|
||||
coreName: cleanName.slice(0, separatorIndex).trim()
|
||||
};
|
||||
};
|
||||
|
||||
async function operator(proxies) {
|
||||
const utils = await loadRenameUtils();
|
||||
const stripSubscriptionSuffix = utils.stripSubscriptionSuffix || ((name, subName) => {
|
||||
const cleanSubName = String(subName || '').trim();
|
||||
if (!cleanSubName) return String(name);
|
||||
const regionCodeOf = createRegionResolver(utils);
|
||||
const regionOrder = Object.create(null);
|
||||
const normalizedItems = [];
|
||||
const passthroughItems = [];
|
||||
let nextRegionOrder = 0;
|
||||
|
||||
const escapeRegex = utils.escapeRegex || (value => String(value).replace(/[.*+?^${}()|[\]\\]/g, '\\$&'));
|
||||
return String(name)
|
||||
.replace(new RegExp(`\\s+-\\s*${escapeRegex(cleanSubName)}(?:\\s+\\d{2})?\\s*$`, 'i'), '')
|
||||
.trim();
|
||||
});
|
||||
const firstCountryCodeOf = createFirstCountryResolver(utils);
|
||||
const withFirstCountryFlag = name => {
|
||||
const cleanName = utils.normalizeLeadingMojibakeFlag
|
||||
? utils.normalizeLeadingMojibakeFlag(name)
|
||||
: String(name);
|
||||
const splitName = utils.splitLeadingFlag
|
||||
? utils.splitLeadingFlag(cleanName)
|
||||
: { flag: '', text: cleanName.replace(/^([\u{1F1E6}-\u{1F1FF}]{2})\s*/u, '').trim() };
|
||||
const regionCode = firstCountryCodeOf(splitName.text || cleanName);
|
||||
if (!regionCode) return name;
|
||||
|
||||
const displayRegionCode = REGION_FLAG_DISPLAY_OVERRIDES[regionCode] || regionCode;
|
||||
const flag = utils.regionCodeToFlag(displayRegionCode);
|
||||
return flag ? `${flag} ${splitName.text}`.trim() : name;
|
||||
};
|
||||
const nameCounts = Object.create(null);
|
||||
const currentCounts = Object.create(null);
|
||||
const baseNames = new Array(proxies.length);
|
||||
const coreNames = new Array(proxies.length);
|
||||
const subNames = new Array(proxies.length);
|
||||
|
||||
proxies.forEach((proxy, proxyIndex) => {
|
||||
// 非对象或非字符串名称不参与处理,避免改动无效节点数据。
|
||||
proxies.forEach((proxy, originalIndex) => {
|
||||
if (!proxy || typeof proxy !== 'object' || typeof proxy.name !== 'string') {
|
||||
baseNames[proxyIndex] = null;
|
||||
passthroughItems.push({ originalIndex, proxy });
|
||||
return;
|
||||
}
|
||||
|
||||
const proxyName = proxy.name;
|
||||
const subName = proxy && typeof proxy._subName === 'string'
|
||||
? proxy._subName.trim()
|
||||
: '';
|
||||
// 只在原后缀逻辑前补齐/修正首国旗,后缀与去重逻辑保持不变。
|
||||
const coreName = withFirstCountryFlag(subName ? stripSubscriptionSuffix(proxyName, subName) : proxyName);
|
||||
const baseName = subName
|
||||
? (coreName ? `${coreName} - ${subName}` : subName)
|
||||
: coreName;
|
||||
const declaredAirport = typeof proxy._subName === 'string' ? proxy._subName : '';
|
||||
const { airport, coreName } = splitAirportSuffix(proxy.name, declaredAirport, utils);
|
||||
const { flag: leadingFlag, text } = utils.splitLeadingFlag(coreName);
|
||||
|
||||
baseNames[proxyIndex] = baseName;
|
||||
coreNames[proxyIndex] = coreName;
|
||||
subNames[proxyIndex] = subName;
|
||||
nameCounts[baseName] = (nameCounts[baseName] || 0) + 1;
|
||||
});
|
||||
|
||||
return proxies.map((proxy, proxyIndex) => {
|
||||
const baseName = baseNames[proxyIndex];
|
||||
if (baseName === null || !proxy || typeof proxy !== 'object') return proxy;
|
||||
|
||||
if (nameCounts[baseName] > 1) {
|
||||
currentCounts[baseName] = (currentCounts[baseName] || 0) + 1;
|
||||
const duplicateNumber = String(currentCounts[baseName]).padStart(2, '0');
|
||||
proxy.name = subNames[proxyIndex]
|
||||
? `${coreNames[proxyIndex]} ${duplicateNumber} - ${subNames[proxyIndex]}`
|
||||
: `${baseName} ${duplicateNumber}`;
|
||||
} else {
|
||||
proxy.name = baseName;
|
||||
if (/\bEmby\b/i.test(text)) {
|
||||
const regionCode = 'EMBY';
|
||||
if (regionOrder[regionCode] === undefined) regionOrder[regionCode] = nextRegionOrder++;
|
||||
normalizedItems.push({ airport, coreName, originalIndex, proxy, regionCode });
|
||||
return;
|
||||
}
|
||||
|
||||
return proxy;
|
||||
const regionCode = regionCodeOf(text, leadingFlag);
|
||||
if (!regionCode) {
|
||||
passthroughItems.push({ originalIndex, proxy });
|
||||
return;
|
||||
}
|
||||
|
||||
if (regionOrder[regionCode] === undefined) regionOrder[regionCode] = nextRegionOrder++;
|
||||
normalizedItems.push({ airport, coreName, originalIndex, proxy, regionCode });
|
||||
});
|
||||
|
||||
normalizedItems.sort((firstItem, secondItem) => (
|
||||
regionOrder[firstItem.regionCode] - regionOrder[secondItem.regionCode] ||
|
||||
firstItem.originalIndex - secondItem.originalIndex
|
||||
));
|
||||
|
||||
const sequenceCounts = Object.create(null);
|
||||
normalizedItems.forEach(item => {
|
||||
sequenceCounts[item.regionCode] = (sequenceCounts[item.regionCode] || 0) + 1;
|
||||
const sequence = String(sequenceCounts[item.regionCode]).padStart(2, '0');
|
||||
const multiplier = multiplierOf(item.coreName);
|
||||
const regionLabel = item.regionCode === 'EMBY' ? 'EMBY' : item.regionCode;
|
||||
const flag = item.regionCode === 'EMBY'
|
||||
? '🏴☠️'
|
||||
: utils.regionCodeToFlag(REGION_FLAG_DISPLAY_OVERRIDES[item.regionCode] || item.regionCode);
|
||||
|
||||
item.proxy.name = [
|
||||
[flag, regionLabel, sequence].filter(Boolean).join(' '),
|
||||
multiplier ? `[${multiplier}]` : '',
|
||||
item.airport ? `- ${item.airport}` : ''
|
||||
].filter(Boolean).join(' ');
|
||||
});
|
||||
|
||||
passthroughItems.sort((firstItem, secondItem) => firstItem.originalIndex - secondItem.originalIndex);
|
||||
return [
|
||||
...normalizedItems.map(item => item.proxy),
|
||||
...passthroughItems.map(item => item.proxy)
|
||||
];
|
||||
}
|
||||
Reference in new issue
Block a user