-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
76 lines (70 loc) · 2.02 KB
/
index.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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
"use strict";
const STRIP_COMMENTS = /((\/\/.*$)|(\/\*[\s\S]*?\*\/))/gm;
function _getParameterBody(func) {
const fnStr = func.toString().replace(STRIP_COMMENTS, "");
const parameterBodyStart = fnStr.indexOf("(") + 1;
let parameterBodySeek = parameterBodyStart;
let parenthesisCounter = 1;
while (parenthesisCounter > 0) {
if (fnStr.charAt(parameterBodySeek) === "(") {
parenthesisCounter += 1;
} else if (fnStr.charAt(parameterBodySeek) === ")") {
parenthesisCounter -= 1;
}
parameterBodySeek += 1;
}
return fnStr.substring(parameterBodyStart, parameterBodySeek - 1);
}
function _getParams(func) {
let paramBody = _getParameterBody(func);
let parenthesisCounter = 0;
let chunks = [];
let chunkStart = 0;
for (let i = 0; i < paramBody.length; i++) {
if (paramBody.charAt(i) === "(") {
parenthesisCounter += 1;
} else if (paramBody.charAt(i) === ")") {
parenthesisCounter -= 1;
} else if (parenthesisCounter === 0 && paramBody.charAt(i) === ",") {
chunks.push(paramBody.substring(chunkStart, i).trim());
chunkStart = i + 1;
}
}
if (paramBody.length > 0) {
chunks.push(paramBody.substring(chunkStart, paramBody.length).trim());
}
return chunks;
}
/**
* Returns the parameters of the given function as a list of strings
* @param {any JS function} func
*/
function getParamNames(func) {
let params = _getParams(func);
return params.map(param => {
return param.split("=")[0].trim();
});
}
/**
* Returns the default parameters of a given function as a list of strings
* @param {any JS function} func
*/
function getParamDefaultValues(func) {
let params = _getParams(func);
return params.map(param => {
let val = param.split("=");
if (val.length < 2) {
return undefined;
}
if (val.length === 2) {
return val[1].trim();
}
return val
.splice(1)
.join("=")
.trim();
});
}
if (typeof module !== "undefined" && module.exports) {
module.exports = { getParamNames, getParamDefaultValues };
}