1 | # uuidv4
|
2 |
|
3 | uuidv4 creates v4 UUIDs.
|
4 |
|
5 | ## Status
|
6 |
|
7 | | Category | Status |
|
8 | | ---------------- | --------------------------------------------------------------------------------------------------- |
|
9 | | Version | [](https://www.npmjs.com/package/uuidv4) |
|
10 | | Dependencies |  |
|
11 | | Dev dependencies |  |
|
12 | | Build |  |
|
13 | | License |  |
|
14 |
|
15 | ## Please note
|
16 |
|
17 | This module will be deprecated in the future in favour of module [uuid](https://www.npmjs.com/package/uuid). Most of the functionality of this module is already included in `uuid` since version `8.3.0`, so most of the functions of this module have already been marked as deprecated.
|
18 |
|
19 | ## Installation
|
20 |
|
21 | ```shell
|
22 | $ npm install uuidv4
|
23 | ```
|
24 |
|
25 | ## Quick start
|
26 |
|
27 | First you need to integrate uuidv4 into your project by using the `require` function:
|
28 |
|
29 | ```javascript
|
30 | const { uuid } = require('uuidv4');
|
31 | ```
|
32 |
|
33 | If you use TypeScript, use the following code instead:
|
34 |
|
35 | ```typescript
|
36 | import { uuid } from 'uuidv4';
|
37 | ```
|
38 |
|
39 | Then you can create UUIDs. To do so simply call the `uuid` function:
|
40 |
|
41 | ```javascript
|
42 | console.log(uuid());
|
43 | // => '11bf5b37-e0b8-42e0-8dcf-dc8c4aefc000'
|
44 | ```
|
45 |
|
46 | ### Verifying a UUID
|
47 |
|
48 | To verify whether a given value is a UUID, use the `isUuid` function:
|
49 |
|
50 | ```javascript
|
51 | import { isUuid } from 'uuidv4';
|
52 |
|
53 | console.log(isUuid('75442486-0878-440c-9db1-a7006c25a39f'));
|
54 | // => true
|
55 | ```
|
56 |
|
57 | _Please note that the `isUuid` function returns `true` for both, `v4` and `v5` UUIDs. In addition, `isUuid` returns `true` for `empty()`._
|
58 |
|
59 | #### Using a regular expression
|
60 |
|
61 | If you want to perform the verification on your own using a regular expression, use the `regex` property, and access its `v4` or `v5` property, depending on what you need:
|
62 |
|
63 | ```javascript
|
64 | import { regex } from 'uuidv4';
|
65 |
|
66 | console.log(regex.v4);
|
67 | console.log(regex.v5);
|
68 | ```
|
69 |
|
70 | _Please note that the regular expressions also consider `empty()` to be a valid UUID._
|
71 |
|
72 | #### Using a JSON schema
|
73 |
|
74 | If you want to perform the verification on your own using a JSON schema, use the `jsonSchema` property, and access its `v4` or `v5` property, depending on what you need:
|
75 |
|
76 | ```javascript
|
77 | import { jsonSchema } from 'uuidv4';
|
78 |
|
79 | console.log(jsonSchema.v4);
|
80 | console.log(jsonSchema.v5);
|
81 | ```
|
82 |
|
83 | _Please note that the JSON schemas also consider `empty()` to be a valid UUID._
|
84 |
|
85 | ### Getting a UUID from a string
|
86 |
|
87 | From time to time you need an identifier that looks like a UUID, but is actually inferred from a string. For that, use the `fromString` function, which returns a UUID `v5`:
|
88 |
|
89 | ```javascript
|
90 | import { fromString } from 'uuidv4';
|
91 |
|
92 | console.log(fromString('the native web'));
|
93 | // => 'cdb63720-9628-5ef6-bbca-2e5ce6094f3c'
|
94 | ```
|
95 |
|
96 | By default, the `fromString` function uses a pre-configured namespace. If you want to use your own namespace, provide a UUID as second parameter:
|
97 |
|
98 | ```javascript
|
99 | import { fromString } from 'uuidv4';
|
100 |
|
101 | console.log(fromString('the native web', '004aadf4-8e1a-4450-905b-6039179f52da'));
|
102 | // => 'b1c4a89e-4905-5e3c-b57f-dc92627d011e'
|
103 | ```
|
104 |
|
105 | ### Getting the empty UUID
|
106 |
|
107 | If you need a UUID that consists only of zeros, use the `empty` function:
|
108 |
|
109 | ```javascript
|
110 | import { empty } from 'uuidv4';
|
111 |
|
112 | console.log(empty());
|
113 | // => '00000000-0000-0000-0000-000000000000'
|
114 | ```
|
115 |
|
116 | ## Running quality assurance
|
117 |
|
118 | To run quality assurance for this module use [roboter](https://www.npmjs.com/package/roboter):
|
119 |
|
120 | ```shell
|
121 | $ npx roboter
|
122 | ```
|