mirror of
https://github.com/whoisclebs/lodash.git
synced 2026-01-29 06:27:49 +00:00
Cleanup baseOrderBy.
This commit is contained in:
@@ -4,8 +4,7 @@ import baseGet from './baseGet.js'
|
||||
import compareMultiple from './compareMultiple.js'
|
||||
import isArrayLike from '../isArrayLike.js'
|
||||
|
||||
// As existing identity function is in ../test/utils.js, so defining it here, it can be moved to utils
|
||||
const identity = value => value;
|
||||
const identity = (value) => value
|
||||
|
||||
/**
|
||||
* The base implementation of `orderBy` without param guards.
|
||||
@@ -17,20 +16,31 @@ const identity = value => value;
|
||||
* @returns {Array} Returns the new sorted array.
|
||||
*/
|
||||
function baseOrderBy(collection, iteratees, orders) {
|
||||
if (iteratees.length) {
|
||||
iteratees = iteratees.map((iteratee) => {
|
||||
if (Array.isArray(iteratee)) {
|
||||
return (value) => baseGet(value, iteratee)
|
||||
}
|
||||
|
||||
return iteratee
|
||||
})
|
||||
} else {
|
||||
iteratees = [identity]
|
||||
}
|
||||
|
||||
let criteriaIndex = -1
|
||||
let eachIndex = -1
|
||||
iteratees = iteratees.length ? iteratees.map((iteratee) => {
|
||||
if (Array.isArray(iteratee)) {
|
||||
return (value) => baseGet(value, iteratee)
|
||||
}
|
||||
return iteratee
|
||||
}) : [identity]
|
||||
|
||||
const result = isArrayLike(collection) ? new Array(collection.length) : []
|
||||
|
||||
baseEach(collection, (value) => {
|
||||
const criteria = iteratees.map((iteratee) => iteratee(value))
|
||||
result[++eachIndex] = { 'criteria': criteria, 'index': ++criteriaIndex, 'value': value }
|
||||
|
||||
result[++eachIndex] = {
|
||||
criteria,
|
||||
index: ++criteriaIndex,
|
||||
value
|
||||
}
|
||||
})
|
||||
|
||||
return baseSortBy(result, (object, other) => compareMultiple(object, other, orders))
|
||||
|
||||
Reference in New Issue
Block a user