From 66d72345f2b178f974f3b4fceafd07179371b85e Mon Sep 17 00:00:00 2001 From: John-David Dalton Date: Sun, 25 Oct 2015 12:42:58 -0700 Subject: [PATCH] Ensure `_.conforms` functions return `true` when comparing an empty source. --- lodash.js | 2 +- test/test.js | 46 ++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 47 insertions(+), 1 deletion(-) diff --git a/lodash.js b/lodash.js index ed3ba97cd..7d149b076 100644 --- a/lodash.js +++ b/lodash.js @@ -2198,7 +2198,7 @@ return function(object) { if (object == null) { - return false; + return !length; } var index = length; while (index--) { diff --git a/test/test.js b/test/test.js index e23fb88b8..7cd110ade 100644 --- a/test/test.js +++ b/test/test.js @@ -2600,6 +2600,52 @@ assert.deepEqual(actual, expected); }); + + QUnit.test('should return `true` when comparing an empty `source` to a nullish `object`', function(assert) { + assert.expect(1); + + var values = [, null, undefined], + expected = lodashStable.map(values, lodashStable.constant(true)), + conforms = _.conforms({}); + + var actual = lodashStable.map(values, function(value, index) { + try { + return index ? conforms(value) : conforms(); + } catch (e) {} + }); + + assert.deepEqual(actual, expected); + }); + + QUnit.test('should return `true` when comparing an empty `source`', function(assert) { + assert.expect(1); + + var object = { 'a': 1 }, + expected = lodashStable.map(empties, lodashStable.constant(true)); + + var actual = lodashStable.map(empties, function(value) { + var conforms = _.conforms(value); + return conforms(object); + }); + + assert.deepEqual(actual, expected); + }); + + QUnit.test('should not change behavior if `source` is modified', function(assert) { + assert.expect(2); + + var source = { + 'a': function(value) { return value > 1; } + }; + + var object = { 'a': 2 }, + conforms = _.conforms(source); + + assert.strictEqual(conforms(object), true); + + source.a = function(value) { return value < 2; }; + assert.strictEqual(conforms(object), true); + }); }()); /*--------------------------------------------------------------------------*/