Initial commit
[yaffs-website] / node_modules / repeat-string / index.js
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  * Results cache
12  */
13
14 var res = '';
15 var cache;
16
17 /**
18  * Expose `repeat`
19  */
20
21 module.exports = repeat;
22
23 /**
24  * Repeat the given `string` the specified `number`
25  * of times.
26  *
27  * **Example:**
28  *
29  * ```js
30  * var repeat = require('repeat-string');
31  * repeat('A', 5);
32  * //=> AAAAA
33  * ```
34  *
35  * @param {String} `string` The string to repeat
36  * @param {Number} `number` The number of times to repeat the string
37  * @return {String} Repeated string
38  * @api public
39  */
40
41 function repeat(str, num) {
42   if (typeof str !== 'string') {
43     throw new TypeError('expected a string');
44   }
45
46   // cover common, quick use cases
47   if (num === 1) return str;
48   if (num === 2) return str + str;
49
50   var max = str.length * num;
51   if (cache !== str || typeof cache === 'undefined') {
52     cache = str;
53     res = '';
54   } else if (res.length >= max) {
55     return res.substr(0, max);
56   }
57
58   while (max > res.length && num > 1) {
59     if (num & 1) {
60       res += str;
61     }
62
63     num >>= 1;
64     str += str;
65   }
66
67   res += str;
68   res = res.substr(0, max);
69   return res;
70 }