From 29eb5713f58bad015ccf0a74da741b8c7184dd2f Mon Sep 17 00:00:00 2001 From: jdalton Date: Fri, 30 Aug 2019 13:44:00 -0700 Subject: [PATCH] Cleanup baseOrderBy. --- .internal/baseOrderBy.js | 28 +++++++++++++++++++--------- 1 file changed, 19 insertions(+), 9 deletions(-) diff --git a/.internal/baseOrderBy.js b/.internal/baseOrderBy.js index 7a5308224b..aab783b9c9 100644 --- a/.internal/baseOrderBy.js +++ b/.internal/baseOrderBy.js @@ -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))