-
Notifications
You must be signed in to change notification settings - Fork 485
/
Copy pathsort.js
54 lines (52 loc) · 1.51 KB
/
sort.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
'use strict';
var parseMarkdown = require('./parse_markdown');
/**
* Sort two documentation objects, given an optional order object. Returns
* a numeric sorting value that is compatible with stream-sort.
*
* @param {Array<Object>} comments all comments
* @param {Object} options options from documentation.yml
* @return {number} sorting value
* @private
*/
module.exports = function sortDocs(comments, options) {
if (!options || !options.toc) {
return comments.sort(function (a, b) {
return a.context.sortKey.localeCompare(b.context.sortKey);
});
}
var indexes = options.toc.reduce(function (memo, val, i) {
if (typeof val === 'object' && val.name) {
val.kind = 'note';
memo[val.name] = i;
} else {
memo[val] = i;
}
return memo;
}, {});
var fixed = options.toc.filter(function (val) {
return typeof val === 'object' && val.name;
}).map(function (val) {
if (val.description) {
val.description = parseMarkdown(val.description);
}
return val;
});
var unfixed = [];
comments.forEach(function (comment) {
if (!comment.memberof && indexes[comment.name] !== undefined) {
fixed.push(comment);
} else {
unfixed.push(comment);
}
});
fixed.sort(function (a, b) {
if (indexes[a.name] !== undefined && indexes[b.name] !== undefined) {
return indexes[a.name] - indexes[b.name];
}
});
unfixed.sort(function (a, b) {
return a.context.sortKey.localeCompare(b.context.sortKey);
});
return fixed.concat(unfixed);
};