* Repeats the given string `n` times. * * @static * @memberOf _ * @category String * @param {string} [string=''] The string to repeat. * @param {number} [n=0] The number of times to repeat the string. * @returns {string} Returns the repeated string. * @example
(string, n)
| 11891 | * // => '' |
| 11892 | */ |
| 11893 | function repeat(string, n) { |
| 11894 | var result = ''; |
| 11895 | string = baseToString(string); |
| 11896 | n = +n; |
| 11897 | if (n < 1 || !string || !nativeIsFinite(n)) { |
| 11898 | return result; |
| 11899 | } |
| 11900 | // Leverage the exponentiation by squaring algorithm for a faster repeat. |
| 11901 | // See https://en.wikipedia.org/wiki/Exponentiation_by_squaring for more details. |
| 11902 | do { |
| 11903 | if (n % 2) { |
| 11904 | result += string; |
| 11905 | } |
| 11906 | n = floor(n / 2); |
| 11907 | string += string; |
| 11908 | } while (n); |
| 11909 | |
| 11910 | return result; |
| 11911 | } |
| 11912 | |
| 11913 | /** |
| 11914 | * Converts `string` to [snake case](https://en.wikipedia.org/wiki/Snake_case). |
no test coverage detected