You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

44 lines
768 B

4 years ago
  1. var isArray = require('./isArray');
  2. /**
  3. * Casts `value` as an array if it's not one.
  4. *
  5. * @static
  6. * @memberOf _
  7. * @since 4.4.0
  8. * @category Lang
  9. * @param {*} value The value to inspect.
  10. * @returns {Array} Returns the cast array.
  11. * @example
  12. *
  13. * _.castArray(1);
  14. * // => [1]
  15. *
  16. * _.castArray({ 'a': 1 });
  17. * // => [{ 'a': 1 }]
  18. *
  19. * _.castArray('abc');
  20. * // => ['abc']
  21. *
  22. * _.castArray(null);
  23. * // => [null]
  24. *
  25. * _.castArray(undefined);
  26. * // => [undefined]
  27. *
  28. * _.castArray();
  29. * // => []
  30. *
  31. * var array = [1, 2, 3];
  32. * console.log(_.castArray(array) === array);
  33. * // => true
  34. */
  35. function castArray() {
  36. if (!arguments.length) {
  37. return [];
  38. }
  39. var value = arguments[0];
  40. return isArray(value) ? value : [value];
  41. }
  42. module.exports = castArray;