uuid 3.3.3 → 7.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +79 -49
- package/CONTRIBUTING.md +20 -0
- package/README.md +245 -155
- package/dist/bin/uuid +2 -0
- package/dist/bytesToUuid.js +27 -0
- package/dist/esm-browser/bytesToUuid.js +18 -0
- package/dist/esm-browser/index.js +4 -0
- package/{lib/md5-browser.js → dist/esm-browser/md5.js} +53 -51
- package/dist/esm-browser/rng.js +15 -0
- package/dist/esm-browser/sha1.js +91 -0
- package/dist/esm-browser/v1.js +95 -0
- package/dist/esm-browser/v3.js +4 -0
- package/dist/esm-browser/v35.js +56 -0
- package/dist/esm-browser/v4.js +27 -0
- package/dist/esm-browser/v5.js +4 -0
- package/dist/esm-node/bytesToUuid.js +18 -0
- package/dist/esm-node/index.js +4 -0
- package/dist/esm-node/md5.js +13 -0
- package/dist/esm-node/rng.js +4 -0
- package/dist/esm-node/sha1.js +13 -0
- package/dist/esm-node/v1.js +95 -0
- package/dist/esm-node/v3.js +4 -0
- package/{lib → dist/esm-node}/v35.js +21 -22
- package/dist/esm-node/v4.js +27 -0
- package/dist/esm-node/v5.js +4 -0
- package/dist/index.js +39 -0
- package/dist/md5-browser.js +225 -0
- package/dist/md5.js +24 -0
- package/dist/rng-browser.js +23 -0
- package/dist/rng.js +16 -0
- package/dist/sha1-browser.js +96 -0
- package/dist/sha1.js +24 -0
- package/dist/umd/uuid.min.js +1 -0
- package/dist/umd/uuidv1.min.js +1 -0
- package/dist/umd/uuidv3.min.js +1 -0
- package/dist/umd/uuidv4.min.js +1 -0
- package/dist/umd/uuidv5.min.js +1 -0
- package/dist/uuid-bin.js +67 -0
- package/dist/v1.js +108 -0
- package/dist/v3.js +17 -0
- package/dist/v35.js +69 -0
- package/dist/v4.js +41 -0
- package/dist/v5.js +17 -0
- package/package.json +86 -19
- package/v1.js +6 -107
- package/v3.js +7 -3
- package/v4.js +6 -27
- package/v5.js +8 -3
- package/AUTHORS +0 -5
- package/bin/uuid +0 -65
- package/index.js +0 -8
- package/lib/bytesToUuid.js +0 -24
- package/lib/md5.js +0 -25
- package/lib/rng-browser.js +0 -34
- package/lib/rng.js +0 -8
- package/lib/sha1-browser.js +0 -89
- package/lib/sha1.js +0 -25
package/README.md
CHANGED
|
@@ -2,130 +2,154 @@
|
|
|
2
2
|
-- This file is auto-generated from README_js.md. Changes should be made there.
|
|
3
3
|
-->
|
|
4
4
|
|
|
5
|
-
# uuid [](https://github.com/uuidjs/uuid/actions)
|
|
6
6
|
|
|
7
|
-
|
|
7
|
+
For the creation of [RFC4122](http://www.ietf.org/rfc/rfc4122.txt) UUIDs
|
|
8
8
|
|
|
9
|
-
|
|
9
|
+
- **Complete** - Support for RFC4122 version 1, 3, 4, and 5 UUIDs
|
|
10
|
+
- **Cross-platform** - Support for ...
|
|
11
|
+
- CommonJS, [ECMAScript Modules](#ecmascript-modules) and UMD builds
|
|
12
|
+
- Node 8, 10, 12
|
|
13
|
+
- Chrome, Safari, Firefox, Edge, IE 11 browsers
|
|
14
|
+
- Webpack and rollup.js module bundlers
|
|
15
|
+
- **Secure** - Cryptographically-strong random values
|
|
16
|
+
- **Small** - Zero-dependency, small footprint, plays nice with "tree shaking" packagers
|
|
17
|
+
- **CLI** - Includes the [`uuid` command line](#command-line) utility
|
|
10
18
|
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
* Uses cryptographically-strong random number APIs (when available)
|
|
14
|
-
* Zero-dependency, small footprint (... but not [this small](https://gist.github.com/982883))
|
|
19
|
+
**Upgrading from uuid\@3?** Your code is probably okay, but check out [Upgrading
|
|
20
|
+
From uuid\@3](#upgrading-from-uuid3) for details.
|
|
15
21
|
|
|
16
|
-
|
|
17
|
-
supported after version 3.x of this module. Instead, use `require('uuid/[v1|v3|v4|v5]')` as shown in the examples below.]
|
|
18
|
-
|
|
19
|
-
## Quickstart - CommonJS (Recommended)
|
|
22
|
+
## Quickstart - Node.js/CommonJS
|
|
20
23
|
|
|
21
24
|
```shell
|
|
22
25
|
npm install uuid
|
|
23
26
|
```
|
|
24
27
|
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
Version 1 (timestamp):
|
|
28
|
+
Once installed, decide which type of UUID you need. RFC4122 provides for four
|
|
29
|
+
versions, all of which are supported here. In order of popularity, they are:
|
|
28
30
|
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
31
|
+
- Version 4 (random) - Created from cryptographically-strong random values
|
|
32
|
+
- Version 1 (timestamp) - Created from the system clock (plus random values)
|
|
33
|
+
- Version 5 (namespace, SHA-1) - Created from user-supplied name and namespace strings
|
|
34
|
+
- Version 3 (namespace, MD5) - Like version 5, above, but with a poorer hash algorithm
|
|
32
35
|
|
|
33
|
-
|
|
36
|
+
**Unsure which one to use?** Use version 4 (random) unless you have a specific need for one of the other versions. See also [this FAQ](https://github.com/tc39/proposal-uuid#faq).
|
|
34
37
|
|
|
35
|
-
Version
|
|
38
|
+
### Create Version 4 (Random) UUIDs
|
|
36
39
|
|
|
37
40
|
```javascript
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
// ... using predefined DNS namespace (for domain names)
|
|
41
|
-
uuidv3('hello.example.com', uuidv3.DNS); // ⇨ '9125a8dc-52ee-365b-a5aa-81b0b3681cf6'
|
|
42
|
-
|
|
43
|
-
// ... using predefined URL namespace (for, well, URLs)
|
|
44
|
-
uuidv3('http://example.com/hello', uuidv3.URL); // ⇨ 'c6235813-3ba4-3801-ae84-e0a6ebb7d138'
|
|
45
|
-
|
|
46
|
-
// ... using a custom namespace
|
|
47
|
-
//
|
|
48
|
-
// Note: Custom namespaces should be a UUID string specific to your application!
|
|
49
|
-
// E.g. the one here was generated using this modules `uuid` CLI.
|
|
50
|
-
const MY_NAMESPACE = '1b671a64-40d5-491e-99b0-da01ff1f3341';
|
|
51
|
-
uuidv3('Hello, World!', MY_NAMESPACE); // ⇨ 'e8b5a51d-11c8-3310-a6ab-367563f20686'
|
|
52
|
-
|
|
41
|
+
import { v4 as uuidv4 } from 'uuid';
|
|
42
|
+
uuidv4(); // ⇨ '9b1deb4d-3b7d-4bad-9bdd-2b0d7b3dcb6d'
|
|
53
43
|
```
|
|
54
44
|
|
|
55
|
-
Version
|
|
45
|
+
### Create Version 1 (Timestamp) UUIDs
|
|
56
46
|
|
|
57
47
|
```javascript
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
48
|
+
import { v1 as uuidv1 } from 'uuid';
|
|
49
|
+
uuidv1(); // ⇨ '2c5ea4c0-4067-11e9-8b2d-1b9d6bcdbbfd'
|
|
61
50
|
```
|
|
62
51
|
|
|
63
|
-
Version 5 (
|
|
52
|
+
### Create Version 3 or Version 5 (Namespace) UUIDs
|
|
53
|
+
|
|
54
|
+
⚠️ Version 3 and Version 5 UUIDs are basically the same, differing
|
|
55
|
+
only in the underlying hash algorithm. Note that per the RFC, "_If backward
|
|
56
|
+
compatibility is not an issue, SHA-1 [Version 5] is preferred_."
|
|
57
|
+
|
|
58
|
+
⚠️ If using a custom namespace **be sure to generate your own
|
|
59
|
+
namespace UUID**. You can grab one [here](https://www.uuidgenerator.net/).
|
|
64
60
|
|
|
65
61
|
```javascript
|
|
66
|
-
|
|
62
|
+
import { v5 as uuidv5 } from 'uuid'; // For version 5
|
|
63
|
+
import { v3 as uuidv3 } from 'uuid'; // For version 3
|
|
67
64
|
|
|
68
|
-
//
|
|
65
|
+
// Using predefined DNS namespace (for domain names)
|
|
69
66
|
uuidv5('hello.example.com', uuidv5.DNS); // ⇨ 'fdda765f-fc57-5604-a269-52a7df8164ec'
|
|
67
|
+
uuidv3('hello.example.com', uuidv3.DNS); // ⇨ '9125a8dc-52ee-365b-a5aa-81b0b3681cf6'
|
|
70
68
|
|
|
71
|
-
//
|
|
69
|
+
// Using predefined URL namespace (for URLs)
|
|
72
70
|
uuidv5('http://example.com/hello', uuidv5.URL); // ⇨ '3bbcee75-cecc-5b56-8031-b6641c1ed1f1'
|
|
71
|
+
uuidv3('http://example.com/hello', uuidv3.URL); // ⇨ 'c6235813-3ba4-3801-ae84-e0a6ebb7d138'
|
|
73
72
|
|
|
74
|
-
//
|
|
75
|
-
//
|
|
76
|
-
// Note: Custom namespaces should be a UUID string specific to your application!
|
|
77
|
-
// E.g. the one here was generated using this modules `uuid` CLI.
|
|
73
|
+
// Using a custom namespace (See note, above, about generating your own
|
|
74
|
+
// namespace UUID)
|
|
78
75
|
const MY_NAMESPACE = '1b671a64-40d5-491e-99b0-da01ff1f3341';
|
|
79
76
|
uuidv5('Hello, World!', MY_NAMESPACE); // ⇨ '630eb68f-e0fa-5ecc-887a-7c7a62614681'
|
|
80
|
-
|
|
77
|
+
uuidv3('Hello, World!', MY_NAMESPACE); // ⇨ 'e8b5a51d-11c8-3310-a6ab-367563f20686'
|
|
81
78
|
```
|
|
82
79
|
|
|
83
|
-
##
|
|
80
|
+
## API
|
|
84
81
|
|
|
85
|
-
|
|
82
|
+
### Version 4 (Random)
|
|
86
83
|
|
|
87
|
-
|
|
84
|
+
```javascript
|
|
85
|
+
import { v4 as uuidv4 } from 'uuid';
|
|
88
86
|
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
</script>
|
|
87
|
+
// Incantations
|
|
88
|
+
uuidv4();
|
|
89
|
+
uuidv4(options);
|
|
90
|
+
uuidv4(options, buffer, offset);
|
|
94
91
|
```
|
|
95
92
|
|
|
96
|
-
|
|
93
|
+
Generate and return a RFC4122 version 4 UUID.
|
|
97
94
|
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
```
|
|
95
|
+
- `options` - (Object) Optional uuid state to apply. Properties may include:
|
|
96
|
+
- `random` - (Number[16]) Array of 16 numbers (0-255) to use in place of randomly generated values. Takes precedence over `options.rng`.
|
|
97
|
+
- `rng` - (Function) Random # generator function that returns an Array[16] of byte values (0-255). Alternative to `options.random`.
|
|
98
|
+
- `buffer` - (Array | Buffer) Array or buffer where UUID bytes are to be written.
|
|
99
|
+
- `offset` - (Number) Starting index in `buffer` at which to begin writing.
|
|
104
100
|
|
|
105
|
-
|
|
101
|
+
Returns `buffer`, if specified, otherwise the string form of the UUID
|
|
106
102
|
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
103
|
+
Example: Generate string UUID with predefined `random` values
|
|
104
|
+
|
|
105
|
+
```javascript
|
|
106
|
+
const v4options = {
|
|
107
|
+
random: [
|
|
108
|
+
0x10,
|
|
109
|
+
0x91,
|
|
110
|
+
0x56,
|
|
111
|
+
0xbe,
|
|
112
|
+
0xc4,
|
|
113
|
+
0xfb,
|
|
114
|
+
0xc1,
|
|
115
|
+
0xea,
|
|
116
|
+
0x71,
|
|
117
|
+
0xb4,
|
|
118
|
+
0xef,
|
|
119
|
+
0xe1,
|
|
120
|
+
0x67,
|
|
121
|
+
0x1c,
|
|
122
|
+
0x58,
|
|
123
|
+
0x36,
|
|
124
|
+
],
|
|
125
|
+
};
|
|
126
|
+
uuidv4(v4options); // ⇨ '109156be-c4fb-41ea-b1b4-efe1671c5836'
|
|
112
127
|
```
|
|
113
128
|
|
|
114
|
-
|
|
129
|
+
Example: Generate two IDs in a single buffer
|
|
115
130
|
|
|
116
|
-
```
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
131
|
+
```javascript
|
|
132
|
+
const buffer = new Array();
|
|
133
|
+
uuidv4(null, buffer, 0); // ⇨
|
|
134
|
+
// [
|
|
135
|
+
// 155, 29, 235, 77, 59,
|
|
136
|
+
// 125, 75, 173, 155, 221,
|
|
137
|
+
// 43, 13, 123, 61, 203,
|
|
138
|
+
// 109
|
|
139
|
+
// ]
|
|
140
|
+
uuidv4(null, buffer, 16); // ⇨
|
|
141
|
+
// [
|
|
142
|
+
// 155, 29, 235, 77, 59, 125, 75, 173,
|
|
143
|
+
// 155, 221, 43, 13, 123, 61, 203, 109,
|
|
144
|
+
// 27, 157, 107, 205, 187, 253, 75, 45,
|
|
145
|
+
// 155, 93, 171, 141, 251, 189, 75, 237
|
|
146
|
+
// ]
|
|
121
147
|
```
|
|
122
148
|
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
### Version 1
|
|
149
|
+
### Version 1 (Timestamp)
|
|
126
150
|
|
|
127
151
|
```javascript
|
|
128
|
-
|
|
152
|
+
import { v1 as uuidv1 } from 'uuid';
|
|
129
153
|
|
|
130
154
|
// Incantations
|
|
131
155
|
uuidv1();
|
|
@@ -133,17 +157,17 @@ uuidv1(options);
|
|
|
133
157
|
uuidv1(options, buffer, offset);
|
|
134
158
|
```
|
|
135
159
|
|
|
136
|
-
Generate and return a RFC4122
|
|
160
|
+
Generate and return a RFC4122 version 1 (timestamp) UUID.
|
|
137
161
|
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
162
|
+
- `options` - (Object) Optional uuid state to apply. Properties may include:
|
|
163
|
+
- `node` - (Array) Node id as Array of 6 bytes (per 4.1.6). Default: Randomly generated ID. See note 1.
|
|
164
|
+
- `clockseq` - (Number between 0 - 0x3fff) RFC clock sequence. Default: An internally maintained clockseq is used.
|
|
165
|
+
- `msecs` - (Number) Time in milliseconds since unix Epoch. Default: The current time is used.
|
|
166
|
+
- `nsecs` - (Number between 0-9999) additional time, in 100-nanosecond units. Ignored if `msecs` is unspecified. Default: internal uuid counter is used, as per 4.2.1.2.
|
|
167
|
+
- `random` - (Number[16]) Array of 16 numbers (0-255) to use for initialization of `node` and `clockseq` as described above. Takes precedence over `options.rng`.
|
|
168
|
+
- `rng` - (Function) Random # generator function that returns an Array[16] of byte values (0-255). Alternative to `options.random`.
|
|
169
|
+
- `buffer` - (Array | Buffer) Array or buffer where UUID bytes are to be written.
|
|
170
|
+
- `offset` - (Number) Starting index in `buffer` at which to begin writing.
|
|
147
171
|
|
|
148
172
|
Returns `buffer`, if specified, otherwise the string form of the UUID
|
|
149
173
|
|
|
@@ -156,10 +180,9 @@ const v1options = {
|
|
|
156
180
|
node: [0x01, 0x23, 0x45, 0x67, 0x89, 0xab],
|
|
157
181
|
clockseq: 0x1234,
|
|
158
182
|
msecs: new Date('2011-11-01').getTime(),
|
|
159
|
-
nsecs: 5678
|
|
183
|
+
nsecs: 5678,
|
|
160
184
|
};
|
|
161
185
|
uuidv1(v1options); // ⇨ '710b962e-041c-11e1-9234-0123456789ab'
|
|
162
|
-
|
|
163
186
|
```
|
|
164
187
|
|
|
165
188
|
Example: In-place generation of two binary IDs
|
|
@@ -167,127 +190,194 @@ Example: In-place generation of two binary IDs
|
|
|
167
190
|
```javascript
|
|
168
191
|
// Generate two ids in an array
|
|
169
192
|
const arr = new Array();
|
|
170
|
-
uuidv1(null, arr, 0);
|
|
171
|
-
|
|
172
|
-
|
|
193
|
+
uuidv1(null, arr, 0); // ⇨
|
|
194
|
+
// [
|
|
195
|
+
// 44, 94, 164, 192, 64,
|
|
196
|
+
// 103, 17, 233, 146, 52,
|
|
197
|
+
// 27, 157, 107, 205, 187,
|
|
198
|
+
// 253
|
|
199
|
+
// ]
|
|
200
|
+
uuidv1(null, arr, 16); // ⇨
|
|
201
|
+
// [
|
|
202
|
+
// 44, 94, 164, 192, 64, 103, 17, 233,
|
|
203
|
+
// 146, 52, 27, 157, 107, 205, 187, 253,
|
|
204
|
+
// 44, 94, 164, 193, 64, 103, 17, 233,
|
|
205
|
+
// 146, 52, 27, 157, 107, 205, 187, 253
|
|
206
|
+
// ]
|
|
173
207
|
```
|
|
174
208
|
|
|
175
|
-
### Version
|
|
209
|
+
### Version 5 (Namespace)
|
|
176
210
|
|
|
177
211
|
```javascript
|
|
178
|
-
|
|
212
|
+
import { v5 as uuidv5 } from 'uuid';
|
|
179
213
|
|
|
180
214
|
// Incantations
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
215
|
+
uuidv5(name, namespace);
|
|
216
|
+
uuidv5(name, namespace, buffer);
|
|
217
|
+
uuidv5(name, namespace, buffer, offset);
|
|
184
218
|
```
|
|
185
219
|
|
|
186
|
-
Generate and return a RFC4122
|
|
220
|
+
Generate and return a RFC4122 version 5 UUID.
|
|
187
221
|
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
222
|
+
- `name` - (String | Array[]) "name" to create UUID with
|
|
223
|
+
- `namespace` - (String | Array[]) "namespace" UUID either as a String or Array[16] of byte values
|
|
224
|
+
- `buffer` - (Array | Buffer) Array or buffer where UUID bytes are to be written.
|
|
225
|
+
- `offset` - (Number) Starting index in `buffer` at which to begin writing. Default = 0
|
|
192
226
|
|
|
193
227
|
Returns `buffer`, if specified, otherwise the string form of the UUID
|
|
194
228
|
|
|
195
229
|
Example:
|
|
196
230
|
|
|
197
231
|
```javascript
|
|
198
|
-
|
|
199
|
-
|
|
232
|
+
uuidv5('hello world', MY_NAMESPACE); // ⇨ '9f282611-e0fd-5650-8953-89c8e342da0b'
|
|
200
233
|
```
|
|
201
234
|
|
|
202
|
-
### Version
|
|
235
|
+
### Version 3 (Namespace)
|
|
236
|
+
|
|
237
|
+
⚠️ Note: Per the RFC, "_If backward compatibility is not an issue, SHA-1 [Version 5] is preferred_."
|
|
203
238
|
|
|
204
239
|
```javascript
|
|
205
|
-
|
|
240
|
+
import { v3 as uuidv3 } from 'uuid';
|
|
206
241
|
|
|
207
242
|
// Incantations
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
243
|
+
uuidv3(name, namespace);
|
|
244
|
+
uuidv3(name, namespace, buffer);
|
|
245
|
+
uuidv3(name, namespace, buffer, offset);
|
|
211
246
|
```
|
|
212
247
|
|
|
213
|
-
Generate and return a RFC4122
|
|
248
|
+
Generate and return a RFC4122 version 3 UUID.
|
|
214
249
|
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
* `offset` - (Number) Starting index in `buffer` at which to begin writing.
|
|
250
|
+
- `name` - (String | Array[]) "name" to create UUID with
|
|
251
|
+
- `namespace` - (String | Array[]) "namespace" UUID either as a String or Array[16] of byte values
|
|
252
|
+
- `buffer` - (Array | Buffer) Array or buffer where UUID bytes are to be written.
|
|
253
|
+
- `offset` - (Number) Starting index in `buffer` at which to begin writing. Default = 0
|
|
220
254
|
|
|
221
255
|
Returns `buffer`, if specified, otherwise the string form of the UUID
|
|
222
256
|
|
|
223
|
-
Example:
|
|
257
|
+
Example:
|
|
224
258
|
|
|
225
259
|
```javascript
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
uuidv4(v4options); // ⇨ '109156be-c4fb-41ea-b1b4-efe1671c5836'
|
|
260
|
+
uuidv3('hello world', MY_NAMESPACE); // ⇨ '042ffd34-d989-321c-ad06-f60826172424'
|
|
261
|
+
```
|
|
262
|
+
|
|
263
|
+
## Command Line
|
|
264
|
+
|
|
265
|
+
UUIDs can be generated from the command line using `uuid`.
|
|
233
266
|
|
|
267
|
+
```shell
|
|
268
|
+
$ uuid
|
|
269
|
+
ddeb27fb-d9a0-4624-be4d-4615062daed4
|
|
234
270
|
```
|
|
235
271
|
|
|
236
|
-
|
|
272
|
+
The default is to generate version 4 UUIDS, however the other versions are supported. Type `uuid --help` for details:
|
|
273
|
+
|
|
274
|
+
```
|
|
275
|
+
$ uuid --help
|
|
276
|
+
|
|
277
|
+
Usage:
|
|
278
|
+
uuid
|
|
279
|
+
uuid v1
|
|
280
|
+
uuid v3 <name> <namespace uuid>
|
|
281
|
+
uuid v4
|
|
282
|
+
uuid v5 <name> <namespace uuid>
|
|
283
|
+
uuid --help
|
|
284
|
+
|
|
285
|
+
Note: <namespace uuid> may be "URL" or "DNS" to use the corresponding UUIDs
|
|
286
|
+
defined by RFC4122
|
|
287
|
+
```
|
|
288
|
+
|
|
289
|
+
## ECMAScript Modules
|
|
290
|
+
|
|
291
|
+
For usage in the browser `uuid` provides support for [ECMAScript
|
|
292
|
+
Modules](https://www.ecma-international.org/ecma-262/6.0/#sec-modules) (ESM) that enable
|
|
293
|
+
tree-shaking for bundlers, like [rollup.js](https://rollupjs.org/guide/en/#tree-shaking)
|
|
294
|
+
([example](./examples/browser-rollup/)) and [webpack](https://webpack.js.org/guides/tree-shaking/)
|
|
295
|
+
([example](./examples/browser-webpack/)).
|
|
237
296
|
|
|
238
297
|
```javascript
|
|
239
|
-
|
|
240
|
-
uuidv4(
|
|
241
|
-
|
|
298
|
+
import { v4 as uuidv4 } from 'uuid';
|
|
299
|
+
uuidv4(); // ⇨ '1b9d6bcd-bbfd-4b2d-9b5d-ab8dfbbd4bed'
|
|
300
|
+
```
|
|
301
|
+
|
|
302
|
+
There is experimental native ESM support for [the browser](./examples/browser-esmodules/) but it
|
|
303
|
+
should not be considered ready for production use and may change or disappear in future releases.
|
|
304
|
+
|
|
305
|
+
To run the examples you must first create a dist build of this library in the module root:
|
|
242
306
|
|
|
307
|
+
```
|
|
308
|
+
npm run build
|
|
243
309
|
```
|
|
244
310
|
|
|
245
|
-
|
|
311
|
+
## UMD Builds
|
|
246
312
|
|
|
247
|
-
|
|
248
|
-
|
|
313
|
+
If you want to load a minified UMD build directly in the browser you can use one of the popular npm
|
|
314
|
+
CDNs:
|
|
249
315
|
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
316
|
+
```html
|
|
317
|
+
<script src="https://unpkg.com/uuid@latest/dist/umd/uuidv4.min.js"></script>
|
|
318
|
+
<script>
|
|
319
|
+
alert(uuidv4());
|
|
320
|
+
</script>
|
|
321
|
+
```
|
|
322
|
+
|
|
323
|
+
or
|
|
324
|
+
|
|
325
|
+
```html
|
|
326
|
+
<script src="https://cdn.jsdelivr.net/npm/uuid@latest/dist/umd/uuidv4.min.js"></script>
|
|
327
|
+
<script>
|
|
328
|
+
alert(uuidv4());
|
|
329
|
+
</script>
|
|
254
330
|
```
|
|
255
331
|
|
|
256
|
-
|
|
332
|
+
Available bundles:
|
|
257
333
|
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
* `buffer` - (Array | Buffer) Array or buffer where UUID bytes are to be written.
|
|
261
|
-
* `offset` - (Number) Starting index in `buffer` at which to begin writing. Default = 0
|
|
334
|
+
- https://unpkg.com/uuid@latest/dist/umd/
|
|
335
|
+
- https://cdn.jsdelivr.net/npm/uuid@latest/dist/umd/
|
|
262
336
|
|
|
263
|
-
|
|
337
|
+
## Upgrading From uuid\@3
|
|
264
338
|
|
|
265
|
-
|
|
339
|
+
"_Wait... what happened to uuid\@4 - uuid\@6?!?_"
|
|
266
340
|
|
|
267
|
-
|
|
268
|
-
|
|
341
|
+
In order to avoid confusion with RFC [version 4](#version-4-random) and [version
|
|
342
|
+
5](#version-5-namespace) UUIDs, and a possible [version
|
|
343
|
+
6](http://gh.peabody.io/uuidv6/), releases 4 thru 6 of this module have been
|
|
344
|
+
skipped. Hence, how we're now at uuid\@7.
|
|
269
345
|
|
|
346
|
+
### Deep Requires Now Deprecated
|
|
347
|
+
|
|
348
|
+
uuid\@3 encouraged the use of deep requires to minimize the bundle size of
|
|
349
|
+
browser builds:
|
|
350
|
+
|
|
351
|
+
```javascript
|
|
352
|
+
const uuidv4 = require('uuid/v4'); // <== NOW DEPRECATED!
|
|
353
|
+
uuidv4();
|
|
270
354
|
```
|
|
271
355
|
|
|
272
|
-
|
|
356
|
+
As of uuid\@7 this library now provides ECMAScript modules builds, which allow
|
|
357
|
+
packagers like Webpack and Rollup to do "tree-shaking" to remove dead code.
|
|
358
|
+
Instead, use the `import` syntax:
|
|
273
359
|
|
|
274
|
-
|
|
360
|
+
```javascript
|
|
361
|
+
import { v4 as uuidv4 } from 'uuid';
|
|
362
|
+
uuidv4();
|
|
363
|
+
```
|
|
275
364
|
|
|
276
|
-
|
|
277
|
-
$ uuid
|
|
278
|
-
ddeb27fb-d9a0-4624-be4d-4615062daed4
|
|
365
|
+
... or for CommonJS:
|
|
279
366
|
|
|
280
|
-
|
|
281
|
-
|
|
367
|
+
```javascript
|
|
368
|
+
const { v4: uuidv4 } = require('uuid');
|
|
369
|
+
uuidv4();
|
|
282
370
|
```
|
|
283
371
|
|
|
284
|
-
|
|
372
|
+
### Default Export Removed
|
|
285
373
|
|
|
286
|
-
|
|
374
|
+
uuid\@3 was exporting the Version 4 UUID method as a default export:
|
|
287
375
|
|
|
288
|
-
```
|
|
289
|
-
|
|
376
|
+
```javascript
|
|
377
|
+
const uuid = require('uuid'); // <== REMOVED!
|
|
290
378
|
```
|
|
291
379
|
|
|
380
|
+
This usage pattern was already discouraged in uuid\@3 and has been removed in uuid\@7.
|
|
381
|
+
|
|
292
382
|
----
|
|
293
383
|
Markdown generated from [README_js.md](README_js.md) by [](https://github.com/broofa/runmd)
|
package/dist/bin/uuid
ADDED
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.default = void 0;
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* Convert array of 16 byte values to UUID string format of the form:
|
|
10
|
+
* XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX
|
|
11
|
+
*/
|
|
12
|
+
var byteToHex = [];
|
|
13
|
+
|
|
14
|
+
for (var i = 0; i < 256; ++i) {
|
|
15
|
+
byteToHex[i] = (i + 0x100).toString(16).substr(1);
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
function bytesToUuid(buf, offset) {
|
|
19
|
+
var i = offset || 0;
|
|
20
|
+
var bth = byteToHex; // join used to fix memory issue caused by concatenation: https://bugs.chromium.org/p/v8/issues/detail?id=3175#c4
|
|
21
|
+
|
|
22
|
+
return [bth[buf[i++]], bth[buf[i++]], bth[buf[i++]], bth[buf[i++]], '-', bth[buf[i++]], bth[buf[i++]], '-', bth[buf[i++]], bth[buf[i++]], '-', bth[buf[i++]], bth[buf[i++]], '-', bth[buf[i++]], bth[buf[i++]], bth[buf[i++]], bth[buf[i++]], bth[buf[i++]], bth[buf[i++]]].join('');
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
var _default = bytesToUuid;
|
|
26
|
+
exports.default = _default;
|
|
27
|
+
module.exports = exports.default;
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Convert array of 16 byte values to UUID string format of the form:
|
|
3
|
+
* XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX
|
|
4
|
+
*/
|
|
5
|
+
var byteToHex = [];
|
|
6
|
+
|
|
7
|
+
for (var i = 0; i < 256; ++i) {
|
|
8
|
+
byteToHex[i] = (i + 0x100).toString(16).substr(1);
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
function bytesToUuid(buf, offset) {
|
|
12
|
+
var i = offset || 0;
|
|
13
|
+
var bth = byteToHex; // join used to fix memory issue caused by concatenation: https://bugs.chromium.org/p/v8/issues/detail?id=3175#c4
|
|
14
|
+
|
|
15
|
+
return [bth[buf[i++]], bth[buf[i++]], bth[buf[i++]], bth[buf[i++]], '-', bth[buf[i++]], bth[buf[i++]], '-', bth[buf[i++]], bth[buf[i++]], '-', bth[buf[i++]], bth[buf[i++]], '-', bth[buf[i++]], bth[buf[i++]], bth[buf[i++]], bth[buf[i++]], bth[buf[i++]], bth[buf[i++]]].join('');
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
export default bytesToUuid;
|