diff --git a/lodash.js b/lodash.js index 149234fe0..2d9e93281 100644 --- a/lodash.js +++ b/lodash.js @@ -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]; }; } diff --git a/test/test.js b/test/test.js index a442e67bb..005112327 100644 --- a/test/test.js +++ b/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]); + }); }()); /*--------------------------------------------------------------------------*/