repeat
string
Repeats the given string n times.
Installation
Import
import { repeat } from '@tulx/utils';Source Code
Implementation
/**
* Repeats the given string n times.
*
* @param string - The string to repeat.
* @param n - The number of times to repeat the string.
* @returns Returns the repeated string.
*
* @example
* ```ts
* repeat('*', 3); // '***'
* repeat('abc', 2); // 'abcabc'
* repeat('abc', 0); // ''
* ```
*/
export function repeat(string: string, n: number = 1): string {
if (n <= 0) {
return '';
}
return string.repeat(n);
}
Example
import { repeat } from '@tulx/utils';
repeat('*', 3); // '***'
repeat('abc', 2); // 'abcabc'
repeat('abc', 0); // ''Related Functions
camelCase
Converts string to camel case.
capitalize
Converts the first character of string to upper case and the remaining to lower case.
deburr
Deburrs string by converting Latin-1 Supplement and Latin Extended-A letters to basic Latin letters and removing combining diacritical marks.
endsWith
Checks if string ends with the given target string.
escape
Converts the characters "&", "<", ">", '"', and "'" in string to their corresponding HTML entities.
escapeRegExp
Escapes the RegExp special characters "^", "$", "\", ".", "*", "+", "?", "(", ")", "[", "]", "{", "}", and "|" in string.