Add _.pullAll.

This commit is contained in:
John-David Dalton
2015-09-27 16:33:10 -07:00
parent 38ac518095
commit 9c79cb7766
2 changed files with 57 additions and 20 deletions

View File

@@ -1348,6 +1348,7 @@
parseFloat = context.parseFloat, parseFloat = context.parseFloat,
pow = Math.pow, pow = Math.pow,
propertyIsEnumerable = objectProto.propertyIsEnumerable, propertyIsEnumerable = objectProto.propertyIsEnumerable,
push = arrayProto.push,
setTimeout = context.setTimeout, setTimeout = context.setTimeout,
splice = arrayProto.splice; splice = arrayProto.splice;
@@ -1430,11 +1431,11 @@
* `memoize`, `merge`, `mergeWith` `method`, `methodOf`, `mixin`, `modArgs`, * `memoize`, `merge`, `mergeWith` `method`, `methodOf`, `mixin`, `modArgs`,
* `modArgsSet', 'negate`, `omit`, `omitBy`, `once`, `pairs`, `partial`, * `modArgsSet', 'negate`, `omit`, `omitBy`, `once`, `pairs`, `partial`,
* `partialRight`, `partition`, `pick`, `pickBy`, `plant`, `property`, * `partialRight`, `partition`, `pick`, `pickBy`, `plant`, `property`,
* `propertyOf`, `pull`, `pullAt`, `push`, `range`, `rearg`, `reject`, * `propertyOf`, `pull`, `pullAll`, `pullAt`, `push`, `range`, `rearg`,
* `remove`, `rest`, `restParam`, `reverse`, `set`, `setWith`, `shuffle`, * `reject`, `remove`, `rest`, `restParam`, `reverse`, `set`, `setWith`,
* `slice`, `sort`, `sortBy`, `sortByOrder`, `splice`, `spread`, `take`, * `shuffle`, `slice`, `sort`, `sortBy`, `sortByOrder`, `splice`, `spread`,
* `takeRight`, `takeRightWhile`, `takeWhile`, `tap`, `throttle`, `thru`, * `take`, `takeRight`, `takeRightWhile`, `takeWhile`, `tap`, `throttle`,
* `times`, `toArray`, `toPath`, `toPlainObject`, `transform`, `union`, * `thru`, `times`, `toArray`, `toPath`, `toPlainObject`, `transform`, `union`,
* `uniq`, `uniqBy`, `unset`, `unshift`, `unzip`, `unzipWith`, `values`, * `uniq`, `uniqBy`, `unset`, `unshift`, `unzip`, `unzipWith`, `values`,
* `valuesIn`, `without`, `wrap`, `xor`, `zip`, `zipObject`, and `zipWith` * `valuesIn`, `without`, `wrap`, `xor`, `zip`, `zipObject`, and `zipWith`
* *
@@ -5156,6 +5157,31 @@
return array; return array;
} }
/**
* This method is like `_.pull` except that it accepts an array of values to remove.
*
* **Note:** Unlike `_.difference`, this method mutates `array`.
*
* @static
* @memberOf _
* @category Array
* @param {Array} array The array to modify.
* @param {Array} values The values to remove.
* @returns {Array} Returns `array`.
* @example
*
* var array = [1, 2, 3, 1, 2, 3];
*
* _.pull(array, [2, 3]);
* console.log(array);
* // => [1, 1]
*/
function pullAll(array, values) {
var args = [array];
push.apply(args, values);
return pull.apply(undefined, args);
}
/** /**
* Removes elements from `array` corresponding to `indexes` and returns an * Removes elements from `array` corresponding to `indexes` and returns an
* array of removed elements. * array of removed elements.
@@ -12184,6 +12210,7 @@
lodash.property = property; lodash.property = property;
lodash.propertyOf = propertyOf; lodash.propertyOf = propertyOf;
lodash.pull = pull; lodash.pull = pull;
lodash.pullAll = pullAll;
lodash.pullAt = pullAt; lodash.pullAt = pullAt;
lodash.range = range; lodash.range = range;
lodash.rearg = rearg; lodash.rearg = rearg;

View File

@@ -14507,50 +14507,59 @@
/*--------------------------------------------------------------------------*/ /*--------------------------------------------------------------------------*/
QUnit.module('lodash.pull'); QUnit.module('pull methods');
(function() { _.each(['pull', 'pullAll'], function(methodName) {
QUnit.test('should modify and return the array', function(assert) { var func = _[methodName],
isPull = methodName == 'pull';
function pull(array, values) {
return isPull
? func.apply(undefined, [array].concat(values))
: func(array, values);
}
QUnit.test('`_.' + methodName + '` should modify and return the array', function(assert) {
assert.expect(2); assert.expect(2);
var array = [1, 2, 3], var array = [1, 2, 3],
actual = _.pull(array, 1, 3); actual = pull(array, [1, 3]);
assert.deepEqual(array, [2]); assert.deepEqual(array, [2]);
assert.ok(actual === array); assert.ok(actual === array);
}); });
QUnit.test('should preserve holes in arrays', function(assert) { QUnit.test('`_.' + methodName + '` should preserve holes in arrays', function(assert) {
assert.expect(2); assert.expect(2);
var array = [1, 2, 3, 4]; var array = [1, 2, 3, 4];
delete array[1]; delete array[1];
delete array[3]; delete array[3];
_.pull(array, 1); pull(array, [1]);
assert.notOk('0' in array); assert.notOk('0' in array);
assert.notOk('2' in array); assert.notOk('2' in array);
}); });
QUnit.test('should treat holes as `undefined`', function(assert) { QUnit.test('`_.' + methodName + '` should treat holes as `undefined`', function(assert) {
assert.expect(1); assert.expect(1);
var array = [1, 2, 3]; var array = [1, 2, 3];
delete array[1]; delete array[1];
_.pull(array, undefined); pull(array, [undefined]);
assert.deepEqual(array, [1, 3]); assert.deepEqual(array, [1, 3]);
}); });
QUnit.test('should match `NaN`', function(assert) { QUnit.test('`_.' + methodName + '` should match `NaN`', function(assert) {
assert.expect(1); assert.expect(1);
var array = [1, NaN, 3, NaN]; var array = [1, NaN, 3, NaN];
_.pull(array, NaN); pull(array, [NaN]);
assert.deepEqual(array, [1, 3]); assert.deepEqual(array, [1, 3]);
}); });
}()); });
/*--------------------------------------------------------------------------*/ /*--------------------------------------------------------------------------*/
@@ -21009,6 +21018,7 @@
'map', 'map',
'pairs', 'pairs',
'pull', 'pull',
'pullAll',
'pullAt', 'pullAt',
'range', 'range',
'reject', 'reject',
@@ -21032,7 +21042,7 @@
var acceptFalsey = _.difference(allMethods, rejectFalsey); var acceptFalsey = _.difference(allMethods, rejectFalsey);
QUnit.test('should accept falsey arguments', function(assert) { QUnit.test('should accept falsey arguments', function(assert) {
assert.expect(241); assert.expect(243);
var emptyArrays = _.map(falsey, _.constant([])); var emptyArrays = _.map(falsey, _.constant([]));
@@ -21052,7 +21062,7 @@
if (methodName == 'noConflict') { if (methodName == 'noConflict') {
root._ = oldDash; root._ = oldDash;
} }
else if (methodName == 'pull') { else if (methodName == 'pull' || methodName == 'pullAll') {
expected = falsey; expected = falsey;
} }
if (_.includes(returnArrays, methodName) && methodName != 'sample') { if (_.includes(returnArrays, methodName) && methodName != 'sample') {
@@ -21070,7 +21080,7 @@
}); });
QUnit.test('should return an array', function(assert) { QUnit.test('should return an array', function(assert) {
assert.expect(66); assert.expect(68);
var array = [1, 2, 3]; var array = [1, 2, 3];
@@ -21090,7 +21100,7 @@
} }
assert.ok(_.isArray(actual), '_.' + methodName + ' returns an array'); assert.ok(_.isArray(actual), '_.' + methodName + ' returns an array');
var isPull = methodName == 'pull'; var isPull = methodName == 'pull' || methodName == 'pullAll';
assert.strictEqual(actual === array, isPull, '_.' + methodName + ' should ' + (isPull ? '' : 'not ') + 'return the provided array'); assert.strictEqual(actual === array, isPull, '_.' + methodName + ' should ' + (isPull ? '' : 'not ') + 'return the provided array');
}); });
}); });