mirror of
https://github.com/whoisclebs/lodash.git
synced 2026-02-09 10:27:49 +00:00
Ensure functions created by _.matches don't error when comparing falsey object values. [closes #523]
This commit is contained in:
10
lodash.js
10
lodash.js
@@ -7786,7 +7786,7 @@
|
|||||||
// property containing a primitive value
|
// property containing a primitive value
|
||||||
if (propsLength == 1 && value === value && !isObject(value)) {
|
if (propsLength == 1 && value === value && !isObject(value)) {
|
||||||
return function(object) {
|
return function(object) {
|
||||||
if (!hasOwnProperty.call(object, key)) {
|
if (!(object && hasOwnProperty.call(object, key))) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
// treat `-0` vs. `+0` as not equal
|
// treat `-0` vs. `+0` as not equal
|
||||||
@@ -7795,9 +7795,11 @@
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
return function(object) {
|
return function(object) {
|
||||||
var length = propsLength,
|
var length = propsLength;
|
||||||
result = true;
|
if (length && !object) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
var result = true;
|
||||||
while (length--) {
|
while (length--) {
|
||||||
var key = props[length];
|
var key = props[length];
|
||||||
if (!(result = hasOwnProperty.call(object, key) &&
|
if (!(result = hasOwnProperty.call(object, key) &&
|
||||||
|
|||||||
42
test/test.js
42
test/test.js
@@ -5808,13 +5808,47 @@
|
|||||||
strictEqual(matches.length, 1);
|
strictEqual(matches.length, 1);
|
||||||
strictEqual(matches(object), true);
|
strictEqual(matches(object), true);
|
||||||
|
|
||||||
matches = _.matches({ 'b': 1 });
|
matches = _.matches({ 'b': 1 });
|
||||||
strictEqual(matches(object), false);
|
strictEqual(matches(object), false);
|
||||||
});
|
});
|
||||||
|
|
||||||
test('should return `true` when comparing an empty `source`', 1, function() {
|
test('should return `true` when comparing an empty `source`', 1, function() {
|
||||||
var matches = _.matches({});
|
var expected = _.map(empties, _.constant(true));
|
||||||
strictEqual(matches(object), true);
|
|
||||||
|
var actual = _.map(empties, function(value) {
|
||||||
|
var matches = _.matches(value);
|
||||||
|
return matches(object) === true;
|
||||||
|
});
|
||||||
|
|
||||||
|
deepEqual(actual, expected);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('should not error error for falsey `object` values', 1, function() {
|
||||||
|
var expected = _.map(falsey, _.constant(true)),
|
||||||
|
matches = _.matches({ 'a': 1 });
|
||||||
|
|
||||||
|
var actual = _.map(falsey, function(value, index) {
|
||||||
|
try {
|
||||||
|
var result = index ? matches(value) : matches();
|
||||||
|
return result === false;
|
||||||
|
} catch(e) { }
|
||||||
|
});
|
||||||
|
|
||||||
|
deepEqual(actual, expected);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('should return `true` when comparing an empty `source` to a falsey `object`', 1, function() {
|
||||||
|
var expected = _.map(falsey, _.constant(true)),
|
||||||
|
matches = _.matches({});
|
||||||
|
|
||||||
|
var actual = _.map(falsey, function(value, index) {
|
||||||
|
try {
|
||||||
|
var result = index ? matches(value) : matches();
|
||||||
|
return result === true;
|
||||||
|
} catch(e) { }
|
||||||
|
});
|
||||||
|
|
||||||
|
deepEqual(actual, expected);
|
||||||
});
|
});
|
||||||
}());
|
}());
|
||||||
|
|
||||||
@@ -6943,7 +6977,7 @@
|
|||||||
strictEqual(property.length, 1);
|
strictEqual(property.length, 1);
|
||||||
strictEqual(property(object), 1);
|
strictEqual(property(object), 1);
|
||||||
|
|
||||||
property = _.property('b');
|
property = _.property('b');
|
||||||
strictEqual(property(object), 2);
|
strictEqual(property(object), 2);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user