Skip to content

Commit 8d86666

Browse files
committed
Add a --sort-order option
Valid values for the `--sort-order` flag are `"source"` (default) and `"alpha"`. When set to `"alpa"` the order of the entries in the docs are sorted alphabetically. Fixes documentationjs#534
1 parent b1f288d commit 8d86666

7 files changed

+749
-6
lines changed

docs/USAGE.md

+2
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,8 @@ Options:
4545
--document-exported Generate documentation for all exported bindings and
4646
members even if there is no JSDoc for them
4747
[boolean] [default: false]
48+
--sort-order The order to sort the documentation
49+
[choices: "source", "alpha"] [default: "source"]
4850
--theme, -t specify a theme: this must be a valid theme module
4951
--name project name. by default, inferred from package.json
5052
--watch, -w watch input files and rebuild documentation when they

lib/commands/shared_options.js

+5
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,11 @@ function sharedInputOptions(parser) {
5959
describe: 'Generate documentation for all exported bindings and members ' +
6060
'even if there is no JSDoc for them',
6161
default: false
62+
})
63+
.option('sort-order', {
64+
describe: 'The order to sort the documentation',
65+
choices: ['source', 'alpha'],
66+
default: 'source'
6267
});
6368
}
6469

lib/sort.js

+15-6
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,7 @@ var chalk = require('chalk');
1414
*/
1515
module.exports = function sortDocs(comments, options) {
1616
if (!options || !options.toc) {
17-
return comments.sort(function (a, b) {
18-
return a.context.sortKey.localeCompare(b.context.sortKey);
19-
});
17+
return sortComments(comments, options && options.sortOrder);
2018
}
2119
var indexes = options.toc.reduce(function (memo, val, i) {
2220
if (typeof val === 'object' && val.name) {
@@ -67,9 +65,7 @@ module.exports = function sortDocs(comments, options) {
6765
return indexes[a.name] - indexes[b.name];
6866
}
6967
});
70-
unfixed.sort(function (a, b) {
71-
return a.context.sortKey.localeCompare(b.context.sortKey);
72-
});
68+
sortComments(unfixed, options.sortOrder);
7369
Object.keys(toBeSorted).filter(function (key) {
7470
return toBeSorted[key] === false;
7571
}).forEach(function (key) {
@@ -78,3 +74,16 @@ module.exports = function sortDocs(comments, options) {
7874
});
7975
return fixed.concat(unfixed);
8076
};
77+
78+
function compareCommentsByName(a, b) {
79+
return a.name.localeCompare(b.name, undefined, {caseFirst: 'upper'});
80+
}
81+
82+
function compareCommentsBySourceLocation(a, b) {
83+
return a.context.sortKey.localeCompare(b.context.sortKey);
84+
}
85+
86+
function sortComments(comments, sortOrder) {
87+
return comments.sort(sortOrder === 'alpha' ?
88+
compareCommentsByName : compareCommentsBySourceLocation);
89+
}
+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
// Options: {"sortOrder": "alpha"}
2+
3+
/** */
4+
function b() {}
5+
6+
/** */
7+
function a() {}
8+
9+
/** */
10+
class A {
11+
/** */
12+
y() {}
13+
/** */
14+
Y() {}
15+
/** */
16+
x() {}
17+
/** */
18+
X() {}
19+
}
20+
21+
/** */
22+
class B {
23+
/** */
24+
y() {}
25+
/** */
26+
Y() {}
27+
/** */
28+
x() {}
29+
/** */
30+
X() {}
31+
}

0 commit comments

Comments
 (0)