mirror of
https://github.com/whoisclebs/lodash.git
synced 2026-02-09 02:17:50 +00:00
Make _.where iterate over only own properties.
Former-commit-id: 29b4cafe5271cad70c711c2d401cea627fa97e33
This commit is contained in:
25
build.js
25
build.js
@@ -156,7 +156,7 @@
|
|||||||
'uniqueId': [],
|
'uniqueId': [],
|
||||||
'value': ['mixin'],
|
'value': ['mixin'],
|
||||||
'values': ['forOwn'],
|
'values': ['forOwn'],
|
||||||
'where': ['filter', 'forIn'],
|
'where': ['filter', 'keys'],
|
||||||
'without': ['indexOf'],
|
'without': ['indexOf'],
|
||||||
'wrap': [],
|
'wrap': [],
|
||||||
'zip': ['max', 'pluck']
|
'zip': ['max', 'pluck']
|
||||||
@@ -988,13 +988,14 @@
|
|||||||
dependencyMap.reduceRight = ['forEach', 'keys'];
|
dependencyMap.reduceRight = ['forEach', 'keys'];
|
||||||
}
|
}
|
||||||
if (isUnderscore) {
|
if (isUnderscore) {
|
||||||
dependencyMap.contains = ['forEach', 'indexOf'],
|
dependencyMap.contains = ['forEach', 'indexOf'];
|
||||||
dependencyMap.isEqual = ['isArray', 'isFunction'];
|
dependencyMap.isEqual = ['isArray', 'isFunction'];
|
||||||
dependencyMap.isEmpty = ['isArray', 'isString'];
|
dependencyMap.isEmpty = ['isArray', 'isString'];
|
||||||
dependencyMap.max = ['forEach', 'isArray'];
|
dependencyMap.max = ['forEach', 'isArray'];
|
||||||
dependencyMap.min = ['forEach', 'isArray'];
|
dependencyMap.min = ['forEach', 'isArray'];
|
||||||
dependencyMap.pick = [];
|
dependencyMap.pick = [];
|
||||||
dependencyMap.template = ['defaults', 'escape'];
|
dependencyMap.template = ['defaults', 'escape'];
|
||||||
|
dependencyMap.where = ['filter', 'forIn'];
|
||||||
|
|
||||||
if (useUnderscoreClone) {
|
if (useUnderscoreClone) {
|
||||||
dependencyMap.clone = ['assign', 'isArray'];
|
dependencyMap.clone = ['assign', 'isArray'];
|
||||||
@@ -1255,6 +1256,26 @@
|
|||||||
' }'
|
' }'
|
||||||
].join('\n'));
|
].join('\n'));
|
||||||
|
|
||||||
|
// replace `_.where`
|
||||||
|
source = source.replace(/^( *)function where[\s\S]+?\n\1}/m, [
|
||||||
|
' function where(collection, properties) {',
|
||||||
|
' var props = [];',
|
||||||
|
' forIn(properties, function(value, prop) {',
|
||||||
|
' props.push(prop);',
|
||||||
|
' });',
|
||||||
|
' return filter(collection, function(object) {',
|
||||||
|
' var length = props.length;',
|
||||||
|
' while (length--) {',
|
||||||
|
' var result = object[props[length]] === properties[props[length]];',
|
||||||
|
' if (!result) {',
|
||||||
|
' break;',
|
||||||
|
' }',
|
||||||
|
' }',
|
||||||
|
' return !!result;',
|
||||||
|
' });',
|
||||||
|
' }'
|
||||||
|
].join('\n'));
|
||||||
|
|
||||||
// replace `_.without`
|
// replace `_.without`
|
||||||
source = source.replace(/^( *)function without[\s\S]+?\n\1}/m, [
|
source = source.replace(/^( *)function without[\s\S]+?\n\1}/m, [
|
||||||
' function without(array) {',
|
' function without(array) {',
|
||||||
|
|||||||
@@ -2537,10 +2537,7 @@
|
|||||||
* // => [{ 'name': 'moe', 'age': 40 }]
|
* // => [{ 'name': 'moe', 'age': 40 }]
|
||||||
*/
|
*/
|
||||||
function where(collection, properties) {
|
function where(collection, properties) {
|
||||||
var props = [];
|
var props = keys(properties);
|
||||||
forIn(properties, function(value, prop) {
|
|
||||||
props.push(prop);
|
|
||||||
});
|
|
||||||
return filter(collection, function(object) {
|
return filter(collection, function(object) {
|
||||||
var length = props.length;
|
var length = props.length;
|
||||||
while (length--) {
|
while (length--) {
|
||||||
|
|||||||
@@ -636,7 +636,7 @@
|
|||||||
|
|
||||||
build(['-s', 'underscore'], function(source, filePath) {
|
build(['-s', 'underscore'], function(source, filePath) {
|
||||||
var last,
|
var last,
|
||||||
array = [{ 'value': 1 }, { 'value': 2 }],
|
array = [{ 'a': 1, 'b': 2 }, { 'a': 2, 'b': 2 }],
|
||||||
basename = path.basename(filePath, '.js'),
|
basename = path.basename(filePath, '.js'),
|
||||||
context = createContext();
|
context = createContext();
|
||||||
|
|
||||||
@@ -658,17 +658,17 @@
|
|||||||
deepEqual(lodash.extend({}, new Foo), Foo.prototype, '_.extend should assign inherited `source` properties: ' + basename);
|
deepEqual(lodash.extend({}, new Foo), Foo.prototype, '_.extend should assign inherited `source` properties: ' + basename);
|
||||||
|
|
||||||
actual = lodash.find(array, function(value) {
|
actual = lodash.find(array, function(value) {
|
||||||
return 'value' in value;
|
return 'a' in value;
|
||||||
});
|
});
|
||||||
|
|
||||||
equal(actual, array[0], '_.find: ' + basename);
|
equal(actual, _.first(array), '_.find: ' + basename);
|
||||||
|
|
||||||
actual = lodash.forEach(array, function(value) {
|
actual = lodash.forEach(array, function(value) {
|
||||||
last = value;
|
last = value;
|
||||||
return false;
|
return false;
|
||||||
});
|
});
|
||||||
|
|
||||||
equal(last.value, 2, '_.forEach should not exit early: ' + basename);
|
equal(last, _.last(array), '_.forEach should not exit early: ' + basename);
|
||||||
equal(actual, undefined, '_.forEach should return `undefined`: ' + basename);
|
equal(actual, undefined, '_.forEach should return `undefined`: ' + basename);
|
||||||
|
|
||||||
object = { 'length': 0, 'splice': Array.prototype.splice };
|
object = { 'length': 0, 'splice': Array.prototype.splice };
|
||||||
@@ -688,6 +688,11 @@
|
|||||||
|
|
||||||
equal(lodash.some([false, true, false]), true, '_.some: ' + basename);
|
equal(lodash.some([false, true, false]), true, '_.some: ' + basename);
|
||||||
equal(lodash.template('${a}', object), '${a}', '_.template should ignore ES6 delimiters: ' + basename);
|
equal(lodash.template('${a}', object), '${a}', '_.template should ignore ES6 delimiters: ' + basename);
|
||||||
|
|
||||||
|
var properties = new Foo;
|
||||||
|
properties.b = 2;
|
||||||
|
deepEqual(lodash.where(array, properties), [_.first(array)], '_.where should filter by inherited properties: ' + basename);
|
||||||
|
|
||||||
start();
|
start();
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -1855,14 +1855,13 @@
|
|||||||
deepEqual(_.where(array, { 'a': 1, 'b': 2 }), [{ 'a': 1, 'b': 2 }]);
|
deepEqual(_.where(array, { 'a': 1, 'b': 2 }), [{ 'a': 1, 'b': 2 }]);
|
||||||
});
|
});
|
||||||
|
|
||||||
test('should filter by inherited properties', function() {
|
test('should not filter by inherited properties', function() {
|
||||||
function Foo() {}
|
function Foo() {}
|
||||||
Foo.prototype = { 'b': 2 };
|
Foo.prototype = { 'a': 2 };
|
||||||
|
|
||||||
var properties = new Foo;
|
var properties = new Foo;
|
||||||
properties.a = 1;
|
properties.b = 2;
|
||||||
|
deepEqual(_.where(array, properties), [{ 'a': 1, 'b': 2 }, { 'a': 2, 'b': 2 }]);
|
||||||
deepEqual(_.where(array, properties), [{ 'a': 1, 'b': 2 }]);
|
|
||||||
});
|
});
|
||||||
|
|
||||||
test('should filter by problem JScript properties (test in IE < 9)', function() {
|
test('should filter by problem JScript properties (test in IE < 9)', function() {
|
||||||
|
|||||||
Reference in New Issue
Block a user