[zhongjyuan]通用UUID函数

 /**
  * 生成指定长度的 UUID
  * @author zhongjyuan
  * @email zhognjyuan@outlook.com
  * @website http://zhongjyuan.club
  * @date 2023年7月13日11:42:16
  * @param {number} len UUID 的长度,默认为 36
  * @param {boolean} removeDash 是否移除连字符,默认为 false
  * @returns {string} 生成的 UUID
  */

 uuidfunction (len = 36, removeDash = false{
  // 获取当前时间的毫秒数
  var d = new Date().getTime();

  // 定义模板字符串
  var result = "xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx".replace(/[xy]/gfunction (c{
   // 生成随机数
   var r = (d + Math.random() * 16) % 16 | 0;
   d = Math.floor(d / 16);
   // 根据模板替换字符
   return (c == "x" ? r : (r & 0x3) | 0x8).toString(16);
  });

  // 如果需要移除连字符,则将连字符替换为空字符串
  if (removeDash) {
   result = result.replace(/-/g"");
  }

  // 返回指定长度的 UUID
  zhongjyuan.logger.trace("[helper] uuid(${0}, ${1}) => ${2}", len, removeDash, result.substring(0, len));
  return result.substring(0, len);
 },
alt

本文由 mdnice 多平台发布

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值