/**
* 重复N次给定字符串
* Repeats the given string `n` times
* @param {string} [string=''] The string to repeat
* @param {number} [n=1] The number of times to repeat the string
* @returns {string} Returns the repeated string
* @example
* repeat('*', 3)
* // => '***'
* repeat('abc', 2)
* // => 'abcabc'
* repeat('abc', 0)
* // => ''
*/
import toInteger from "./toInteger"
import toString from "./toString"
function baseRepeat(string, n) {
var result = ""
if (!string || 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 += string
}
n = Math.floor(n / 2)
if (n) {
string += string
}
} while (n)
return result
}
function repeat(string, n, guard) {
if (n === undefined) {
n = 1
} else {
n = toInteger(n)
}
return baseRepeat(toString(string), n)
}
export default repeat
/**
* 转换value为整数
* Converts `value` to an integer.
*
* @param {*} value The value to convert.
* @returns {number} Returns the converted integer.
* @example
*
* toInteger(3.3)
* // => 3
*
* toInteger('3.3')
* // => 3
*
* toInteger(Number.MIN_VALUE)
* // => 0
*
* toInteger(Infinity)
* // => 1.7976931348623157e+308
*
* toInteger(-3.3)
* // => -3
*
* toInteger(new Date())
* // => 1561453581720
*
* toInteger(() => {})
* // => 0
*/
const NAN = 0 / 0
const INFINITY = 1 / 0
const symbolTag = "[object Symbol]"
const MAX_INTEGER = Number.MAX_VALUE || 1.7976931348623157e308
function isObjectLike(value) {
return typeof value == "object" && value !== null
}
function isSymbol(value) {
return (
typeof value === "symbol" ||
(isObjectLike(value) && Object.prototype.toString.call(value) === symbolTag)
)
}
function toNumber(value) {
if (typeof value === "number") {
return value
}
if (isSymbol(value)) {
return NAN
}
return Number(value)
}
function toFinite(value) {
if (!value) {
return value === 0 ? value : 0
}
value = toNumber(value)
if (value === INFINITY || value === -INFINITY) {
var sign = value < 0 ? -1 : 1 //可用Math.sign代替
return sign * MAX_INTEGER
}
return value === value ? value : 0 //NaN 不等于 NaN
}
function toInteger(value) {
var result = toFinite(value)
var remainder = result % 1
return result === result ? (remainder ? result - remainder : result) : 0 //可用Math.trunc代替
}
export default toInteger
/**
* Converts `value` to a string. An empty string is returned for `null` and `undefined` values.
* The sign of `-0` is preserved.
* 转换`value`成字符串,`null`和`undefined`返回空字符串,`-0`转成'-0'
* @param {*} value The value to convert
* @retuens {string} Returns the converted string
* @example
* toString(null)
* // => ''
* toString(-0)
* // => '-0'
* toString([1, 2, 3])
* // => '1, 2, 3'
* toString({a: 1})
* // => [object Object]
*/
const symbolTag = "[object Symbol]"
function isObjectLike(value) {
return typeof value == "object" && value !== null;
}
function isSymbol(value) {
return (
typeof value === "symbol" ||
(isObjectLike(value) && Object.prototype.toString.call(value) === symbolTag)
)
}
function arrayMap(array, iteratee) {
var index = -1,
length = array == null ? 0 : array.length,
result = Array(length)
while (++index < length) {
result[index] = iteratee(array[index], index, array)
}
return result
}
function baseToString(value) {
if (typeof value === "string") {
return value
}
if (Array.isArray(value)) {
return arrayMap(value, baseToString) + ""
}
if (isSymbol(value)) {
return Symbol.prototype.toString
? Symbol.prototype.toString.call(value)
: ""
}
var result = value + ""
return result == "0" && 1 / value == -Infinity ? "-0" : result
}
function toString(value) {
return value == null ? "" : baseToString(value)
}
export default toString