Urara-Blog/node_modules/.pnpm-store/v3/files/e0/48c2c7976ef555b4b91ec999c6d4893947a843f5d6f568d9a464738cf21c499b2224a9d4c222588b2f379bbe35e06335675ced20c893ee736340cd4239819b
2022-08-14 01:14:53 +08:00

37 lines
1 KiB
Text

var arraySampleSize = require('./_arraySampleSize'),
baseSampleSize = require('./_baseSampleSize'),
isArray = require('./isArray'),
isIterateeCall = require('./_isIterateeCall'),
toInteger = require('./toInteger');
/**
* Gets `n` random elements at unique keys from `collection` up to the
* size of `collection`.
*
* @static
* @memberOf _
* @since 4.0.0
* @category Collection
* @param {Array|Object} collection The collection 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(collection, n, guard) {
if ((guard ? isIterateeCall(collection, n, guard) : n === undefined)) {
n = 1;
} else {
n = toInteger(n);
}
var func = isArray(collection) ? arraySampleSize : baseSampleSize;
return func(collection, n);
}
module.exports = sampleSize;