Files
script/sntp-rename.js
Orion 1243e88e93 perf(core): ️ 优化节点名称注入性能
通过引入 O(1) 字典映射与预编译复合正则替换原有 for 循环逻辑,大幅提升处理效率,并新增对 Anytls 特征的支持。
2026-04-18 01:08:05 +08:00

66 lines
2.8 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
/**
* Sub-Store 节点名称属性注入脚本 (极致性能 Hash Map 版)
* 核心逻辑:单次复合正则提取 + O(1) 字典映射查找,彻底消除 for 循环。
* 参考文档 (MDN Map/Object): https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Object
*/
function operator(proxies) {
// 1. O(1) 映射字典 (Hash Map)
// 键名统一为大写且去除所有空格,以确保后续动态查找时的绝对稳定性
const featureMap = {
"GTM0.5X": "三网",
"GTM": "三网",
"S1": "广移",
"S2": "广电信",
"S3": "广移",
"S4": "广联通",
"BGP": "cn2|5x",
"ANYTLS": "直连" // 新增Anytls 映射为 直连
};
// 2. 预编译复合正则表达式 (性能优化的核心)
// 利用正则的 alternation (|) 进行单次扫描。
// 严格顺序:长尾特征 (GTM\s*0\.5x) 必须排在 (GTM) 之前,防止短路。
// \b 用于单词边界,防止匹配到形如 "US1" 或 "AnytlsX" 的错误字符。
const featureRegex = /(GTM\s*0\.5x|GTM|\bS1\b|\bS2\b|\bS3\b|\bS4\b|\bBGP\b|\bAnytls\b)/i;
// 后缀匹配正则预编译,避免在 map 循环中重复创建
const suffixRegex = /(\s*-\s*SNTP.*)$/i;
// 3. 执行节点遍历
return proxies.map(p => {
const name = p.name;
// 步骤 A: 执行单次正则提取
// match 方法如果匹配成功match[1] 将包含括号中捕获到的具体特征字符(如 "s1", "GTM 0.5x", "Anytls"
const match = name.match(featureRegex);
// 如果未命中任何特征,直接返回原节点,开销极小
if (!match) return p;
// 步骤 B: 结果清洗与 O(1) 字典检索
// 将捕获到的特征串(如 "Gtm 0.5x")统一转为大写并剔除空格 -> "GTM0.5X",完美匹配 Map 的 Key
const rawFeature = match[1];
const mapKey = rawFeature.toUpperCase().replace(/\s+/g, '');
const injectPayload = featureMap[mapKey];
// 兜底安全校验,防止异常空值
if (!injectPayload) return p;
// 步骤 C: 格式化与幂等性校验 (防重复注入)
const formatStr = ` [${injectPayload}]`;
if (name.includes(formatStr)) {
return p; // 已包含该属性,跳过
}
// 步骤 D: 执行中缀注入
if (suffixRegex.test(name)) {
// $1 代表 suffixRegex 中捕获到的后缀内容
p.name = name.replace(suffixRegex, `${formatStr}$1`);
} else {
// 针对类似 🇯🇵 JP-Anytls-1-0.2x 这种没有任何 - SNTP 后缀的纯净节点,直接追加
p.name = name + formatStr;
}
return p; // 返回处理后的最终节点
});
}