Skip to content

Commit 0e83245

Browse files
author
v_yongztan
committed
fix command 兼容index 不是数字的题目
1 parent 4d6d7fc commit 0e83245

File tree

1 file changed

+18
-1
lines changed

1 file changed

+18
-1
lines changed

createByUrl.js

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,12 +42,29 @@ async function getPageInfo(url) {
4242
}
4343
});
4444
const funcReg = /var\s(.*)\s=\sfunction/
45-
const index = questionTitle.split('.')[0]
45+
let index = questionTitle.split('.')[0]
4646
const funcMatch = code.match(funcReg)
4747
let funcName = ''
4848
if (funcMatch && funcMatch.length === 2) {
4949
funcName = funcMatch[1]
5050
}
51+
52+
// 文件前缀是否数字,不是则用leetcode作为前缀
53+
const numReg = /^[0-9]+?$/;
54+
if (!numReg.test(index)) {
55+
const leetCodeFileReg = new RegExp(`^leetcode_[0-9]+?_${funcName}.js$`) //;
56+
const libDir = fs.readdirSync('lib/')
57+
let i = 1
58+
for (const item of libDir) {
59+
if (item.indexOf('leetcode') === 0) {
60+
if (leetCodeFileReg.test(item)) {
61+
throw new Error("file may exist - " + item);
62+
}
63+
i++
64+
}
65+
}
66+
index = `leetcode_${i}`
67+
}
5168
const fileName = `${index}_${funcName}.js`
5269

5370

0 commit comments

Comments
 (0)