| Leo Repp | 58b9f11 | 2021-11-22 11:57:47 +0100 | [diff] [blame^] | 1 | var arrayMap = require('./_arrayMap'), |
| 2 | baseGet = require('./_baseGet'), |
| 3 | baseIteratee = require('./_baseIteratee'), |
| 4 | baseMap = require('./_baseMap'), |
| 5 | baseSortBy = require('./_baseSortBy'), |
| 6 | baseUnary = require('./_baseUnary'), |
| 7 | compareMultiple = require('./_compareMultiple'), |
| 8 | identity = require('./identity'), |
| 9 | isArray = require('./isArray'); |
| 10 | |
| 11 | /** |
| 12 | * The base implementation of `_.orderBy` without param guards. |
| 13 | * |
| 14 | * @private |
| 15 | * @param {Array|Object} collection The collection to iterate over. |
| 16 | * @param {Function[]|Object[]|string[]} iteratees The iteratees to sort by. |
| 17 | * @param {string[]} orders The sort orders of `iteratees`. |
| 18 | * @returns {Array} Returns the new sorted array. |
| 19 | */ |
| 20 | function baseOrderBy(collection, iteratees, orders) { |
| 21 | if (iteratees.length) { |
| 22 | iteratees = arrayMap(iteratees, function(iteratee) { |
| 23 | if (isArray(iteratee)) { |
| 24 | return function(value) { |
| 25 | return baseGet(value, iteratee.length === 1 ? iteratee[0] : iteratee); |
| 26 | } |
| 27 | } |
| 28 | return iteratee; |
| 29 | }); |
| 30 | } else { |
| 31 | iteratees = [identity]; |
| 32 | } |
| 33 | |
| 34 | var index = -1; |
| 35 | iteratees = arrayMap(iteratees, baseUnary(baseIteratee)); |
| 36 | |
| 37 | var result = baseMap(collection, function(value, key, collection) { |
| 38 | var criteria = arrayMap(iteratees, function(iteratee) { |
| 39 | return iteratee(value); |
| 40 | }); |
| 41 | return { 'criteria': criteria, 'index': ++index, 'value': value }; |
| 42 | }); |
| 43 | |
| 44 | return baseSortBy(result, function(object, other) { |
| 45 | return compareMultiple(object, other, orders); |
| 46 | }); |
| 47 | } |
| 48 | |
| 49 | module.exports = baseOrderBy; |