repeat-string
Advanced tools
Comparing version 1.0.0 to 1.1.0
@@ -10,8 +10,4 @@ # {%= name %} {%= badge("fury") %} | ||
```js | ||
var repeat = require('{%= name %}'); | ||
{%= apidocs("index.js") %} | ||
repeat('A', 5); | ||
//=> AAAAA | ||
``` | ||
@@ -26,5 +22,6 @@ ## Author | ||
- [assemble](https://github.com/jonschlinkert/assemble) | ||
- [verb](https://github.com/jonschlinkert/verb) | ||
- [remarkable](https://github.com/jonschlinkert/remarkable) | ||
- [less.js](https://github.com/jonschlinkert/less.js) | ||
- [handlebars-helpers](https://github.com/jonschlinkert/handlebars-helpers) | ||
- [verb](https://github.com/jonschlinkert/verb) | ||
- [arr](https://github.com/jonschlinkert/arr) | ||
@@ -31,0 +28,0 @@ - [arr-diff](https://github.com/jonschlinkert/arr-diff) |
{ | ||
"name": "repeat-string", | ||
"version": "0.2.1", | ||
"version": "1.1.0", | ||
"main": [ | ||
"index.js" | ||
], | ||
"authors": [ | ||
{ | ||
"name": "Jon Schlinkert", | ||
"homepage": "http://github.com/jonschlinkert/" | ||
} | ||
] | ||
} |
56
index.js
@@ -10,3 +10,34 @@ /*! | ||
module.exports = function repeat(str, num) { | ||
/** | ||
* Expose `repeat` | ||
*/ | ||
module.exports = repeat; | ||
/** | ||
* Results cache | ||
*/ | ||
var res = ''; | ||
var cache; | ||
/** | ||
* Repeat the given `string` the specified `number` | ||
* of times. | ||
* | ||
* **Example:** | ||
* | ||
* ```js | ||
* var repeat = require('repeat-string'); | ||
* repeat('A', 5); | ||
* //=> AAAAA | ||
* ``` | ||
* | ||
* @param {String} `string` The string to repeat | ||
* @param {Number} `number` The number of times to repeat the string | ||
* @return {String} Repeated string | ||
* @api public | ||
*/ | ||
function repeat(str, num) { | ||
if (typeof str !== 'string') { | ||
@@ -16,8 +47,23 @@ throw new TypeError('repeat-string expects a string.'); | ||
var res = ''; | ||
cache = cache || str; | ||
if (cache !== str) { | ||
res = ''; | ||
} | ||
while (num) { | ||
var max = (str.length * num); | ||
var i = 0; | ||
if (res.length >= max) { | ||
return res.slice(0, max); | ||
} | ||
for (; num > 0; i++) { | ||
if (num & 1) { | ||
res += str; | ||
} | ||
if (res.length >= max) { | ||
return res.slice(0, max); | ||
} | ||
num >>= 1; | ||
@@ -27,3 +73,3 @@ str += str; | ||
return res; | ||
}; | ||
return res.slice(0, max); | ||
} |
{ | ||
"name": "repeat-string", | ||
"description": "Fastest implementation for repeating a string.", | ||
"version": "1.0.0", | ||
"description": "Repeat the given string n times. Fastest implementation for repeating a string.", | ||
"version": "1.1.0", | ||
"homepage": "https://github.com/jonschlinkert/repeat-string", | ||
@@ -32,2 +32,4 @@ "author": { | ||
"keywords": [ | ||
"fast", | ||
"fastest", | ||
"fill", | ||
@@ -34,0 +36,0 @@ "javascript", |
# repeat-string [![NPM version](https://badge.fury.io/js/repeat-string.svg)](http://badge.fury.io/js/repeat-string) | ||
> Fastest implementation for repeating a string. | ||
> Repeat the given string n times. Fastest implementation for repeating a string. | ||
@@ -18,5 +18,14 @@ ## Install with [npm](npmjs.org) | ||
### [repeat](index.js#L41) | ||
Repeat the given `string` the specified `number` of times. | ||
* `string` **{String}**: The string to repeat | ||
* `number` **{Number}**: The number of times to repeat the string | ||
* `returns` **{String}**: Repeated string | ||
**Example:** | ||
```js | ||
var repeat = require('repeat-string'); | ||
repeat('A', 5); | ||
@@ -26,2 +35,4 @@ //=> AAAAA | ||
## Author | ||
@@ -28,0 +39,0 @@ |
@@ -28,7 +28,2 @@ /*! | ||
it('should throw an error when no string is given:', function () { | ||
(function() {repeat(10); }).should.throw('repeat-string expects a string.'); | ||
(function() {repeat(null); }).should.throw('repeat-string expects a string.'); | ||
}); | ||
it('should repeat the given string n times', function () { | ||
@@ -39,4 +34,5 @@ repeat('a', 0).should.equal(''); | ||
repeat('a', 10).should.equal('aaaaaaaaaa'); | ||
repeat('b ', 10).trim().should.equal('b b b b b b b b b b'); | ||
repeat('a ', 10).trim().should.equal('a a a a a a a a a a'); | ||
}); | ||
}); |
10173
100
77