Make _.head avoid accessing array when its length is 0.

This commit is contained in:
John-David Dalton
2016-04-11 17:30:15 -07:00
parent 93168e6018
commit 091b5fbe30
2 changed files with 4 additions and 5 deletions

View File

@@ -6415,7 +6415,7 @@
* // => undefined
*/
function head(array) {
return array ? array[0] : undefined;
return (array && array.length) ? array[0] : undefined;
}
/**

View File

@@ -7386,10 +7386,9 @@
QUnit.test('should return `undefined` when querying empty arrays', function(assert) {
assert.expect(1);
var array = [];
array['-1'] = 1;
assert.strictEqual(_.head(array), undefined);
arrayProto[0] = 1;
assert.strictEqual(_.head([]), undefined);
arrayProto.length = 0;
});
QUnit.test('should work as an iteratee for methods like `_.map`', function(assert) {