Skip to content

Commit 4f93fc8

Browse files
committed
docs: document esm features
1 parent b238510 commit 4f93fc8

3 files changed

Lines changed: 146 additions & 112 deletions

File tree

README.md

Lines changed: 76 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -9,88 +9,104 @@ Simple, fast generation of [RFC4122](http://www.ietf.org/rfc/rfc4122.txt) UUIDS.
99
Features:
1010

1111
* Support for version 1, 3, 4 and 5 UUIDs
12-
* Cross-platform
12+
* Cross-platform: CommonJS build for Node.js and [ECMAScript Modules](#ecmascript-modules) for the
13+
browser.
1314
* Uses cryptographically-strong random number APIs (when available)
1415
* Zero-dependency, small footprint (... but not [this small](https://github.com/__gist/982883))
1516

16-
[**Deprecation warning**: The use of `require('uuid')` is deprecated and will not be
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)
17+
## Quickstart - Node.js/CommonJS
2018

2119
```shell
2220
npm install uuid
2321
```
2422

25-
Then generate your uuid version of choice ...
23+
Then generate a random UUID (v4 algorithm), which is almost always what you want ...
24+
25+
Version 4 (random):
26+
27+
```javascript
28+
const uuid = require('uuid');
29+
uuid.v4(); // ⇨ '9b1deb4d-3b7d-4bad-9bdd-2b0d7b3dcb6d'
30+
31+
```
32+
33+
Or generate UUIDs with other algorithms of your choice ...
2634

2735
Version 1 (timestamp):
2836

2937
```javascript
30-
const uuidv1 = require('uuid/v1');
31-
uuidv1(); // ⇨ '2c5ea4c0-4067-11e9-8bad-9b1deb4d3b7d'
38+
const uuid = require('uuid');
39+
uuid.v1(); // ⇨ '2c5ea4c0-4067-11e9-8b2d-1b9d6bcdbbfd'
3240

3341
```
3442

3543
Version 3 (namespace):
3644

3745
```javascript
38-
const uuidv3 = require('uuid/v3');
46+
const uuid = require('uuid');
3947

4048
// ... using predefined DNS namespace (for domain names)
41-
uuidv3('hello.example.com', uuidv3.DNS); // ⇨ '9125a8dc-52ee-365b-a5aa-81b0b3681cf6'
49+
uuid.v3('hello.example.com', uuid.v3.DNS); // ⇨ '9125a8dc-52ee-365b-a5aa-81b0b3681cf6'
4250

4351
// ... using predefined URL namespace (for, well, URLs)
44-
uuidv3('http://example.com/hello', uuidv3.URL); // ⇨ 'c6235813-3ba4-3801-ae84-e0a6ebb7d138'
52+
uuid.v3('http://example.com/hello', uuid.v3.URL); // ⇨ 'c6235813-3ba4-3801-ae84-e0a6ebb7d138'
4553

4654
// ... using a custom namespace
4755
//
4856
// Note: Custom namespaces should be a UUID string specific to your application!
4957
// E.g. the one here was generated using this modules `uuid` CLI.
5058
const MY_NAMESPACE = '1b671a64-40d5-491e-99b0-da01ff1f3341';
51-
uuidv3('Hello, World!', MY_NAMESPACE); // ⇨ 'e8b5a51d-11c8-3310-a6ab-367563f20686'
52-
53-
```
54-
55-
Version 4 (random):
56-
57-
```javascript
58-
const uuidv4 = require('uuid/v4');
59-
uuidv4(); // ⇨ '1b9d6bcd-bbfd-4b2d-9b5d-ab8dfbbd4bed'
59+
uuid.v3('Hello, World!', MY_NAMESPACE); // ⇨ 'e8b5a51d-11c8-3310-a6ab-367563f20686'
6060

6161
```
6262

6363
Version 5 (namespace):
6464

6565
```javascript
66-
const uuidv5 = require('uuid/v5');
66+
const uuid = require('uuid');
6767

6868
// ... using predefined DNS namespace (for domain names)
69-
uuidv5('hello.example.com', uuidv5.DNS); // ⇨ 'fdda765f-fc57-5604-a269-52a7df8164ec'
69+
uuid.v5('hello.example.com', uuid.v5.DNS); // ⇨ 'fdda765f-fc57-5604-a269-52a7df8164ec'
7070

7171
// ... using predefined URL namespace (for, well, URLs)
72-
uuidv5('http://example.com/hello', uuidv5.URL); // ⇨ '3bbcee75-cecc-5b56-8031-b6641c1ed1f1'
72+
uuid.v5('http://example.com/hello', uuid.v5.URL); // ⇨ '3bbcee75-cecc-5b56-8031-b6641c1ed1f1'
7373

7474
// ... using a custom namespace
7575
//
7676
// Note: Custom namespaces should be a UUID string specific to your application!
7777
// E.g. the one here was generated using this modules `uuid` CLI.
7878
const MY_NAMESPACE = '1b671a64-40d5-491e-99b0-da01ff1f3341';
79-
uuidv5('Hello, World!', MY_NAMESPACE); // ⇨ '630eb68f-e0fa-5ecc-887a-7c7a62614681'
79+
uuid.v5('Hello, World!', MY_NAMESPACE); // ⇨ '630eb68f-e0fa-5ecc-887a-7c7a62614681'
80+
81+
```
82+
83+
## ECMAScript Modules / ESM
84+
85+
For usage in the browser `uuid` provides support for [ECMAScript
86+
Modules](https://www.ecma-international.org/ecma-262/6.0/#sec-modules) (ESM) that enable
87+
tree-shaking for bundlers, like [rollup.js](https://rollupjs.org/guide/en/#tree-shaking)
88+
([example](./examples/browser-rollup/)) and [webpack](https://webpack.js.org/guides/tree-shaking/)
89+
([example](./examples/browser-webpack/)).
8090

91+
```javascript
92+
import {v4 as uuid} from 'uuid';
93+
uuid(); // ⇨ '1b9d6bcd-bbfd-4b2d-9b5d-ab8dfbbd4bed'
8194
```
8295

96+
There is experimental native ESM support for [the browser](./examples/browser-esmodules/) but it
97+
should not be considered ready for production use and may change or disappear in future releases.
98+
8399
## API
84100

85-
### Version 1
101+
### Version 1 (Timestamp + Node)
86102

87103
```javascript
88-
const uuidv1 = require('uuid/v1');
104+
const uuid = require('uuid');
89105

90106
// Incantations
91-
uuidv1();
92-
uuidv1(options);
93-
uuidv1(options, buffer, offset);
107+
uuid.v1();
108+
uuid.v1(options);
109+
uuid.v1(options, buffer, offset);
94110
```
95111

96112
Generate and return a RFC4122 v1 (timestamp-based) UUID.
@@ -118,7 +134,7 @@ const v1options = {
118134
msecs: new Date('2011-11-01').getTime(),
119135
nsecs: 5678
120136
};
121-
uuidv1(v1options); // ⇨ '710b962e-041c-11e1-9234-0123456789ab'
137+
uuid.v1(v1options); // ⇨ '710b962e-041c-11e1-9234-0123456789ab'
122138

123139
```
124140

@@ -127,31 +143,32 @@ Example: In-place generation of two binary IDs
127143
```javascript
128144
// Generate two ids in an array
129145
const arr = new Array();
130-
uuidv1(null, arr, 0); //
146+
uuid.v1(null, arr, 0); //
131147
// [
132-
// 44, 94, 164, 192, 64, 103,
133-
// 17, 233, 146, 52, 155, 29,
134-
// 235, 77, 59, 125
148+
// 44, 94, 164, 192, 64,
149+
// 103, 17, 233, 146, 52,
150+
// 27, 157, 107, 205, 187,
151+
// 253
135152
// ]
136-
uuidv1(null, arr, 16); //
153+
uuid.v1(null, arr, 16); //
137154
// [
138-
// 44, 94, 164, 192, 64, 103, 17, 233,
139-
// 146, 52, 155, 29, 235, 77, 59, 125,
140-
// 44, 94, 164, 193, 64, 103, 17, 233,
141-
// 146, 52, 155, 29, 235, 77, 59, 125
155+
// 44, 94, 164, 192, 64, 103, 17, 233,
156+
// 146, 52, 27, 157, 107, 205, 187, 253,
157+
// 44, 94, 164, 193, 64, 103, 17, 233,
158+
// 146, 52, 27, 157, 107, 205, 187, 253
142159
// ]
143160

144161
```
145162

146-
### Version 3
163+
### Version 3 (Namespace)
147164

148165
```javascript
149-
const uuidv3 = require('uuid/v3');
166+
const uuid = require('uuid');
150167

151168
// Incantations
152-
uuidv3(name, namespace);
153-
uuidv3(name, namespace, buffer);
154-
uuidv3(name, namespace, buffer, offset);
169+
uuid.v3(name, namespace);
170+
uuid.v3(name, namespace, buffer);
171+
uuid.v3(name, namespace, buffer, offset);
155172
```
156173

157174
Generate and return a RFC4122 v3 UUID.
@@ -166,19 +183,19 @@ Returns `buffer`, if specified, otherwise the string form of the UUID
166183
Example:
167184

168185
```javascript
169-
uuidv3('hello world', MY_NAMESPACE); // ⇨ '042ffd34-d989-321c-ad06-f60826172424'
186+
uuid.v3('hello world', MY_NAMESPACE); // ⇨ '042ffd34-d989-321c-ad06-f60826172424'
170187

171188
```
172189

173-
### Version 4
190+
### Version 4 (Random)
174191

175192
```javascript
176-
const uuidv4 = require('uuid/v4')
193+
const uuid = require('uuid');
177194

178195
// Incantations
179-
uuidv4();
180-
uuidv4(options);
181-
uuidv4(options, buffer, offset);
196+
uuid.v4();
197+
uuid.v4(options);
198+
uuid.v4(options, buffer, offset);
182199
```
183200

184201
Generate and return a RFC4122 v4 UUID.
@@ -200,22 +217,22 @@ const v4options = {
200217
0x71, 0xb4, 0xef, 0xe1, 0x67, 0x1c, 0x58, 0x36
201218
]
202219
};
203-
uuidv4(v4options); // ⇨ '109156be-c4fb-41ea-b1b4-efe1671c5836'
220+
uuid.v4(v4options); // ⇨ '109156be-c4fb-41ea-b1b4-efe1671c5836'
204221

205222
```
206223

207224
Example: Generate two IDs in a single buffer
208225

209226
```javascript
210227
const buffer = new Array();
211-
uuidv4(null, buffer, 0); //
228+
uuid.v4(null, buffer, 0); //
212229
// [
213230
// 155, 29, 235, 77, 59,
214231
// 125, 75, 173, 155, 221,
215232
// 43, 13, 123, 61, 203,
216233
// 109
217234
// ]
218-
uuidv4(null, buffer, 16); //
235+
uuid.v4(null, buffer, 16); //
219236
// [
220237
// 155, 29, 235, 77, 59, 125, 75, 173,
221238
// 155, 221, 43, 13, 123, 61, 203, 109,
@@ -225,15 +242,15 @@ uuidv4(null, buffer, 16); // ⇨
225242

226243
```
227244

228-
### Version 5
245+
### Version 5 (Namespace)
229246

230247
```javascript
231-
const uuidv5 = require('uuid/v5');
248+
const uuid = require('uuid');
232249

233250
// Incantations
234-
uuidv5(name, namespace);
235-
uuidv5(name, namespace, buffer);
236-
uuidv5(name, namespace, buffer, offset);
251+
uuid.v5(name, namespace);
252+
uuid.v5(name, namespace, buffer);
253+
uuid.v5(name, namespace, buffer, offset);
237254
```
238255

239256
Generate and return a RFC4122 v5 UUID.
@@ -248,7 +265,7 @@ Returns `buffer`, if specified, otherwise the string form of the UUID
248265
Example:
249266

250267
```javascript
251-
uuidv5('hello world', MY_NAMESPACE); // ⇨ '9f282611-e0fd-5650-8953-89c8e342da0b'
268+
uuid.v5('hello world', MY_NAMESPACE); // ⇨ '9f282611-e0fd-5650-8953-89c8e342da0b'
252269

253270
```
254271

0 commit comments

Comments
 (0)