From c5cc9078381889d35e0409caaa34d5e827c8beb7 Mon Sep 17 00:00:00 2001 From: jdalton Date: Tue, 31 Mar 2015 22:30:57 -0700 Subject: [PATCH] Ensure `_.match` and `_.matchesProperty` compares functions by reference. [closes #1101] --- lodash.src.js | 4 ++-- test/test.js | 36 +++++++++++++++++++++++------------- 2 files changed, 25 insertions(+), 15 deletions(-) diff --git a/lodash.src.js b/lodash.src.js index 7b71bde2c..bd6e592d4 100644 --- a/lodash.src.js +++ b/lodash.src.js @@ -2342,8 +2342,8 @@ othIsArr = isTypedArray(other); } } - var objIsObj = (objTag == objectTag || (isLoose && objTag == funcTag)) && !isHostObject(object), - othIsObj = (othTag == objectTag || (isLoose && othTag == funcTag)) && !isHostObject(other), + var objIsObj = objTag == objectTag && !isHostObject(object), + othIsObj = othTag == objectTag && !isHostObject(other), isSameTag = objTag == othTag; if (isSameTag && !(objIsArr || objIsObj)) { diff --git a/test/test.js b/test/test.js index 445e5066f..a155aba71 100644 --- a/test/test.js +++ b/test/test.js @@ -9402,6 +9402,17 @@ strictEqual(matches(object2), false); }); + test('should compare functions by reference', 3, function() { + var object1 = { 'a': _.noop }, + object2 = { 'a': noop }, + object3 = { 'a': {} }, + matches = _.matches(object1); + + strictEqual(matches(object1), true); + strictEqual(matches(object2), false); + strictEqual(matches(object3), false); + }); + test('should not change match behavior if `source` is augmented', 9, function() { _.each([{ 'a': { 'b': 2, 'c': 3 } }, { 'a': 1, 'b': 2 }, { 'a': 1 }], function(source, index) { var object = _.cloneDeep(source), @@ -9524,9 +9535,7 @@ test('should match properties when `value` is a function', 1, function() { function Foo() {} - Foo.a = function() {}; - Foo.a.b = 1; - Foo.a.c = 2; + Foo.a = { 'b': 1, 'c': 2 }; var matches = _.matches({ 'a': { 'b': 1 } }); strictEqual(matches(Foo), true); @@ -9613,6 +9622,17 @@ strictEqual(matches({ 'a': object2 }), false); }); + test('should compare functions by reference', 3, function() { + var object1 = { 'a': _.noop }, + object2 = { 'a': noop }, + object3 = { 'a': {} }, + matches = _.matchesProperty('a', object1); + + strictEqual(matches({ 'a': object1 }), true); + strictEqual(matches({ 'a': object2 }), false); + strictEqual(matches({ 'a': object3 }), false); + }); + test('should not change match behavior if `value` is augmented', 9, function() { _.each([{ 'a': { 'b': 2, 'c': 3 } }, { 'a': 1, 'b': 2 }, { 'a': 1 }], function(source, index) { var object = _.cloneDeep(source), @@ -9710,16 +9730,6 @@ strictEqual(matches(object), true); }); - test('should match properties when `value` is a function', 1, function() { - function Foo() {} - Foo.a = function() {}; - Foo.a.b = 1; - Foo.a.c = 2; - - var matches = _.matchesProperty('a', { 'b': 1 }); - strictEqual(matches(Foo), true); - }); - test('should match properties when `value` is not a plain object', 1, function() { function Foo(object) { _.assign(this, object); }