uuid插件

uuid.js是一个轻量级的JavaScript库,用于生成符合RFC4122标准的UUID(Universally Unique Identifier,通用唯一识别码)。它的大小只有2KB左右,支持多种生成模式,并可以用于浏览器和Node.js环境。

uuid.js的核心代码由一个单独的函数uuid()实现。它的使用非常简单,只需要调用这个函数即可生成一个新的UUID。

 

npm install uuid

const uuid = require('uuid');
const myUuid = uuid();
console.log(myUuid);

 

其他生产uuid的方法

  function generateUUID() {
    var d = new Date().getTime();
    if (window.performance && typeof window.performance.now === 'function') {
      d += performance.now(); //use high-precision timer if available
    }
    var uuid = 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(
      /[xy]/g,
      function (c) {
        var r = (d + Math.random() * 16) % 16 | 0;
        d = Math.floor(d / 16);
        return (c == 'x' ? r : (r & 0x3) | 0x8).toString(16);
      },
    );
    return uuid;
  }
// 指定长度和基数
function getUuid(len, radix) {
    var chars = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'.split('')
    let uuid = []
    let i
    radix = radix || chars.length
 
    if (len) {
        // Compact form
        for (i = 0; i < len; i++) uuid[i] = chars[0 | Math.random() * radix]
    } else {
        // rfc4122, version 4 form
        let r
 
        // rfc4122 requires these characters
        uuid[8] = uuid[13] = uuid[18] = uuid[23] = '-'
        uuid[14] = '4'
 
        // Fill in random data.  At i==19 set the high bits of clock sequence as
        // per rfc4122, sec. 4.1.5
        for (i = 0; i < 36; i++) {
            if (!uuid[i]) {
                r = 0 | Math.random() * 16
                uuid[i] = chars[(i == 19) ? (r & 0x3) | 0x8 : r]
            }
        }
    }
 
    return uuid.join('')
}
getUuid(80, 32)

 

参考:

https://blog.csdn.net/qq_37904209/article/details/107576400

https://www.python100.com/html/83040.html

https://blog.csdn.net/weixin_46600931/article/details/127648398

https://zhuanlan.zhihu.com/p/469763011?utm_id=0

 

posted @ 2023-07-17 10:53  小小强学习网  阅读(209)  评论(0编辑  收藏  举报