1
- const { ListNode, parse, toArray } = require ( "../structures/ListNode" ) ;
2
-
3
-
4
- /**
1
+ /**
5
2
* 获取test case 入参的数据类型
6
3
* @param {string } jsCode leetcode的实例函数体
7
4
* @param {string } type 类型,param入参,returns返回值
8
5
* @returns {string[] }
9
6
*/
10
7
const getDataStructure = ( jsCode , type = 'param' ) => {
11
- const regexMap = {
12
- param : / @ p a r a m \s + { \s * ( [ ^ } \s ] + ) \s * } / g,
13
- return : / @ r e t u r n \s + { \s * ( [ ^ } \s ] + ) \s * } / g
14
- } ;
15
- const regex = regexMap [ type ] ;
16
- const paramTypes = [ ] ;
17
- let match ;
18
- while ( ( match = regex . exec ( jsCode ) ) !== null ) {
19
- paramTypes . push ( match [ 1 ] ) ;
20
- }
21
- return paramTypes
22
- }
23
-
24
-
8
+ const regexMap = {
9
+ param : / @ p a r a m \s + { \s * ( [ ^ } \s ] + ) \s * } / g,
10
+ return : / @ r e t u r n \s + { \s * ( [ ^ } \s ] + ) \s * } / g,
11
+ } ;
12
+ const regex = regexMap [ type ] ;
13
+ const paramTypes = [ ] ;
14
+ let match ;
15
+ while ( ( match = regex . exec ( jsCode ) ) !== null ) {
16
+ paramTypes . push ( match [ 1 ] ) ;
17
+ }
18
+ return paramTypes ;
19
+ } ;
25
20
26
21
/**
27
22
* test case 需要从两个地方拿到内容
@@ -30,14 +25,14 @@ const getDataStructure = (jsCode, type = 'param') => {
30
25
* @param question
31
26
* @returns {string }
32
27
*/
33
- function getTestCase ( question ) {
34
- const detail = question . detail . replaceAll ( "`" , "" ) ;
35
- const cases = detail . match ( / ( < [ a - z A - Z ] + > ) ? 输 入 [ : | : ] ( < \/ [ a - z A - Z ] + > ) ? .+ \n / g)
36
- ?. map ( str => `[${ str ?. replace ( / ( < [ a - z A - Z ] + > ) ? 输 入 [ : | : ] / gm, "" ) ?. replace ( / ( < \/ [ a - z A - Z ] + > ) ? / , "" ) ?. replace ( "\n" , "" ) } ]` )
37
- const expires = detail . match ( / ( < [ a - z A - Z ] + > ) ? 输 出 [ : | : ] ( < \/ [ a - z A - Z ] + > ) ? .+ \n / g)
38
- ?. map ( str => str ?. replace ( / ( < [ a - z A - Z ] + > ) ? 输 出 [ : | : ] ( < \/ [ a - z A - Z ] + > ) ? / gm, "" ) ?. replace ( / ( < \/ [ a - z A - Z ] + > ) ? / gm, "" ) ?. replace ( "\n" , "" ) )
39
- const functionName = question . jsCode ?. match ( / v a r .+ = / g) ?. [ 0 ] ?. replace ( " var " , "" ) ?. replace ( " =" , "" ) ;
40
- return `showLogs(
28
+ function getTestCase ( question ) {
29
+ const detail = question . detail . replaceAll ( '`' , '' ) ;
30
+ const cases = detail . match ( / ( < [ a - z A - Z ] + > ) ? 输 入 [ : | : ] ( < \/ [ a - z A - Z ] + > ) ? .+ \n / g)
31
+ ?. map ( ( str ) => `[${ str ?. replace ( / ( < [ a - z A - Z ] + > ) ? 输 入 [ : | : ] / gm, '' ) ?. replace ( / ( < \/ [ a - z A - Z ] + > ) ? / , '' ) ?. replace ( '\n' , '' ) } ]` ) ;
32
+ const expires = detail . match ( / ( < [ a - z A - Z ] + > ) ? 输 出 [ : | : ] ( < \/ [ a - z A - Z ] + > ) ? .+ \n / g)
33
+ ?. map ( ( str ) => str ?. replace ( / ( < [ a - z A - Z ] + > ) ? 输 出 [ : | : ] ( < \/ [ a - z A - Z ] + > ) ? / gm, '' ) ?. replace ( / ( < \/ [ a - z A - Z ] + > ) ? / gm, '' ) ?. replace ( '\n' , '' ) ) ;
34
+ const functionName = question . jsCode ?. match ( / v a r .+ = / g) ?. [ 0 ] ?. replace ( ' var ' , '' ) ?. replace ( ' =' , '' ) ;
35
+ return `showLogs(
41
36
${ functionName } ,
42
37
{
43
38
data: [${ cases } ],
@@ -47,6 +42,6 @@ function getTestCase(question){
47
42
data: [${ expires } ],
48
43
structrue: ${ JSON . stringify ( getDataStructure ( question . jsCode , 'return' ) ) }
49
44
}
50
- )`
45
+ )` ;
51
46
}
52
- module . exports = { getTestCase, getDataStructure}
47
+ module . exports = { getTestCase, getDataStructure } ;
0 commit comments