-
Notifications
You must be signed in to change notification settings - Fork 55
/
Copy pathCode.gs
84 lines (76 loc) · 3.65 KB
/
Code.gs
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
77
78
79
80
81
82
83
84
function jsDoc2JSON(input) {
input = UrlFetchApp.fetch("https://raw.githubusercontent.com/custom-functions/google-sheets/main/functions/DOUBLE.gs").getBlob().getDataAsString()
const jsDocJSON = {};
const jsDocComment = input.match(/\/\*\*.*\*\//s);
const jsDocDescription = jsDocComment ? jsDocComment[0].match(/^[^@]*/s) : false;
const description = jsDocDescription ? jsDocDescription[0].split("*").map(el => el.trim()).filter(el => el !== '' && el !== '/').join(" ") : false;
const jsDocTags = jsDocComment ? jsDocComment[0].match(/@.*(?=\@)/s) : false;
const rawTags = jsDocTags ? jsDocTags[0].split("*").map(el => el.trim()).filter(el => el !== '') : false;
const tags = [];
let components;
rawTags.forEach(el => {
if (el.startsWith("@param ")) { // https://jsdoc.app/tags-param.html
components = el.match(/^\@(param)(?: )\{(.*)\}(?: )(?:(?=\[)(?:\[(.*?)\])|(?!\[)(?:(.*?)))(?:(?= )(?: )(?:\- )?(.*)|(?! )$)/i);
if (components) {
components = components.filter(el => el !== undefined);
tags.push({
"tag": "param",
"type": components[2] ? components[2] : null,
"name": components[3] ? components[3] : null,
"description": components[4] ? components[4] : null,
});
} else {
components = el.match(/^\@(param) (?:(?=\[)(?:\[(.*)\]$)|(?!\[)(?:([^\s]+)$))/i);
if (components) {
components = components.filter(el => el !== undefined);
tags.push({
"tag": "param",
"type": components[2] ? components[2] : null,
"name": components[3] ? components[3] : null,
"description": components[4] ? components[4] : null,
});
} else {
console.log(`invalid @param tag: ${el}`);
}
}
} else if (el.startsWith("@return ") || el.startsWith("@returns ")) { // https://jsdoc.app/tags-returns.html
components = el.match(/^\@(returns?)(?: )\{(.*)\}(?:(?= )(?: )(?:\- )?(.*)|(?! )$)/i);
if (components) {
components = components.filter(el => el !== undefined);
tags.push({
"tag": "return",
"type": components[2] ? components[2] : null,
"description": components[3] ? components[3] : null,
});
} else {
console.log(`invalid @return tag: ${el}`);
}
} else {
console.log(`unknown tag: ${el}`);
}
});
jsDocJSON.description = description;
jsDocJSON.tags = tags;
console.log(JSON.stringify(jsDocJSON, null, 2));
}
// https://jsdoc.app/tags-param.html
// ^\@(param) (?:(?=\[)(?:\[(.*)\]$)|(?!\[)(?:([^\s]+)$))
// @param somebody
// ^\@(param)(?: )\{(.*)\}(?: )(?:(?=\[)(?:\[(.*?)\])|(?!\[)(?:(.*?)))(?:(?= )(?: )(?:\- )?(.*)|(?! )$)
// @param {string} somebody
// @param {string} somebody Somebody's name.
// @param {string} somebody - Somebody's name.
// @param {string} employee.name - The name of the employee.
// @param {Object[]} employees - The employees who are responsible for the project.
// @param {string} employees[].department - The employee's department.
// @param {string=} somebody - Somebody's name.
// @param {*} somebody - Whatever you want.
// @param {string} [somebody=John Doe] - Somebody's name.
// @param {(string|string[])} [somebody=John Doe] - Somebody's name, or an array of names.
// @param {string} [somebody] - Somebody's name.
// https://jsdoc.app/tags-returns.html
// ^\@(returns)(?: )\{(.*)\}(?:(?= )(?: )(?:\- )?(.*)|(?! )$)
// @returns {number}
// @returns {number} Sum of a and b
// @returns {(number|Array)} Sum of a and b or an array that contains a, b and the sum of a and b.
// @returns {Promise} Promise object represents the sum of a and b