00001 00002 var MAX_SAFE_INTEGER = 9007199254740991; 00003 00004 /* Built-in method references for those with the same name as other `lodash` methods. */ 00005 var nativeFloor = Math.floor; 00006 00015 function baseRepeat(string, n) { 00016 var result = ''; 00017 if (!string || n < 1 || n > MAX_SAFE_INTEGER) { 00018 return result; 00019 } 00020 // Leverage the exponentiation by squaring algorithm for a faster repeat. 00021 // See https://en.wikipedia.org/wiki/Exponentiation_by_squaring for more details. 00022 do { 00023 if (n % 2) { 00024 result += string; 00025 } 00026 n = nativeFloor(n / 2); 00027 if (n) { 00028 string += string; 00029 } 00030 } while (n); 00031 00032 return result; 00033 } 00034 00035 module.exports = baseRepeat;