From 8396ed31676cbfdec2e805ade46773226f164257 Mon Sep 17 00:00:00 2001 From: John-David Dalton Date: Tue, 1 May 2012 10:47:15 -0400 Subject: [PATCH] lodash: Simplify first argument checks in "Collections" methods. [jddalton] Former-commit-id: f3b658acccc20d864cf4987f2d2473453297a1c8 --- lodash.js | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/lodash.js b/lodash.js index a8e08578c..d60d59d8c 100644 --- a/lodash.js +++ b/lodash.js @@ -119,9 +119,11 @@ /** Compilation options for `_.map` */ var mapFactoryOptions = { - 'init': '[]', + 'init': '', + 'exit': 'if (!collection) return []', 'beforeLoop': { - 'array': 'result=Array(length)' + 'array': 'result=Array(length)', + 'object': 'result=[]' }, 'inLoop': { 'array': 'result[index] = callback(collection[index], index, collection)', @@ -274,7 +276,7 @@ // assign the `result` variable an initial value ('var index, result' + (init ? '=' + init : '')) + ';\n' + // add code to exit early or do so if the first argument is nullish - (options.exit || 'if (' + firstArg + ' == undefined) return result') + ';\n' + + (options.exit || 'if (!' + firstArg + ') return result') + ';\n' + // add code after the exit snippet but before the iteration branches (options.top || '') + ';\n' + // the following branch is for iterating arrays and array-like objects @@ -650,7 +652,7 @@ * // => [4, 5, 2, 3, 0, 1] */ function reduceRight(collection, callback, result, thisArg) { - if (collection == undefined) { + if (!collection) { return result; } @@ -986,7 +988,7 @@ */ function indexOf(array, value, isSorted) { var index, length; - if (array == undefined) { + if (!array) { return -1; } if (isSorted) { @@ -1121,7 +1123,7 @@ * // => 4 */ function lastIndexOf(array, value) { - if (array == undefined) { + if (!array) { return -1; } var index = array.length; @@ -2480,7 +2482,7 @@ * // => 'nonsense' */ function result(object, property) { - if (object == undefined) { + if (!object) { return null; } var value = object[property];