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,15 +0,0 @@
import baseRandom from './baseRandom.js'
/**
* A specialized version of `sample` for arrays.
*
* @private
* @param {Array} array The array to sample.
* @returns {*} Returns the random element.
*/
function arraySample(array) {
const length = array.length
return length ? array[baseRandom(0, length - 1)] : undefined
}
export default arraySample

View File

@@ -1,17 +0,0 @@
import baseClamp from './baseClamp.js'
import copyArray from './copyArray.js'
import shuffleSelf from './shuffleSelf.js'
/**
* A specialized version of `sampleSize` for arrays.
*
* @private
* @param {Array} array The array to sample.
* @param {number} n The number of elements to sample.
* @returns {Array} Returns the random elements.
*/
function arraySampleSize(array, n) {
return shuffleSelf(copyArray(array), baseClamp(n, 0, array.length))
}
export default arraySampleSize

View File

@@ -1,15 +0,0 @@
import copyArray from './copyArray.js'
import shuffleSelf from './shuffleSelf.js'
/**
* A specialized version of `shuffle` for arrays.
*
* @private
* @param {Array} array The array to shuffle.
* @returns {Array} Returns the new shuffled array.
*/
function arrayShuffle(array) {
return shuffleSelf(copyArray(array))
}
export default arrayShuffle

View File

@@ -1,14 +0,0 @@
/**
* The base implementation of `random` without support for returning
* floating-point numbers.
*
* @private
* @param {number} lower The lower bound.
* @param {number} upper The upper bound.
* @returns {number} Returns the random number.
*/
function baseRandom(lower, upper) {
return lower + Math.floor(Math.random() * (upper - lower + 1))
}
export default baseRandom

View File

@@ -1,15 +0,0 @@
import arraySample from './arraySample.js'
import values from '../values.js'
/**
* The base implementation of `sample`.
*
* @private
* @param {Array|Object} collection The collection to sample.
* @returns {*} Returns the random element.
*/
function baseSample(collection) {
return arraySample(values(collection))
}
export default baseSample

View File

@@ -1,18 +0,0 @@
import baseClamp from './baseClamp.js'
import shuffleSelf from './shuffleSelf.js'
import values from '../values.js'
/**
* The base implementation of `sampleSize` without param guards.
*
* @private
* @param {Array|Object} collection The collection to sample.
* @param {number} n The number of elements to sample.
* @returns {Array} Returns the random elements.
*/
function baseSampleSize(collection, n) {
const array = values(collection)
return shuffleSelf(array, baseClamp(n, 0, array.length))
}
export default baseSampleSize

View File

@@ -1,15 +0,0 @@
import shuffleSelf from './shuffleSelf.js'
import values from '../values.js'
/**
* The base implementation of `shuffle`.
*
* @private
* @param {Array|Object} collection The collection to shuffle.
* @returns {Array} Returns the new shuffled array.
*/
function baseShuffle(collection) {
return shuffleSelf(values(collection))
}
export default baseShuffle

View File

@@ -1,28 +0,0 @@
import baseRandom from './baseRandom.js'
/**
* A specialized version of `shuffle` which mutates and sets the size of `array`.
*
* @private
* @param {Array} array The array to shuffle.
* @param {number} [size=array.length] The size of `array`.
* @returns {Array} Returns `array`.
*/
function shuffleSelf(array, size) {
let index = -1
const length = array.length
const lastIndex = length - 1
size = size === undefined ? length : size
while (++index < size) {
const rand = baseRandom(index, lastIndex)
const value = array[rand]
array[rand] = array[index]
array[index] = value
}
array.length = size
return array
}
export default shuffleSelf