uuid生成_nodejs 生成唯一标识 uuid

uuid 热度

github star 7.5K+, 虽然看起来不多,但是npm 周下载量 15,533k+(一千五百多万啊)比react(周下载量 5000k+(五百万))整整多出2倍。有图有真相。

fb9093d853f1dce609696ece53202acb.png
804e2ef36a8a1c51c2b46ed2b1131d0c.png

uuid 使用场景

需要给某些数据定义一个唯一标识符,便于寻找,关联,给某些组件定义唯一key,或者属性的唯一性。

uid 快速开发(来自官网文档)

npm install uuid
  • 版本1(基于时间戳):
const uuidv1 = require('uuid/v1');uuidv1(); // ⇨ '45745c60-7b1a-11e8-9c9c-2d42b21b1a3e'
  • 版本3(基于命名空间)
const uuidv3 = require('uuid/v3');// ... using predefined DNS namespace (for domain names)uuidv3('hello.example.com', uuidv3.DNS); // ⇨ '9125a8dc-52ee-365b-a5aa-81b0b3681cf6' // ... using predefined URL namespace (for, well, URLs)uuidv3('http://example.com/hello', uuidv3.URL); // ⇨ 'c6235813-3ba4-3801-ae84-e0a6ebb7d138' // ... using a custom namespace Note: Custom namespaces should be a UUID string specific to your application!// E.g. the one here was generated using this modules `uuid` CLI.const MY_NAMESPACE = '1b671a64-40d5-491e-99b0-da01ff1f3341';uuidv3('Hello, World!', MY_NAMESPACE); // ⇨ 'e8b5a51d-11c8-3310-a6ab-367563f20686' 
  • 版本4 (随机):
const uuidv4 = require('uuid/v4');uuidv4(); // ⇨ '10ba038e-48da-487b-96e8-8d3b99b6d18a' 
  • 版本5(基于命名空间)
const uuidv5 = require('uuid/v5'); // ... using predefined DNS namespace (for domain names)uuidv5('hello.example.com', uuidv5.DNS); // ⇨ 'fdda765f-fc57-5604-a269-52a7df8164ec' // ... using predefined URL namespace (for, well, URLs)uuidv5('http://example.com/hello', uuidv5.URL); // ⇨ '3bbcee75-cecc-5b56-8031-b6641c1ed1f1' // ... using a custom namespace Note: Custom namespaces should be a UUID string specific to your application!// E.g. the one here was generated using this modules `uuid` CLI.const MY_NAMESPACE = '1b671a64-40d5-491e-99b0-da01ff1f3341';uuidv5('Hello, World!', MY_NAMESPACE); // ⇨ '630eb68f-e0fa-5ecc-887a-7c7a62614681' 

PS

还有基于浏览器使用方法,uuid 提供的v1,v3,v4,v5的浏览器使用方法,uuid 命令行使用。请点击了解更多,进入github uuid 官网。uuid 目前最新版本3.3.2。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值