c781229b2cd07ef621f141de4878755ca9be092b
[platform/framework/web/crosswalk-tizen.git] /
1 /*!
2  * repeat-string <https://github.com/jonschlinkert/repeat-string>
3  *
4  * Copyright (c) 2014-2015, Jon Schlinkert.
5  * Licensed under the MIT License.
6  */
7
8 'use strict';
9
10 /**
11  * Expose `repeat`
12  */
13
14 module.exports = repeat;
15
16 /**
17  * Repeat the given `string` the specified `number`
18  * of times.
19  *
20  * **Example:**
21  *
22  * ```js
23  * var repeat = require('repeat-string');
24  * repeat('A', 5);
25  * //=> AAAAA
26  * ```
27  *
28  * @param {String} `string` The string to repeat
29  * @param {Number} `number` The number of times to repeat the string
30  * @return {String} Repeated string
31  * @api public
32  */
33
34 function repeat(str, num) {
35   if (typeof str !== 'string') {
36     throw new TypeError('repeat-string expects a string.');
37   }
38
39   if (num === 1) return str;
40   if (num === 2) return str + str;
41
42   var max = str.length * num;
43   if (cache !== str || typeof cache === 'undefined') {
44     cache = str;
45     res = '';
46   }
47
48   while (max > res.length && num > 0) {
49     if (num & 1) {
50       res += str;
51     }
52
53     num >>= 1;
54     if (!num) break;
55     str += str;
56   }
57
58   return res.substr(0, max);
59 }
60
61 /**
62  * Results cache
63  */
64
65 var res = '';
66 var cache;