mirror of
https://github.com/whoisclebs/lodash.git
synced 2026-01-31 15:27: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
|
|
||||||
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
|
* @since 2.0.0
|
||||||
* @category Collection
|
* @category Array
|
||||||
* @param {Array|Object} collection The collection to sample.
|
* @param {Array} array The array to sample.
|
||||||
* @returns {*} Returns the random element.
|
* @returns {*} Returns the random element.
|
||||||
* @example
|
* @example
|
||||||
*
|
*
|
||||||
* sample([1, 2, 3, 4])
|
* sample([1, 2, 3, 4])
|
||||||
* // => 2
|
* // => 2
|
||||||
*/
|
*/
|
||||||
function sample(collection) {
|
function sample(array) {
|
||||||
const func = Array.isArray(collection) ? arraySample : baseSample
|
const length = array == null ? 0 : array.length
|
||||||
return func(collection)
|
return length ? array[Math.floor(Math.random() * length)] : undefined
|
||||||
}
|
}
|
||||||
|
|
||||||
export default sample
|
export default sample
|
||||||
|
|||||||
@@ -1,15 +1,13 @@
|
|||||||
import arraySampleSize from './.internal/arraySampleSize.js'
|
|
||||||
import baseSampleSize from './.internal/baseSampleSize.js'
|
|
||||||
import isIterateeCall from './.internal/isIterateeCall.js'
|
import isIterateeCall from './.internal/isIterateeCall.js'
|
||||||
import toInteger from './toInteger.js'
|
import toInteger from './toInteger.js'
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Gets `n` random elements at unique keys from `collection` up to the
|
* Gets `n` random elements at unique keys from `array` up to the
|
||||||
* size of `collection`.
|
* size of `array`.
|
||||||
*
|
*
|
||||||
* @since 4.0.0
|
* @since 4.0.0
|
||||||
* @category Collection
|
* @category Array
|
||||||
* @param {Array|Object} collection The collection to sample.
|
* @param {Array} array The array to sample.
|
||||||
* @param {number} [n=1] The number of elements to sample.
|
* @param {number} [n=1] The number of elements to sample.
|
||||||
* @param- {Object} [guard] Enables use as an iteratee for methods like `map`.
|
* @param- {Object} [guard] Enables use as an iteratee for methods like `map`.
|
||||||
* @returns {Array} Returns the random elements.
|
* @returns {Array} Returns the random elements.
|
||||||
@@ -21,14 +19,27 @@ import toInteger from './toInteger.js'
|
|||||||
* sampleSize([1, 2, 3], 4)
|
* sampleSize([1, 2, 3], 4)
|
||||||
* // => [2, 3, 1]
|
* // => [2, 3, 1]
|
||||||
*/
|
*/
|
||||||
function sampleSize(collection, n, guard) {
|
function sampleSize(array, n, guard) {
|
||||||
if ((guard ? isIterateeCall(collection, n, guard) : n === undefined)) {
|
if ((guard ? isIterateeCall(collection, n, guard) : n === undefined)) {
|
||||||
n = 1
|
n = 1
|
||||||
} else {
|
} else {
|
||||||
n = toInteger(n)
|
n = toInteger(n)
|
||||||
}
|
}
|
||||||
const func = Array.isArray(collection) ? arraySampleSize : baseSampleSize
|
const length = array == null ? 0 : array.length
|
||||||
return func(collection, n)
|
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
|
export default sampleSize
|
||||||
|
|||||||
25
shuffle.js
25
shuffle.js
@@ -1,22 +1,33 @@
|
|||||||
import arrayShuffle from './.internal/arrayShuffle.js'
|
import copyArray from './copyArray.js'
|
||||||
import baseShuffle from './.internal/baseShuffle.js'
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Creates an array of shuffled values, using a version of the
|
* Creates an array of shuffled values, using a version of the
|
||||||
* [Fisher-Yates shuffle](https://en.wikipedia.org/wiki/Fisher-Yates_shuffle).
|
* [Fisher-Yates shuffle](https://en.wikipedia.org/wiki/Fisher-Yates_shuffle).
|
||||||
*
|
*
|
||||||
* @since 0.1.0
|
* @since 0.1.0
|
||||||
* @category Collection
|
* @category Array
|
||||||
* @param {Array|Object} collection The collection to shuffle.
|
* @param {Array} array The array to shuffle.
|
||||||
* @returns {Array} Returns the new shuffled array.
|
* @returns {Array} Returns the new shuffled array.
|
||||||
* @example
|
* @example
|
||||||
*
|
*
|
||||||
* shuffle([1, 2, 3, 4])
|
* shuffle([1, 2, 3, 4])
|
||||||
* // => [4, 1, 3, 2]
|
* // => [4, 1, 3, 2]
|
||||||
*/
|
*/
|
||||||
function shuffle(collection) {
|
function shuffle(array) {
|
||||||
const func = Array.isArray(collection) ? arrayShuffle : baseShuffle
|
const length = array == null ? 0 : array.length
|
||||||
return func(collection)
|
if (!length) {
|
||||||
|
return []
|
||||||
|
}
|
||||||
|
let index = -1
|
||||||
|
const lastIndex = length - 1
|
||||||
|
const result = copyArray(array)
|
||||||
|
while (++index < length) {
|
||||||
|
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 shuffle
|
export default shuffle
|
||||||
|
|||||||
Reference in New Issue
Block a user