all files / scour/lib/ sort_values.js

100% Statements 17/17
83.33% Branches 10/12
100% Functions 1/1
100% Lines 13/13
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               18× 204× 204× 204× 200× 46×       18× 20×   73×      
const indexedMap = require('../utilities/indexed_map')
const map = require('../utilities/map')
 
/*
 * Internal: Sorts a `{ key, value, criteria, index }` tuple array by
 * `criteria`. Returns the array of values if `isArray` is not `false`,
 * or an object indexed by `key` otherwise.
 */
 
module.exports = function sortValues (values, isArray) {
  var sorted = values.sort((left, right) => {
    const a = left.criteria
    const b = right.criteria
    if (a !== b) {
      if (a > b || a === void 0) return 1
      Eif (a < b || b === void 0) return -1
    }
    return a.index - b.index
  })
 
  if (isArray === false) {
    return indexedMap(sorted, (res) => [ res.key, res.value ])
  } else {
    return map(sorted, (res) => res.value)
  }
}