source: frontend/node_modules/underscore/modules/sample.js

Last change on this file was 9af201e, checked in by MBK <marija.karapandzova@…>, 11 days ago

Fix frontend appearance

  • Property mode set to 100644
File size: 978 bytes
Line 
1import isArrayLike from './_isArrayLike.js';
2import values from './values.js';
3import getLength from './_getLength.js';
4import random from './random.js';
5import toArray from './toArray.js';
6
7// Sample **n** random values from a collection using the modern version of the
8// [Fisher-Yates shuffle](https://en.wikipedia.org/wiki/Fisher–Yates_shuffle).
9// If **n** is not specified, returns a single random element.
10// The internal `guard` argument allows it to work with `_.map`.
11export default function sample(obj, n, guard) {
12 if (n == null || guard) {
13 if (!isArrayLike(obj)) obj = values(obj);
14 return obj[random(obj.length - 1)];
15 }
16 var sample = toArray(obj);
17 var length = getLength(sample);
18 n = Math.max(Math.min(n, length), 0);
19 var last = length - 1;
20 for (var index = 0; index < n; index++) {
21 var rand = random(index, last);
22 var temp = sample[index];
23 sample[index] = sample[rand];
24 sample[rand] = temp;
25 }
26 return sample.slice(0, n);
27}
Note: See TracBrowser for help on using the repository browser.