1 | import baseClamp from './_baseClamp';
|
2 | import baseRandom from './_baseRandom';
|
3 | import isIterateeCall from './_isIterateeCall';
|
4 | import toArray from './toArray';
|
5 | import toInteger from './toInteger';
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 |
|
19 |
|
20 |
|
21 |
|
22 |
|
23 |
|
24 |
|
25 |
|
26 |
|
27 | function sampleSize(collection, n, guard) {
|
28 | var index = -1,
|
29 | result = toArray(collection),
|
30 | length = result.length,
|
31 | lastIndex = length - 1;
|
32 |
|
33 | if ((guard ? isIterateeCall(collection, n, guard) : n === undefined)) {
|
34 | n = 1;
|
35 | } else {
|
36 | n = baseClamp(toInteger(n), 0, length);
|
37 | }
|
38 | while (++index < n) {
|
39 | var rand = baseRandom(index, lastIndex),
|
40 | value = result[rand];
|
41 |
|
42 | result[rand] = result[index];
|
43 | result[index] = value;
|
44 | }
|
45 | result.length = n;
|
46 | return result;
|
47 | }
|
48 |
|
49 | export default sampleSize;
|