Skip to content

Commit 80d56b9

Browse files
author
Дмитрий
committed
fixed return undefined in first and last with guard
1 parent d5fe0fd commit 80d56b9

File tree

2 files changed

+6
-7
lines changed

2 files changed

+6
-7
lines changed

test/arrays.js

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@
1212
assert.deepEqual(_.first([1, 2, 3], 5), [1, 2, 3], 'returns the whole array if n > length');
1313
var result = (function(){ return _.first(arguments); }(4, 3, 2, 1));
1414
assert.strictEqual(result, 4, 'works on an arguments object');
15-
result = _.map([[1, 2, 3], [1, 2, 3]], _.first);
16-
assert.deepEqual(result, [1, 1], 'works well with _.map');
15+
result = _.map([[1, 2, 3], [], [1, 2, 3]], _.first);
16+
assert.deepEqual(result, [1, void 0, 1], 'works well with _.map');
1717
assert.strictEqual(_.first(null), void 0, 'returns undefined when called on null');
1818
assert.deepEqual(_.first([], 10), [], 'returns an empty array when called with an explicit number of elements to return');
1919
assert.deepEqual(_.first([], 1), [], 'returns an empty array when called with an explicit number of elements to return');
@@ -70,10 +70,9 @@
7070
assert.deepEqual(_.last([1, 2, 3], 5), [1, 2, 3], 'returns the whole array if n > length');
7171
var result = (function(){ return _(arguments).last(); }(1, 2, 3, 4));
7272
assert.strictEqual(result, 4, 'works on an arguments object');
73-
result = _.map([[1, 2, 3], [1, 2, 3]], _.last);
74-
assert.deepEqual(result, [3, 3], 'works well with _.map');
73+
result = _.map([[1, 2, 3], [], [1, 2, 3]], _.last);
74+
assert.deepEqual(result, [3, void 0, 3], 'works well with _.map');
7575
assert.strictEqual(_.last(null), void 0, 'returns undefined when called on null');
76-
7776
assert.deepEqual(_.last([], 10), [], 'returns an empty array when called with an explicit number of elements to return');
7877
assert.deepEqual(_.last([], 1), [], 'returns an empty array when called with an explicit number of elements to return');
7978
assert.deepEqual(_.last(null, 5), [], 'returns an empty array when called with an explicit number of elements to return');

underscore.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -497,7 +497,7 @@
497497
// values in the array. Aliased as `head` and `take`. The **guard** check
498498
// allows it to work with `_.map`.
499499
_.first = _.head = _.take = function(array, n, guard) {
500-
if (array == null || array.length < 1) return n == null ? void 0 : [];
500+
if (array == null || array.length < 1) return n == null || guard ? void 0 : [];
501501
if (n == null || guard) return array[0];
502502
return _.initial(array, array.length - n);
503503
};
@@ -512,7 +512,7 @@
512512
// Get the last element of an array. Passing **n** will return the last N
513513
// values in the array.
514514
_.last = function(array, n, guard) {
515-
if (array == null || array.length < 1) return n == null ? void 0 : [];
515+
if (array == null || array.length < 1) return n == null || guard ? void 0 : [];
516516
if (n == null || guard) return array[array.length - 1];
517517
return _.rest(array, Math.max(0, array.length - n));
518518
};

0 commit comments

Comments
 (0)