Ensure array sequence methods don't error for falsey values.

This commit is contained in:
John-David Dalton
2016-03-19 22:11:32 -07:00
parent 6ed9c005d7
commit b2bff1ad45
2 changed files with 100 additions and 2 deletions

View File

@@ -15394,18 +15394,26 @@
};
});
// Add `Array` and `String` methods to `lodash.prototype`.
// Add `Array` methods to `lodash.prototype`.
arrayEach(['pop', 'push', 'shift', 'sort', 'splice', 'unshift'], function(methodName) {
var func = arrayProto[methodName],
chainName = /^(?:push|sort|unshift)$/.test(methodName) ? 'tap' : 'thru',
mutates = methodName != 'sort',
retUnwrapped = /^(?:pop|shift)$/.test(methodName);
lodash.prototype[methodName] = function() {
var args = arguments;
if (retUnwrapped && !this.__chain__) {
return func.apply(this.value(), args);
var value = this.value();
if ((mutates ? !isArray(value) : (value == null))) {
value = [];
}
return func.apply(value, args);
}
return this[chainName](function(value) {
if ((mutates ? !isArray(value) : (value == null))) {
value = [];
}
return func.apply(value, args);
});
};