重复N次给定字符串

/**
 * 重复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

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值