UNPKG

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