CodeHelper CodeHelper
首页
JavaScript
PHP
Python
Git速查表 (opens new window)
博客 (opens new window)
首页
JavaScript
PHP
Python
Git速查表 (opens new window)
博客 (opens new window)
  • 开始

    • 使用
  • 函数方法

    • 字符串
      • 全局唯一标识符
      • 去空
      • 获取 url 的所有参数(对象)
      • 生成十六进制颜色字符串
      • 生成 rgb 颜色字符串
    • 数字
    • 数组
    • 日期时间
    • 缓存
    • 深浅拷贝
    • 正则
    • md5
    • 其它
  • 梳理

    • 循环
  • 高级操作

    • 类型转换技巧
    • 防抖与节流
    • Promise实现
目录

字符串

# 全局唯一标识符

来源 (opens new window)

/**
 * 本算法来源于简书开源代码,详见:https://www.jianshu.com/p/fdbf293d0a85
 * 全局唯一标识符(uuid,Globally Unique Identifier),也称作 uuid(Universally Unique IDentifier)
 * 一般用于多个组件之间,给它一个唯一的标识符,或者v-for循环的时候,如果使用数组的index可能会导致更新列表出现问题
 * 最可能的情况是左滑删除item或者对某条信息流"不喜欢"并去掉它的时候,会导致组件内的数据可能出现错乱
 * v-for的时候,推荐使用后端返回的id而不是循环的index
 * @param {Number} len uuid的长度
 * @param {Boolean} firstU 将返回的首字母置为"u"
 * @param {Number} radix 生成uuid的基数(意味着返回的字符串都是这个基数),2-二进制,8-八进制,10-十进制,16-十六进制
 */
function guid(len = 32, firstU = true, radix = null) {
  let chars =
    "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz".split("")
  let uuid = []
  radix = radix || chars.length

  if (len) {
    // 如果指定uuid长度,只是取随机的字符,0|x为位运算,能去掉x的小数位,返回整数位
    for (let i = 0; i < len; i++) uuid[i] = chars[0 | (Math.random() * radix)]
  } else {
    let r
    // rfc4122标准要求返回的uuid中,某些位为固定的字符
    uuid[8] = uuid[13] = uuid[18] = uuid[23] = "-"
    uuid[14] = "4"

    for (let i = 0; i < 36; i++) {
      if (!uuid[i]) {
        r = 0 | (Math.random() * 16)
        uuid[i] = chars[i == 19 ? (r & 0x3) | 0x8 : r]
      }
    }
  }
  // 移除第一个字符,并用u替代,因为第一个字符为数值时,该guid不能用作id或者class
  if (firstU) {
    uuid.shift()
    return "u" + uuid.join("")
  } else {
    return uuid.join("")
  }
}

export default guid

# 去空

来源 (opens new window)

function trim(str, pos = "both") {
  if (pos == "both") {
    return str.replace(/^\s+|\s+$/g, "")
  } else if (pos == "left") {
    return str.replace(/^\s*/, "")
  } else if (pos == "right") {
    return str.replace(/(\s*$)/g, "")
  } else if (pos == "all") {
    return str.replace(/\s+/g, "")
  } else {
    return str
  }
}

export default trim

# 获取 url 的所有参数(对象)

    function getUrlParams() {
      let str = window.location.search.split('?')[1]||'';
    
      let arr = str.split("&");
      let obj = new Object();
      for (let i = 0; i < arr.length; i++) {
          let tmp_arr = arr[i].split("=");
          obj[decodeURIComponent(tmp_arr[0])] = decodeURIComponent(tmp_arr[1]);
      }
      return obj;
    }
    
    function getUrlParams() {
      const getUrlParams = (query) =>Array.from(new URLSearchParams(query)).reduce((p, [k, v]) => Object.assign({}, p, { [k]: p[k] ? (Array.isArray(p[k]) ? p[k] : [p[k]]).concat(v) : v }),{});
    
      // let str = window.location.search;
      // str = str.substring(1, str.length);
      let str = window.location.search.split('?')[1]||'';
    
      return getUrlParams(str)
    }
    
    // Make sure to add code blocks to your code group

    # 生成十六进制颜色字符串

    const randomColor = () => `#${Math.random().toString(16).slice(2, 8).padEnd(6, '0')}`;
    

    # 生成 rgb 颜色字符串

    const randomRgbColor = () => `rgb(${Math.floor(Math.random()*255)}, ${Math.floor(Math.random()*255)}, ${Math.floor(Math.random()*255)})`
    
    上次更新: 2022/08/26, 14:34:29
    使用
    数字

    ← 使用 数字→

    Theme by Vdoing | Copyright © 2022-2023 CodeHelper
    • 跟随系统
    • 浅色模式
    • 深色模式
    • 阅读模式