main
Last change
on this file was d24f17c, checked in by Aleksandar Panovski <apano77@…>, 15 months ago |
Initial commit
|
-
Property mode
set to
100644
|
File size:
1.2 KB
|
Rev | Line | |
---|
[d24f17c] | 1 | import isNotFinite from '../../isNotFinite';
|
---|
| 2 | import isNegative from '../../isNegative';
|
---|
| 3 | var repeat = function repeat(value, count) {
|
---|
| 4 | var validCount = Number(count);
|
---|
| 5 | if (validCount !== count) {
|
---|
| 6 | validCount = 0;
|
---|
| 7 | }
|
---|
| 8 | if (isNegative(validCount)) {
|
---|
| 9 | throw new RangeError('repeat count must be non-negative');
|
---|
| 10 | }
|
---|
| 11 | if (isNotFinite(validCount)) {
|
---|
| 12 | throw new RangeError('repeat count must be less than infinity');
|
---|
| 13 | }
|
---|
| 14 | validCount = Math.floor(validCount);
|
---|
| 15 | if (value.length === 0 || validCount === 0) {
|
---|
| 16 | return '';
|
---|
| 17 | }
|
---|
| 18 |
|
---|
| 19 | // Ensuring validCount is a 31-bit integer allows us to heavily optimize the
|
---|
| 20 | // main part. But anyway, most current (August 2014) browsers can't handle
|
---|
| 21 | // strings 1 << 28 chars or longer, so:
|
---|
| 22 | // eslint-disable-next-line no-bitwise
|
---|
| 23 | if (value.length * validCount >= 1 << 28) {
|
---|
| 24 | throw new RangeError('repeat count must not overflow maximum string size');
|
---|
| 25 | }
|
---|
| 26 | var maxCount = value.length * validCount;
|
---|
| 27 | validCount = Math.floor(Math.log(validCount) / Math.log(2));
|
---|
| 28 | var result = value;
|
---|
| 29 | while (validCount) {
|
---|
| 30 | result += value;
|
---|
| 31 | validCount -= 1;
|
---|
| 32 | }
|
---|
| 33 | result += result.substring(0, maxCount - result.length);
|
---|
| 34 | return result;
|
---|
| 35 | };
|
---|
| 36 | export default repeat; |
---|
Note:
See
TracBrowser
for help on using the repository browser.