lodash: Simplify first argument checks in "Collections" methods. [jddalton]

Former-commit-id: f3b658acccc20d864cf4987f2d2473453297a1c8
This commit is contained in:
John-David Dalton
2012-05-01 10:47:15 -04:00
parent 6cc360d613
commit 8396ed3167

View File

@@ -119,9 +119,11 @@
/** Compilation options for `_.map` */ /** Compilation options for `_.map` */
var mapFactoryOptions = { var mapFactoryOptions = {
'init': '[]', 'init': '',
'exit': 'if (!collection) return []',
'beforeLoop': { 'beforeLoop': {
'array': 'result=Array(length)' 'array': 'result=Array(length)',
'object': 'result=[]'
}, },
'inLoop': { 'inLoop': {
'array': 'result[index] = callback(collection[index], index, collection)', 'array': 'result[index] = callback(collection[index], index, collection)',
@@ -274,7 +276,7 @@
// assign the `result` variable an initial value // assign the `result` variable an initial value
('var index, result' + (init ? '=' + init : '')) + ';\n' + ('var index, result' + (init ? '=' + init : '')) + ';\n' +
// add code to exit early or do so if the first argument is nullish // 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 // add code after the exit snippet but before the iteration branches
(options.top || '') + ';\n' + (options.top || '') + ';\n' +
// the following branch is for iterating arrays and array-like objects // the following branch is for iterating arrays and array-like objects
@@ -650,7 +652,7 @@
* // => [4, 5, 2, 3, 0, 1] * // => [4, 5, 2, 3, 0, 1]
*/ */
function reduceRight(collection, callback, result, thisArg) { function reduceRight(collection, callback, result, thisArg) {
if (collection == undefined) { if (!collection) {
return result; return result;
} }
@@ -986,7 +988,7 @@
*/ */
function indexOf(array, value, isSorted) { function indexOf(array, value, isSorted) {
var index, length; var index, length;
if (array == undefined) { if (!array) {
return -1; return -1;
} }
if (isSorted) { if (isSorted) {
@@ -1121,7 +1123,7 @@
* // => 4 * // => 4
*/ */
function lastIndexOf(array, value) { function lastIndexOf(array, value) {
if (array == undefined) { if (!array) {
return -1; return -1;
} }
var index = array.length; var index = array.length;
@@ -2480,7 +2482,7 @@
* // => 'nonsense' * // => 'nonsense'
*/ */
function result(object, property) { function result(object, property) {
if (object == undefined) { if (!object) {
return null; return null;
} }
var value = object[property]; var value = object[property];