Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Download
80542 views
1
/**
2
* Copyright 2013 Facebook, Inc.
3
*
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
* you may not use this file except in compliance with the License.
6
* You may obtain a copy of the License at
7
*
8
* http://www.apache.org/licenses/LICENSE-2.0
9
*
10
* Unless required by applicable law or agreed to in writing, software
11
* distributed under the License is distributed on an "AS IS" BASIS,
12
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
* See the License for the specific language governing permissions and
14
* limitations under the License.
15
*/
16
17
var docblockRe = /^\s*(\/\*\*(.|\r?\n)*?\*\/)/;
18
var ltrimRe = /^\s*/;
19
/**
20
* @param {String} contents
21
* @return {String}
22
*/
23
function extract(contents) {
24
var match = contents.match(docblockRe);
25
if (match) {
26
return match[0].replace(ltrimRe, '') || '';
27
}
28
return '';
29
}
30
31
32
var commentStartRe = /^\/\*\*?/;
33
var commentEndRe = /\*+\/$/;
34
var wsRe = /[\t ]+/g;
35
var stringStartRe = /(\r?\n|^) *\*/g;
36
var multilineRe = /(?:^|\r?\n) *(@[^\r\n]*?) *\r?\n *([^@\r\n\s][^@\r\n]+?) *\r?\n/g;
37
var propertyRe = /(?:^|\r?\n) *@(\S+) *([^\r\n]*)/g;
38
39
/**
40
* @param {String} contents
41
* @return {Array}
42
*/
43
function parse(docblock) {
44
docblock = docblock
45
.replace(commentStartRe, '')
46
.replace(commentEndRe, '')
47
.replace(wsRe, ' ')
48
.replace(stringStartRe, '$1');
49
50
// Normalize multi-line directives
51
var prev = '';
52
while (prev != docblock) {
53
prev = docblock;
54
docblock = docblock.replace(multilineRe, "\n$1 $2\n");
55
}
56
docblock = docblock.trim();
57
58
var result = [];
59
var match;
60
while (match = propertyRe.exec(docblock)) {
61
result.push([match[1], match[2]]);
62
}
63
64
return result;
65
}
66
67
/**
68
* Same as parse but returns an object of prop: value instead of array of paris
69
* If a property appers more than once the last one will be returned
70
*
71
* @param {String} contents
72
* @return {Object}
73
*/
74
function parseAsObject(docblock) {
75
var pairs = parse(docblock);
76
var result = {};
77
for (var i = 0; i < pairs.length; i++) {
78
result[pairs[i][0]] = pairs[i][1];
79
}
80
return result;
81
}
82
83
84
exports.extract = extract;
85
exports.parse = parse;
86
exports.parseAsObject = parseAsObject;
87
88