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