source: node_modules/ramda-adjunct/src/internal/ponyfills/String.repeat.js@ d24f17c

main
Last change on this file since d24f17c was d24f17c, checked in by Aleksandar Panovski <apano77@…>, 15 months ago

Initial commit

  • Property mode set to 100644
File size: 1.2 KB
Line 
1import isNotFinite from '../../isNotFinite';
2import isNegative from '../../isNegative';
3
4const repeat = (value, count) => {
5 let validCount = Number(count);
6
7 if (validCount !== count) {
8 validCount = 0;
9 }
10
11 if (isNegative(validCount)) {
12 throw new RangeError('repeat count must be non-negative');
13 }
14
15 if (isNotFinite(validCount)) {
16 throw new RangeError('repeat count must be less than infinity');
17 }
18
19 validCount = Math.floor(validCount);
20
21 if (value.length === 0 || validCount === 0) {
22 return '';
23 }
24
25 // Ensuring validCount is a 31-bit integer allows us to heavily optimize the
26 // main part. But anyway, most current (August 2014) browsers can't handle
27 // strings 1 << 28 chars or longer, so:
28 // eslint-disable-next-line no-bitwise
29 if (value.length * validCount >= 1 << 28) {
30 throw new RangeError('repeat count must not overflow maximum string size');
31 }
32
33 const maxCount = value.length * validCount;
34
35 validCount = Math.floor(Math.log(validCount) / Math.log(2));
36
37 let result = value;
38
39 while (validCount) {
40 result += value;
41 validCount -= 1;
42 }
43
44 result += result.substring(0, maxCount - result.length);
45
46 return result;
47};
48
49export default repeat;
Note: See TracBrowser for help on using the repository browser.