From 6dec9cb8314aac276f0b9e608bf00c6316d1cf12 Mon Sep 17 00:00:00 2001 From: Miro Mannino Date: Mon, 12 Oct 2015 13:46:06 +0400 Subject: [PATCH] Clarify exit early behavior in `_.every` doc and refine related test. --- lodash.js | 3 ++- test/test.js | 30 ++++++++++++++++++++++-------- 2 files changed, 24 insertions(+), 9 deletions(-) diff --git a/lodash.js b/lodash.js index 8fbb44301..7c0d6c7a2 100644 --- a/lodash.js +++ b/lodash.js @@ -6739,7 +6739,8 @@ /** * Checks if `predicate` returns truthy for **all** elements of `collection`. - * The predicate is invoked with three arguments: (value, index|key, collection). + * Iteration is stopped once `predicate` returns falsey. The predicate is + * invoked with three arguments: (value, index|key, collection). * * @static * @memberOf _ diff --git a/test/test.js b/test/test.js index 46ef5c830..e279a9f71 100644 --- a/test/test.js +++ b/test/test.js @@ -4453,9 +4453,16 @@ }); QUnit.test('should return `false` as soon as `predicate` returns falsey', function(assert) { - assert.expect(1); + assert.expect(2); - assert.strictEqual(_.every([true, null, true], _.identity), false); + var count = 0; + + assert.strictEqual(_.every([true, null, true], function(value) { + count++; + return value; + }), false); + + assert.strictEqual(count, 2); }); QUnit.test('should work with collections of `undefined` values (test in IE < 9)', function(assert) { @@ -16928,6 +16935,19 @@ assert.deepEqual(actual, expected); }); + QUnit.test('should return `true` as soon as `predicate` returns truthy', function(assert) { + assert.expect(2); + + var count = 0; + + assert.strictEqual(_.some([null, true, null], function(value) { + count++; + return value; + }), true); + + assert.strictEqual(count, 2); + }); + QUnit.test('should return `false` if `predicate` returns falsey for all elements', function(assert) { assert.expect(2); @@ -16935,12 +16955,6 @@ assert.strictEqual(_.some([null, 0, ''], _.identity), false); }); - QUnit.test('should return `true` as soon as `predicate` returns truthy', function(assert) { - assert.expect(1); - - assert.strictEqual(_.some([null, true, null], _.identity), true); - }); - QUnit.test('should use `_.identity` when `predicate` is nullish', function(assert) { assert.expect(2);