{"size": 2437, "ext": "js", "max_stars_repo_path": "src/utils/index.js", "max_stars_repo_name": "xiaoziguys/react-blog", "max_stars_repo_stars_event_min_datetime": "2019-02-17T18:24:52.000Z", "max_stars_repo_stars_event_max_datetime": "2020-05-25T10:19:05.000Z", "max_issues_repo_path": "src/utils/index.js", "max_issues_repo_name": "xiaoziguys/react-blog", "max_issues_repo_issues_event_min_datetime": "2019-02-25T14:21:51.000Z", "max_issues_repo_issues_event_max_datetime": "2020-05-22T08:16:57.000Z", "max_forks_repo_path": "src/utils/index.js", "max_forks_repo_name": "charliecha1990/react_blog", "max_forks_repo_forks_event_min_datetime": "2019-02-25T03:17:37.000Z", "max_forks_repo_forks_event_max_datetime": "2020-05-27T00:47:41.000Z"}
{"max_stars_count": 448, "max_issues_count": 34, "max_forks_count": 135, "avg_line_length": 21.0086206897, "max_line_length": 72, "alphanum_fraction": 0.6216659828}
import marked from 'marked'
import { COLOR_LIST } from '@/utils/config'
import xss from 'xss'
import { clear, get } from '@/utils/storage'

// 转化 md 语法为 html
export const translateMarkdown = (plainText, isGuardXss = false) => {
  return marked(isGuardXss ? xss(plainText) : plainText, {
    renderer: new marked.Renderer(),
    gfm: true,
    pedantic: false,
    sanitize: false,
    tables: true,
    breaks: true,
    smartLists: true,
    smartypants: true,
    highlight: function(code) {
      /*eslint no-undef: "off"*/
      return hljs.highlightAuto(code).value
    }
  })
}

// 获取 url query 参数
export const decodeQuery = url => {
  const params = {}
  const paramsStr = url.replace(/\.*\?/, '') // a=1&b=2&c=&d=xxx&e
  paramsStr.split('&').forEach(v => {
    const d = v.split('=')
    if (d[1] && d[0]) params[d[0]] = d[1]
  })
  return params
}

// 计算 评论数
export const calcCommentsCount = commentList => {
  let count = commentList.length
  commentList.forEach(item => {
    count += item.replies.length
  })
  return count
}

// 取数组中的随机数
export const randomIndex = arr => Math.floor(Math.random() * arr.length)

/**
 * 对数组进行分组
 * @param {Array} arr - 分组对象
 * @param {Function} f
 * @returns 数组分组后的新数组
 */
export const groupBy = (arr, f) => {
  const groups = {}
  arr.forEach(item => {
    const group = JSON.stringify(f(item))
    groups[group] = groups[group] || []
    groups[group].push(item)
  })
  return Object.keys(groups).map(group => groups[group])
}

/**
 * @param {string} path
 * @returns {Boolean}
 */
export function isExternal(path) {
  return /^(https?:|mailto:|tel:|http:)/.test(path)
}

// 获取 token
export function getToken() {
  let token = ''
  const userInfo = get('userInfo')

  if (userInfo && userInfo.token) {
    token = 'Bearer ' + userInfo.token
  }

  return token
}

/**
 * 生成随机 ID
 * @param {Number} len - 长度
 */
export function RandomId(len) {
  return Math.random()
    .toString(36)
    .substr(3, len)
}

/**
 * debounce
 */
export function debounce(func, wait) {
  let timer = null
  return function() {
    const context = this
    const args = arguments
    clearTimeout(timer)
    timer = setTimeout(function() {
      func.apply(context, args)
    }, wait)
  }
}

// 生成 color
export function genertorColor(list = [], colorList = COLOR_LIST) {
  const _list = [...list]
  _list.forEach((l, i) => {
    l.color = colorList[i] || colorList[randomIndex(colorList)]
  })
  return _list
}
