Files
lodash/sampleSize.js
2017-02-13 08:01:40 -08:00

47 lines
1.2 KiB
JavaScript

import isIterateeCall from './.internal/isIterateeCall.js'
import copyArray from './.internal/copyArray.js'
import toInteger from './toInteger.js'
/**
* Gets `n` random elements at unique keys from `array` up to the
* size of `array`.
*
* @since 4.0.0
* @category Array
* @param {Array} array The array to sample.
* @param {number} [n=1] The number of elements to sample.
* @param- {Object} [guard] Enables use as an iteratee for methods like `map`.
* @returns {Array} Returns the random elements.
* @example
*
* sampleSize([1, 2, 3], 2)
* // => [3, 1]
*
* sampleSize([1, 2, 3], 4)
* // => [2, 3, 1]
*/
function sampleSize(array, n, guard) {
if ((guard ? isIterateeCall(array, n, guard) : n === undefined)) {
n = 1
} else {
n = toInteger(n)
}
const length = array == null ? 0 : array.length
if (!length || n < 1) {
return []
}
n = n > length ? length : n
let index = -1
const lastIndex = n - 1
const result = copyArray(array)
while (++index < n) {
const rand = index + Math.floor(Math.random() * (lastIndex - index + 1))
const value = result[rand]
result[rand] = result[index]
result[index] = value
}
return result
}
export default sampleSize