string.prototype.repeat
Advanced tools
Comparing version
{ | ||
"name": "string.prototype.repeat", | ||
"version": "0.1.0", | ||
"version": "0.2.0", | ||
"description": "A robust & optimized `String.prototype.repeat` polyfill, based on the ECMAScript 6 specification.", | ||
@@ -5,0 +5,0 @@ "homepage": "http://mths.be/repeat", |
@@ -1,2 +0,2 @@ | ||
# ES6 `String.prototype.repeat` polyfill [](https://travis-ci.org/mathiasbynens/String.prototype.repeat) | ||
# ES6 `String.prototype.repeat` polyfill [](https://travis-ci.org/mathiasbynens/String.prototype.repeat) | ||
@@ -35,3 +35,3 @@ A robust & optimized ES3-compatible polyfill for [the `String.prototype.repeat` method in ECMAScript 6](http://people.mozilla.org/~jorendorff/es6-draft.html#sec-string.prototype.repeat). | ||
| [](http://twitter.com/mathias "Follow @mathias on Twitter") | | ||
| [](https://twitter.com/mathias "Follow @mathias on Twitter") | | ||
|---| | ||
@@ -38,0 +38,0 @@ | [Mathias Bynens](http://mathiasbynens.be/) | |
@@ -1,5 +0,14 @@ | ||
/*! http://mths.be/repeat v0.1.0 by @mathias */ | ||
/*! http://mths.be/repeat v0.2.0 by @mathias */ | ||
if (!String.prototype.repeat) { | ||
(function() { | ||
'use strict'; // needed to support `apply`/`call` with `undefined`/`null` | ||
var defineProperty = (function() { | ||
// IE 8 only supports `Object.defineProperty` on DOM elements | ||
try { | ||
var object = {}; | ||
var $defineProperty = Object.defineProperty; | ||
var result = $defineProperty(object, object, object) && $defineProperty; | ||
} catch(error) {} | ||
return result; | ||
}()); | ||
var repeat = function(count) { | ||
@@ -19,13 +28,16 @@ if (this == null) { | ||
} | ||
if (n == 0) { | ||
return ''; | ||
} | ||
var result = ''; | ||
while (n--) { | ||
result += string; | ||
while (n) { | ||
if (n % 2 == 1) { | ||
result += string; | ||
} | ||
if (n > 1) { | ||
string += string; | ||
} | ||
n >>= 1; | ||
} | ||
return result; | ||
}; | ||
if (Object.defineProperty) { | ||
Object.defineProperty(String.prototype, 'repeat', { | ||
if (defineProperty) { | ||
defineProperty(String.prototype, 'repeat', { | ||
'value': repeat, | ||
@@ -32,0 +44,0 @@ 'configurable': true, |
4551
8.13%50
31.58%