From a898c3d7bcde2d0bfbacbff6de8173fb80cb18b3 Mon Sep 17 00:00:00 2001 From: jdalton Date: Sat, 4 Jul 2015 14:52:21 -0700 Subject: [PATCH] Absorb `_.sortByAll` into `_.sortBy`. --- lodash.src.js | 82 +++++++++++++-------------------------------------- test/test.js | 38 ++---------------------- 2 files changed, 23 insertions(+), 97 deletions(-) diff --git a/lodash.src.js b/lodash.src.js index ea8727690..057792706 100644 --- a/lodash.src.js +++ b/lodash.src.js @@ -2752,17 +2752,21 @@ * @private * @param {Array|Object|string} collection The collection to iterate over. * @param {Function[]|Object[]|string[]} iteratees The iteratees to sort by. - * @param {boolean[]} orders The sort orders of `iteratees`. + * @param {boolean[]|string[]} orders The sort orders of `iteratees`. * @returns {Array} Returns the new sorted array. */ function baseSortByOrder(collection, iteratees, orders) { - var callback = getIteratee(), + var toIteratee = getIteratee(), index = -1; - iteratees = arrayMap(iteratees, function(iteratee) { return callback(iteratee); }); + iteratees = arrayMap(iteratees.length ? iteratees : Array(1), function(iteratee) { + return toIteratee(iteratee); + }); - var result = baseMap(collection, function(value) { - var criteria = arrayMap(iteratees, function(iteratee) { return iteratee(value); }); + var result = baseMap(collection, function(value, key, collection) { + var criteria = arrayMap(iteratees, function(iteratee) { + return iteratee(value, key, collection); + }); return { 'criteria': criteria, 'index': ++index, 'value': value }; }); @@ -6822,67 +6826,22 @@ /** * Creates an array of elements, sorted in ascending order by the results of - * running each element in a collection through `iteratee`. This method performs - * a stable sort, that is, it preserves the original sort order of equal elements. - * The iteratee is invoked with three arguments: (value, index|key, collection). + * running each element in a collection through each iteratee. This method + * performs a stable sort, that is, it preserves the original sort order of + * equal elements. The iteratee is invoked with three arguments: + * (value, index|key, collection). * * @static * @memberOf _ * @category Collection * @param {Array|Object|string} collection The collection to iterate over. - * @param {Function|Object|string} [iteratee=_.identity] The function invoked per iteration. - * @returns {Array} Returns the new sorted array. - * @example - * - * _.sortBy([1, 2, 3], function(n) { - * return Math.sin(n); - * }); - * // => [3, 1, 2] - * - * _.sortBy([1, 2, 3], function(n) { - * return this.sin(n); - * }, Math); - * // => [3, 1, 2] - * - * var users = [ - * { 'user': 'fred' }, - * { 'user': 'pebbles' }, - * { 'user': 'barney' } - * ]; - * - * // using the `_.property` callback shorthand - * _.map(_.sortBy(users, 'user'), 'user'); - * // => ['barney', 'fred', 'pebbles'] - */ - function sortBy(collection, iteratee, thisArg) { - if (collection == null) { - return []; - } - if (thisArg && isIterateeCall(collection, iteratee, thisArg)) { - iteratee = undefined; - } - var index = -1; - iteratee = getIteratee(iteratee, thisArg, 3); - - var result = baseMap(collection, function(value, key, collection) { - return { 'criteria': iteratee(value, key, collection), 'index': ++index, 'value': value }; - }); - return baseSortBy(result, compareAscending); - } - - /** - * This method is like `_.sortBy` except that it can sort by multiple iteratees - * or property names. - * - * @static - * @memberOf _ - * @category Collection - * @param {Array|Object|string} collection The collection to iterate over. - * @param {...(Function|Function[]|Object|Object[]|string|string[])} iteratees + * @param {...(Function|Function[]|Object|Object[]|string|string[])} [iteratees=[_.identity]] * The iteratees to sort by, specified as individual values or arrays of values. * @returns {Array} Returns the new sorted array. * @example * + * var resolve = _.partial(_.map, _, _.values); + * * var users = [ * { 'user': 'fred', 'age': 48 }, * { 'user': 'barney', 'age': 36 }, @@ -6890,8 +6849,6 @@ * { 'user': 'barney', 'age': 34 } * ]; * - * var resolve = _.partial(_.map, _, _.values); - * * resolve( _.sortBy(users, function(o) { return o.user; }) ); * // => // => [['barney', 36], ['barney', 34], ['fred', 48], ['fred', 42]] * @@ -6907,8 +6864,10 @@ if (collection == null) { return []; } - var guard = iteratees[2]; - if (guard && isIterateeCall(iteratees[0], iteratees[1], guard)) { + var length = iteratees.length; + if (length > 1 && isIterateeCall(collection, iteratees[0], iteratees[1])) { + iteratees = []; + } else if (length > 2 && isIterateeCall(iteratees[0], iteratees[1], iteratees[2])) { iteratees.length = 1; } return baseSortByOrder(collection, baseFlatten(iteratees), []); @@ -11560,7 +11519,6 @@ lodash.shuffle = shuffle; lodash.slice = slice; lodash.sortBy = sortBy; - lodash.sortByAll = sortByAll; lodash.sortByOrder = sortByOrder; lodash.spread = spread; lodash.take = take; diff --git a/test/test.js b/test/test.js index 9872c9939..075178d78 100644 --- a/test/test.js +++ b/test/test.js @@ -14146,12 +14146,6 @@ QUnit.module('lodash.sortBy'); (function() { - function Pair(a, b, c) { - this.a = a; - this.b = b; - this.c = c; - } - var objects = [ { 'a': 'x', 'b': 3 }, { 'a': 'y', 'b': 4 }, @@ -14159,21 +14153,6 @@ { 'a': 'y', 'b': 2 } ]; - var stableArray = [ - new Pair(1, 1, 1), new Pair(1, 2, 1), - new Pair(1, 1, 1), new Pair(1, 2, 1), - new Pair(1, 3, 1), new Pair(1, 4, 1), - new Pair(1, 5, 1), new Pair(1, 6, 1), - new Pair(2, 1, 2), new Pair(2, 2, 2), - new Pair(2, 3, 2), new Pair(2, 4, 2), - new Pair(2, 5, 2), new Pair(2, 6, 2), - new Pair(undefined, 1, 1), new Pair(undefined, 2, 1), - new Pair(undefined, 3, 1), new Pair(undefined, 4, 1), - new Pair(undefined, 5, 1), new Pair(undefined, 6, 1) - ]; - - var stableObject = _.zipObject('abcdefghijklmnopqrst'.split(''), stableArray); - test('should sort in ascending order', 1, function() { var actual = _.map(_.sortBy(objects, function(object) { return object.b; @@ -14182,16 +14161,6 @@ deepEqual(actual, [1, 2, 3, 4]); }); - test('should perform a stable sort (test in V8)', 2, function() { - _.each([stableArray, stableObject], function(value, index) { - var actual = _.sortBy(value, function(pair) { - return pair.a; - }); - - deepEqual(actual, stableArray, index ? 'object' : 'array'); - }); - }); - test('should provide the correct `iteratee` arguments', 1, function() { var args; @@ -14286,7 +14255,7 @@ QUnit.module('sortBy methods'); - _.each(['sortByAll', 'sortByOrder'], function(methodName) { + _.each(['sortBy', 'sortByOrder'], function(methodName) { var func = _[methodName]; function Pair(a, b, c) { @@ -17548,7 +17517,6 @@ 'sample', 'shuffle', 'sortBy', - 'sortByAll', 'sortByOrder', 'take', 'times', @@ -17563,7 +17531,7 @@ var acceptFalsey = _.difference(allMethods, rejectFalsey); - test('should accept falsey arguments', 207, function() { + test('should accept falsey arguments', 205, function() { var emptyArrays = _.map(falsey, _.constant([])); _.each(acceptFalsey, function(methodName) { @@ -17599,7 +17567,7 @@ }); }); - test('should return an array', 68, function() { + test('should return an array', 66, function() { var array = [1, 2, 3]; _.each(returnArrays, function(methodName) {