1a4d813695f6dfa61243bd4ff2b8b83ef35a7f9d
[yaffs-website] / node_modules / grunt-legacy-log-utils / node_modules / lodash / sample.js
1 var baseRandom = require('./_baseRandom'),
2     isArrayLike = require('./isArrayLike'),
3     values = require('./values');
4
5 /**
6  * Gets a random element from `collection`.
7  *
8  * @static
9  * @memberOf _
10  * @category Collection
11  * @param {Array|Object} collection The collection to sample.
12  * @returns {*} Returns the random element.
13  * @example
14  *
15  * _.sample([1, 2, 3, 4]);
16  * // => 2
17  */
18 function sample(collection) {
19   var array = isArrayLike(collection) ? collection : values(collection),
20       length = array.length;
21
22   return length > 0 ? array[baseRandom(0, length - 1)] : undefined;
23 }
24
25 module.exports = sample;