mirror of
https://github.com/whoisclebs/lodash.git
synced 2026-02-07 10:07:48 +00:00
Make _.where work with empty sources.
This commit is contained in:
@@ -4808,7 +4808,6 @@
|
|||||||
*
|
*
|
||||||
* @static
|
* @static
|
||||||
* @memberOf _
|
* @memberOf _
|
||||||
* @type Function
|
|
||||||
* @category Collections
|
* @category Collections
|
||||||
* @param {Array|Object|string} collection The collection to iterate over.
|
* @param {Array|Object|string} collection The collection to iterate over.
|
||||||
* @param {Object} source The object of property values to filter by.
|
* @param {Object} source The object of property values to filter by.
|
||||||
@@ -4826,7 +4825,9 @@
|
|||||||
* _.where(characters, { 'pets': ['dino'] });
|
* _.where(characters, { 'pets': ['dino'] });
|
||||||
* // => [{ 'name': 'fred', 'age': 40, 'pets': ['baby puss', 'dino'] }]
|
* // => [{ 'name': 'fred', 'age': 40, 'pets': ['baby puss', 'dino'] }]
|
||||||
*/
|
*/
|
||||||
var where = filter;
|
function where(collection, source) {
|
||||||
|
return filter(collection, isObject(source) ? source : {});
|
||||||
|
}
|
||||||
|
|
||||||
/*--------------------------------------------------------------------------*/
|
/*--------------------------------------------------------------------------*/
|
||||||
|
|
||||||
|
|||||||
15
test/test.js
15
test/test.js
@@ -9569,13 +9569,18 @@
|
|||||||
deepEqual(_.where(collection, { 'a': 1 }), [{ 'a': 1 }, { 'a': 1, 'b': 2 }]);
|
deepEqual(_.where(collection, { 'a': 1 }), [{ 'a': 1 }, { 'a': 1, 'b': 2 }]);
|
||||||
});
|
});
|
||||||
|
|
||||||
test('should match all elements when provided an empty `properties` object', 2, function() {
|
test('should match all elements when provided an empty `source`', 1, function() {
|
||||||
var actual = _.where(array, {});
|
var expected = _.map(empties, _.constant(array));
|
||||||
ok(actual !== array);
|
|
||||||
deepEqual(actual, array);
|
var actual = _.map(empties, function(value) {
|
||||||
|
var result = _.where(array, value);
|
||||||
|
return result !== array && result;
|
||||||
|
});
|
||||||
|
|
||||||
|
deepEqual(actual, expected);
|
||||||
});
|
});
|
||||||
|
|
||||||
test('should deep compare `properties` values', 1, function() {
|
test('should deep compare `source` values', 1, function() {
|
||||||
var collection = [{ 'a': { 'b': { 'c': 1, 'd': 2 }, 'e': 3 }, 'f': 4 }],
|
var collection = [{ 'a': { 'b': { 'c': 1, 'd': 2 }, 'e': 3 }, 'f': 4 }],
|
||||||
expected = _.cloneDeep(collection);
|
expected = _.cloneDeep(collection);
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user