voca
Version:
The ultimate JavaScript string library
45 lines (37 loc) • 1.05 kB
JavaScript
import { i as isNil } from './internal/is_nil.js';
import './is_string.js';
import { c as coerceToString } from './internal/coerce_to_string.js';
import { c as clipNumber, M as MAX_SAFE_INTEGER, t as toInteger } from './internal/to_integer.js';
/**
* Repeats the `subject` number of `times`.
*
* @function repeat
* @static
* @since 1.0.0
* @memberOf Manipulate
* @param {string} [subject=''] The string to repeat.
* @param {number} [times=1] The number of times to repeat.
* @return {string} Returns the repeated string.
* @example
* v.repeat('w', 3);
* // => 'www'
*
* v.repeat('world', 0);
* // => ''
*/
function repeat(subject, times) {
var subjectString = coerceToString(subject);
var timesInt = isNil(times) ? 1 : clipNumber(toInteger(times), 0, MAX_SAFE_INTEGER);
var repeatString = '';
while (timesInt) {
if (timesInt & 1) {
repeatString += subjectString;
}
if (timesInt > 1) {
subjectString += subjectString;
}
timesInt >>= 1;
}
return repeatString;
}
export default repeat;