Use basePullAt in _.remove.

This commit is contained in:
jdalton
2015-03-26 08:46:45 -07:00
parent d2b98323dd
commit 075cae4efb
2 changed files with 38 additions and 37 deletions

View File

@@ -1780,12 +1780,12 @@
}
/**
* The base implementation of `_.at` without support for strings and individual
* key arguments.
* The base implementation of `_.at` without support for string collections
* and individual key arguments.
*
* @private
* @param {Array|Object} collection The collection to iterate over.
* @param {number[]|string[]} [props] The property names or indexes of elements to pick.
* @param {number[]|string[]} props The property names or indexes of elements to pick.
* @returns {Array} Returns the new array of picked elements.
*/
function baseAt(collection, props) {
@@ -2598,6 +2598,27 @@
};
}
/**
* The base implementation of `_.pullAt` without support for individual
* index arguments and capturing the removed elements.
*
* @private
* @param {Array} array The array to modify.
* @param {number[]} indexes The indexes of elements to remove.
* @returns {Array} Returns `array`.
*/
function basePullAt(array, indexes) {
var length = indexes.length;
while (length--) {
var index = parseFloat(indexes[length]);
if (index != previous && isIndex(index)) {
var previous = index;
splice.call(array, index, 1);
}
}
return array;
}
/**
* The base implementation of `_.random` without support for argument juggling
* and returning floating-point numbers.
@@ -5254,17 +5275,8 @@
array || (array = []);
indexes = baseFlatten(indexes);
var length = indexes.length,
result = baseAt(array, indexes);
indexes.sort(baseCompareAscending);
while (length--) {
var index = parseFloat(indexes[length]);
if (index != previous && isIndex(index)) {
var previous = index;
splice.call(array, index, 1);
}
}
var result = baseAt(array, indexes);
basePullAt(array, indexes.sort(baseCompareAscending));
return result;
});
@@ -5308,34 +5320,23 @@
* // => [2, 4]
*/
function remove(array, predicate, thisArg) {
var result = [];
if (!(array && array.length)) {
return result;
}
var index = -1,
length = array ? array.length : 0,
result = [],
removes = 0,
indexes = [],
last;
length = array.length;
predicate = getCallback(predicate, thisArg, 3);
while (++index < length) {
var value = array[index];
if (predicate(value, index, array)) {
if (last && last[0] + last[1] === index) {
++last[1];
} else {
last = [index - removes, 1];
indexes.push(last);
}
result.push(value);
++removes;
indexes.push(index);
}
}
index = -1;
length = indexes.length;
while (++index < length) {
last = indexes[index];
splice.call(array, last[0], last[1]);
}
basePullAt(array, indexes);
return result;
}

View File

@@ -12360,16 +12360,17 @@
test('should provide the correct `predicate` arguments', 1, function() {
var argsList = [],
array = [1, 2, 3, 4];
array = [1, 2, 3],
clone = array.slice();
_.remove(array, function(value, index) {
var args = slice.call(arguments);
args[2] = args[2].slice();
argsList.push(args);
return index % 2;
return index % 2 == 0;
});
deepEqual(argsList, [[1, 0, [1, 2, 3, 4]], [2, 1, [1, 2, 3, 4]], [3, 1, [1, 3, 4]], [4, 1, [1, 4]]]);
deepEqual(argsList, [[1, 0, clone], [2, 1, clone], [3, 2, clone]]);
});
test('should support the `thisArg` argument', 1, function() {
@@ -12420,8 +12421,7 @@
test('should not mutate the array until all elements to remove are determined', 1, function() {
var array = [1, 2, 3];
_.remove(array, function(num, i) { return i % 2 == 0; });
_.remove(array, function(num, index) { return index % 2 == 0; });
deepEqual(array, [2]);
});
}());