mirror of
https://github.com/whoisclebs/lodash.git
synced 2026-02-07 18:07:49 +00:00
Make _.head avoid accessing array when its length is 0.
This commit is contained in:
@@ -6415,7 +6415,7 @@
|
|||||||
* // => undefined
|
* // => undefined
|
||||||
*/
|
*/
|
||||||
function head(array) {
|
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) {
|
QUnit.test('should return `undefined` when querying empty arrays', function(assert) {
|
||||||
assert.expect(1);
|
assert.expect(1);
|
||||||
|
|
||||||
var array = [];
|
arrayProto[0] = 1;
|
||||||
array['-1'] = 1;
|
assert.strictEqual(_.head([]), undefined);
|
||||||
|
arrayProto.length = 0;
|
||||||
assert.strictEqual(_.head(array), undefined);
|
|
||||||
});
|
});
|
||||||
|
|
||||||
QUnit.test('should work as an iteratee for methods like `_.map`', function(assert) {
|
QUnit.test('should work as an iteratee for methods like `_.map`', function(assert) {
|
||||||
|
|||||||
Reference in New Issue
Block a user