UNPKG

777 BJavaScriptView Raw
1var indexedMap = require('../utilities/indexed_map')
2var map = require('../utilities/map')
3
4/*
5 * Internal: Sorts a `{ key, value, criteria, index }` tuple array by
6 * `criteria`. Returns the array of values if `isArray` is not `false`,
7 * or an object indexed by `key` otherwise.
8 */
9
10module.exports = function sortValues (values, isArray) {
11 var sorted = values.sort(function (left, right) {
12 var a = left.criteria
13 var b = right.criteria
14 if (a !== b) {
15 if (a > b || a === void 0) return 1
16 if (a < b || b === void 0) return -1
17 }
18 return a.index - b.index
19 })
20
21 if (isArray === false) {
22 return indexedMap(sorted, function (res) { return [ res.key, res.value ] })
23 } else {
24 return map(sorted, function (res) { return res.value })
25 }
26}