Make _.where iterate over only own properties.

Former-commit-id: 29b4cafe5271cad70c711c2d401cea627fa97e33
This commit is contained in:
John-David Dalton
2012-11-11 20:00:20 -08:00
parent ed66ff88a1
commit f6d28a90e3
4 changed files with 37 additions and 15 deletions

View File

@@ -636,7 +636,7 @@
build(['-s', 'underscore'], function(source, filePath) {
var last,
array = [{ 'value': 1 }, { 'value': 2 }],
array = [{ 'a': 1, 'b': 2 }, { 'a': 2, 'b': 2 }],
basename = path.basename(filePath, '.js'),
context = createContext();
@@ -658,17 +658,17 @@
deepEqual(lodash.extend({}, new Foo), Foo.prototype, '_.extend should assign inherited `source` properties: ' + basename);
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) {
last = value;
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);
object = { 'length': 0, 'splice': Array.prototype.splice };
@@ -688,6 +688,11 @@
equal(lodash.some([false, true, false]), true, '_.some: ' + 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();
});
});

View File

@@ -1855,14 +1855,13 @@
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() {}
Foo.prototype = { 'b': 2 };
Foo.prototype = { 'a': 2 };
var properties = new Foo;
properties.a = 1;
deepEqual(_.where(array, properties), [{ 'a': 1, 'b': 2 }]);
properties.b = 2;
deepEqual(_.where(array, properties), [{ 'a': 1, 'b': 2 }, { 'a': 2, 'b': 2 }]);
});
test('should filter by problem JScript properties (test in IE < 9)', function() {