读取函数'的参数以及它们是否是可选的

Read a function's arguments and whether they are optional

本文关键字:是否是 参数 函数 读取      更新时间:2023-09-26

这是我的字符串(作为字符串):

bytearray([source[, encoding[, errors]]])

这是我想要的数据:

arguments: [ { name: 'source', optional: true }, ... ]

或者换句话说,我想要每个参数的名称,并知道它是否是可选的。括号表示它是否是可选的。

我编写了只获取参数的代码,并看到了其他示例,但没有获取可选参数。

假设所需参数在可选参数之前,可选性可以由它们的直接环境决定:

Argumente = []
'bytearray([source[, encoding[, errors]]])'
.replace(/'[[, ]*('w+)|('w+)(?=[[,)])/g,
         function(m, p1, p2) 
         { 
             Argumente.push({ name: p1||p2, optional: !p2 })
         })
console.log(Argumente)