mirror of
https://github.com/whoisclebs/lodash.git
synced 2026-02-04 00:57:48 +00:00
Simplify shuffle and sample.
This commit is contained in:
15
sample.js
15
sample.js
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user