Make _(…) wrapper versions of _.first and _.last capable of returning wrapped and unwrapped values.

Former-commit-id: b30704c1ce359213aa09069b290ee55edfb3e33e
This commit is contained in:
John-David Dalton
2012-11-20 07:32:51 -08:00
parent bd4bff3b6b
commit b57fe466ce
4 changed files with 60 additions and 20 deletions

View File

@@ -695,13 +695,13 @@
equal(lodash.some([false, true, false]), true, '_.some: ' + basename);
equal(lodash.template('${a}', object), '${a}', '_.template should ignore ES6 delimiters: ' + basename);
object = lodash(1);
equal(object.clone() instanceof lodash, false, '_(...) wrapped values are not chainable by default: ' + basename);
equal(String(object) === '1', false, '_#toString should not be implemented: ' + basename);
equal(Number(object) === 1 , false, '_#valueOf should not be implemented: ' + basename);
var wrapped = lodash(1);
equal(wrapped.clone() instanceof lodash, false, '_(...) wrapped values are not chainable by default: ' + basename);
equal(String(wrapped) === '1', false, '_#toString should not be implemented: ' + basename);
equal(Number(wrapped) === 1 , false, '_#valueOf should not be implemented: ' + basename);
object.chain();
equal(typeof object.has('x') == 'object', true, '_#has returns wrapped values when chaining: ' + basename);
wrapped.chain();
equal(wrapped.has('x') instanceof lodash, true, '_#has returns wrapped values when chaining: ' + basename);
start();
});

View File

@@ -1957,10 +1957,35 @@
/*--------------------------------------------------------------------------*/
QUnit.module('lodash(...) methods returning non-wrapped values');
QUnit.module('lodash(...) methods capable of returning wrapped and unwrapped values');
(function() {
var array = [1, 2, 3];
var array = [1, 2, 3],
wrapped = _(array);
var funcs = [
'first',
'last'
];
_.each(funcs, function(methodName) {
test('_.' + methodName + ' should return an unwrapped value', function() {
equal(typeof wrapped[methodName](), 'number');
});
test('_.' + methodName + ' should return a wrapped value', function() {
ok(wrapped[methodName](1) instanceof _);
});
});
}());
/*--------------------------------------------------------------------------*/
QUnit.module('lodash(...) methods that return unwrapped values');
(function() {
var array = [1, 2, 3],
wrapped = _(array);
var funcs = [
'contains',
@@ -1992,11 +2017,12 @@
];
_.each(funcs, function(methodName) {
test('_.' + methodName + ' should return non-wrapped values', function() {
var func = _[methodName],
result = methodName == 'reduceRight' ? func(array, _.identity) : func;
test('_.' + methodName + ' should return unwrapped values', function() {
var result = methodName == 'reduceRight'
? wrapped[methodName](_.identity)
: wrapped[methodName];
notEqual(typeof result, 'object', '_.' + methodName + ' returns non-wrapped values');
notEqual(typeof result, 'object', '_.' + methodName + ' returns unwrapped values');
});
});
}());