@newdash/newdash
Version:
javascript/typescript utility library
31 lines (30 loc) • 726 B
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.baseRepeat = void 0;
/**
* @private
* @ignore
* @internal
* @param str
* @param n
*/
function baseRepeat(str, n) {
let result = '';
if (!str || n < 1 || n > Number.MAX_SAFE_INTEGER) {
return result;
}
// Leverage the exponentiation by squaring algorithm for a faster repeat.
// See https://en.wikipedia.org/wiki/Exponentiation_by_squaring for more details.
do {
if (n % 2) {
result += str;
}
n = Math.floor(n / 2);
if (n) {
str += str;
}
} while (n);
return result;
}
exports.baseRepeat = baseRepeat;
exports.default = baseRepeat;