Ensure baseAt, basePullAt, and pullAt handle nullish values correctly.

This commit is contained in:
jdalton
2015-04-28 21:04:45 -07:00
parent 2c6d880542
commit ce6ccef2d0
2 changed files with 11 additions and 11 deletions

View File

@@ -1829,8 +1829,9 @@
*/ */
function baseAt(collection, props) { function baseAt(collection, props) {
var index = -1, var index = -1,
isArr = isArrayLike(collection), isNil = collection == null,
length = collection.length, isArr = !isNil && isArrayLike(collection),
length = isArr && collection.length,
propsLength = props.length, propsLength = props.length,
result = Array(propsLength); result = Array(propsLength);
@@ -1839,7 +1840,7 @@
if (isArr) { if (isArr) {
result[index] = isIndex(key, length) ? collection[key] : undefined; result[index] = isIndex(key, length) ? collection[key] : undefined;
} else { } else {
result[index] = collection[key]; result[index] = isNil ? undefined : collection[key];
} }
} }
return result; return result;
@@ -2711,7 +2712,7 @@
* @returns {Array} Returns `array`. * @returns {Array} Returns `array`.
*/ */
function basePullAt(array, indexes) { function basePullAt(array, indexes) {
var length = indexes.length; var length = array ? indexes.length : 0;
while (length--) { while (length--) {
var index = parseFloat(indexes[length]); var index = parseFloat(indexes[length]);
if (index != previous && isIndex(index)) { if (index != previous && isIndex(index)) {
@@ -5476,7 +5477,6 @@
* // => [10, 20] * // => [10, 20]
*/ */
var pullAt = restParam(function(array, indexes) { var pullAt = restParam(function(array, indexes) {
array || (array = []);
indexes = baseFlatten(indexes); indexes = baseFlatten(indexes);
var result = baseAt(array, indexes); var result = baseAt(array, indexes);

View File

@@ -1219,11 +1219,11 @@
}); });
test('should work with a falsey `collection` argument when keys are provided', 1, function() { test('should work with a falsey `collection` argument when keys are provided', 1, function() {
var expected = _.map(falsey, _.constant([undefined, undefined])); var expected = _.map(falsey, _.constant(Array(4)));
var actual = _.map(falsey, function(value) { var actual = _.map(falsey, function(value) {
try { try {
return _.at(value, 0, 1); return _.at(value, 0, 1, 'pop', 'push');
} catch(e) {} } catch(e) {}
}); });
@@ -4456,7 +4456,7 @@
var array = [1, 2, 3], var array = [1, 2, 3],
actual = _.fill(array); actual = _.fill(array);
deepEqual(actual, [undefined, undefined, undefined]); deepEqual(actual, Array(3));
ok(_.every(actual, function(value, index) { return index in actual; })); ok(_.every(actual, function(value, index) { return index in actual; }));
}); });
@@ -12467,7 +12467,7 @@
var values = _.reject(empties, function(value) { var values = _.reject(empties, function(value) {
return value === 0 || _.isArray(value); return value === 0 || _.isArray(value);
}).concat(-1, 1.1); }).concat(-1, 1.1, 'pop', 'push');
var expected = _.map(values, _.constant(undefined)), var expected = _.map(values, _.constant(undefined)),
actual = _.pullAt(array, values); actual = _.pullAt(array, values);
@@ -12506,11 +12506,11 @@
}); });
test('should work with a falsey `array` argument when keys are provided', 1, function() { test('should work with a falsey `array` argument when keys are provided', 1, function() {
var expected = _.map(falsey, _.constant([undefined, undefined])); var expected = _.map(falsey, _.constant(Array(4)));
var actual = _.map(falsey, function(value) { var actual = _.map(falsey, function(value) {
try { try {
return _.pullAt(value, 0, 1); return _.pullAt(value, 0, 1, 'pop', 'push');
} catch(e) {} } catch(e) {}
}); });