Simplify shuffle and sample.

This commit is contained in:
John-David Dalton
2017-02-06 23:53:17 -08:00
parent b0980a90fc
commit cb7612aef6
11 changed files with 44 additions and 162 deletions

View File

@@ -1,21 +1,18 @@
import arraySample from './.internal/arraySample.js'
import baseSample from './.internal/baseSample.js'
/**
* Gets a random element from `collection`.
* Gets a random element from `array`.
*
* @since 2.0.0
* @category Collection
* @param {Array|Object} collection The collection to sample.
* @category Array
* @param {Array} array The array to sample.
* @returns {*} Returns the random element.
* @example
*
* sample([1, 2, 3, 4])
* // => 2
*/
function sample(collection) {
const func = Array.isArray(collection) ? arraySample : baseSample
return func(collection)
function sample(array) {
const length = array == null ? 0 : array.length
return length ? array[Math.floor(Math.random() * length)] : undefined
}
export default sample