mirror of
https://github.com/whoisclebs/lodash.git
synced 2026-02-03 08:37:49 +00:00
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:
37
test/test.js
37
test/test.js
@@ -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);
|
||||
});
|
||||
});
|
||||
}());
|
||||
|
||||
|
||||
Reference in New Issue
Block a user