mirror of
https://github.com/whoisclebs/lodash.git
synced 2026-02-10 10:57:49 +00:00
Avoid changes to object affecting the result of a match function.
This commit is contained in:
20
lodash.js
20
lodash.js
@@ -8524,14 +8524,17 @@
|
|||||||
* // => { 'name': 'barney', 'age': 36 }
|
* // => { 'name': 'barney', 'age': 36 }
|
||||||
*/
|
*/
|
||||||
function matches(source) {
|
function matches(source) {
|
||||||
var props = keys(source),
|
var keyVals = pairs(source),
|
||||||
propsLength = props.length,
|
keyValsLength = keyVals.length;
|
||||||
key = props[0],
|
|
||||||
value = propsLength && source[key];
|
|
||||||
|
|
||||||
|
if (keyValsLength) {
|
||||||
|
var keyVal = keyVals[0],
|
||||||
|
key = keyVal[0],
|
||||||
|
value = keyVal[1];
|
||||||
|
}
|
||||||
// fast path the common case of providing an object with a single
|
// fast path the common case of providing an object with a single
|
||||||
// property containing a primitive value
|
// property containing a primitive value
|
||||||
if (propsLength == 1 && value === value && !isObject(value)) {
|
if (keyValsLength == 1 && value === value && !isObject(value)) {
|
||||||
return function(object) {
|
return function(object) {
|
||||||
if (object == null) {
|
if (object == null) {
|
||||||
return false;
|
return false;
|
||||||
@@ -8542,14 +8545,15 @@
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
return function(object) {
|
return function(object) {
|
||||||
var length = propsLength;
|
var length = keyValsLength;
|
||||||
if (length && object == null) {
|
if (length && object == null) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
while (length--) {
|
while (length--) {
|
||||||
var key = props[length];
|
keyVal = keyVals[length];
|
||||||
|
key = keyVal[0];
|
||||||
if (!(hasOwnProperty.call(object, key) &&
|
if (!(hasOwnProperty.call(object, key) &&
|
||||||
baseIsEqual(source[key], object[key], null, true))) {
|
baseIsEqual(keyVal[1], object[key], null, true))) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
16
test/test.js
16
test/test.js
@@ -6689,6 +6689,22 @@
|
|||||||
strictEqual(matches(otherObject), false);
|
strictEqual(matches(otherObject), false);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
test('should not change match behavior if `source` is augmented', 6, function() {
|
||||||
|
_.each([{ 'a': 1 }, { 'a': 1, 'b': 2 }], function(source) {
|
||||||
|
var object = _.clone(source),
|
||||||
|
matches = _.matches(source);
|
||||||
|
|
||||||
|
strictEqual(matches(object), true);
|
||||||
|
|
||||||
|
source.a = 2;
|
||||||
|
source.b = 1;
|
||||||
|
source.c = 3;
|
||||||
|
|
||||||
|
strictEqual(matches(object), true);
|
||||||
|
strictEqual(matches(source), false);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
test('should return `true` when comparing an empty `source`', 1, function() {
|
test('should return `true` when comparing an empty `source`', 1, function() {
|
||||||
var object = { 'a': 1 },
|
var object = { 'a': 1 },
|
||||||
expected = _.map(empties, _.constant(true));
|
expected = _.map(empties, _.constant(true));
|
||||||
|
|||||||
Reference in New Issue
Block a user