mirror of
https://github.com/whoisclebs/lodash.git
synced 2026-01-31 15:27:50 +00:00
Make _.head avoid accessing array when its length is 0.
This commit is contained in:
@@ -6415,7 +6415,7 @@
|
||||
* // => undefined
|
||||
*/
|
||||
function head(array) {
|
||||
return array ? array[0] : undefined;
|
||||
return (array && array.length) ? array[0] : undefined;
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -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) {
|
||||
|
||||
Reference in New Issue
Block a user