Remove _.findWhere, _.pluck, & _.where.

This commit is contained in:
jdalton
2015-07-03 10:48:52 -07:00
committed by John-David Dalton
parent b5dd2e605d
commit 5ff9f01aba
2 changed files with 47 additions and 318 deletions

View File

@@ -887,9 +887,8 @@
*
* The wrapper methods that support shortcut fusion are:
* `compact`, `drop`, `dropRight`, `dropRightWhile`, `dropWhile`, `filter`,
* `first`, `initial`, `last`, `map`, `pluck`, `reject`, `rest`, `reverse`,
* `slice`, `take`, `takeRight`, `takeRightWhile`, `takeWhile`, `toArray`,
* and `where`
* `first`, `initial`, `last`, `map`, `reject`, `rest`, `reverse`, `slice`,
* `take`, `takeRight`, `takeRightWhile`, `takeWhile`, and `toArray`
*
* The chainable wrapper methods are:
* `after`, `ary`, `assign`, `at`, `before`, `bind`, `bindAll`, `bindKey`,
@@ -902,29 +901,29 @@
* `invoke`, `keys`, `keysIn`, `map`, `mapKeys`, `mapValues`, `matches`,
* `matchesProperty`, `memoize`, `merge`, `method`, `methodOf`, `mixin`,
* `modArgs`, `negate`, `omit`, `once`, `pairs`, `partial`, `partialRight`,
* `partition`, `pick`, `plant`, `pluck`, `property`, `propertyOf`, `pull`,
* `pullAt`, `push`, `range`, `rearg`, `reject`, `remove`, `rest`, `restParam`,
* `reverse`, `set`, `shuffle`, `slice`, `sort`, `sortBy`, `sortByAll`,
* `sortByOrder`, `splice`, `spread`, `take`, `takeRight`, `takeRightWhile`,
* `takeWhile`, `tap`, `throttle`, `thru`, `times`, `toArray`, `toPlainObject`,
* `transform`, `union`, `uniq`, `unshift`, `unzip`, `unzipWith`, `values`,
* `valuesIn`, `where`, `without`, `wrap`, `xor`, `zip`, `zipObject`, `zipWith`
* `partition`, `pick`, `plant`, `property`, `propertyOf`, `pull`, `pullAt`,
* `push`, `range`, `rearg`, `reject`, `remove`, `rest`, `restParam`, `reverse`,
* `set`, `shuffle`, `slice`, `sort`, `sortBy`, `sortByAll`, `sortByOrder`,
* `splice`, `spread`, `take`, `takeRight`, `takeRightWhile`, `takeWhile`,
* `tap`, `throttle`, `thru`, `times`, `toArray`, `toPlainObject`, `transform`,
* `union`, `uniq`, `unshift`, `unzip`, `unzipWith`, `values`, `valuesIn`,
* `without`, `wrap`, `xor`, `zip`, `zipObject`, `zipWith`
*
* The wrapper methods that are **not** chainable by default are:
* `add`, `attempt`, `camelCase`, `capitalize`, `ceil`, `clone`, `cloneDeep`,
* `deburr`, `endsWith`, `escape`, `escapeRegExp`, `every`, `find`, `findIndex`,
* `findKey`, `findLast`, `findLastIndex`, `findLastKey`, `findWhere`, `first`,
* `floor`, `get`, `gt`, `gte`, `has`, `identity`, `includes`, `indexOf`,
* `inRange`, `isArguments`, `isArray`, `isBoolean`, `isDate`, `isElement`,
* `isEmpty`, `isEqual`, `isError`, `isFinite` `isFunction`, `isMatch`,
* `isNative`, `isNaN`, `isNull`, `isNumber`, `isObject`, `isPlainObject`,
* `isRegExp`, `isString`, `isUndefined`, `isTypedArray`, `join`, `kebabCase`,
* `last`, `lastIndexOf`, `lt`, `lte`, `max`, `min`, `noConflict`, `noop`,
* `now`, `pad`, `padLeft`, `padRight`, `parseInt`, `pop`, `random`, `reduce`,
* `reduceRight`, `repeat`, `result`, `round`, `runInContext`, `shift`, `size`,
* `snakeCase`, `some`, `sortedIndex`, `sortedLastIndex`, `startCase`,
* `startsWith`, `sum`, `template`, `trim`, `trimLeft`, `trimRight`, `trunc`,
* `unescape`, `uniqueId`, `value`, and `words`
* `findKey`, `findLast`, `findLastIndex`, `findLastKey`, `first`, `floor`,
* `get`, `gt`, `gte`, `has`, `identity`, `includes`, `indexOf`, `inRange`,
* `isArguments`, `isArray`, `isBoolean`, `isDate`, `isElement`, `isEmpty`,
* `isEqual`, `isError`, `isFinite` `isFunction`, `isMatch`, `isNative`,
* `isNaN`, `isNull`, `isNumber`, `isObject`, `isPlainObject`, `isRegExp`,
* `isString`, `isUndefined`, `isTypedArray`, `join`, `kebabCase`, `last`,
* `lastIndexOf`, `lt`, `lte`, `max`, `min`, `noConflict`, `noop`, `now`, `pad`,
* `padLeft`, `padRight`, `parseInt`, `pop`, `random`, `reduce`, `reduceRight`,
* `repeat`, `result`, `round`, `runInContext`, `shift`, `size`,`snakeCase`,
* `some`, `sortedIndex`, `sortedLastIndex`, `startCase`, `startsWith`, `sum`,
* `template`, `trim`, `trimLeft`, `trimRight`, `trunc`, `unescape`, `uniqueId`,
* `value`, and `words`
*
* The wrapper method `sample` will return a wrapped value when `n` is provided,
* otherwise an unwrapped value is returned.
@@ -4812,15 +4811,15 @@
* ];
*
* // using the `_.matches` callback shorthand
* _.pluck(_.dropRightWhile(users, { 'user': 'pebbles', 'active': false }), 'user');
* _.map(_.dropRightWhile(users, { 'user': 'pebbles', 'active': false }), 'user');
* // => ['barney', 'fred']
*
* // using the `_.matchesProperty` callback shorthand
* _.pluck(_.dropRightWhile(users, 'active', false), 'user');
* _.map(_.dropRightWhile(users, 'active', false), 'user');
* // => ['barney']
*
* // using the `_.property` callback shorthand
* _.pluck(_.dropRightWhile(users, 'active'), 'user');
* _.map(_.dropRightWhile(users, 'active'), 'user');
* // => ['barney', 'fred', 'pebbles']
*/
function dropRightWhile(array, predicate, thisArg) {
@@ -4867,15 +4866,15 @@
* ];
*
* // using the `_.matches` callback shorthand
* _.pluck(_.dropWhile(users, { 'user': 'barney', 'active': false }), 'user');
* _.map(_.dropWhile(users, { 'user': 'barney', 'active': false }), 'user');
* // => ['fred', 'pebbles']
*
* // using the `_.matchesProperty` callback shorthand
* _.pluck(_.dropWhile(users, 'active', false), 'user');
* _.map(_.dropWhile(users, 'active', false), 'user');
* // => ['pebbles']
*
* // using the `_.property` callback shorthand
* _.pluck(_.dropWhile(users, 'active'), 'user');
* _.map(_.dropWhile(users, 'active'), 'user');
* // => ['barney', 'fred', 'pebbles']
*/
function dropWhile(array, predicate, thisArg) {
@@ -5636,15 +5635,15 @@
* ];
*
* // using the `_.matches` callback shorthand
* _.pluck(_.takeRightWhile(users, { 'user': 'pebbles', 'active': false }), 'user');
* _.map(_.takeRightWhile(users, { 'user': 'pebbles', 'active': false }), 'user');
* // => ['pebbles']
*
* // using the `_.matchesProperty` callback shorthand
* _.pluck(_.takeRightWhile(users, 'active', false), 'user');
* _.map(_.takeRightWhile(users, 'active', false), 'user');
* // => ['fred', 'pebbles']
*
* // using the `_.property` callback shorthand
* _.pluck(_.takeRightWhile(users, 'active'), 'user');
* _.map(_.takeRightWhile(users, 'active'), 'user');
* // => []
*/
function takeRightWhile(array, predicate, thisArg) {
@@ -5691,15 +5690,15 @@
* ];
*
* // using the `_.matches` callback shorthand
* _.pluck(_.takeWhile(users, { 'user': 'barney', 'active': false }), 'user');
* _.map(_.takeWhile(users, { 'user': 'barney', 'active': false }), 'user');
* // => ['barney']
*
* // using the `_.matchesProperty` callback shorthand
* _.pluck(_.takeWhile(users, 'active', false), 'user');
* _.map(_.takeWhile(users, 'active', false), 'user');
* // => ['barney', 'fred']
*
* // using the `_.property` callback shorthand
* _.pluck(_.takeWhile(users, 'active'), 'user');
* _.map(_.takeWhile(users, 'active'), 'user');
* // => []
*/
function takeWhile(array, predicate, thisArg) {
@@ -6463,15 +6462,15 @@
* ];
*
* // using the `_.matches` callback shorthand
* _.pluck(_.filter(users, { 'age': 36, 'active': true }), 'user');
* _.map(_.filter(users, { 'age': 36, 'active': true }), 'user');
* // => ['barney']
*
* // using the `_.matchesProperty` callback shorthand
* _.pluck(_.filter(users, 'active', false), 'user');
* _.map(_.filter(users, 'active', false), 'user');
* // => ['fred']
*
* // using the `_.property` callback shorthand
* _.pluck(_.filter(users, 'active'), 'user');
* _.map(_.filter(users, 'active'), 'user');
* // => ['barney']
*/
function filter(collection, predicate, thisArg) {
@@ -6553,39 +6552,6 @@
*/
var findLast = createFind(baseEachRight, true);
/**
* Performs a deep comparison between each element in `collection` and the
* source object, returning the first element that has equivalent property
* values.
*
* **Note:** This method supports comparing arrays, booleans, `Date` objects,
* numbers, `Object` objects, regexes, and strings. Objects are compared by
* their own, not inherited, enumerable properties. For comparing a single
* own or inherited property value see `_.matchesProperty`.
*
* @static
* @memberOf _
* @category Collection
* @param {Array|Object|string} collection The collection to search.
* @param {Object} source The object of property values to match.
* @returns {*} Returns the matched element, else `undefined`.
* @example
*
* var users = [
* { 'user': 'barney', 'age': 36, 'active': true },
* { 'user': 'fred', 'age': 40, 'active': false }
* ];
*
* _.result(_.findWhere(users, { 'age': 36, 'active': true }), 'user');
* // => 'barney'
*
* _.result(_.findWhere(users, { 'age': 40, 'active': false }), 'user');
* // => 'fred'
*/
function findWhere(collection, source) {
return find(collection, baseMatches(source));
}
/**
* Iterates over elements of `collection` invoking `iteratee` for each element.
* The `iteratee` is bound to `thisArg` and invoked with three arguments:
@@ -6925,7 +6891,7 @@
* ];
*
* var mapper = function(array) {
* return _.pluck(array, 'user');
* return _.map(array, 'user');
* };
*
* // using the `_.matches` callback shorthand
@@ -6944,33 +6910,6 @@
result[key ? 0 : 1].push(value);
}, function() { return [[], []]; });
/**
* Gets the property value of `path` from all elements in `collection`.
*
* @static
* @memberOf _
* @category Collection
* @param {Array|Object|string} collection The collection to iterate over.
* @param {Array|string} path The path of the property to pluck.
* @returns {Array} Returns the property values.
* @example
*
* var users = [
* { 'user': 'barney', 'age': 36 },
* { 'user': 'fred', 'age': 40 }
* ];
*
* _.pluck(users, 'user');
* // => ['barney', 'fred']
*
* var userIndex = _.indexBy(users, 'user');
* _.pluck(userIndex, 'age');
* // => [36, 40] (iteration order is not guaranteed)
*/
function pluck(collection, path) {
return map(collection, property(path));
}
/**
* Reduces `collection` to a value which is the accumulated result of running
* each element in `collection` through `iteratee`, where each successive
@@ -7059,15 +6998,15 @@
* ];
*
* // using the `_.matches` callback shorthand
* _.pluck(_.reject(users, { 'age': 40, 'active': true }), 'user');
* _.map(_.reject(users, { 'age': 40, 'active': true }), 'user');
* // => ['barney']
*
* // using the `_.matchesProperty` callback shorthand
* _.pluck(_.reject(users, 'active', false), 'user');
* _.map(_.reject(users, 'active', false), 'user');
* // => ['fred']
*
* // using the `_.property` callback shorthand
* _.pluck(_.reject(users, 'active'), 'user');
* _.map(_.reject(users, 'active'), 'user');
* // => ['barney']
*/
function reject(collection, predicate, thisArg) {
@@ -7267,7 +7206,7 @@
* ];
*
* // using the `_.property` callback shorthand
* _.pluck(_.sortBy(users, 'user'), 'user');
* _.map(_.sortBy(users, 'user'), 'user');
* // => ['barney', 'fred', 'pebbles']
*/
function sortBy(collection, iteratee, thisArg) {
@@ -7382,39 +7321,6 @@
return baseSortByOrder(collection, iteratees, orders);
}
/**
* Performs a deep comparison between each element in `collection` and the
* source object, returning an array of all elements that have equivalent
* property values.
*
* **Note:** This method supports comparing arrays, booleans, `Date` objects,
* numbers, `Object` objects, regexes, and strings. Objects are compared by
* their own, not inherited, enumerable properties. For comparing a single
* own or inherited property value see `_.matchesProperty`.
*
* @static
* @memberOf _
* @category Collection
* @param {Array|Object|string} collection The collection to search.
* @param {Object} source The object of property values to match.
* @returns {Array} Returns the new filtered array.
* @example
*
* var users = [
* { 'user': 'barney', 'age': 36, 'active': false, 'pets': ['hoppy'] },
* { 'user': 'fred', 'age': 40, 'active': true, 'pets': ['baby puss', 'dino'] }
* ];
*
* _.pluck(_.where(users, { 'age': 36, 'active': false }), 'user');
* // => ['barney']
*
* _.pluck(_.where(users, { 'pets': ['dino'] }), 'user');
* // => ['fred']
*/
function where(collection, source) {
return filter(collection, baseMatches(source));
}
/*------------------------------------------------------------------------*/
/**
@@ -11636,7 +11542,7 @@
* _.map(objects, _.property('a.b.c'));
* // => [2, 1]
*
* _.pluck(_.sortBy(objects, _.property(['a', 'b', 'c'])), 'a.b.c');
* _.map(_.sortBy(objects, _.property(['a', 'b', 'c'])), 'a.b.c');
* // => [1, 2]
*/
function property(path) {
@@ -12109,7 +12015,6 @@
lodash.partialRight = partialRight;
lodash.partition = partition;
lodash.pick = pick;
lodash.pluck = pluck;
lodash.property = property;
lodash.propertyOf = propertyOf;
lodash.pull = pull;
@@ -12144,7 +12049,6 @@
lodash.unzipWith = unzipWith;
lodash.values = values;
lodash.valuesIn = valuesIn;
lodash.where = where;
lodash.without = without;
lodash.wrap = wrap;
lodash.xor = xor;
@@ -12180,7 +12084,6 @@
lodash.findLast = findLast;
lodash.findLastIndex = findLastIndex;
lodash.findLastKey = findLastKey;
lodash.findWhere = findWhere;
lodash.first = first;
lodash.floor = floor;
lodash.forEach = forEach;
@@ -12353,16 +12256,6 @@
};
});
// Add `LazyWrapper` methods for `_.pluck` and `_.where`.
arrayEach(['pluck', 'where'], function(methodName, index) {
var operationName = index ? 'filter' : 'map',
createCallback = index ? baseMatches : property;
LazyWrapper.prototype[methodName] = function(value) {
return this[operationName](createCallback(value));
};
});
LazyWrapper.prototype.compact = function() {
return this.filter(identity);
};

View File

@@ -2303,7 +2303,7 @@
});
test('`_.' + methodName + '` should work with curried functions with placeholders', 1, function() {
var curried = _.curry(_.pluck),
var curried = _.curry(_.ary(_.map, 2), 2),
getProp = curried(curried.placeholder, 'a'),
objects = [{ 'a': 1 }, { 'a': 2 }, { 'a': 1 }];
@@ -4123,46 +4123,6 @@
/*--------------------------------------------------------------------------*/
QUnit.module('lodash.findWhere');
(function() {
var objects = [
{ 'a': 1 },
{ 'a': 1 },
{ 'a': 1, 'b': 2 },
{ 'a': 2, 'b': 2 },
{ 'a': 3 }
];
test('should filter by `source` properties', 6, function() {
strictEqual(_.findWhere(objects, { 'a': 1 }), objects[0]);
strictEqual(_.findWhere(objects, { 'a': 2 }), objects[3]);
strictEqual(_.findWhere(objects, { 'a': 3 }), objects[4]);
strictEqual(_.findWhere(objects, { 'b': 1 }), undefined);
strictEqual(_.findWhere(objects, { 'b': 2 }), objects[2]);
strictEqual(_.findWhere(objects, { 'a': 1, 'b': 2 }), objects[2]);
});
test('should work with a function for `source`', 1, function() {
function source() {}
source.a = 2;
strictEqual(_.findWhere(objects, source), objects[3]);
});
test('should match all elements when provided an empty `source`', 1, function() {
var expected = _.map(empties, _.constant(true));
var actual = _.map(empties, function(value) {
return _.findWhere(objects, value) === objects[0];
});
deepEqual(actual, expected);
});
}());
/*--------------------------------------------------------------------------*/
QUnit.module('lodash.first');
(function() {
@@ -12372,72 +12332,6 @@
/*--------------------------------------------------------------------------*/
QUnit.module('lodash.pluck');
(function() {
test('should return an array of property values from each element of a collection', 1, function() {
var objects = [{ 'name': 'barney', 'age': 36 }, { 'name': 'fred', 'age': 40 }];
deepEqual(_.pluck(objects, 'name'), ['barney', 'fred']);
});
test('should pluck inherited property values', 1, function() {
function Foo() { this.a = 1; }
Foo.prototype.b = 2;
deepEqual(_.pluck([new Foo], 'b'), [2]);
});
test('should work with an object for `collection`', 1, function() {
var object = { 'a': [1], 'b': [1, 2], 'c': [1, 2, 3] };
deepEqual(_.pluck(object, 'length'), [1, 2, 3]);
});
test('should return `undefined` for undefined properties', 1, function() {
var array = [{ 'a': 1 }],
actual = [_.pluck(array, 'b'), _.pluck(array, 'c')];
deepEqual(actual, [[undefined], [undefined]]);
});
test('should work with nullish elements', 1, function() {
var objects = [{ 'a': 1 }, null, undefined, { 'a': 4 }];
deepEqual(_.pluck(objects, 'a'), [1, undefined, undefined, 4]);
});
test('should coerce `key` to a string', 1, function() {
function fn() {}
fn.toString = _.constant('fn');
var objects = [{ 'null': 1 }, { 'undefined': 2 }, { 'fn': 3 }, { '[object Object]': 4 }],
values = [null, undefined, fn, {}]
var actual = _.map(objects, function(object, index) {
return _.pluck([object], values[index]);
});
deepEqual(actual, [[1], [2], [3], [4]]);
});
test('should work in a lazy chain sequence', 2, function() {
if (!isNpm) {
var array = _.times(LARGE_ARRAY_SIZE + 1, function(index) {
return index ? { 'a': index } : null;
});
var actual = _(array).slice(1).pluck('a').value();
deepEqual(actual, _.pluck(array.slice(1), 'a'));
actual = _(array).slice(1).filter().pluck('a').value();
deepEqual(actual, _.pluck(_.filter(array.slice(1)), 'a'));
}
else {
skipTest(2);
}
});
}());
/*--------------------------------------------------------------------------*/
QUnit.module('lodash.property');
(function() {
@@ -14541,7 +14435,7 @@
var stableObject = _.zipObject('abcdefghijklmnopqrst'.split(''), stableArray);
test('should sort in ascending order', 1, function() {
var actual = _.pluck(_.sortBy(objects, function(object) {
var actual = _.map(_.sortBy(objects, function(object) {
return object.b;
}), 'b');
@@ -14589,7 +14483,7 @@
});
test('should work with a "_.property" style `iteratee`', 1, function() {
var actual = _.pluck(_.sortBy(objects.concat(undefined), 'b'), 'b');
var actual = _.map(_.sortBy(objects.concat(undefined), 'b'), 'b');
deepEqual(actual, [1, 2, 3, 4, undefined]);
});
@@ -16808,62 +16702,6 @@
/*--------------------------------------------------------------------------*/
QUnit.module('lodash.where');
(function() {
test('should filter by `source` properties', 12, function() {
var objects = [
{ 'a': 1 },
{ 'a': 1 },
{ 'a': 1, 'b': 2 },
{ 'a': 2, 'b': 2 },
{ 'a': 3 }
];
var pairs = [
[{ 'a': 1 }, [{ 'a': 1 }, { 'a': 1 }, { 'a': 1, 'b': 2 }]],
[{ 'a': 2 }, [{ 'a': 2, 'b': 2 }]],
[{ 'a': 3 }, [{ 'a': 3 }]],
[{ 'b': 1 }, []],
[{ 'b': 2 }, [{ 'a': 1, 'b': 2 }, { 'a': 2, 'b': 2 }]],
[{ 'a': 1, 'b': 2 }, [{ 'a': 1, 'b': 2 }]]
];
_.each(pairs, function(pair) {
var actual = _.where(objects, pair[0]);
deepEqual(actual, pair[1]);
ok(_.isEmpty(_.difference(actual, objects)));
});
});
test('should work with an object for `collection`', 1, function() {
var object = {
'x': { 'a': 1 },
'y': { 'a': 3 },
'z': { 'a': 1, 'b': 2 }
};
var actual = _.where(object, { 'a': 1 });
deepEqual(actual, [object.x, object.z]);
});
test('should work in a lazy chain sequence', 1, function() {
if (!isNpm) {
var array = _.times(LARGE_ARRAY_SIZE + 1, function(index) {
return index ? { 'a': 1, 'b': index } : { 'a': 3 };
});
var actual = _(array).slice(1).where({ 'a': 1 }).value();
deepEqual(actual, _.where(array.slice(1), { 'a': 1 }));
}
else {
skipTest();
}
});
}());
/*--------------------------------------------------------------------------*/
QUnit.module('lodash.without');
(function() {
@@ -18042,7 +17880,6 @@
'keys',
'map',
'pairs',
'pluck',
'pull',
'pullAt',
'range',
@@ -18060,7 +17897,6 @@
'union',
'uniq',
'values',
'where',
'without',
'xor',
'zip'
@@ -18068,7 +17904,7 @@
var acceptFalsey = _.difference(allMethods, rejectFalsey);
test('should accept falsey arguments', 212, function() {
test('should accept falsey arguments', 207, function() {
var emptyArrays = _.map(falsey, _.constant([]));
_.each(acceptFalsey, function(methodName) {
@@ -18104,7 +17940,7 @@
});
});
test('should return an array', 72, function() {
test('should return an array', 68, function() {
var array = [1, 2, 3];
_.each(returnArrays, function(methodName) {