toArray.js 697 B

1234567891011121314151617181920212223242526272829303132
  1. var arrayCopy = require('../internal/arrayCopy'),
  2. getLength = require('../internal/getLength'),
  3. isLength = require('../internal/isLength'),
  4. values = require('../object/values');
  5. /**
  6. * Converts `value` to an array.
  7. *
  8. * @static
  9. * @memberOf _
  10. * @category Lang
  11. * @param {*} value The value to convert.
  12. * @returns {Array} Returns the converted array.
  13. * @example
  14. *
  15. * (function() {
  16. * return _.toArray(arguments).slice(1);
  17. * }(1, 2, 3));
  18. * // => [2, 3]
  19. */
  20. function toArray(value) {
  21. var length = value ? getLength(value) : 0;
  22. if (!isLength(length)) {
  23. return values(value);
  24. }
  25. if (!length) {
  26. return [];
  27. }
  28. return arrayCopy(value);
  29. }
  30. module.exports = toArray;