Changed to function as documented. (#3596)

This commit is contained in:
Oscar Hallgren
2018-01-20 01:05:02 +01:00
committed by John-David Dalton
parent 75690245aa
commit 77c128fc20

View File

@@ -1,4 +1,5 @@
import copyArray from './.internal/copyArray.js'
import slice from './slice.js'
/**
* Gets `n` random elements at unique keys from `array` up to the
@@ -18,13 +19,14 @@ import copyArray from './.internal/copyArray.js'
* // => [2, 3, 1]
*/
function sampleSize(array, n) {
n = n == null ? 1 : 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 lastIndex = length - 1
const result = copyArray(array)
while (++index < n) {
const rand = index + Math.floor(Math.random() * (lastIndex - index + 1))
@@ -32,7 +34,7 @@ function sampleSize(array, n) {
result[rand] = result[index]
result[index] = value
}
return result
return slice(result, 0, n);
}
export default sampleSize