Cleanup _.sortBy, _.sortByAll, _.sortByOrder, & baseSortByOrder.

This commit is contained in:
jdalton
2015-04-13 08:17:01 -07:00
parent 38ab42f855
commit 63d493aa1f
2 changed files with 52 additions and 61 deletions

View File

@@ -2841,24 +2841,16 @@
* *
* @private * @private
* @param {Array|Object|string} collection The collection to iterate over. * @param {Array|Object|string} collection The collection to iterate over.
* @param {string[]} props The property names to sort by. * @param {Function[]|string[]} iteratees The iteratees to sort by.
* @param {boolean[]} orders The sort orders of `props`. * @param {boolean[]} orders The sort orders of `iteratees`.
* @returns {Array} Returns the new sorted array. * @returns {Array} Returns the new sorted array.
*/ */
function baseSortByOrder(collection, comparators, orders) { function baseSortByOrder(collection, iteratees, orders) {
var callback = getCallback(); var callback = getCallback();
iteratees = arrayMap(iteratees, function(iteratee) { return callback(iteratee); });
comparators = arrayMap(comparators, function(comparitor) {
return callback(comparitor);
});
var result = baseMap(collection, function(value, index) { var result = baseMap(collection, function(value, index) {
var length = comparators.length, var criteria = arrayMap(iteratees, function(iteratee) { return iteratee(value); });
criteria = Array(length);
while (length--) {
criteria[length] = comparators[length](value);
}
return { 'criteria': criteria, 'index': index, 'value': value }; return { 'criteria': criteria, 'index': index, 'value': value };
}); });
@@ -7293,99 +7285,101 @@
if (collection == null) { if (collection == null) {
return []; return [];
} }
var index = -1,
length = getLength(collection),
result = isLength(length) ? Array(length) : [];
if (thisArg && isIterateeCall(collection, iteratee, thisArg)) { if (thisArg && isIterateeCall(collection, iteratee, thisArg)) {
iteratee = null; iteratee = null;
} }
var index = -1;
iteratee = getCallback(iteratee, thisArg, 3); iteratee = getCallback(iteratee, thisArg, 3);
baseEach(collection, function(value, key, collection) {
result[++index] = { 'criteria': iteratee(value, key, collection), 'index': index, 'value': value }; var result = baseMap(collection, function(value, key, collection) {
return { 'criteria': iteratee(value, key, collection), 'index': ++index, 'value': value };
}); });
return baseSortBy(result, compareAscending); return baseSortBy(result, compareAscending);
} }
/** /**
* This method is like `_.sortBy` except that it sorts by property names * This method is like `_.sortBy` except that it can sort by multiple iteratees
* instead of an iteratee function. * or property names.
*
* If a property name is provided for an iteratee the created `_.property`
* style callback returns the property value of the given element.
* *
* @static * @static
* @memberOf _ * @memberOf _
* @category Collection * @category Collection
* @param {Array|Object|string} collection The collection to iterate over. * @param {Array|Object|string} collection The collection to iterate over.
* @param {...(string|string[]|func[])} args The iteratees to sort by, * @param {...(Function|Function[]|string|string[])} iteratees The iteratees
* specified by a single, an array or multiple property names and functions. * to sort by, specified as individual values or arrays of values.
* @returns {Array} Returns the new sorted array. * @returns {Array} Returns the new sorted array.
* @example * @example
* *
* var users = [ * var users = [
* { 'user': 'barney', 'age': 36, 'years': 1}, * { 'user': 'fred', 'age': 48 },
* { 'user': 'fred', 'age': 40, 'years': 5}, * { 'user': 'barney', 'age': 36 },
* { 'user': 'barney', 'age': 26, 'years': 0}, * { 'user': 'fred', 'age': 42 },
* { 'user': 'fred', 'age': 30, 'years': 3} * { 'user': 'barney', 'age': 34 }
* ]; * ];
* *
* _.map(_.sortByAll(users, ['user', 'age']), _.values); * _.map(_.sortByAll(users, ['user', 'age']), _.values);
* // => [['barney', 26, 0], ['barney', 40, 6], ['fred', 30, 3], ['fred', 36, 1]] * // => [['barney', 34], ['barney', 36], ['fred', 42], ['fred', 48]]
* *
* function a(customer) {return customer.age - customer.years} * _.map(_.sortByAll(users, 'user', function(chr) {
* _.sortByAll(users, a, 'age') * return Math.floor(chr.age / 10);
* // => [['barney', 26, 0], ['fred', 30, 3], ['barney', 40, 6], ['fred', 36, 1]] * }), _.values);
* // => [['barney', 36], ['barney', 34], ['fred', 48], ['fred', 42]]
*/ */
var sortByAll = restParam(function(collection, args) { var sortByAll = restParam(function(collection, iteratees) {
if (collection == null) { if (collection == null) {
return []; return [];
} }
var guard = args[2]; var guard = iteratees[2];
if (guard && isIterateeCall(args[0], args[1], guard)) { if (guard && isIterateeCall(iteratees[0], iteratees[1], guard)) {
args.length = 1; iteratees.length = 1;
} }
return baseSortByOrder(collection, baseFlatten(args), []); return baseSortByOrder(collection, baseFlatten(iteratees), []);
}); });
/** /**
* This method is like `_.sortByAll` except that it allows specifying the * This method is like `_.sortByAll` except that it allows specifying the
* sort orders of the property names to sort by. A truthy value in `orders` * sort orders of the iteratees to sort by. A truthy value in `orders` will
* will sort the corresponding property name in ascending order while a * sort the corresponding property name in ascending order while a falsey
* falsey value will sort it in descending order. * value will sort it in descending order.
* *
* @static * @static
* @memberOf _ * @memberOf _
* @category Collection * @category Collection
* @param {Array|Object|string} collection The collection to iterate over. * @param {Array|Object|string} collection The collection to iterate over.
* @param {string[]|func[]} comparators The iteratees to sort by. * @param {Function[]|string[]} iteratees The iteratees to sort by.
* @param {boolean[]} orders The sort orders of `props`. * @param {boolean[]} orders The sort orders of `iteratees`.
* @param- {Object} [guard] Enables use as a callback for functions like `_.reduce`. * @param- {Object} [guard] Enables use as a callback for functions like `_.reduce`.
* @returns {Array} Returns the new sorted array. * @returns {Array} Returns the new sorted array.
* @example * @example
* *
* var users = [ * var users = [
* { 'user': 'barney', 'age': 26 }, * { 'user': 'fred', 'age': 48 },
* { 'user': 'fred', 'age': 40 }, * { 'user': 'barney', 'age': 34 },
* { 'user': 'barney', 'age': 36 }, * { 'user': 'fred', 'age': 42 },
* { 'user': 'fred', 'age': 30 } * { 'user': 'barney', 'age': 36 }
* ]; * ];
* *
* // sort by `user` in ascending order and by `age` in descending order * // sort by `user` in ascending order and by `age` in descending order
* _.map(_.sortByOrder(users, ['user', 'age'], [true, false]), _.values); * _.map(_.sortByOrder(users, ['user', 'age'], [true, false]), _.values);
* // => [['barney', 36], ['barney', 26], ['fred', 40], ['fred', 30]] * // => [['barney', 36], ['barney', 34], ['fred', 48], ['fred', 42]]
*/ */
function sortByOrder(collection, comparators, orders, guard) { function sortByOrder(collection, iteratees, orders, guard) {
if (collection == null) { if (collection == null) {
return []; return [];
} }
if (guard && isIterateeCall(comparators, orders, guard)) { if (guard && isIterateeCall(iteratees, orders, guard)) {
orders = null; orders = null;
} }
if (!isArray(comparators)) { if (!isArray(iteratees)) {
comparators = comparators == null ? [] : [comparators]; iteratees = iteratees == null ? [] : [iteratees];
} }
if (!isArray(orders)) { if (!isArray(orders)) {
orders = orders == null ? [] : [orders]; orders = orders == null ? [] : [orders];
} }
return baseSortByOrder(collection, comparators, orders); return baseSortByOrder(collection, iteratees, orders);
} }
/** /**

View File

@@ -14185,10 +14185,10 @@
} }
var objects = [ var objects = [
{ 'a': 'x', 'b': 3, c: 2 }, { 'a': 'x', 'b': 3 },
{ 'a': 'y', 'b': 4, c: 1 }, { 'a': 'y', 'b': 4 },
{ 'a': 'x', 'b': 1, c: 4}, { 'a': 'x', 'b': 1 },
{ 'a': 'y', 'b': 2, c: 3} { 'a': 'y', 'b': 2 }
]; ];
var stableOrder = [ var stableOrder = [
@@ -14209,12 +14209,9 @@
deepEqual(actual, [objects[2], objects[0], objects[3], objects[1]]); deepEqual(actual, [objects[2], objects[0], objects[3], objects[1]]);
}); });
test('`_.' + methodName + '` should permit function comparators', 1, function() { test('`_.' + methodName + '` should support iteratees', 1, function() {
function b(obj) { var actual = func(objects, ['a', function(object) { return object.b; }]);
return obj.b * obj.c; deepEqual(actual, [objects[2], objects[0], objects[3], objects[1]]);
}
var actual = func(objects, [b, 'a']);
deepEqual(actual, [objects[2], objects[1], objects[0], objects[3]]);
}); });
test('`_.' + methodName + '` should perform a stable sort (test in IE > 8, Opera, and V8)', 1, function() { test('`_.' + methodName + '` should perform a stable sort (test in IE > 8, Opera, and V8)', 1, function() {