Table of Contents
String Repeat
Repeat a string.
Install
npm i string-repeater --save
Usage
var repeat = str = ;// 'foo foo foo foo foo '
Or if you prefer to polyfill String.prototype
:
var repeat = ;Stringprototyperepeat = Stringprototyperepeat || repeatimpl;
Benchmark
string-repeater x 4439603 ops/sec ±215% 85 runs sampledstring-repeat x 60621 ops/sec ±463% 81 runs sampledstringprototyperepeat x 4071996 ops/sec ±214% 84 runs sampled
Source
"use strict" /** * Repeat a string. * * @param input The string to repeat. * @param times The number of times to repeat. * * @return A new repeated string. */ { return impl;} /** * Prototype implementation called with the string as the scope. * * Note that this implementation: * * return new Array(Math.abs(times) + 1).join(this); * * Is very, very slow. * * This implementation: * * var ret = ''; * for(var i = 0; i < times; i++) { * ret += this; * } * return ret; * * Is faster than `string-repeat` but slower than `string.prototype.repeat`. * * @param times The number of times to repeat. * * @return A new repeated string. */ { // conditional is faster than Math.abs() var n = times < 0 ? -times : times result = '' string = '' + this; // optimized loop from string.prototype.repeat whilen ifn % 2 === 1 result += string; ifn > 1 string += string; n >>= 1; return result;} repeatimpl = impl; moduleexports = repeat;
Developer
Test
To run the test suite:
npm test
Cover
To generate code coverage run:
npm run cover
Lint
Run the source tree through jshint and jscs:
npm run lint
Clean
Remove generated files:
npm run clean
Readme
To build the readme file from the partial definitions:
npm run readme
Generated by mdp(1).