♻️ refactor(substore): 简化 rename.js 中的序号分配逻辑并移除冗余代码
This commit is contained in:
+12
-33
@@ -9,10 +9,10 @@ function operator(proxies) {
|
|||||||
const globalCount = Object.create(null);
|
const globalCount = Object.create(null);
|
||||||
const currentCount = Object.create(null);
|
const currentCount = Object.create(null);
|
||||||
const baseNames = new Array(proxies.length);
|
const baseNames = new Array(proxies.length);
|
||||||
|
const finalCoreNames = new Array(proxies.length);
|
||||||
const normalizedCoreNames = new Array(proxies.length);
|
const normalizedCoreNames = new Array(proxies.length);
|
||||||
const subNames = new Array(proxies.length);
|
const subNames = new Array(proxies.length);
|
||||||
const sequenceInfos = new Array(proxies.length);
|
const sequenceInfos = new Array(proxies.length);
|
||||||
const sequenceGroups = Object.create(null);
|
|
||||||
const suffixRegexCache = Object.create(null);
|
const suffixRegexCache = Object.create(null);
|
||||||
|
|
||||||
const regionAliases = [
|
const regionAliases = [
|
||||||
@@ -706,8 +706,7 @@ function operator(proxies) {
|
|||||||
prefix,
|
prefix,
|
||||||
regionCode,
|
regionCode,
|
||||||
sequenceNumber,
|
sequenceNumber,
|
||||||
rest,
|
rest
|
||||||
key: `${prefix}${regionCode}\u0000${rest.trim()}`
|
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -717,18 +716,6 @@ function operator(proxies) {
|
|||||||
.trim();
|
.trim();
|
||||||
};
|
};
|
||||||
|
|
||||||
const nextRegionSequence = sequenceGroup => {
|
|
||||||
while (sequenceGroup.used[sequenceGroup.next.toString().padStart(2, '0')]) {
|
|
||||||
sequenceGroup.next += 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
const sequenceNumber = sequenceGroup.next.toString().padStart(2, '0');
|
|
||||||
sequenceGroup.used[sequenceNumber] = true;
|
|
||||||
sequenceGroup.next += 1;
|
|
||||||
|
|
||||||
return sequenceNumber;
|
|
||||||
};
|
|
||||||
|
|
||||||
const normalizeBilingualName = name => {
|
const normalizeBilingualName = name => {
|
||||||
const bilingualMatch = name.match(/^(.+?)\s*|\s*(.+)$/);
|
const bilingualMatch = name.match(/^(.+?)\s*|\s*(.+)$/);
|
||||||
if (!bilingualMatch) return name;
|
if (!bilingualMatch) return name;
|
||||||
@@ -808,42 +795,34 @@ function operator(proxies) {
|
|||||||
normalizedCoreNames[proxyIndex] = normalizedCoreName;
|
normalizedCoreNames[proxyIndex] = normalizedCoreName;
|
||||||
subNames[proxyIndex] = subName;
|
subNames[proxyIndex] = subName;
|
||||||
sequenceInfos[proxyIndex] = sequenceInfo;
|
sequenceInfos[proxyIndex] = sequenceInfo;
|
||||||
|
|
||||||
if (sequenceInfo) {
|
|
||||||
const sequenceGroupKey = `${subName}\u0000${sequenceInfo.key}`;
|
|
||||||
const sequenceGroup = sequenceGroups[sequenceGroupKey] || (
|
|
||||||
sequenceGroups[sequenceGroupKey] = { used: Object.create(null), next: 1 }
|
|
||||||
);
|
|
||||||
|
|
||||||
if (sequenceInfo.sequenceNumber) {
|
|
||||||
sequenceGroup.used[sequenceInfo.sequenceNumber] = true;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
});
|
});
|
||||||
|
|
||||||
normalizedCoreNames.forEach((normalizedCoreName, proxyIndex) => {
|
normalizedCoreNames.forEach((normalizedCoreName, proxyIndex) => {
|
||||||
const subName = subNames[proxyIndex];
|
const subName = subNames[proxyIndex];
|
||||||
const sequenceInfo = sequenceInfos[proxyIndex];
|
const sequenceInfo = sequenceInfos[proxyIndex];
|
||||||
const needsLeadingSequence = sequenceInfo && !sequenceInfo.sequenceNumber;
|
const needsLeadingSequence = sequenceInfo && !sequenceInfo.sequenceNumber;
|
||||||
const sequenceGroupKey = sequenceInfo ? `${subName}\u0000${sequenceInfo.key}` : '';
|
|
||||||
const sequenceNumber = needsLeadingSequence
|
|
||||||
? nextRegionSequence(sequenceGroups[sequenceGroupKey])
|
|
||||||
: '';
|
|
||||||
const coreName = needsLeadingSequence
|
const coreName = needsLeadingSequence
|
||||||
? withRegionSequence(sequenceInfo, sequenceNumber)
|
? withRegionSequence(sequenceInfo, '01')
|
||||||
: normalizedCoreName;
|
: normalizedCoreName;
|
||||||
const baseName = subName ? `${coreName} - ${subName}` : coreName;
|
const baseName = subName ? `${coreName} - ${subName}` : coreName;
|
||||||
|
|
||||||
|
finalCoreNames[proxyIndex] = coreName;
|
||||||
baseNames[proxyIndex] = baseName;
|
baseNames[proxyIndex] = baseName;
|
||||||
globalCount[baseName] = (globalCount[baseName] || 0) + 1;
|
globalCount[baseName] = (globalCount[baseName] || 0) + 1;
|
||||||
});
|
});
|
||||||
|
|
||||||
return proxies.map((proxy, proxyIndex) => {
|
return proxies.map((proxy, proxyIndex) => {
|
||||||
const baseName = baseNames[proxyIndex];
|
const baseName = baseNames[proxyIndex];
|
||||||
|
const coreName = finalCoreNames[proxyIndex];
|
||||||
|
const subName = subNames[proxyIndex];
|
||||||
|
const sequenceInfo = sequenceInfos[proxyIndex];
|
||||||
|
|
||||||
if (globalCount[baseName] > 1) {
|
if (globalCount[baseName] > 1 && !sequenceInfo) {
|
||||||
currentCount[baseName] = (currentCount[baseName] || 0) + 1;
|
currentCount[baseName] = (currentCount[baseName] || 0) + 1;
|
||||||
proxy.name = `${baseName} ${currentCount[baseName].toString().padStart(2, '0')}`;
|
const duplicateNumber = currentCount[baseName].toString().padStart(2, '0');
|
||||||
|
proxy.name = subName
|
||||||
|
? `${coreName} ${duplicateNumber} - ${subName}`
|
||||||
|
: `${baseName} ${duplicateNumber}`;
|
||||||
} else {
|
} else {
|
||||||
proxy.name = baseName;
|
proxy.name = baseName;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user