main
Last change
on this file was d565449, checked in by stefan toskovski <stefantoska84@…>, 4 weeks ago |
Update repo after prototype presentation
|
-
Property mode
set to
100644
|
File size:
723 bytes
|
Line | |
---|
1 | /*! https://mths.be/repeat v1.0.0 by @mathias */
|
---|
2 |
|
---|
3 | 'use strict';
|
---|
4 |
|
---|
5 | var RequireObjectCoercible = require('es-abstract/2019/RequireObjectCoercible');
|
---|
6 | var ToString = require('es-abstract/2019/ToString');
|
---|
7 | var ToInteger = require('es-abstract/2019/ToInteger');
|
---|
8 |
|
---|
9 | module.exports = function repeat(count) {
|
---|
10 | var O = RequireObjectCoercible(this);
|
---|
11 | var string = ToString(O);
|
---|
12 | var n = ToInteger(count);
|
---|
13 | // Account for out-of-bounds indices
|
---|
14 | if (n < 0 || n == Infinity) {
|
---|
15 | throw RangeError('String.prototype.repeat argument must be greater than or equal to 0 and not be Infinity');
|
---|
16 | }
|
---|
17 |
|
---|
18 | var result = '';
|
---|
19 | while (n) {
|
---|
20 | if (n % 2 == 1) {
|
---|
21 | result += string;
|
---|
22 | }
|
---|
23 | if (n > 1) {
|
---|
24 | string += string;
|
---|
25 | }
|
---|
26 | n >>= 1;
|
---|
27 | }
|
---|
28 | return result;
|
---|
29 | };
|
---|
Note:
See
TracBrowser
for help on using the repository browser.