mirror of
https://github.com/whoisclebs/lodash.git
synced 2026-02-07 18:07:49 +00:00
Move _.remove to the "Arrays" category and add unit tests.
Former-commit-id: b0542496b45582b8ca59de19e950dc2368deee0a
This commit is contained in:
10
build.js
10
build.js
@@ -194,7 +194,7 @@
|
|||||||
'reduce': ['baseCreateCallback', 'baseEach', 'isArray'],
|
'reduce': ['baseCreateCallback', 'baseEach', 'isArray'],
|
||||||
'reduceRight': ['baseCreateCallback', 'forEachRight'],
|
'reduceRight': ['baseCreateCallback', 'forEachRight'],
|
||||||
'reject': ['createCallback', 'filter'],
|
'reject': ['createCallback', 'filter'],
|
||||||
'remove': ['baseEach', 'createCallback', 'isArray'],
|
'remove': ['createCallback'],
|
||||||
'rest': ['createCallback', 'slice'],
|
'rest': ['createCallback', 'slice'],
|
||||||
'result': ['isFunction'],
|
'result': ['isFunction'],
|
||||||
'runInContext': ['defaults', 'pick'],
|
'runInContext': ['defaults', 'pick'],
|
||||||
@@ -338,6 +338,7 @@
|
|||||||
'lastIndexOf',
|
'lastIndexOf',
|
||||||
'pull',
|
'pull',
|
||||||
'range',
|
'range',
|
||||||
|
'remove',
|
||||||
'rest',
|
'rest',
|
||||||
'sortedIndex',
|
'sortedIndex',
|
||||||
'union',
|
'union',
|
||||||
@@ -375,7 +376,6 @@
|
|||||||
'reduce',
|
'reduce',
|
||||||
'reduceRight',
|
'reduceRight',
|
||||||
'reject',
|
'reject',
|
||||||
'remove',
|
|
||||||
'shuffle',
|
'shuffle',
|
||||||
'size',
|
'size',
|
||||||
'some',
|
'some',
|
||||||
@@ -3001,11 +3001,11 @@
|
|||||||
_.pull(funcDepMap[funcName], 'baseEach').push('forEach');
|
_.pull(funcDepMap[funcName], 'baseEach').push('forEach');
|
||||||
});
|
});
|
||||||
|
|
||||||
_.each(['contains', 'every', 'filter', 'find', 'forEach', 'map', 'max', 'min', 'reduce', 'remove', 'some'], function(funcName) {
|
_.each(['contains', 'every', 'filter', 'find', 'forEach', 'map', 'max', 'min', 'reduce', 'some'], function(funcName) {
|
||||||
_.pull(funcDepMap[funcName], 'baseEach').push('forOwn');
|
_.pull(funcDepMap[funcName], 'baseEach').push('forOwn');
|
||||||
});
|
});
|
||||||
|
|
||||||
_.each(['every', 'find', 'filter', 'forEach', 'forIn', 'forOwn', 'map', 'reduce', 'remove', 'shimKeys'], function(funcName) {
|
_.each(['every', 'find', 'filter', 'forEach', 'forIn', 'forOwn', 'map', 'reduce', 'shimKeys'], function(funcName) {
|
||||||
if (!(isUnderscore && isLodash(funcName))) {
|
if (!(isUnderscore && isLodash(funcName))) {
|
||||||
_.pull(funcDepMap[funcName], 'isArray');
|
_.pull(funcDepMap[funcName], 'isArray');
|
||||||
}
|
}
|
||||||
@@ -3260,7 +3260,7 @@
|
|||||||
].join('\n'));
|
].join('\n'));
|
||||||
|
|
||||||
// replace `isArray(collection)` checks in "Collections" functions with simpler type checks
|
// replace `isArray(collection)` checks in "Collections" functions with simpler type checks
|
||||||
_.each(['every', 'filter', 'find', 'max', 'min', 'reduce', 'remove', 'some'], function(funcName) {
|
_.each(['every', 'filter', 'find', 'max', 'min', 'reduce', 'some'], function(funcName) {
|
||||||
source = source.replace(matchFunction(source, funcName), function(match) {
|
source = source.replace(matchFunction(source, funcName), function(match) {
|
||||||
if (funcName == 'reduce') {
|
if (funcName == 'reduce') {
|
||||||
match = match.replace(/^( *)var noaccum\b/m, '$1if (!collection) return accumulator;\n$&');
|
match = match.replace(/^( *)var noaccum\b/m, '$1if (!collection) return accumulator;\n$&');
|
||||||
|
|||||||
11
lodash.js
11
lodash.js
@@ -4647,11 +4647,10 @@
|
|||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Removes all elements from the given array that the callback returns truthy
|
* Removes all elements from an array that the callback returns truthy for
|
||||||
* for and returns an array of removed elements. The callback is bound to
|
* and returns an array of removed elements. The callback is bound to `thisArg`
|
||||||
* `thisArg` and invoked with three arguments; (value, index, array).
|
* and invoked with three arguments; (value, index, array).
|
||||||
*
|
*
|
||||||
* If a property name is provided for `callback` the created "_.pluck" style
|
* If a property name is provided for `callback` the created "_.pluck" style
|
||||||
* callback will return the property value of the given element.
|
* callback will return the property value of the given element.
|
||||||
@@ -4663,7 +4662,7 @@
|
|||||||
* @static
|
* @static
|
||||||
* @memberOf _
|
* @memberOf _
|
||||||
* @category Arrays
|
* @category Arrays
|
||||||
* @param {Array|Object|String} array The array to modify.
|
* @param {Array} array The array to modify.
|
||||||
* @param {Function|Object|String} [callback=identity] The function called
|
* @param {Function|Object|String} [callback=identity] The function called
|
||||||
* per iteration. If a property name or object is provided it will be used
|
* per iteration. If a property name or object is provided it will be used
|
||||||
* to create a "_.pluck" or "_.where" style callback, respectively.
|
* to create a "_.pluck" or "_.where" style callback, respectively.
|
||||||
@@ -4682,7 +4681,7 @@
|
|||||||
*/
|
*/
|
||||||
function remove(array, callback, thisArg) {
|
function remove(array, callback, thisArg) {
|
||||||
var index = -1,
|
var index = -1,
|
||||||
length = array.length,
|
length = array ? array.length : 0,
|
||||||
result = [];
|
result = [];
|
||||||
|
|
||||||
callback = lodash.createCallback(callback, thisArg, 3);
|
callback = lodash.createCallback(callback, thisArg, 3);
|
||||||
|
|||||||
@@ -107,6 +107,7 @@
|
|||||||
'lastIndexOf',
|
'lastIndexOf',
|
||||||
'pull',
|
'pull',
|
||||||
'range',
|
'range',
|
||||||
|
'remove',
|
||||||
'rest',
|
'rest',
|
||||||
'sortedIndex',
|
'sortedIndex',
|
||||||
'union',
|
'union',
|
||||||
@@ -144,7 +145,6 @@
|
|||||||
'reduce',
|
'reduce',
|
||||||
'reduceRight',
|
'reduceRight',
|
||||||
'reject',
|
'reject',
|
||||||
'remove',
|
|
||||||
'shuffle',
|
'shuffle',
|
||||||
'size',
|
'size',
|
||||||
'some',
|
'some',
|
||||||
|
|||||||
67
test/test.js
67
test/test.js
@@ -2734,6 +2734,20 @@
|
|||||||
|
|
||||||
/*--------------------------------------------------------------------------*/
|
/*--------------------------------------------------------------------------*/
|
||||||
|
|
||||||
|
QUnit.module('lodash.pull');
|
||||||
|
|
||||||
|
(function() {
|
||||||
|
test('should modify and return the array', function() {
|
||||||
|
var array = [1, 2, 3],
|
||||||
|
actual = _.pull(array, 1, 3);
|
||||||
|
|
||||||
|
deepEqual(array, [2]);
|
||||||
|
ok(actual === array);
|
||||||
|
})
|
||||||
|
}());
|
||||||
|
|
||||||
|
/*--------------------------------------------------------------------------*/
|
||||||
|
|
||||||
QUnit.module('lodash.random');
|
QUnit.module('lodash.random');
|
||||||
|
|
||||||
(function() {
|
(function() {
|
||||||
@@ -2897,6 +2911,44 @@
|
|||||||
|
|
||||||
/*--------------------------------------------------------------------------*/
|
/*--------------------------------------------------------------------------*/
|
||||||
|
|
||||||
|
QUnit.module('lodash.remove');
|
||||||
|
|
||||||
|
(function() {
|
||||||
|
test('should modify the array and return removed elements', function() {
|
||||||
|
var array = [1, 2, 3];
|
||||||
|
|
||||||
|
var actual = _.remove(array, function(value) {
|
||||||
|
return value < 3;
|
||||||
|
});
|
||||||
|
|
||||||
|
deepEqual(array, [3]);
|
||||||
|
deepEqual(actual, [1, 2]);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('should pass the correct `callback` arguments', function() {
|
||||||
|
var args,
|
||||||
|
array = [1, 2, 3];
|
||||||
|
|
||||||
|
_.remove(array, function() {
|
||||||
|
args || (args = slice.call(arguments));
|
||||||
|
});
|
||||||
|
|
||||||
|
deepEqual(args, [1, 0, array]);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('should support the `thisArg` argument', function() {
|
||||||
|
var array = [1, 2, 3];
|
||||||
|
|
||||||
|
var actual = _.remove(array, function(value, index) {
|
||||||
|
return this[index] < 3;
|
||||||
|
}, array);
|
||||||
|
|
||||||
|
deepEqual(actual, [1, 2]);
|
||||||
|
});
|
||||||
|
}());
|
||||||
|
|
||||||
|
/*--------------------------------------------------------------------------*/
|
||||||
|
|
||||||
QUnit.module('lodash.result');
|
QUnit.module('lodash.result');
|
||||||
|
|
||||||
(function() {
|
(function() {
|
||||||
@@ -4011,6 +4063,9 @@
|
|||||||
deepEqual(_.uniq(args), [1, null, [3], 5], message('uniq'));
|
deepEqual(_.uniq(args), [1, null, [3], 5], message('uniq'));
|
||||||
deepEqual(_.without(args, null), [1, [3], 5], message('without'));
|
deepEqual(_.without(args, null), [1, [3], 5], message('without'));
|
||||||
deepEqual(_.zip(args, args), [[1, 1], [null, null], [[3], [3]], [null, null], [5, 5]], message('zip'));
|
deepEqual(_.zip(args, args), [[1, 1], [null, null], [[3], [3]], [null, null], [5, 5]], message('zip'));
|
||||||
|
|
||||||
|
deepEqual(_.pull(args, null), { '0': 1, '1': [3], '2': 5 }, message('pull'));
|
||||||
|
deepEqual(_.remove(args, function(value) { return typeof value == 'number'; }), [1, 5], message('remove'));
|
||||||
});
|
});
|
||||||
|
|
||||||
test('should allow falsey primary arguments', function() {
|
test('should allow falsey primary arguments', function() {
|
||||||
@@ -4125,7 +4180,6 @@
|
|||||||
|
|
||||||
test('should handle `null` `thisArg` arguments', function() {
|
test('should handle `null` `thisArg` arguments', function() {
|
||||||
var thisArg,
|
var thisArg,
|
||||||
array = ['a'],
|
|
||||||
callback = function() { thisArg = this; },
|
callback = function() { thisArg = this; },
|
||||||
expected = (function() { return this; }).call(null);
|
expected = (function() { return this; }).call(null);
|
||||||
|
|
||||||
@@ -4134,10 +4188,17 @@
|
|||||||
'every',
|
'every',
|
||||||
'filter',
|
'filter',
|
||||||
'find',
|
'find',
|
||||||
|
'findIndex',
|
||||||
|
'findKey',
|
||||||
'findLast',
|
'findLast',
|
||||||
|
'findLastIndex',
|
||||||
|
'findLastKey',
|
||||||
'forEach',
|
'forEach',
|
||||||
|
'forEachRight',
|
||||||
'forIn',
|
'forIn',
|
||||||
|
'forInRight',
|
||||||
'forOwn',
|
'forOwn',
|
||||||
|
'forOwnRight',
|
||||||
'groupBy',
|
'groupBy',
|
||||||
'map',
|
'map',
|
||||||
'max',
|
'max',
|
||||||
@@ -4147,6 +4208,7 @@
|
|||||||
'reduce',
|
'reduce',
|
||||||
'reduceRight',
|
'reduceRight',
|
||||||
'reject',
|
'reject',
|
||||||
|
'remove',
|
||||||
'some',
|
'some',
|
||||||
'sortBy',
|
'sortBy',
|
||||||
'sortedIndex',
|
'sortedIndex',
|
||||||
@@ -4155,7 +4217,8 @@
|
|||||||
];
|
];
|
||||||
|
|
||||||
_.forEach(funcs, function(methodName) {
|
_.forEach(funcs, function(methodName) {
|
||||||
var func = _[methodName],
|
var array = ['a'],
|
||||||
|
func = _[methodName],
|
||||||
message = '`_.' + methodName + '` handles `null` `thisArg` arguments';
|
message = '`_.' + methodName + '` handles `null` `thisArg` arguments';
|
||||||
|
|
||||||
thisArg = undefined;
|
thisArg = undefined;
|
||||||
|
|||||||
Reference in New Issue
Block a user