Cleanup baseOrderBy.

This commit is contained in:
jdalton
2019-08-30 13:44:00 -07:00
parent 5df1777477
commit 29eb5713f5

View File

@@ -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))