JavaScript中如何规范编写带版本的记录名及版本查询问题求助
我来帮你搞定这个JavaScript里版本化记录名的问题,先把规则捋清楚,再一步步实现核心逻辑,保证命名规范和查询逻辑都准确。
先把咱们的命名规则明确下来,避免歧义:
- 基础文件名格式:
[记录名].json,比如1a.json、user_profile.json - 版本化文件名格式:
[记录名]_V[版本号].json,其中[版本号]必须是正整数,比如1a_V1.json、user_profile_V3.json - 关键约束:
_V是版本标识的固定前缀,后面只能跟数字;记录名本身可以包含数字、字母、符号,但不能包含_V组合(避免解析冲突)
下面是几个关键的工具函数,覆盖解析文件名、生成新版本名、查询目标版本这几个核心场景:
1. 解析文件名:提取记录名和版本号
这个函数能把任意符合规则的文件名拆成基础记录名和对应的版本号,方便后续处理:
function parseRecordFileName(filename) { // 匹配版本化文件名的正则:结尾是_V数字.json const versionRegex = /_V(\d+)\.json$/; const match = filename.match(versionRegex); if (match) { // 提取基础记录名(去掉_Vx.json部分) const recordName = filename.slice(0, match.index); const version = parseInt(match[1], 10); return { recordName, version, isVersioned: true }; } else { // 基础文件名,版本号视为0(代表初始版本,方便排序比较) const recordName = filename.replace(/\.json$/, ''); return { recordName, version: 0, isVersioned: false }; } }
举个例子:解析1a_V1.json会得到{ recordName: '1a', version: 1, isVersioned: true };解析user_abc.json会得到{ recordName: 'user_abc', version: 0, isVersioned: false }。
2. 生成新版本文件名
当用户创建同名记录时,自动找到当前最高版本,生成下一个版本的文件名:
function generateNewVersionFilename(existingFiles, targetRecordName) { // 过滤出所有属于该记录名的文件 const relatedFiles = existingFiles.filter(file => { const parsed = parseRecordFileName(file); return parsed.recordName === targetRecordName; }); // 找到当前最高版本号 const maxVersion = relatedFiles.reduce((max, file) => { const parsed = parseRecordFileName(file); return Math.max(max, parsed.version); }, 0); // 生成新版本名:如果是初始版本(maxVersion=0),则生成_V1,否则生成_V(当前最高+1) return maxVersion === 0 ? `${targetRecordName}_V1.json` : `${targetRecordName}_V${maxVersion + 1}.json`; }
测试场景:如果现有文件里有1a.json和1a_V3.json,调用这个函数生成的新文件名是1a_V4.json;如果是全新的记录名,会直接生成[记录名]_V1.json。
3. 查询指定记录名的目标版本
从数组中筛选出指定记录名的、版本号大于等于指定值的所有文件,还会按版本从高到低排序:
function getTargetVersions(existingFiles, targetRecordName, minVersion = 0) { return existingFiles .map(file => ({ file, parsed: parseRecordFileName(file) })) .filter(({ parsed }) => parsed.recordName === targetRecordName && parsed.version >= minVersion) .sort((a, b) => b.parsed.version - a.parsed.version) // 按版本降序排列 .map(({ file, parsed }) => ({ filename: file, version: parsed.version })); }
比如现有文件是["1a.json", "1a_V1.json", "1a_V2.json", "2b.json"],调用getTargetVersions(files, "1a", 1)会返回:
[ { "filename": "1a_V2.json", "version": 2 }, { "filename": "1a_V1.json", "version": 1 } ]
把这些函数结合起来用的完整例子:
// 模拟现有文件数组 const existingFiles = ["1a.json", "user_abc.json", "1a_V1.json", "user_abc_V2.json", "1a_V3.json"]; // 1. 解析文件名 console.log(parseRecordFileName("1a_V1.json")); // 输出:{ recordName: '1a', version: 1, isVersioned: true } // 2. 生成新版本文件名 console.log(generateNewVersionFilename(existingFiles, "1a")); // 输出:"1a_V4.json" // 3. 查询版本>=1的记录 const targetVersions = getTargetVersions(existingFiles, "1a", 1); console.log(targetVersions); /* 输出: [ { filename: '1a_V3.json', version: 3 }, { filename: '1a_V1.json', version: 1 } ] */
如果担心用户输入不符合规则的文件名,可以加一个校验函数:
function isValidRecordFileName(filename) { // 基础文件名规则:以.json结尾,且不包含_V数字.json的格式 // 版本化文件名规则:以_V数字.json结尾 const baseValid = /^.+\.json$/.test(filename) && !/_V\d+\.json$/.test(filename); const versionedValid = /^.+_V\d+\.json$/.test(filename); return baseValid || versionedValid; }
这个函数可以在用户上传文件前校验,避免非法格式干扰后续逻辑。
内容的提问来源于stack exchange,提问作者Manjunath Siddappa

