documentation/lib/sort.js
Tom MacWright d069b6a40b Support new node versions, including v4 and v5
* Compare strings as lowercase, to permit for v4+ compatibility
* Fix bad reference to loc.name
* Update README badge and fixture
* Remove brfs dependency
* Remove traverse
* Remove browser transform support
2015-12-01 15:34:50 -05:00

50 lines
1.2 KiB
JavaScript

'use strict';
/**
* Given a comment, get its sorting key: this is either the comment's
* name tag, or a hardcoded sorting index given by a user-provided
* `order` array.
*
* @param {Object} comment parsed documentation object
* @param {Array<string>} [order=[]] an optional list of namepaths
* @returns {string} sortable key
* @private
*/
function getSortKey(comment, order) {
var key = comment.name || comment.context.file;
if (order && order.indexOf(key) !== -1) {
return order.indexOf(key);
}
return key;
}
/**
* Sort two documentation objects, given an optional order object. Returns
* a numeric sorting value that is compatible with stream-sort.
*
* @param {Array<string>} order an array of namepaths that will be sorted
* in the order given.
* @param {Object} a documentation object
* @param {Object} b documentation object
* @return {number} sorting value
* @private
*/
module.exports = function sortDocs(order, a, b) {
a = getSortKey(a, order);
b = getSortKey(b, order);
if (typeof a === 'number' && typeof b === 'number') {
return a - b;
}
if (typeof a === 'number') {
return -1;
}
if (typeof b === 'number') {
return 1;
}
return a.toLowerCase().localeCompare(b.toLowerCase());
};