sample.js 551B

12345678910111213141516171819202122232425
  1. var arraySample = require('./_arraySample'),
  2. baseSample = require('./_baseSample'),
  3. isArray = require('./isArray');
  4. /**
  5. * Gets a random element from `collection`.
  6. *
  7. * @static
  8. * @memberOf _
  9. * @since 2.0.0
  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 func = isArray(collection) ? arraySample : baseSample;
  20. return func(collection);
  21. }
  22. module.exports = sample;