mirror of
https://github.com/whoisclebs/lodash.git
synced 2026-02-02 16:17:50 +00:00
Allow null or undefined elements in _.pluck, _.property, and _.invoke.
This commit is contained in:
@@ -4023,7 +4023,8 @@
|
||||
result = Array(length < 0 ? 0 : length >>> 0);
|
||||
|
||||
baseEach(collection, function(value) {
|
||||
result[++index] = (isFunc ? methodName : value[methodName]).apply(value, args);
|
||||
var func = isFunc ? methodName : (value != null && value[methodName]);
|
||||
result[++index] = func ? func.apply(value, args) : undefined;
|
||||
});
|
||||
return result;
|
||||
}
|
||||
@@ -7904,7 +7905,7 @@
|
||||
*/
|
||||
function property(key) {
|
||||
return function(object) {
|
||||
return object[key];
|
||||
return object == null ? undefined : object[key];
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
14
test/test.js
14
test/test.js
@@ -3636,8 +3636,8 @@
|
||||
|
||||
(function() {
|
||||
test('should invoke a methods on each element of a collection', 1, function() {
|
||||
var actual = _.invoke(['a', 'b', 'c'], 'toUpperCase');
|
||||
deepEqual(actual, ['A', 'B', 'C']);
|
||||
var array = ['a', 'b', 'c'];
|
||||
deepEqual( _.invoke(array, 'toUpperCase'), ['A', 'B', 'C']);
|
||||
});
|
||||
|
||||
test('should work with a function `methodName` argument', 1, function() {
|
||||
@@ -3652,6 +3652,11 @@
|
||||
var object = { 'a': 1, 'b': 2, 'c': 3 };
|
||||
deepEqual(_.invoke(object, 'toFixed', 1), ['1.0', '2.0', '3.0']);
|
||||
});
|
||||
|
||||
test('should work with `null` or `undefined` elements', 1, function() {
|
||||
var array = ['a', null, undefined, 'd'];
|
||||
deepEqual(_.invoke(array, 'toUpperCase'), ['A', undefined, undefined, 'D']);
|
||||
});
|
||||
}());
|
||||
|
||||
/*--------------------------------------------------------------------------*/
|
||||
@@ -6480,6 +6485,11 @@
|
||||
var object = { 'a': [1], 'b': [1, 2], 'c': [1, 2, 3] };
|
||||
deepEqual(_.pluck(object, 'length'), [1, 2, 3]);
|
||||
});
|
||||
|
||||
test('should work with `null` or `undefined` elements', 1, function() {
|
||||
var objects = [{ 'a': 1 }, null, undefined, { 'a': 4 }];
|
||||
deepEqual(_.pluck(objects, 'a'), [1, undefined, undefined, 4]);
|
||||
});
|
||||
}());
|
||||
|
||||
/*--------------------------------------------------------------------------*/
|
||||
|
||||
Reference in New Issue
Block a user