uuid 热度
github star 7.5K+, 虽然看起来不多,但是npm 周下载量 15,533k+(一千五百多万啊)比react(周下载量 5000k+(五百万))整整多出2倍。有图有真相。
![fb9093d853f1dce609696ece53202acb.png](https://img-blog.csdnimg.cn/img_convert/fb9093d853f1dce609696ece53202acb.png)
![804e2ef36a8a1c51c2b46ed2b1131d0c.png](https://img-blog.csdnimg.cn/img_convert/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。