sample.js 549 B

123456789101112131415161718192021222324
  1. import arraySample from './_arraySample.js';
  2. import baseSample from './_baseSample.js';
  3. import isArray from './isArray.js';
  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. export default sample;