mirror of
https://github.com/whoisclebs/lodash.git
synced 2026-02-10 02:47:50 +00:00
Support iteratee use cases for _.sortByAll and _.sortByOrder.
This commit is contained in:
@@ -2845,19 +2845,21 @@
|
|||||||
* @param {boolean[]} orders The sort orders of `props`.
|
* @param {boolean[]} orders The sort orders of `props`.
|
||||||
* @returns {Array} Returns the new sorted array.
|
* @returns {Array} Returns the new sorted array.
|
||||||
*/
|
*/
|
||||||
function baseSortByOrder(collection, props, orders) {
|
function baseSortByOrder(collection, comparators, orders) {
|
||||||
var index = -1,
|
var callback = getCallback();
|
||||||
length = getLength(collection),
|
|
||||||
result = isLength(length) ? Array(length) : [];
|
|
||||||
|
|
||||||
baseEach(collection, function(value) {
|
comparators = arrayMap(comparators, function(comparitor) {
|
||||||
var length = props.length,
|
return callback(comparitor);
|
||||||
|
});
|
||||||
|
|
||||||
|
var result = baseMap(collection, function(value, index) {
|
||||||
|
var length = comparators.length,
|
||||||
criteria = Array(length);
|
criteria = Array(length);
|
||||||
|
|
||||||
while (length--) {
|
while (length--) {
|
||||||
criteria[length] = value == null ? undefined : value[props[length]];
|
criteria[length] = comparators[length](value);
|
||||||
}
|
}
|
||||||
result[++index] = { 'criteria': criteria, 'index': index, 'value': value };
|
return { 'criteria': criteria, 'index': index, 'value': value };
|
||||||
});
|
});
|
||||||
|
|
||||||
return baseSortBy(result, function(object, other) {
|
return baseSortBy(result, function(object, other) {
|
||||||
@@ -7313,20 +7315,24 @@
|
|||||||
* @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[])} props The property names to sort by,
|
* @param {...(string|string[]|func[])} args The iteratees to sort by,
|
||||||
* specified as individual property names or arrays of property names.
|
* specified by a single, an array or multiple property names and functions.
|
||||||
* @returns {Array} Returns the new sorted array.
|
* @returns {Array} Returns the new sorted array.
|
||||||
* @example
|
* @example
|
||||||
*
|
*
|
||||||
* var users = [
|
* var users = [
|
||||||
* { 'user': 'barney', 'age': 36 },
|
* { 'user': 'barney', 'age': 36, 'years': 1},
|
||||||
* { 'user': 'fred', 'age': 40 },
|
* { 'user': 'fred', 'age': 40, 'years': 5},
|
||||||
* { 'user': 'barney', 'age': 26 },
|
* { 'user': 'barney', 'age': 26, 'years': 0},
|
||||||
* { 'user': 'fred', 'age': 30 }
|
* { 'user': 'fred', 'age': 30, 'years': 3}
|
||||||
* ];
|
* ];
|
||||||
*
|
*
|
||||||
* _.map(_.sortByAll(users, ['user', 'age']), _.values);
|
* _.map(_.sortByAll(users, ['user', 'age']), _.values);
|
||||||
* // => [['barney', 26], ['barney', 36], ['fred', 30], ['fred', 40]]
|
* // => [['barney', 26, 0], ['barney', 40, 6], ['fred', 30, 3], ['fred', 36, 1]]
|
||||||
|
*
|
||||||
|
* function a(customer) {return customer.age - customer.years}
|
||||||
|
* _.sortByAll(users, a, 'age')
|
||||||
|
* // => [['barney', 26, 0], ['fred', 30, 3], ['barney', 40, 6], ['fred', 36, 1]]
|
||||||
*/
|
*/
|
||||||
var sortByAll = restParam(function(collection, args) {
|
var sortByAll = restParam(function(collection, args) {
|
||||||
if (collection == null) {
|
if (collection == null) {
|
||||||
@@ -7349,7 +7355,7 @@
|
|||||||
* @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[]} props The property names to sort by.
|
* @param {string[]|func[]} comparators The iteratees to sort by.
|
||||||
* @param {boolean[]} orders The sort orders of `props`.
|
* @param {boolean[]} orders The sort orders of `props`.
|
||||||
* @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.
|
||||||
@@ -7366,20 +7372,20 @@
|
|||||||
* _.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', 26], ['fred', 40], ['fred', 30]]
|
||||||
*/
|
*/
|
||||||
function sortByOrder(collection, props, orders, guard) {
|
function sortByOrder(collection, comparators, orders, guard) {
|
||||||
if (collection == null) {
|
if (collection == null) {
|
||||||
return [];
|
return [];
|
||||||
}
|
}
|
||||||
if (guard && isIterateeCall(props, orders, guard)) {
|
if (guard && isIterateeCall(comparators, orders, guard)) {
|
||||||
orders = null;
|
orders = null;
|
||||||
}
|
}
|
||||||
if (!isArray(props)) {
|
if (!isArray(comparators)) {
|
||||||
props = props == null ? [] : [props];
|
comparators = comparators == null ? [] : [comparators];
|
||||||
}
|
}
|
||||||
if (!isArray(orders)) {
|
if (!isArray(orders)) {
|
||||||
orders = orders == null ? [] : [orders];
|
orders = orders == null ? [] : [orders];
|
||||||
}
|
}
|
||||||
return baseSortByOrder(collection, props, orders);
|
return baseSortByOrder(collection, comparators, orders);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
16
test/test.js
16
test/test.js
@@ -14185,10 +14185,10 @@
|
|||||||
}
|
}
|
||||||
|
|
||||||
var objects = [
|
var objects = [
|
||||||
{ 'a': 'x', 'b': 3 },
|
{ 'a': 'x', 'b': 3, c: 2 },
|
||||||
{ 'a': 'y', 'b': 4 },
|
{ 'a': 'y', 'b': 4, c: 1 },
|
||||||
{ 'a': 'x', 'b': 1 },
|
{ 'a': 'x', 'b': 1, c: 4},
|
||||||
{ 'a': 'y', 'b': 2 }
|
{ 'a': 'y', 'b': 2, c: 3}
|
||||||
];
|
];
|
||||||
|
|
||||||
var stableOrder = [
|
var stableOrder = [
|
||||||
@@ -14209,6 +14209,14 @@
|
|||||||
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() {
|
||||||
|
function b(obj) {
|
||||||
|
return obj.b * obj.c;
|
||||||
|
}
|
||||||
|
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() {
|
||||||
var actual = func(stableOrder, ['a', 'c']);
|
var actual = func(stableOrder, ['a', 'c']);
|
||||||
deepEqual(actual, stableOrder);
|
deepEqual(actual, stableOrder);
|
||||||
|
|||||||
Reference in New Issue
Block a user