fill.js 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. var baseFill = require('./_baseFill'),
  2. isIterateeCall = require('./_isIterateeCall');
  3. /**
  4. * Fills elements of `array` with `value` from `start` up to, but not
  5. * including, `end`.
  6. *
  7. * **Note:** This method mutates `array`.
  8. *
  9. * @static
  10. * @memberOf _
  11. * @since 3.2.0
  12. * @category Array
  13. * @param {Array} array The array to fill.
  14. * @param {*} value The value to fill `array` with.
  15. * @param {number} [start=0] The start position.
  16. * @param {number} [end=array.length] The end position.
  17. * @returns {Array} Returns `array`.
  18. * @example
  19. *
  20. * var array = [1, 2, 3];
  21. *
  22. * _.fill(array, 'a');
  23. * console.log(array);
  24. * // => ['a', 'a', 'a']
  25. *
  26. * _.fill(Array(3), 2);
  27. * // => [2, 2, 2]
  28. *
  29. * _.fill([4, 6, 8, 10], '*', 1, 3);
  30. * // => [4, '*', '*', 10]
  31. */
  32. function fill(array, value, start, end) {
  33. var length = array == null ? 0 : array.length;
  34. if (!length) {
  35. return [];
  36. }
  37. if (start && typeof start != 'number' && isIterateeCall(array, value, start)) {
  38. start = 0;
  39. end = length;
  40. }
  41. return baseFill(array, value, start, end);
  42. }
  43. module.exports = fill;