Skip to content

Commit

Permalink
Cleanup baseOrderBy.
Browse files Browse the repository at this point in the history
  • Loading branch information
jdalton committed Aug 30, 2019
1 parent 5df1777 commit 29eb571
Showing 1 changed file with 19 additions and 9 deletions.
28 changes: 19 additions & 9 deletions .internal/baseOrderBy.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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))
Expand Down

0 comments on commit 29eb571

Please sign in to comment.