Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Download
80668 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
18
var docblockRe = /^\s*(\/\*\*(.|\r?\n)*?\*\/)/;
19
20
var ltrimRe = /^\s*/;
21
/**
22
* @param {String} contents
23
* @return {String}
24
*/
25
function extract(contents) {
26
var match = contents.match(docblockRe);
27
if (match) {
28
return match[0].replace(ltrimRe, '') || '';
29
}
30
return '';
31
}
32
33
34
var commentStartRe = /^\/\*\*?/;
35
var commentEndRe = /\*\/$/;
36
var wsRe = /[\t ]+/g;
37
var stringStartRe = /(\r?\n|^) *\*/g;
38
var multilineRe = /(?:^|\r?\n) *(@[^\r\n]*?) *\r?\n *([^@\r\n\s][^@\r\n]+?) *\r?\n/g;
39
var propertyRe = /(?:^|\r?\n) *@(\S+) *([^\r\n]*)/g;
40
41
/**
42
* @param {String} contents
43
* @return {Array}
44
*/
45
function parse(docblock) {
46
docblock = docblock
47
.replace(commentStartRe, '')
48
.replace(commentEndRe, '')
49
.replace(wsRe, ' ')
50
.replace(stringStartRe, '$1');
51
52
// Normalize multi-line directives
53
var prev = '';
54
while (prev != docblock) {
55
prev = docblock;
56
docblock = docblock.replace(multilineRe, "\n$1 $2\n");
57
}
58
docblock = docblock.trim();
59
60
var result = [];
61
var match;
62
while (match = propertyRe.exec(docblock)) {
63
result.push([match[1], match[2]]);
64
}
65
66
return result;
67
}
68
69
/**
70
* Same as parse but returns an object of prop: value instead of array of paris
71
* If a property appers more than once the last one will be returned
72
*
73
* @param {String} contents
74
* @return {Object}
75
*/
76
function parseAsObject(docblock) {
77
var pairs = parse(docblock);
78
var result = {};
79
for (var i = 0; i < pairs.length; i++) {
80
result[pairs[i][0]] = pairs[i][1];
81
}
82
return result;
83
}
84
85
86
exports.extract = extract;
87
exports.parse = parse;
88
exports.parseAsObject = parseAsObject;
89
90