main
Last change
on this file since d565449 was d565449, checked in by stefan toskovski <stefantoska84@…>, 4 weeks ago |
Update repo after prototype presentation
|
-
Property mode
set to
100644
|
File size:
1.0 KB
|
Rev | Line | |
---|
[d565449] | 1 | import arraySampleSize from './_arraySampleSize.js';
|
---|
| 2 | import baseSampleSize from './_baseSampleSize.js';
|
---|
| 3 | import isArray from './isArray.js';
|
---|
| 4 | import isIterateeCall from './_isIterateeCall.js';
|
---|
| 5 | import toInteger from './toInteger.js';
|
---|
| 6 |
|
---|
| 7 | /**
|
---|
| 8 | * Gets `n` random elements at unique keys from `collection` up to the
|
---|
| 9 | * size of `collection`.
|
---|
| 10 | *
|
---|
| 11 | * @static
|
---|
| 12 | * @memberOf _
|
---|
| 13 | * @since 4.0.0
|
---|
| 14 | * @category Collection
|
---|
| 15 | * @param {Array|Object} collection The collection to sample.
|
---|
| 16 | * @param {number} [n=1] The number of elements to sample.
|
---|
| 17 | * @param- {Object} [guard] Enables use as an iteratee for methods like `_.map`.
|
---|
| 18 | * @returns {Array} Returns the random elements.
|
---|
| 19 | * @example
|
---|
| 20 | *
|
---|
| 21 | * _.sampleSize([1, 2, 3], 2);
|
---|
| 22 | * // => [3, 1]
|
---|
| 23 | *
|
---|
| 24 | * _.sampleSize([1, 2, 3], 4);
|
---|
| 25 | * // => [2, 3, 1]
|
---|
| 26 | */
|
---|
| 27 | function sampleSize(collection, n, guard) {
|
---|
| 28 | if ((guard ? isIterateeCall(collection, n, guard) : n === undefined)) {
|
---|
| 29 | n = 1;
|
---|
| 30 | } else {
|
---|
| 31 | n = toInteger(n);
|
---|
| 32 | }
|
---|
| 33 | var func = isArray(collection) ? arraySampleSize : baseSampleSize;
|
---|
| 34 | return func(collection, n);
|
---|
| 35 | }
|
---|
| 36 |
|
---|
| 37 | export default sampleSize;
|
---|
Note:
See
TracBrowser
for help on using the repository browser.