refactor(substore): 将通用工具函数迁移至公共库
- 从各个重命名脚本中移除重复的工具函数实现 - 将 multiplierOf、multiplierValueOf、stripSubscriptionSuffix 等函数统一到 common.js - 使用 utils.multiplierOf、utils.multiplierValueOf 替代本地实现 - 引入 utils.createSequenceCounter 替代手动计数逻辑 - 更新版本号从 20260608-1 到 20260702-1 - 移除重复的正则表达式匹配逻辑 - 标准化序列计数器实现方式 - 优化 yinyun-rename.js 中的信息节点过滤逻辑
This commit is contained in:
+39
-2
@@ -2,10 +2,10 @@
|
||||
* Sub-Store 节点命名公共工具
|
||||
*
|
||||
* 远程引用地址:
|
||||
* https://git.orionc.me/orion/script/raw/branch/main/substore/common.js?v=20260608-1
|
||||
* https://git.orionc.me/orion/script/raw/branch/main/substore/common.js?v=20260702-1
|
||||
*/
|
||||
(function (root) {
|
||||
const VERSION = '20260608-1';
|
||||
const VERSION = '20260702-1';
|
||||
const regexSpecialChars = new Set(['.', '*', '+', '?', '^', '$', '{', '}', '(', ')', '|', '[', ']', '\\']);
|
||||
const leadingFlagRegex = /^([\u{1F1E6}-\u{1F1FF}]{2}|🏴☠️)\s*/u;
|
||||
const inlineFlagRegex = /^(.{1,24}?)([\u{1F1E6}-\u{1F1FF}]{2}|🏴☠️)\s*(.*)$/u;
|
||||
@@ -206,6 +206,38 @@
|
||||
return `${nameWithoutMultiplier} ${multipliers.join(' ')}`.trim();
|
||||
};
|
||||
|
||||
const multiplierOf = name => {
|
||||
const match = String(name).match(/\b(\d+(?:\.\d+)?)(?:\s*[xX]|倍)\b/i);
|
||||
return match ? `${match[1]}X` : '';
|
||||
};
|
||||
|
||||
const multiplierValueOf = multiplier => (
|
||||
multiplier ? Number(String(multiplier).replace(/X$/i, '')) : Number.NEGATIVE_INFINITY
|
||||
);
|
||||
|
||||
const stripSubscriptionSuffix = (name, subName) => {
|
||||
const cleanSubName = String(subName || '').trim();
|
||||
if (!cleanSubName) return String(name).trim();
|
||||
|
||||
return String(name)
|
||||
.replace(new RegExp(`\\s+-\\s*${escapeRegex(cleanSubName)}(?:\\s+\\d{2})?\\s*$`, 'i'), '')
|
||||
.trim();
|
||||
};
|
||||
|
||||
const leadingRegionPrefixOf = name => {
|
||||
const match = String(name).match(/^([A-Z]{2})(?:\s+([A-Z]{3}))?(?=\s|$|-|\d|\|)/);
|
||||
if (!match) return '';
|
||||
return match[2] ? `${match[1]} ${match[2]}` : match[1];
|
||||
};
|
||||
|
||||
const createSequenceCounter = () => {
|
||||
const counts = Object.create(null);
|
||||
return key => {
|
||||
counts[key] = (counts[key] || 0) + 1;
|
||||
return String(counts[key]).padStart(2, '0');
|
||||
};
|
||||
};
|
||||
|
||||
const normalizeRegionSequence = (name, options) => {
|
||||
const allowCityCode = Boolean(options && options.allowCityCode);
|
||||
const cacheKey = allowCityCode ? 'city' : 'country';
|
||||
@@ -341,7 +373,10 @@
|
||||
escapeRegex,
|
||||
flagToRegionCode,
|
||||
joinAlias,
|
||||
leadingRegionPrefixOf,
|
||||
moveMultiplierToEnd,
|
||||
multiplierOf,
|
||||
multiplierValueOf,
|
||||
normalizeHyphenatedRegionFeatures,
|
||||
normalizeLeadingFlagSpacing,
|
||||
normalizeLeadingMojibakeFlag,
|
||||
@@ -353,6 +388,8 @@
|
||||
stripLeadingMojibakeFlag,
|
||||
splitLeadingFlag,
|
||||
splitLeadingOrInlineFlag,
|
||||
createSequenceCounter,
|
||||
stripSubscriptionSuffix,
|
||||
stripLineDescriptors,
|
||||
version: VERSION
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user