uuid 8.1.0 → 8.3.0

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.
Files changed (52) hide show
  1. package/CHANGELOG.md +23 -15
  2. package/CONTRIBUTING.md +2 -4
  3. package/LICENSE.md +3 -15
  4. package/README.md +222 -200
  5. package/dist/esm-browser/index.js +6 -1
  6. package/dist/esm-browser/nil.js +1 -0
  7. package/dist/esm-browser/parse.js +35 -0
  8. package/dist/esm-browser/regex.js +1 -0
  9. package/dist/esm-browser/sha1.js +3 -0
  10. package/dist/esm-browser/stringify.js +30 -0
  11. package/dist/esm-browser/v1.js +3 -3
  12. package/dist/esm-browser/v35.js +23 -22
  13. package/dist/esm-browser/v4.js +4 -9
  14. package/dist/esm-browser/validate.js +7 -0
  15. package/dist/esm-browser/version.js +11 -0
  16. package/dist/esm-node/index.js +6 -1
  17. package/dist/esm-node/nil.js +1 -0
  18. package/dist/esm-node/parse.js +35 -0
  19. package/dist/esm-node/regex.js +1 -0
  20. package/dist/esm-node/stringify.js +29 -0
  21. package/dist/esm-node/v1.js +3 -3
  22. package/dist/esm-node/v35.js +23 -22
  23. package/dist/esm-node/v4.js +4 -9
  24. package/dist/esm-node/validate.js +7 -0
  25. package/dist/esm-node/version.js +11 -0
  26. package/dist/index.js +40 -0
  27. package/dist/nil.js +8 -0
  28. package/dist/parse.js +45 -0
  29. package/dist/regex.js +8 -0
  30. package/dist/sha1-browser.js +3 -0
  31. package/dist/stringify.js +39 -0
  32. package/dist/umd/uuid.min.js +1 -1
  33. package/dist/umd/uuidNIL.min.js +1 -0
  34. package/dist/umd/uuidParse.min.js +1 -0
  35. package/dist/umd/uuidStringify.min.js +1 -0
  36. package/dist/umd/uuidValidate.min.js +1 -0
  37. package/dist/umd/uuidVersion.min.js +1 -0
  38. package/dist/umd/uuidv1.min.js +1 -1
  39. package/dist/umd/uuidv3.min.js +1 -1
  40. package/dist/umd/uuidv4.min.js +1 -1
  41. package/dist/umd/uuidv5.min.js +1 -1
  42. package/dist/uuid-bin.js +18 -4
  43. package/dist/v1.js +3 -3
  44. package/dist/v35.js +24 -22
  45. package/dist/v4.js +4 -9
  46. package/dist/validate.js +17 -0
  47. package/dist/version.js +21 -0
  48. package/package.json +34 -29
  49. package/wrapper.mjs +5 -0
  50. package/dist/bytesToUuid.js +0 -27
  51. package/dist/esm-browser/bytesToUuid.js +0 -19
  52. package/dist/esm-node/bytesToUuid.js +0 -19
package/README.md CHANGED
@@ -2,13 +2,13 @@
2
2
  -- This file is auto-generated from README_js.md. Changes should be made there.
3
3
  -->
4
4
 
5
- # uuid [![Build Status](https://github.com/uuidjs/uuid/workflows/CI/badge.svg)](https://github.com/uuidjs/uuid/actions)
5
+ # uuid [![CI](https://github.com/uuidjs/uuid/workflows/CI/badge.svg)](https://github.com/uuidjs/uuid/actions?query=workflow%3ACI) [![Browser](https://github.com/uuidjs/uuid/workflows/Browser/badge.svg)](https://github.com/uuidjs/uuid/actions?query=workflow%3ABrowser)
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
10
  - **Cross-platform** - Support for ...
11
- - CommonJS, [ECMAScript Modules](#ecmascript-modules) and UMD builds
11
+ - CommonJS, [ECMAScript Modules](#ecmascript-modules) and [CDN builds](#cdn-builds)
12
12
  - Node 8, 10, 12, 14
13
13
  - Chrome, Safari, Firefox, Edge, IE 11 browsers
14
14
  - Webpack and rollup.js module bundlers
@@ -17,102 +17,185 @@ For the creation of [RFC4122](http://www.ietf.org/rfc/rfc4122.txt) UUIDs
17
17
  - **Small** - Zero-dependency, small footprint, plays nice with "tree shaking" packagers
18
18
  - **CLI** - Includes the [`uuid` command line](#command-line) utility
19
19
 
20
- **Upgrading from uuid\@3?** Your code is probably okay, but check out [Upgrading
21
- From uuid\@3](#upgrading-from-uuid3) for details.
20
+ **Upgrading from `uuid@3.x`?** Your code is probably okay, but check out [Upgrading From `uuid@3.x`](#upgrading-from-uuid3x) for details.
22
21
 
23
22
  ## Quickstart
24
23
 
24
+ To create a random UUID...
25
+
26
+ **1. Install**
27
+
25
28
  ```shell
26
29
  npm install uuid
27
30
  ```
28
31
 
29
- Once installed, decide which type of UUID you need. RFC4122 provides for four
30
- versions, all of which are supported here. In order of popularity, they are:
31
-
32
- - Version 4 (random) - Created from cryptographically-strong random values
33
- - Version 1 (timestamp) - Created from the system clock (plus random values)
34
- - Version 5 (namespace, SHA-1) - Created from user-supplied name and namespace strings
35
- - Version 3 (namespace, MD5) - Like version 5, above, but with a poorer hash algorithm
36
-
37
- **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).
38
-
39
- ### Create Version 4 (Random) UUIDs
40
-
41
- ECMAScript Module syntax:
32
+ **2. Create a UUID** (ES6 module syntax)
42
33
 
43
34
  ```javascript
44
35
  import { v4 as uuidv4 } from 'uuid';
45
36
  uuidv4(); // ⇨ '9b1deb4d-3b7d-4bad-9bdd-2b0d7b3dcb6d'
46
37
  ```
47
38
 
48
- CommonJS syntax:
39
+ ... or using CommonJS syntax:
49
40
 
50
41
  ```javascript
51
42
  const { v4: uuidv4 } = require('uuid');
52
43
  uuidv4(); // ⇨ '1b9d6bcd-bbfd-4b2d-9b5d-ab8dfbbd4bed'
53
44
  ```
54
45
 
55
- ### Create Version 1 (Timestamp) UUIDs
46
+ For timestamp UUIDs, namespace UUIDs, and other options read on ...
47
+
48
+ ## API Summary
49
+
50
+ | | | |
51
+ | --- | --- | --- |
52
+ | [`uuid.NIL`](#uuidnil) | The nil UUID string (all zeros) | New in `uuid@8.3` |
53
+ | [`uuid.parse()`](#uuidparsestr) | Convert UUID string to array of bytes | New in `uuid@8.3` |
54
+ | [`uuid.stringify()`](#uuidstringifyarr-offset) | Convert array of bytes to UUID string | New in `uuid@8.3` |
55
+ | [`uuid.v1()`](#uuidv1options-buffer-offset) | Create a version 1 (timestamp) UUID | |
56
+ | [`uuid.v3()`](#uuidv3name-namespace-buffer-offset) | Create a version 3 (namespace w/ MD5) UUID | |
57
+ | [`uuid.v4()`](#uuidv4options-buffer-offset) | Create a version 4 (random) UUID | |
58
+ | [`uuid.v5()`](#uuidv5name-namespace-buffer-offset) | Create a version 5 (namespace w/ SHA-1) UUID | |
59
+ | [`uuid.validate()`](#uuidvalidatestr) | Test a string to see if it is a valid UUID | New in `uuid@8.3` |
60
+ | [`uuid.version()`](#uuidversionstr) | Detect RFC version of a UUID | New in `uuid@8.3` |
61
+
62
+ ## API
63
+
64
+ ### uuid.NIL
65
+
66
+ The nil UUID string (all zeros).
67
+
68
+ Example:
56
69
 
57
70
  ```javascript
58
- import { v1 as uuidv1 } from 'uuid';
59
- uuidv1(); // ⇨ '2c5ea4c0-4067-11e9-8bad-9b1deb4d3b7d'
71
+ import { NIL as NIL_UUID } from 'uuid';
72
+
73
+ NIL_UUID; // ⇨ '00000000-0000-0000-0000-000000000000'
60
74
  ```
61
75
 
62
- ### Create Version 3 or Version 5 (Namespace) UUIDs
76
+ ### uuid.parse(str)
77
+
78
+ Convert UUID string to array of bytes
63
79
 
64
- ⚠️ Version 3 and Version 5 UUIDs are basically the same, differing
65
- only in the underlying hash algorithm. Note that per the RFC, "_If backward
66
- compatibility is not an issue, SHA-1 [Version 5] is preferred_."
80
+ | | |
81
+ | --------- | ---------------------------------------- |
82
+ | `str` | A valid UUID `String` |
83
+ | _returns_ | `Uint8Array[16]` |
84
+ | _throws_ | `TypeError` if `str` is not a valid UUID |
67
85
 
68
- ⚠️ If using a custom namespace **be sure to generate your own
69
- namespace UUID**. You can grab one [here](https://www.uuidgenerator.net/).
86
+ Example:
70
87
 
71
88
  ```javascript
72
- import { v5 as uuidv5 } from 'uuid'; // For version 5
73
- import { v3 as uuidv3 } from 'uuid'; // For version 3
89
+ import { parse as uuidParse } from 'uuid';
90
+
91
+ uuidParse('6ec0bd7f-11c0-43da-975e-2a8ad9ebae0b'); // ⇨
92
+ // Uint8Array(16) [
93
+ // 110, 192, 189, 127, 17,
94
+ // 192, 67, 218, 151, 94,
95
+ // 42, 138, 217, 235, 174,
96
+ // 11
97
+ // ]
98
+ ```
74
99
 
75
- // Using predefined DNS namespace (for domain names)
76
- uuidv5('hello.example.com', uuidv5.DNS); // ⇨ 'fdda765f-fc57-5604-a269-52a7df8164ec'
77
- uuidv3('hello.example.com', uuidv3.DNS); // ⇨ '9125a8dc-52ee-365b-a5aa-81b0b3681cf6'
100
+ ### uuid.stringify(arr[, offset])
78
101
 
79
- // Using predefined URL namespace (for URLs)
80
- uuidv5('http://example.com/hello', uuidv5.URL); // ⇨ '3bbcee75-cecc-5b56-8031-b6641c1ed1f1'
81
- uuidv3('http://example.com/hello', uuidv3.URL); // ⇨ 'c6235813-3ba4-3801-ae84-e0a6ebb7d138'
102
+ Convert array of bytes to UUID string
82
103
 
83
- // Using a custom namespace (See note, above, about generating your own
84
- // namespace UUID)
85
- const MY_NAMESPACE = '1b671a64-40d5-491e-99b0-da01ff1f3341';
86
- uuidv5('Hello, World!', MY_NAMESPACE); // '630eb68f-e0fa-5ecc-887a-7c7a62614681'
87
- uuidv3('Hello, World!', MY_NAMESPACE); // ⇨ 'e8b5a51d-11c8-3310-a6ab-367563f20686'
104
+ | | |
105
+ | -------------- | --------------------------------------------------------------------------- |
106
+ | `arr` | `Array`-like collection of 16 values (starting from `offset`) between 0-255 |
107
+ | [`offset` = 0] | `Number` Starting index in the Array |
108
+ | _returns_ | `String` |
109
+ | _throws_ | `TypeError` if a valid UUID string cannot be generated |
110
+
111
+ Example:
112
+
113
+ ```javascript
114
+ import { stringify as uuidStringify } from 'uuid';
115
+
116
+ const uuidBytes = [110, 192, 189, 127, 17, 192, 67, 218, 151, 94, 42, 138, 217, 235, 174, 11];
117
+
118
+ uuidStringify(uuidBytes); // ⇨ '6ec0bd7f-11c0-43da-975e-2a8ad9ebae0b'
88
119
  ```
89
120
 
90
- ## API
121
+ ### uuid.v1([options[, buffer[, offset]]])
91
122
 
92
- ### Version 4 (Random)
123
+ Create an RFC version 1 (timestamp) UUID
124
+
125
+ | | |
126
+ | --- | --- |
127
+ | [`options`] | `Object` with one or more of the following properties: |
128
+ | [`options.node` ] | RFC "node" field as an `Array[6]` of byte values (per 4.1.6) |
129
+ | [`options.clockseq`] | RFC "clock sequence" as a `Number` between 0 - 0x3fff |
130
+ | [`options.msecs`] | RFC "timestamp" field (`Number` of milliseconds, unix epoch) |
131
+ | [`options.nsecs`] | RFC "timestamp" field (`Number` of nanseconds to add to `msecs`, should be 0-10,000) |
132
+ | [`options.random`] | `Array` of 16 random bytes (0-255) |
133
+ | [`options.rng`] | Alternative to `options.random`, a `Function` that returns an `Array` of 16 random bytes (0-255) |
134
+ | [`buffer`] | `Array \| Buffer` If specified, uuid will be written here in byte-form, starting at `offset` |
135
+ | [`offset` = 0] | `Number` Index to start writing UUID bytes in `buffer` |
136
+ | _returns_ | UUID `String` if no `buffer` is specified, otherwise returns `buffer` |
137
+ | _throws_ | `Error` if more than 10M UUIDs/sec are requested |
138
+
139
+ Note: The default [node id](https://tools.ietf.org/html/rfc4122#section-4.1.6) (the last 12 digits in the UUID) is generated once, randomly, on process startup, and then remains unchanged for the duration of the process.
140
+
141
+ Note: `options.random` and `options.rng` are only meaningful on the very first call to `v1()`, where they may be passed to initialize the internal `node` and `clockseq` fields.
142
+
143
+ Example:
93
144
 
94
145
  ```javascript
95
- import { v4 as uuidv4 } from 'uuid';
146
+ import { v1 as uuidv1 } from 'uuid';
96
147
 
97
- // Incantations
98
- uuidv4();
99
- uuidv4(options);
100
- uuidv4(options, buffer, offset);
148
+ uuidv1(); // ⇨ '2c5ea4c0-4067-11e9-8bad-9b1deb4d3b7d'
101
149
  ```
102
150
 
103
- Generate and return a RFC4122 version 4 UUID.
151
+ Example using `options`:
104
152
 
105
- - `options` - (Object) Optional uuid state to apply. Properties may include:
106
- - `random` - (Number[16]) Array of 16 numbers (0-255) to use in place of randomly generated values. Takes precedence over `options.rng`.
107
- - `rng` - (Function) Random # generator function that returns an Array[16] of byte values (0-255). Alternative to `options.random`.
108
- - `buffer` - (Array | Buffer) Array or buffer where UUID bytes are to be written.
109
- - `offset` - (Number) Starting index in `buffer` at which to begin writing.
153
+ ```javascript
154
+ import { v1 as uuidv1 } from 'uuid';
110
155
 
111
- Returns `buffer`, if specified, otherwise the string form of the UUID
156
+ const v1options = {
157
+ node: [0x01, 0x23, 0x45, 0x67, 0x89, 0xab],
158
+ clockseq: 0x1234,
159
+ msecs: new Date('2011-11-01').getTime(),
160
+ nsecs: 5678,
161
+ };
162
+ uuidv1(v1options); // ⇨ '710b962e-041c-11e1-9234-0123456789ab'
163
+ ```
164
+
165
+ ### uuid.v3(name, namespace[, buffer[, offset]])
166
+
167
+ Create an RFC version 3 (namespace w/ MD5) UUID
168
+
169
+ API is identical to `v5()`, but uses "v3" instead.
170
+
171
+ ⚠️ Note: Per the RFC, "_If backward compatibility is not an issue, SHA-1 [Version 5] is preferred_."
172
+
173
+ ### uuid.v4([options[, buffer[, offset]]])
112
174
 
113
- Example: Generate string UUID with predefined `random` values
175
+ Create an RFC version 4 (random) UUID
176
+
177
+ | | |
178
+ | --- | --- |
179
+ | [`options`] | `Object` with one or more of the following properties: |
180
+ | [`options.random`] | `Array` of 16 random bytes (0-255) |
181
+ | [`options.rng`] | Alternative to `options.random`, a `Function` that returns an `Array` of 16 random bytes (0-255) |
182
+ | [`buffer`] | `Array \| Buffer` If specified, uuid will be written here in byte-form, starting at `offset` |
183
+ | [`offset` = 0] | `Number` Index to start writing UUID bytes in `buffer` |
184
+ | _returns_ | UUID `String` if no `buffer` is specified, otherwise returns `buffer` |
185
+
186
+ Example:
114
187
 
115
188
  ```javascript
189
+ import { v4 as uuidv4 } from 'uuid';
190
+
191
+ uuidv4(); // ⇨ '1b9d6bcd-bbfd-4b2d-9b5d-ab8dfbbd4bed'
192
+ ```
193
+
194
+ Example using predefined `random` values:
195
+
196
+ ```javascript
197
+ import { v4 as uuidv4 } from 'uuid';
198
+
116
199
  const v4options = {
117
200
  random: [
118
201
  0x10,
@@ -136,137 +219,75 @@ const v4options = {
136
219
  uuidv4(v4options); // ⇨ '109156be-c4fb-41ea-b1b4-efe1671c5836'
137
220
  ```
138
221
 
139
- Example: Generate two IDs in a single buffer
140
-
141
- ```javascript
142
- const buffer = new Array();
143
- uuidv4(null, buffer, 0); // ⇨
144
- // [
145
- // 27, 157, 107, 205, 187,
146
- // 253, 75, 45, 155, 93,
147
- // 171, 141, 251, 189, 75,
148
- // 237
149
- // ]
150
- uuidv4(null, buffer, 16); // ⇨
151
- // [
152
- // 27, 157, 107, 205, 187, 253, 75, 45,
153
- // 155, 93, 171, 141, 251, 189, 75, 237,
154
- // 155, 29, 235, 77, 59, 125, 75, 173,
155
- // 155, 221, 43, 13, 123, 61, 203, 109
156
- // ]
157
- ```
158
-
159
- ### Version 1 (Timestamp)
160
-
161
- ```javascript
162
- import { v1 as uuidv1 } from 'uuid';
222
+ ### uuid.v5(name, namespace[, buffer[, offset]])
163
223
 
164
- // Incantations
165
- uuidv1();
166
- uuidv1(options);
167
- uuidv1(options, buffer, offset);
168
- ```
224
+ Createa an RFC version 5 (namespace w/ SHA-1) UUID
169
225
 
170
- Generate and return a RFC4122 version 1 (timestamp) UUID.
226
+ | | |
227
+ | --- | --- |
228
+ | `name` | `String \| Array` |
229
+ | `namespace` | `String \| Array[16]` Namespace UUID |
230
+ | [`buffer`] | `Array \| Buffer` If specified, uuid will be written here in byte-form, starting at `offset` |
231
+ | [`offset` = 0] | `Number` Index to start writing UUID bytes in `buffer` |
232
+ | _returns_ | UUID `String` if no `buffer` is specified, otherwise returns `buffer` |
171
233
 
172
- - `options` - (Object) Optional uuid state to apply. Properties may include:
173
- - `node` - (Array) Node id as Array of 6 bytes (per 4.1.6). Default: Randomly generated ID. See note 1.
174
- - `clockseq` - (Number between 0 - 0x3fff) RFC clock sequence. Default: An internally maintained clockseq is used.
175
- - `msecs` - (Number) Time in milliseconds since unix Epoch. Default: The current time is used.
176
- - `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.
177
- - `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`.
178
- - `rng` - (Function) Random # generator function that returns an Array[16] of byte values (0-255). Alternative to `options.random`.
179
- - `buffer` - (Array | Buffer) Array or buffer where UUID bytes are to be written.
180
- - `offset` - (Number) Starting index in `buffer` at which to begin writing.
234
+ Note: The RFC `DNS` and `URL` namespaces are available as `v5.DNS` and `v5.URL`.
181
235
 
182
- Returns `buffer`, if specified, otherwise the string form of the UUID
183
-
184
- Note: The default [node id](https://tools.ietf.org/html/rfc4122#section-4.1.6) (the last 12 digits in the UUID) is generated once, randomly, on process startup, and then remains unchanged for the duration of the process.
185
-
186
- Example: Generate string UUID with fully-specified options
236
+ Example with custom namespace:
187
237
 
188
238
  ```javascript
189
- const v1options = {
190
- node: [0x01, 0x23, 0x45, 0x67, 0x89, 0xab],
191
- clockseq: 0x1234,
192
- msecs: new Date('2011-11-01').getTime(),
193
- nsecs: 5678,
194
- };
195
- uuidv1(v1options); // ⇨ '710b962e-041c-11e1-9234-0123456789ab'
196
- ```
239
+ import { v5 as uuidv5 } from 'uuid';
197
240
 
198
- Example: In-place generation of two binary IDs
241
+ // Define a custom namespace. Readers, create your own using something like
242
+ // https://www.uuidgenerator.net/
243
+ const MY_NAMESPACE = '1b671a64-40d5-491e-99b0-da01ff1f3341';
199
244
 
200
- ```javascript
201
- // Generate two ids in an array
202
- const arr = new Array();
203
- uuidv1(null, arr, 0); // ⇨
204
- // [
205
- // 44, 94, 164, 192, 64, 103,
206
- // 17, 233, 146, 52, 155, 29,
207
- // 235, 77, 59, 125
208
- // ]
209
- uuidv1(null, arr, 16); // ⇨
210
- // [
211
- // 44, 94, 164, 192, 64, 103, 17, 233,
212
- // 146, 52, 155, 29, 235, 77, 59, 125,
213
- // 44, 94, 164, 193, 64, 103, 17, 233,
214
- // 146, 52, 155, 29, 235, 77, 59, 125
215
- // ]
245
+ uuidv5('Hello, World!', MY_NAMESPACE); // ⇨ '630eb68f-e0fa-5ecc-887a-7c7a62614681'
216
246
  ```
217
247
 
218
- ### Version 5 (Namespace)
248
+ Example with RFC `URL` namespace:
219
249
 
220
250
  ```javascript
221
251
  import { v5 as uuidv5 } from 'uuid';
222
252
 
223
- // Incantations
224
- uuidv5(name, namespace);
225
- uuidv5(name, namespace, buffer);
226
- uuidv5(name, namespace, buffer, offset);
253
+ uuidv5('https://www.w3.org/', uuidv5.URL); // ⇨ 'c106a26a-21bb-5538-8bf2-57095d1976c1'
227
254
  ```
228
255
 
229
- Generate and return a RFC4122 version 5 UUID.
256
+ ### uuid.validate(str)
230
257
 
231
- - `name` - (String | Array[]) "name" to create UUID with
232
- - `namespace` - (String | Array[]) "namespace" UUID either as a String or Array[16] of byte values
233
- - `buffer` - (Array | Buffer) Array or buffer where UUID bytes are to be written.
234
- - `offset` - (Number) Starting index in `buffer` at which to begin writing. Default = 0
258
+ Test a string to see if it is a valid UUID
235
259
 
236
- Returns `buffer`, if specified, otherwise the string form of the UUID
260
+ | | |
261
+ | --------- | --------------------------------------------------- |
262
+ | `str` | `String` to validate |
263
+ | _returns_ | `true` if string is a valid UUID, `false` otherwise |
237
264
 
238
265
  Example:
239
266
 
240
267
  ```javascript
241
- uuidv5('hello world', MY_NAMESPACE); // '9f282611-e0fd-5650-8953-89c8e342da0b'
242
- ```
243
-
244
- ### Version 3 (Namespace)
245
-
246
- ⚠️ Note: Per the RFC, "_If backward compatibility is not an issue, SHA-1 [Version 5] is preferred_."
247
-
248
- ```javascript
249
- import { v3 as uuidv3 } from 'uuid';
268
+ import { validate as uuidValidate } from 'uuid';
250
269
 
251
- // Incantations
252
- uuidv3(name, namespace);
253
- uuidv3(name, namespace, buffer);
254
- uuidv3(name, namespace, buffer, offset);
270
+ uuidValidate('not a UUID'); // ⇨ false
271
+ uuidValidate('6ec0bd7f-11c0-43da-975e-2a8ad9ebae0b'); // ⇨ true
255
272
  ```
256
273
 
257
- Generate and return a RFC4122 version 3 UUID.
274
+ ### uuid.version(str)
258
275
 
259
- - `name` - (String | Array[]) "name" to create UUID with
260
- - `namespace` - (String | Array[]) "namespace" UUID either as a String or Array[16] of byte values
261
- - `buffer` - (Array | Buffer) Array or buffer where UUID bytes are to be written.
262
- - `offset` - (Number) Starting index in `buffer` at which to begin writing. Default = 0
276
+ Detect RFC version of a UUID
263
277
 
264
- Returns `buffer`, if specified, otherwise the string form of the UUID
278
+ | | |
279
+ | --------- | ---------------------------------------- |
280
+ | `str` | A valid UUID `String` |
281
+ | _returns_ | `Number` The RFC version of the UUID |
282
+ | _throws_ | `TypeError` if `str` is not a valid UUID |
265
283
 
266
284
  Example:
267
285
 
268
286
  ```javascript
269
- uuidv3('hello world', MY_NAMESPACE); // '042ffd34-d989-321c-ad06-f60826172424'
287
+ import { version as uuidVersion } from 'uuid';
288
+
289
+ uuidVersion('45637ec4-c85f-11ea-87d0-0242ac130003'); // ⇨ 1
290
+ uuidVersion('6ec0bd7f-11c0-43da-975e-2a8ad9ebae0b'); // ⇨ 4
270
291
  ```
271
292
 
272
293
  ## Command Line
@@ -280,7 +301,7 @@ ddeb27fb-d9a0-4624-be4d-4615062daed4
280
301
 
281
302
  The default is to generate version 4 UUIDS, however the other versions are supported. Type `uuid --help` for details:
282
303
 
283
- ```
304
+ ```shell
284
305
  $ uuid --help
285
306
 
286
307
  Usage:
@@ -297,12 +318,7 @@ defined by RFC4122
297
318
 
298
319
  ## ECMAScript Modules
299
320
 
300
- This library comes with [ECMAScript
301
- Modules](https://www.ecma-international.org/ecma-262/6.0/#sec-modules) (ESM) support for Node.js
302
- versions that support it ([example](./examples/node-esmodules/)) as well as bundlers like
303
- [rollup.js](https://rollupjs.org/guide/en/#tree-shaking) ([example](./examples/browser-rollup/))
304
- and [webpack](https://webpack.js.org/guides/tree-shaking/)
305
- ([example](./examples/browser-webpack/)) (targeting both, Node.js and browser environments).
321
+ This library comes with [ECMAScript Modules](https://www.ecma-international.org/ecma-262/6.0/#sec-modules) (ESM) support for Node.js versions that support it ([example](./examples/node-esmodules/)) as well as bundlers like [rollup.js](https://rollupjs.org/guide/en/#tree-shaking) ([example](./examples/browser-rollup/)) and [webpack](https://webpack.js.org/guides/tree-shaking/) ([example](./examples/browser-webpack/)) (targeting both, Node.js and browser environments).
306
322
 
307
323
  ```javascript
308
324
  import { v4 as uuidv4 } from 'uuid';
@@ -311,41 +327,58 @@ uuidv4(); // ⇨ '1b9d6bcd-bbfd-4b2d-9b5d-ab8dfbbd4bed'
311
327
 
312
328
  To run the examples you must first create a dist build of this library in the module root:
313
329
 
314
- ```
330
+ ```shell
315
331
  npm run build
316
332
  ```
317
333
 
318
- ## UMD Builds
334
+ ## CDN Builds
319
335
 
320
- If you want to load a minified UMD build directly in the browser you can use one of the popular npm
321
- CDNs:
336
+ ### ECMAScript Modules
337
+
338
+ To load this module directly into modern browsers that [support loading ECMAScript Modules](https://caniuse.com/#feat=es6-module) you can make use of [jspm](https://jspm.org/):
322
339
 
323
340
  ```html
324
- <script src="https://unpkg.com/uuid@latest/dist/umd/uuidv4.min.js"></script>
325
- <script>
326
- alert(uuidv4());
341
+ <script type="module">
342
+ import { v4 as uuidv4 } from 'https://jspm.dev/uuid';
343
+ console.log(uuidv4()); // ⇨ '1b9d6bcd-bbfd-4b2d-9b5d-ab8dfbbd4bed'
327
344
  </script>
328
345
  ```
329
346
 
330
- or
347
+ ### UMD
348
+
349
+ To load this module directly into older browsers you can use the [UMD (Universal Module Definition)](https://github.com/umdjs/umd) builds from any of the following CDNs:
350
+
351
+ **Using [UNPKG](https://unpkg.com/uuid@latest/dist/umd/)**:
352
+
353
+ ```html
354
+ <script src="https://unpkg.com/uuid@latest/dist/umd/uuidv4.min.js"></script>
355
+ ```
356
+
357
+ **Using [jsDelivr](https://cdn.jsdelivr.net/npm/uuid@latest/dist/umd/)**:
331
358
 
332
359
  ```html
333
360
  <script src="https://cdn.jsdelivr.net/npm/uuid@latest/dist/umd/uuidv4.min.js"></script>
361
+ ```
362
+
363
+ **Using [cdnjs](https://cdnjs.com/libraries/uuid)**:
364
+
365
+ ```html
366
+ <script src="https://cdnjs.cloudflare.com/ajax/libs/uuid/8.1.0/uuidv4.min.js"></script>
367
+ ```
368
+
369
+ These CDNs all provide the same [`uuidv4()`](#uuidv4options-buffer-offset) method:
370
+
371
+ ```html
334
372
  <script>
335
- alert(uuidv4());
373
+ uuidv4(); // ⇨ '55af1e37-0734-46d8-b070-a1e42e4fc392'
336
374
  </script>
337
375
  ```
338
376
 
339
- Available bundles:
340
-
341
- - https://unpkg.com/uuid@latest/dist/umd/
342
- - https://cdn.jsdelivr.net/npm/uuid@latest/dist/umd/
377
+ Methods for the other algorithms ([`uuidv1()`](#uuidv1options-buffer-offset), [`uuidv3()`](#uuidv3name-namespace-buffer-offset) and [`uuidv5()`](#uuidv5name-namespace-buffer-offset)) are available from the files `uuidv1.min.js`, `uuidv3.min.js` and `uuidv5.min.js` respectively.
343
378
 
344
379
  ## "getRandomValues() not supported"
345
380
 
346
- This error occurs in environments where the standard
347
- [`crypto.getRandomValues()`](https://developer.mozilla.org/en-US/docs/Web/API/Crypto/getRandomValues)
348
- API is not supported. This issue can be resolved by adding an appropriate polyfill:
381
+ This error occurs in environments where the standard [`crypto.getRandomValues()`](https://developer.mozilla.org/en-US/docs/Web/API/Crypto/getRandomValues) API is not supported. This issue can be resolved by adding an appropriate polyfill:
349
382
 
350
383
  ### React Native
351
384
 
@@ -359,17 +392,13 @@ import { v4 as uuidv4 } from 'uuid';
359
392
 
360
393
  ### Web Workers / Service Workers (Edge <= 18)
361
394
 
362
- [In Edge <= 18, Web Crypto is not supported in Web Workers or Service
363
- Workers](https://caniuse.com/#feat=cryptography) and we are not aware of a polyfill (let us know if
364
- you find one, please).
395
+ [In Edge <= 18, Web Crypto is not supported in Web Workers or Service Workers](https://caniuse.com/#feat=cryptography) and we are not aware of a polyfill (let us know if you find one, please).
365
396
 
366
- ## Upgrading From uuid\@7
397
+ ## Upgrading From `uuid@7.x`
367
398
 
368
399
  ### Only Named Exports Supported When Using with Node.js ESM
369
400
 
370
- uuid\@7 did not come with native ECMAScript Module (ESM) support for Node.js. Importing it in
371
- Node.js ESM consequently imported the CommonJS source with a default export. This library now comes
372
- with true Node.js ESM support and only provides named exports.
401
+ `uuid@7.x` did not come with native ECMAScript Module (ESM) support for Node.js. Importing it in Node.js ESM consequently imported the CommonJS source with a default export. This library now comes with true Node.js ESM support and only provides named exports.
373
402
 
374
403
  Instead of doing:
375
404
 
@@ -387,31 +416,24 @@ uuidv4();
387
416
 
388
417
  ### Deep Requires No Longer Supported
389
418
 
390
- Deep requires like `require('uuid/v4')` [which have been deprecated in
391
- uuid\@7](#deep-requires-now-deprecated) are no longer supported.
419
+ Deep requires like `require('uuid/v4')` [which have been deprecated in `uuid@7.x`](#deep-requires-now-deprecated) are no longer supported.
392
420
 
393
- ## Upgrading From uuid\@3
421
+ ## Upgrading From `uuid@3.x`
394
422
 
395
- "_Wait... what happened to uuid\@4 - uuid\@6?!?_"
423
+ "_Wait... what happened to `uuid@4.x` - `uuid@6.x`?!?_"
396
424
 
397
- In order to avoid confusion with RFC [version 4](#version-4-random) and [version
398
- 5](#version-5-namespace) UUIDs, and a possible [version
399
- 6](http://gh.peabody.io/uuidv6/), releases 4 thru 6 of this module have been
400
- skipped. Hence, how we're now at uuid\@7.
425
+ In order to avoid confusion with RFC [version 4](#uuidv4options-buffer-offset) and [version 5](#uuidv5name-namespace-buffer-offset) UUIDs, and a possible [version 6](http://gh.peabody.io/uuidv6/), releases 4 thru 6 of this module have been skipped.
401
426
 
402
427
  ### Deep Requires Now Deprecated
403
428
 
404
- uuid\@3 encouraged the use of deep requires to minimize the bundle size of
405
- browser builds:
429
+ `uuid@3.x` encouraged the use of deep requires to minimize the bundle size of browser builds:
406
430
 
407
431
  ```javascript
408
432
  const uuidv4 = require('uuid/v4'); // <== NOW DEPRECATED!
409
433
  uuidv4();
410
434
  ```
411
435
 
412
- As of uuid\@7 this library now provides ECMAScript modules builds, which allow
413
- packagers like Webpack and Rollup to do "tree-shaking" to remove dead code.
414
- Instead, use the `import` syntax:
436
+ As of `uuid@7.x` this library now provides ECMAScript modules builds, which allow packagers like Webpack and Rollup to do "tree-shaking" to remove dead code. Instead, use the `import` syntax:
415
437
 
416
438
  ```javascript
417
439
  import { v4 as uuidv4 } from 'uuid';
@@ -427,13 +449,13 @@ uuidv4();
427
449
 
428
450
  ### Default Export Removed
429
451
 
430
- uuid\@3 was exporting the Version 4 UUID method as a default export:
452
+ `uuid@3.x` was exporting the Version 4 UUID method as a default export:
431
453
 
432
454
  ```javascript
433
455
  const uuid = require('uuid'); // <== REMOVED!
434
456
  ```
435
457
 
436
- This usage pattern was already discouraged in uuid\@3 and has been removed in uuid\@7.
458
+ This usage pattern was already discouraged in `uuid@3.x` and has been removed in `uuid@7.x`.
437
459
 
438
460
  ----
439
461
  Markdown generated from [README_js.md](README_js.md) by [![RunMD Logo](http://i.imgur.com/h0FVyzU.png)](https://github.com/broofa/runmd)
@@ -1,4 +1,9 @@
1
1
  export { default as v1 } from './v1.js';
2
2
  export { default as v3 } from './v3.js';
3
3
  export { default as v4 } from './v4.js';
4
- export { default as v5 } from './v5.js';
4
+ export { default as v5 } from './v5.js';
5
+ export { default as NIL } from './nil.js';
6
+ export { default as version } from './version.js';
7
+ export { default as validate } from './validate.js';
8
+ export { default as stringify } from './stringify.js';
9
+ export { default as parse } from './parse.js';
@@ -0,0 +1 @@
1
+ export default '00000000-0000-0000-0000-000000000000';
@@ -0,0 +1,35 @@
1
+ import validate from './validate.js';
2
+
3
+ function parse(uuid) {
4
+ if (!validate(uuid)) {
5
+ throw TypeError('Invalid UUID');
6
+ }
7
+
8
+ var v;
9
+ var arr = new Uint8Array(16); // Parse ########-....-....-....-............
10
+
11
+ arr[0] = (v = parseInt(uuid.slice(0, 8), 16)) >>> 24;
12
+ arr[1] = v >>> 16 & 0xff;
13
+ arr[2] = v >>> 8 & 0xff;
14
+ arr[3] = v & 0xff; // Parse ........-####-....-....-............
15
+
16
+ arr[4] = (v = parseInt(uuid.slice(9, 13), 16)) >>> 8;
17
+ arr[5] = v & 0xff; // Parse ........-....-####-....-............
18
+
19
+ arr[6] = (v = parseInt(uuid.slice(14, 18), 16)) >>> 8;
20
+ arr[7] = v & 0xff; // Parse ........-....-....-####-............
21
+
22
+ arr[8] = (v = parseInt(uuid.slice(19, 23), 16)) >>> 8;
23
+ arr[9] = v & 0xff; // Parse ........-....-....-....-############
24
+ // (Use "/" to avoid 32-bit truncation when bit-shifting high-order bytes)
25
+
26
+ arr[10] = (v = parseInt(uuid.slice(24, 36), 16)) / 0x10000000000 & 0xff;
27
+ arr[11] = v / 0x100000000 & 0xff;
28
+ arr[12] = v >>> 24 & 0xff;
29
+ arr[13] = v >>> 16 & 0xff;
30
+ arr[14] = v >>> 8 & 0xff;
31
+ arr[15] = v & 0xff;
32
+ return arr;
33
+ }
34
+
35
+ export default parse;
@@ -0,0 +1 @@
1
+ export default /^(?:[0-9a-f]{8}-[0-9a-f]{4}-[1-5][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}|00000000-0000-0000-0000-000000000000)$/i;
@@ -32,6 +32,9 @@ function sha1(bytes) {
32
32
  for (var i = 0; i < msg.length; ++i) {
33
33
  bytes.push(msg.charCodeAt(i));
34
34
  }
35
+ } else if (!Array.isArray(bytes)) {
36
+ // Convert Array-like to Array
37
+ bytes = Array.prototype.slice.call(bytes);
35
38
  }
36
39
 
37
40
  bytes.push(0x80);