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

@@ -1397,9 +1397,6 @@
}); });
}); });
// remove Lo-Dash specific `lodash.prototype` extensions
source = source.replace(/(?:\s*\/\/.*)*\n( *)forEach\(filter[\s\S]+?lodash\.[\s\S]+?\n\1}.+/, '');
// remove unneeded template related variables // remove unneeded template related variables
source = removeVar(source, 'reComplexDelimiter'); source = removeVar(source, 'reComplexDelimiter');
source = removeVar(source, 'reEmptyStringLeading'); source = removeVar(source, 'reEmptyStringLeading');
@@ -1419,6 +1416,11 @@
// remove `lodash.prototype.toString` and `lodash.prototype.valueOf` assignments // remove `lodash.prototype.toString` and `lodash.prototype.valueOf` assignments
source = source.replace(/ *lodash\.prototype\.(?:toString|valueOf) *=.+\n/g, ''); source = source.replace(/ *lodash\.prototype\.(?:toString|valueOf) *=.+\n/g, '');
// remove `lodash.prototype` batch method assignments
source = source
.replace(/(?:\s*\/\/.*)*\n( *)forEach\(\['first'[\s\S]+?\n\1}.+/, '')
.replace(/(?:\s*\/\/.*)*\n( *)forEach\(filter[\s\S]+?lodash\.[\s\S]+?\n\1}.+/, '');
// remove unused features from `createBound` // remove unused features from `createBound`
if (buildMethods.indexOf('partial') == -1) { if (buildMethods.indexOf('partial') == -1) {
source = source.replace(matchFunction(source, 'createBound'), function(match) { source = source.replace(matchFunction(source, 'createBound'), function(match) {

View File

@@ -4225,11 +4225,23 @@
lodash.prototype.value = wrapperValue; lodash.prototype.value = wrapperValue;
lodash.prototype.valueOf = wrapperValue; lodash.prototype.valueOf = wrapperValue;
// add all methods that return non-wrapped values // add methods that are capable of returning wrapped and unwrapped values
forEach(['first', 'last'], function(methodName) {
var func = lodash[methodName];
if (func) {
lodash.prototype[methodName] = function(n, guard) {
var result = func(this.__wrapped__, n, guard);
return (n == null || guard)
? result
: new lodash(result);
};
}
});
// add all methods that return unwrapped values
forEach(filter(functions(lodash), function(methodName) { forEach(filter(functions(lodash), function(methodName) {
return /^(?:contains|every|find|first|has|is[A-Z].+|last|reduce.*|some)$/.test(methodName); return /^(?:contains|every|find|has|is[A-Z].+|reduce.*|some)$/.test(methodName);
}), }), function(methodName) {
function(methodName) {
var func = lodash[methodName]; var func = lodash[methodName];
lodash.prototype[methodName] = function() { lodash.prototype[methodName] = function() {
@@ -4252,7 +4264,7 @@
if (hasObjectSpliceBug && value.length === 0) { if (hasObjectSpliceBug && value.length === 0) {
delete value[0]; delete value[0];
} }
return new lodash(value); return this;
}; };
}); });

View File

@@ -695,13 +695,13 @@
equal(lodash.some([false, true, false]), true, '_.some: ' + basename); equal(lodash.some([false, true, false]), true, '_.some: ' + basename);
equal(lodash.template('${a}', object), '${a}', '_.template should ignore ES6 delimiters: ' + basename); equal(lodash.template('${a}', object), '${a}', '_.template should ignore ES6 delimiters: ' + basename);
object = lodash(1); var wrapped = lodash(1);
equal(object.clone() instanceof lodash, false, '_(...) wrapped values are not chainable by default: ' + basename); equal(wrapped.clone() instanceof lodash, false, '_(...) wrapped values are not chainable by default: ' + basename);
equal(String(object) === '1', false, '_#toString should not be implemented: ' + basename); equal(String(wrapped) === '1', false, '_#toString should not be implemented: ' + basename);
equal(Number(object) === 1 , false, '_#valueOf should not be implemented: ' + basename); equal(Number(wrapped) === 1 , false, '_#valueOf should not be implemented: ' + basename);
object.chain(); wrapped.chain();
equal(typeof object.has('x') == 'object', true, '_#has returns wrapped values when chaining: ' + basename); equal(wrapped.has('x') instanceof lodash, true, '_#has returns wrapped values when chaining: ' + basename);
start(); 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() { (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 = [ var funcs = [
'contains', 'contains',
@@ -1992,11 +2017,12 @@
]; ];
_.each(funcs, function(methodName) { _.each(funcs, function(methodName) {
test('_.' + methodName + ' should return non-wrapped values', function() { test('_.' + methodName + ' should return unwrapped values', function() {
var func = _[methodName], var result = methodName == 'reduceRight'
result = methodName == 'reduceRight' ? func(array, _.identity) : func; ? wrapped[methodName](_.identity)
: wrapped[methodName];
notEqual(typeof result, 'object', '_.' + methodName + ' returns non-wrapped values'); notEqual(typeof result, 'object', '_.' + methodName + ' returns unwrapped values');
}); });
}); });
}()); }());