diff --git a/lodash.js b/lodash.js index 063e111a0..bc2674b3d 100644 --- a/lodash.js +++ b/lodash.js @@ -712,6 +712,19 @@ 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 * shorthands. @@ -9575,10 +9588,7 @@ * // => 2 */ function sample(collection) { - var array = isArrayLike(collection) ? collection : values(collection), - length = array.length; - - return length > 0 ? array[baseRandom(0, length - 1)] : undefined; + return arraySample(isArrayLike(collection) ? collection : values(collection)); } /**