mirror of
https://github.com/whoisclebs/lodash.git
synced 2026-02-01 07:47:49 +00:00
Make _.xyzBy methods provide just a single param to their callbacks.
This commit is contained in:
31
lodash.js
31
lodash.js
@@ -2339,8 +2339,8 @@
|
||||
*/
|
||||
function basePickBy(object, predicate) {
|
||||
var result = {};
|
||||
baseForIn(object, function(value, key, object) {
|
||||
if (predicate(value, key, object)) {
|
||||
baseForIn(object, function(value, key) {
|
||||
if (predicate(value)) {
|
||||
result[key] = value;
|
||||
}
|
||||
});
|
||||
@@ -2576,12 +2576,12 @@
|
||||
index = -1;
|
||||
|
||||
iteratees = arrayMap(iteratees.length ? iteratees : Array(1), function(iteratee) {
|
||||
return toIteratee(iteratee, 3);
|
||||
return toIteratee(iteratee, 1);
|
||||
});
|
||||
|
||||
var result = baseMap(collection, function(value, key, collection) {
|
||||
var criteria = arrayMap(iteratees, function(iteratee) {
|
||||
return iteratee(value, key, collection);
|
||||
return iteratee(value);
|
||||
});
|
||||
return { 'criteria': criteria, 'index': ++index, 'value': value };
|
||||
});
|
||||
@@ -3029,11 +3029,11 @@
|
||||
|
||||
while (++index < length) {
|
||||
var value = collection[index];
|
||||
setter(result, value, iteratee(value, index, collection), collection);
|
||||
setter(result, value, iteratee(value), collection);
|
||||
}
|
||||
} else {
|
||||
baseEach(collection, function(value, key, collection) {
|
||||
setter(result, value, iteratee(value, key, collection), collection);
|
||||
setter(result, value, iteratee(value), collection);
|
||||
});
|
||||
}
|
||||
return result;
|
||||
@@ -5892,7 +5892,7 @@
|
||||
* Creates an object composed of keys generated from the results of running
|
||||
* each element of `collection` through `iteratee`. The corresponding value
|
||||
* of each key is the number of times the key was returned by `iteratee`.
|
||||
* The iteratee is invoked with three arguments: (value, index|key, collection).
|
||||
* The iteratee is invoked with one argument: (value).
|
||||
*
|
||||
* @static
|
||||
* @memberOf _
|
||||
@@ -6128,7 +6128,7 @@
|
||||
* Creates an object composed of keys generated from the results of running
|
||||
* each element of `collection` through `iteratee`. The corresponding value
|
||||
* of each key is an array of the elements responsible for generating the key.
|
||||
* The iteratee is invoked with three arguments: (value, index|key, collection).
|
||||
* The iteratee is invoked with one argument: (value).
|
||||
*
|
||||
* @static
|
||||
* @memberOf _
|
||||
@@ -6201,7 +6201,7 @@
|
||||
* Creates an object composed of keys generated from the results of running
|
||||
* each element of `collection` through `iteratee`. The corresponding value
|
||||
* of each key is the last element responsible for generating the key. The
|
||||
* iteratee is invoked with three arguments: (value, index|key, collection).
|
||||
* iteratee is invoked with one argument: (value).
|
||||
*
|
||||
* @static
|
||||
* @memberOf _
|
||||
@@ -6596,8 +6596,7 @@
|
||||
* Creates an array of elements, sorted in ascending order by the results of
|
||||
* 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 iteratees are invoked with three arguments:
|
||||
* (value, index|key, collection).
|
||||
* equal elements. The iteratees are invoked with one argument: (value).
|
||||
*
|
||||
* @static
|
||||
* @memberOf _
|
||||
@@ -9425,9 +9424,9 @@
|
||||
* // => { 'user': 'fred' }
|
||||
*/
|
||||
function omitBy(object, predicate) {
|
||||
predicate = getIteratee(predicate, 3);
|
||||
return basePickBy(object, function(value, key, object) {
|
||||
return !predicate(value, key, object);
|
||||
predicate = getIteratee(predicate);
|
||||
return basePickBy(object, function(value) {
|
||||
return !predicate(value);
|
||||
});
|
||||
}
|
||||
|
||||
@@ -9475,7 +9474,7 @@
|
||||
|
||||
/**
|
||||
* Creates an object composed of the `object` properties `predicate` returns
|
||||
* truthy for. The predicate is invoked with three arguments: (value, key, object).
|
||||
* truthy for. The predicate is invoked with one argument: (value).
|
||||
*
|
||||
* @static
|
||||
* @memberOf _
|
||||
@@ -9491,7 +9490,7 @@
|
||||
* // => { 'user': 'fred' }
|
||||
*/
|
||||
function pickBy(object, predicate) {
|
||||
return object == null ? {} : basePickBy(object, getIteratee(predicate, 3));
|
||||
return object == null ? {} : basePickBy(object, getIteratee(predicate));
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
66
test/test.js
66
test/test.js
@@ -4747,7 +4747,7 @@
|
||||
_.each(methods, function(methodName) {
|
||||
var array = [1, 2, 3],
|
||||
func = _[methodName],
|
||||
isExtremum = /^(?:max|min)/.test(methodName),
|
||||
isBy = /(^partition|By)$/.test(methodName),
|
||||
isFind = /^find/.test(methodName),
|
||||
isSome = methodName == 'some';
|
||||
|
||||
@@ -4767,7 +4767,7 @@
|
||||
if (_.includes(objectMethods, methodName)) {
|
||||
expected[1] += '';
|
||||
}
|
||||
if (isExtremum) {
|
||||
if (isBy) {
|
||||
expected.length = 1;
|
||||
}
|
||||
deepEqual(args, expected);
|
||||
@@ -4784,12 +4784,12 @@
|
||||
|
||||
var expected = [[1, 0, array], [undefined, 1, array], [3, 2, array]];
|
||||
|
||||
if (isExtremum) {
|
||||
if (isBy) {
|
||||
expected = _.map(expected, function(args) {
|
||||
return args.slice(0, 1);
|
||||
});
|
||||
}
|
||||
if (_.includes(objectMethods, methodName)) {
|
||||
else if (_.includes(objectMethods, methodName)) {
|
||||
expected = _.map(expected, function(args) {
|
||||
args[1] += '';
|
||||
return args;
|
||||
@@ -4888,9 +4888,9 @@
|
||||
Foo.prototype.b = 2;
|
||||
|
||||
if (func) {
|
||||
var keys = [];
|
||||
func(new Foo, function(value, key) { keys.push(key); });
|
||||
deepEqual(keys, ['a']);
|
||||
var values = [];
|
||||
func(new Foo, function(value) { values.push(value); });
|
||||
deepEqual(values, [1]);
|
||||
}
|
||||
else {
|
||||
skipTest();
|
||||
@@ -11204,29 +11204,33 @@
|
||||
_.each(['omit', 'omitBy'], function(methodName) {
|
||||
var expected = { 'b': 2, 'd': 4 },
|
||||
func = _[methodName],
|
||||
object = { 'a': 1, 'b': 2, 'c': 3, 'd': 4 };
|
||||
object = { 'a': 1, 'b': 2, 'c': 3, 'd': 4 },
|
||||
prop = function(object, props) { return props; };
|
||||
|
||||
var prop = methodName == 'omit' ? _.identity : function(props) {
|
||||
props = typeof props == 'string' ? [props] : props;
|
||||
return function(value, key) {
|
||||
return _.includes(props, key);
|
||||
if (methodName == 'omitBy') {
|
||||
prop = function(object, props) {
|
||||
props = typeof props == 'string' ? [props] : props;
|
||||
return function(value) {
|
||||
return _.some(props, function(key) { return object[key] === value; });
|
||||
};
|
||||
};
|
||||
};
|
||||
|
||||
}
|
||||
test('`_.' + methodName + '` should create an object with omitted properties', 2, function() {
|
||||
deepEqual(func(object, prop('a')), { 'b': 2, 'c': 3, 'd': 4 });
|
||||
deepEqual(func(object, prop(['a', 'c'])), expected);
|
||||
deepEqual(func(object, prop(object, 'a')), { 'b': 2, 'c': 3, 'd': 4 });
|
||||
deepEqual(func(object, prop(object, ['a', 'c'])), expected);
|
||||
});
|
||||
|
||||
test('`_.' + methodName + '` should iterate over inherited properties', 1, function() {
|
||||
function Foo() {}
|
||||
Foo.prototype = object;
|
||||
|
||||
deepEqual(func(new Foo, prop(['a', 'c'])), expected);
|
||||
var foo = new Foo;
|
||||
deepEqual(func(foo, prop(object, ['a', 'c'])), expected);
|
||||
});
|
||||
|
||||
test('`_.' + methodName + '` should work with an array `object` argument', 1, function() {
|
||||
deepEqual(func([1, 2, 3], prop(['0', '2'])), { '1': 2 });
|
||||
var array = [1, 2, 3];
|
||||
deepEqual(func(array, prop(array, ['0', '2'])), { '1': 2 });
|
||||
});
|
||||
});
|
||||
|
||||
@@ -11921,29 +11925,33 @@
|
||||
_.each(['pick', 'pickBy'], function(methodName) {
|
||||
var expected = { 'a': 1, 'c': 3 },
|
||||
func = _[methodName],
|
||||
object = { 'a': 1, 'b': 2, 'c': 3, 'd': 4 };
|
||||
object = { 'a': 1, 'b': 2, 'c': 3, 'd': 4 },
|
||||
prop = function(object, props) { return props; };
|
||||
|
||||
var prop = methodName == 'pick' ? _.identity : function(props) {
|
||||
props = typeof props == 'string' ? [props] : props;
|
||||
return function(value, key) {
|
||||
return _.includes(props, key);
|
||||
if (methodName == 'pickBy') {
|
||||
prop = function(object, props) {
|
||||
props = typeof props == 'string' ? [props] : props;
|
||||
return function(value) {
|
||||
return _.some(props, function(key) { return object[key] === value; });
|
||||
};
|
||||
};
|
||||
};
|
||||
|
||||
}
|
||||
test('`_.' + methodName + '` should create an object of picked properties', 2, function() {
|
||||
deepEqual(func(object, prop('a')), { 'a': 1 });
|
||||
deepEqual(func(object, prop(['a', 'c'])), expected);
|
||||
deepEqual(func(object, prop(object, 'a')), { 'a': 1 });
|
||||
deepEqual(func(object, prop(object, ['a', 'c'])), expected);
|
||||
});
|
||||
|
||||
test('`_.' + methodName + '` should iterate over inherited properties', 1, function() {
|
||||
function Foo() {}
|
||||
Foo.prototype = object;
|
||||
|
||||
deepEqual(func(new Foo, prop(['a', 'c'])), expected);
|
||||
var foo = new Foo;
|
||||
deepEqual(func(foo, prop(foo, ['a', 'c'])), expected);
|
||||
});
|
||||
|
||||
test('`_.' + methodName + '` should work with an array `object` argument', 1, function() {
|
||||
deepEqual(func([1, 2, 3], prop('1')), { '1': 2 });
|
||||
var array = [1, 2, 3];
|
||||
deepEqual(func(array, prop(array, '1')), { '1': 2 });
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
Reference in New Issue
Block a user