-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathparseStructure.js
54 lines (51 loc) · 1.54 KB
/
parseStructure.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
import { Node } from '../../structures/Node.js'
import { TreeNode } from '../../structures/TreeNode.js'
import { ListNode } from '#common/structures/ListNode.js'
const paramMap = {
// 入参map
cases: {
ListNode: (_param) => ListNode.parse(_param),
'ListNode[]': (param) => param.map((res) => ListNode.parse(res)),
TreeNode: (param) => TreeNode.parse(param),
Node: (param) => Node.parse(param),
default: (param) => param
},
// 返回值map
return: {
ListNode: (param) => ListNode.toArray(param),
'ListNode[]': (param) => param.map((res) => ListNode.toArray(res)),
TreeNode: (param) => TreeNode.toArray(param),
Node: (param) => Node.toArray(param),
default: (param) => param
}
}
/**
*
* @param {Array} params
* @param {string[]} structs
* @param {string} type
*/
export function setDataStructure(params, structs, type = 'cases') {
return params.map((param, index) => {
const struct = structs[index]
const map = paramMap[type]
return map[struct] ? map[struct](param) : map.default(param)
})
}
/**
* 获取test case 入参的数据类型
* @param {string} code leetcode的实例函数体
* @param {string} type 类型,param入参,returns返回值
* @returns {string[]}
*/
export function getDataStructure(code, type = 'param') {
const regexMap = {
param: /@param\s+{\s*([^}\s]+)\s*}/g,
return: /@return\s+{\s*([^}\s]+)\s*}/g
}
const regex = regexMap[type]
const paramTypes = []
let match
while ((match = regex.exec(code)) !== null) paramTypes.push(match[1])
return paramTypes
}