Skip to content

Commit a025fce

Browse files
committed
perf:prefer template showLogs performance
1 parent de8dc07 commit a025fce

File tree

5 files changed

+42
-4
lines changed

5 files changed

+42
-4
lines changed

common/utils/create-check/createUtil.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ export async function createQuestionByTitleSlug(
3131

3232
await createQuestionById(question.questionId, baseDir)
3333
}
34-
export async function createQuestionById(id, baseDir) {
34+
export async function createQuestionById(id, baseDir = process.cwd()) {
3535
const question = await getQuestionById(id)
3636
if (!question?.id)
3737
console.log(`指定编号: [ ${id} ] 题目不存在.`)

common/utils/question-handler/getTestCase.js

+36-1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,38 @@ import { getDataStructure } from './parseStructure.js'
33
import { DefaultLang } from '#common/constants/question.const.js'
44
import { setBlockComment } from '#common/utils/question-handler/questionLanguage.js'
55

6+
/**
7+
* 输出的日志
8+
* @param question
9+
* @param functionName
10+
* @param cases
11+
* @param expires
12+
* @returns {`showLogs(
13+
${string},
14+
{
15+
data: [${string}],
16+
structure: ${string}
17+
},
18+
{
19+
data: [${string}],
20+
structure: ${string}
21+
}
22+
)`}
23+
*/
24+
function logsTemplate(question, functionName, cases, expires) {
25+
return `showLogs(
26+
${functionName},
27+
{
28+
data: [${cases}],
29+
structure: ${JSON.stringify(getDataStructure(question.code))}
30+
},
31+
{
32+
data: [${expires}],
33+
structure: ${JSON.stringify(getDataStructure(question.code, 'return'))}
34+
}
35+
)`
36+
}
37+
638
/**
739
* test case 需要从两个地方拿到内容
840
* 1.详情:拿到默认的几个用例
@@ -47,7 +79,10 @@ export function getTestCase(question) {
4779
.trim()
4880
if (!functionName)
4981
return ''
50-
return `showLogs(\n${functionName},\n{\ndata: [${cases}],\nstructure: ${JSON.stringify(getDataStructure(question.code))},\n},\n{\ndata: [${expires}],\nstructure: ${JSON.stringify(getDataStructure(question.code, 'return'))}\n}\n)`
82+
return (
83+
setBlockComment(question.lang, 'Test Cases')
84+
+ logsTemplate(question, functionName, cases, expires)
85+
)
5186
}
5287
else {
5388
// 其他语言无法支持测试 只能提供测试数据

common/utils/question-handler/questionLanguage.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -303,7 +303,7 @@ export function setBlockComment(lang = DefaultLang, comment = '') {
303303
(p, line) => (p += `${startTag} ${line}\n`),
304304
'',
305305
)
306-
return `${start}\n${content}${end}`
306+
return `${start}\n${content}${end}\n`
307307
}
308308
default:
309309
return comment

common/utils/question-handler/showLogs.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,9 @@ export function showLogs(fnName, paramMap, compareMap) {
7777
= item[key]?.length >= 40 ? `${item[key].slice(0, 37)}...` : item[key]
7878
}
7979
}
80-
logTable.addRow(item, { color: item.测试结果 === '通过' ? 'green' : 'red' })
80+
logTable.addRow(item, {
81+
color: item['测试结果'] === '通过' ? 'green' : 'red',
82+
})
8183
})
8284
logTable.printTable()
8385
}

resources/template/template.js

+1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
export const template = `@Title
33
@Describe
44
@Function
5+
56
@TestCase
67
78
@Console

0 commit comments

Comments
 (0)