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

@@ -4225,11 +4225,23 @@
lodash.prototype.value = 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) {
return /^(?:contains|every|find|first|has|is[A-Z].+|last|reduce.*|some)$/.test(methodName);
}),
function(methodName) {
return /^(?:contains|every|find|has|is[A-Z].+|reduce.*|some)$/.test(methodName);
}), function(methodName) {
var func = lodash[methodName];
lodash.prototype[methodName] = function() {
@@ -4252,7 +4264,7 @@
if (hasObjectSpliceBug && value.length === 0) {
delete value[0];
}
return new lodash(value);
return this;
};
});