Split _.sample out to arraySample.

This commit is contained in:
John-David Dalton
2016-09-01 20:03:40 -07:00
parent 212e4a6407
commit 4a5f8fa974

View File

@@ -712,6 +712,19 @@
return accumulator; return accumulator;
} }
/**
* A specialized version of `_.sample` for arrays without support for iteratee
* shorthands.
*
* @private
* @param {Array} array The array to sample.
* @returns {*} Returns the random element.
*/
function arraySample(array) {
var length = array.length;
return length ? array[baseRandom(0, length - 1)] : undefined;
}
/** /**
* A specialized version of `_.some` for arrays without support for iteratee * A specialized version of `_.some` for arrays without support for iteratee
* shorthands. * shorthands.
@@ -9575,10 +9588,7 @@
* // => 2 * // => 2
*/ */
function sample(collection) { function sample(collection) {
var array = isArrayLike(collection) ? collection : values(collection), return arraySample(isArrayLike(collection) ? collection : values(collection));
length = array.length;
return length > 0 ? array[baseRandom(0, length - 1)] : undefined;
} }
/** /**