Code coverage report for lib/sort.js

Statements: 100% (15 / 15)      Branches: 92.86% (13 / 14)      Functions: 100% (2 / 2)      Lines: 100% (15 / 15)      Ignored: none     

All files » lib/ » 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                        26 684   684 20     664                           26 342 342   342 8   334 1   333 3     330    
'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());
};