Add repeat polyfill for repeating a string
This commit is contained in:
@@ -11,3 +11,31 @@
|
|||||||
export function contains(arr: any[], el: any): boolean {
|
export function contains(arr: any[], el: any): boolean {
|
||||||
return arr.indexOf(el) >= 0;
|
return arr.indexOf(el) >= 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns a string repeated a given number of times
|
||||||
|
* Polyfill from https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/repeat
|
||||||
|
* @param {Number} count The number of times to repeat the string
|
||||||
|
* @param {String} string The string that is to be repeated
|
||||||
|
*/
|
||||||
|
export function repeat(count: number, str: string): string {
|
||||||
|
if (count < 0) throw new RangeError('repeat count must be non-negative');
|
||||||
|
if (count === Infinity) throw new RangeError('repeat count must be less than infinity');
|
||||||
|
|
||||||
|
count = Math.floor(count);
|
||||||
|
if (str.length === 0 || count === 0) return '';
|
||||||
|
|
||||||
|
// Ensuring count is a 31-bit integer allows us to heavily optimize the
|
||||||
|
// main part. But anyway, most current (August 2014) browsers can't handle
|
||||||
|
// strings 1 << 28 chars or longer, so:
|
||||||
|
if (str.length * count >= 1 << 28) {
|
||||||
|
throw new RangeError('repeat count must not overflow maximum string size');
|
||||||
|
}
|
||||||
|
|
||||||
|
let rpt = '';
|
||||||
|
for (let i = 0; i < count; i++) {
|
||||||
|
rpt += str;
|
||||||
|
}
|
||||||
|
|
||||||
|
return rpt;
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user