This repository was archived by the owner on May 3, 2024. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathindex.js
112 lines (98 loc) · 3.43 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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
const dox = require('dox');
/**
* Format string as name.
*
* @example formatStringForName('module.exports.parser');
* @param {String} contents String to format.
* @return {String} Formatted string.
* @private
*/
const formatStringForName = content =>
content.toString().replace(/module\.exports\.|\.prototype|\(\)/gu, '');
/**
* Format string as param.
*
* @example formatStringForParam('[optional param]');
* @param {String} contents String to format.
* @return {String} Formatted string.
* @private
*/
const formatStringForParam = content =>
content.toString().replace(/\[|\]/gu, '');
/**
* Format string as UID.
*
* @example formatStringForUID('example string');
* @param {String} contents String to format.
* @return {String} Formatted string.
* @private
*/
const formatStringForUID = content =>
content
.toString()
.toLowerCase()
.replace(/[^\w\.]+/gu, '-')
.replace(/^-|-$/gu, '');
/**
* Dox parser for doxdox.
*
* @example parser(content, 'index.js').then(methods => console.log(methods));
* @param {String} content Contents of file.
* @param {String} filename Name of file. Used to generate UIDs.
* @return {Promise} Promise with methods parsed from contents.
* @public
*/
const parser = (content, filename) =>
dox
.parseComments(content, {
'raw': true,
'skipSingleStar': true
})
.filter(method => !method.ignore && method.ctx)
.map(method => ({
'uid': formatStringForUID(`${filename}-${method.ctx.string}`),
'isPrivate': method.isPrivate,
'type': method.ctx.type,
'name': formatStringForName(method.ctx.string),
'description': method.description.full,
'empty': !method.description.full && !method.tags.length,
'params': method.tags
.filter(tag => tag.type === 'param' && !tag.name.match(/\./u))
.map(tag => {
if (tag.optional) {
return `[${formatStringForParam(tag.name)}]`;
}
return formatStringForParam(tag.name);
})
.join(', ')
.replace(/\], \[/gu, ', ')
.replace(', [', '[, '),
'tags': {
'example': method.tags
.filter(tag => tag.type === 'example')
.map(tag => tag.string),
'param': method.tags
.filter(tag => tag.type === 'param')
.map(tag => ({
'name': formatStringForParam(tag.name),
'isOptional': tag.optional,
'types': tag.types,
'description': tag.description
})),
'property': method.tags
.filter(tag => tag.type === 'property')
.map(tag => ({
'name': tag.name,
'types': tag.types,
'description': tag.description
})),
'return': method.tags
.filter(tag => tag.type === 'return' || tag.type === 'returns')
.map(tag => ({
'types': tag.types,
'description': tag.description
}))
}
}))
.filter(method => !method.empty);
module.exports = parser;