Initial commit
[yaffs-website] / node_modules / grunt-contrib-watch / node_modules / lodash / string / repeat.js
1 var baseToString = require('../internal/baseToString');
2
3 /* Native method references for those with the same name as other `lodash` methods. */
4 var nativeFloor = Math.floor,
5     nativeIsFinite = global.isFinite;
6
7 /**
8  * Repeats the given string `n` times.
9  *
10  * @static
11  * @memberOf _
12  * @category String
13  * @param {string} [string=''] The string to repeat.
14  * @param {number} [n=0] The number of times to repeat the string.
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) {
28   var result = '';
29   string = baseToString(string);
30   n = +n;
31   if (n < 1 || !string || !nativeIsFinite(n)) {
32     return result;
33   }
34   // Leverage the exponentiation by squaring algorithm for a faster repeat.
35   // See https://en.wikipedia.org/wiki/Exponentiation_by_squaring for more details.
36   do {
37     if (n % 2) {
38       result += string;
39     }
40     n = nativeFloor(n / 2);
41     string += string;
42   } while (n);
43
44   return result;
45 }
46
47 module.exports = repeat;