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

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

Fix frontend appearance

  • Property mode set to 100644
File size: 1008 bytes
Line 
1var _isArrayLike = require('./_isArrayLike.js');
2var values = require('./values.js');
3var _getLength = require('./_getLength.js');
4var random = require('./random.js');
5var toArray = require('./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`.
11function 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}
28
29module.exports = sample;
Note: See TracBrowser for help on using the repository browser.