Ensure _.first, _.last, and _.sample return the undefined if called with a falsey argument or an empty array if called with a falsey argument and an n value.

This commit is contained in:
John-David Dalton
2013-09-09 08:21:24 -07:00
parent fb61dfd39d
commit 21a45a940e
2 changed files with 60 additions and 38 deletions

View File

@@ -4320,9 +4320,8 @@
* // => [{ 'name': 'apple', 'type': 'fruit' }, { 'name': 'banana', 'type': 'fruit' }]
*/
function first(array, callback, thisArg) {
if (array) {
var n = 0,
length = array.length;
length = array ? array.length : 0;
if (typeof callback != 'number' && callback != null) {
var index = -1;
@@ -4333,12 +4332,11 @@
} else {
n = callback;
if (n == null || thisArg) {
return array[0];
return array ? array[0] : undefined;
}
}
return slice(array, 0, nativeMin(nativeMax(0, n), length));
}
}
/**
* Flattens a nested array (the nesting can be to any depth). If `isShallow`
@@ -4485,11 +4483,8 @@
* // => [{ 'name': 'banana', 'type': 'fruit' }]
*/
function initial(array, callback, thisArg) {
if (!array) {
return [];
}
var n = 0,
length = array.length;
length = array ? array.length : 0;
if (typeof callback != 'number' && callback != null) {
var index = length;
@@ -4618,9 +4613,8 @@
* // => [{ 'name': 'beet', 'type': 'vegetable' }, { 'name': 'carrot', 'type': 'vegetable' }]
*/
function last(array, callback, thisArg) {
if (array) {
var n = 0,
length = array.length;
length = array ? array.length : 0;
if (typeof callback != 'number' && callback != null) {
var index = length;
@@ -4631,12 +4625,11 @@
} else {
n = callback;
if (n == null || thisArg) {
return array[length - 1];
return array ? array[length - 1] : undefined;
}
}
return slice(array, nativeMax(0, length - n));
}
}
/**
* Gets the index at which the last occurrence of `value` is found using strict

View File

@@ -4380,7 +4380,7 @@
];
_.forEach(funcs, function(methodName) {
test('`_.' + methodName + '` should return an unwrapped value', function() {
test('`_(...).' + methodName + '` should return an unwrapped value', function() {
var result = methodName == 'reduceRight'
? wrapped[methodName](_.identity)
: wrapped[methodName]();
@@ -4400,17 +4400,46 @@
var funcs = [
'first',
'last'
'last',
'sample'
];
_.forEach(funcs, function(methodName) {
test('`_.' + methodName + '` should return an unwrapped value', function() {
test('`_(...).' + methodName + '` called without an `n` argument should return an unwrapped value', function() {
equal(typeof wrapped[methodName](), 'number');
});
test('`_.' + methodName + '` should return a wrapped value', function() {
test('`_(...).' + methodName + '` called with an `n` argument should return a wrapped value', function() {
ok(wrapped[methodName](1) instanceof _);
});
test('`_.' + methodName + '` should return `undefined` when querying falsey arguments without an `n` argument', function() {
var actual = [],
expected = _.map(falsey, function() { return undefined; }),
func = _[methodName];
_.forEach(falsey, function(value, index) {
try {
actual.push(index ? func(value) : func());
} catch(e) { console.log(e)}
});
deepEqual(actual, expected);
});
test('`_.' + methodName + '` should return an empty array when querying falsey arguments with an `n` argument', function() {
var actual = [],
expected = _.map(falsey, function() { return []; }),
func = _[methodName];
_.forEach(falsey, function(value, index) {
try {
actual.push(func(value, 2));
} catch(e) { }
});
deepEqual(actual, expected);
});
});
}());