From 8a5eb89aa879e977812570d251987af222a8a26e Mon Sep 17 00:00:00 2001 From: John-David Dalton Date: Fri, 25 May 2012 15:15:16 -0400 Subject: [PATCH] Ensure `_.find` returns `undefined`when a value cannot be found. [closes #15] Former-commit-id: e6dc89f98a1df81e1b1d67c5e8f5725e4df3bc5a --- lodash.js | 1 + test/test.js | 16 ++++++++++++++++ 2 files changed, 17 insertions(+) diff --git a/lodash.js b/lodash.js index cc73224bd..bfadc4c4d 100644 --- a/lodash.js +++ b/lodash.js @@ -585,6 +585,7 @@ * // => 2 */ var find = createIterator(baseIteratorOptions, { + 'init': '', 'inLoop': 'if (callback(collection[index], index, collection)) return collection[index]' }); diff --git a/test/test.js b/test/test.js index 2036fa49b..9eb1b1844 100644 --- a/test/test.js +++ b/test/test.js @@ -174,6 +174,22 @@ /*--------------------------------------------------------------------------*/ + QUnit.module('lodash.find'); + + (function() { + var array = [1, 2, 3, 4]; + + test('should return found `value`', function() { + equal(_.find(array, function(n) { return n > 2; }), 3); + }); + + test('should return `undefined` if `value` is not found', function() { + equal(_.find(array, function(n) { return n == 5; }), undefined); + }); + }()); + + /*--------------------------------------------------------------------------*/ + QUnit.module('lodash.flatten'); (function() {