repeat.js 893 B

1234567891011121314151617181920212223242526272829303132333435363738
  1. var baseRepeat = require('./_baseRepeat'),
  2. isIterateeCall = require('./_isIterateeCall'),
  3. toInteger = require('./toInteger'),
  4. toString = require('./toString');
  5. /**
  6. * Repeats the given string `n` times.
  7. *
  8. * @static
  9. * @memberOf _
  10. * @since 3.0.0
  11. * @category String
  12. * @param {string} [string=''] The string to repeat.
  13. * @param {number} [n=1] The number of times to repeat the string.
  14. * @param- {Object} [guard] Enables use as an iteratee for methods like `_.map`.
  15. * @returns {string} Returns the repeated string.
  16. * @example
  17. *
  18. * _.repeat('*', 3);
  19. * // => '***'
  20. *
  21. * _.repeat('abc', 2);
  22. * // => 'abcabc'
  23. *
  24. * _.repeat('abc', 0);
  25. * // => ''
  26. */
  27. function repeat(string, n, guard) {
  28. if ((guard ? isIterateeCall(string, n, guard) : n === undefined)) {
  29. n = 1;
  30. } else {
  31. n = toInteger(n);
  32. }
  33. return baseRepeat(toString(string), n);
  34. }
  35. module.exports = repeat;