[79a0317] | 1 | <!--
|
---|
| 2 | -- This file is auto-generated from README_js.md. Changes should be made there.
|
---|
| 3 | -->
|
---|
| 4 |
|
---|
| 5 |
|
---|
| 6 | # 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)
|
---|
| 7 |
|
---|
| 8 | For the creation of [RFC9562](https://www.rfc-editor.org/rfc/rfc9562.html) (formally [RFC4122](https://www.rfc-editor.org/rfc/rfc4122.html)) UUIDs
|
---|
| 9 |
|
---|
| 10 | - **Complete** - Support for all RFC9562 UUID versions
|
---|
| 11 | - **Cross-platform** - Support for ...
|
---|
| 12 | - CommonJS, [ECMAScript Modules](#ecmascript-modules)
|
---|
| 13 | - NodeJS 16+ ([LTS releases](https://github.com/nodejs/Release))
|
---|
| 14 | - Chrome, Safari, Firefox, Edge browsers
|
---|
| 15 | - [React Native / Expo](#react-native--expo)
|
---|
| 16 | - **Secure** - Cryptographically-strong random values
|
---|
| 17 | - **Compact** - No dependencies, [tree-shakable](https://developer.mozilla.org/en-US/docs/Glossary/Tree_shaking)
|
---|
| 18 | - **CLI** - Includes the [`uuid` command line](#command-line) utility
|
---|
| 19 | - **Typescript** - Types now included
|
---|
| 20 |
|
---|
| 21 | <!-- prettier-ignore -->
|
---|
| 22 | > [!NOTE]
|
---|
| 23 | w> `uuid@11` is now available: See the [CHANGELOG](./CHANGELOG.md) for details. TL;DR:
|
---|
| 24 | > * TypeScript support is now included (remove `@types/uuid` from your dependencies)
|
---|
| 25 | > * Subtle changes to how the `options` arg is interpreted for `v1()`, `v6()`, and `v7()`. [See details](#options-handling-for-timestamp-uuids)
|
---|
| 26 | > * Binary UUIDs are now `Uint8Array`s. (May impact callers of `parse()`, `stringify()`, or that pass an `option#buf` argument to `v1()`-`v7()`.)
|
---|
| 27 |
|
---|
| 28 | ## Quickstart
|
---|
| 29 |
|
---|
| 30 | **1. Install**
|
---|
| 31 |
|
---|
| 32 | ```shell
|
---|
| 33 | npm install uuid
|
---|
| 34 | ```
|
---|
| 35 |
|
---|
| 36 | **2. Create a UUID**
|
---|
| 37 |
|
---|
| 38 | ESM-syntax (must use named exports):
|
---|
| 39 |
|
---|
| 40 | ```javascript
|
---|
| 41 | import { v4 as uuidv4 } from 'uuid';
|
---|
| 42 | uuidv4(); // ⇨ '9b1deb4d-3b7d-4bad-9bdd-2b0d7b3dcb6d'
|
---|
| 43 | ```
|
---|
| 44 |
|
---|
| 45 | ... CommonJS:
|
---|
| 46 |
|
---|
| 47 | ```javascript
|
---|
| 48 | const { v4: uuidv4 } = require('uuid');
|
---|
| 49 | uuidv4(); // ⇨ '1b9d6bcd-bbfd-4b2d-9b5d-ab8dfbbd4bed'
|
---|
| 50 | ```
|
---|
| 51 |
|
---|
| 52 | For timestamp UUIDs, namespace UUIDs, and other options read on ...
|
---|
| 53 |
|
---|
| 54 | ## API Summary
|
---|
| 55 |
|
---|
| 56 | | | | |
|
---|
| 57 | | --- | --- | --- |
|
---|
| 58 | | [`uuid.NIL`](#uuidnil) | The nil UUID string (all zeros) | New in `uuid@8.3` |
|
---|
| 59 | | [`uuid.MAX`](#uuidmax) | The max UUID string (all ones) | New in `uuid@9.1` |
|
---|
| 60 | | [`uuid.parse()`](#uuidparsestr) | Convert UUID string to array of bytes | New in `uuid@8.3` |
|
---|
| 61 | | [`uuid.stringify()`](#uuidstringifyarr-offset) | Convert array of bytes to UUID string | New in `uuid@8.3` |
|
---|
| 62 | | [`uuid.v1()`](#uuidv1options-buffer-offset) | Create a version 1 (timestamp) UUID | |
|
---|
| 63 | | [`uuid.v1ToV6()`](#uuidv1tov6uuid) | Create a version 6 UUID from a version 1 UUID | New in `uuid@10` |
|
---|
| 64 | | [`uuid.v3()`](#uuidv3name-namespace-buffer-offset) | Create a version 3 (namespace w/ MD5) UUID | |
|
---|
| 65 | | [`uuid.v4()`](#uuidv4options-buffer-offset) | Create a version 4 (random) UUID | |
|
---|
| 66 | | [`uuid.v5()`](#uuidv5name-namespace-buffer-offset) | Create a version 5 (namespace w/ SHA-1) UUID | |
|
---|
| 67 | | [`uuid.v6()`](#uuidv6options-buffer-offset) | Create a version 6 (timestamp, reordered) UUID | New in `uuid@10` |
|
---|
| 68 | | [`uuid.v6ToV1()`](#uuidv6tov1uuid) | Create a version 1 UUID from a version 6 UUID | New in `uuid@10` |
|
---|
| 69 | | [`uuid.v7()`](#uuidv7options-buffer-offset) | Create a version 7 (Unix Epoch time-based) UUID | New in `uuid@10` |
|
---|
| 70 | | ~~[`uuid.v8()`](#uuidv8)~~ | "Intentionally left blank" | |
|
---|
| 71 | | [`uuid.validate()`](#uuidvalidatestr) | Test a string to see if it is a valid UUID | New in `uuid@8.3` |
|
---|
| 72 | | [`uuid.version()`](#uuidversionstr) | Detect RFC version of a UUID | New in `uuid@8.3` |
|
---|
| 73 |
|
---|
| 74 | ## API
|
---|
| 75 |
|
---|
| 76 | ### uuid.NIL
|
---|
| 77 |
|
---|
| 78 | The nil UUID string (all zeros).
|
---|
| 79 |
|
---|
| 80 | Example:
|
---|
| 81 |
|
---|
| 82 | ```javascript
|
---|
| 83 | import { NIL as NIL_UUID } from 'uuid';
|
---|
| 84 |
|
---|
| 85 | NIL_UUID; // ⇨ '00000000-0000-0000-0000-000000000000'
|
---|
| 86 | ```
|
---|
| 87 |
|
---|
| 88 | ### uuid.MAX
|
---|
| 89 |
|
---|
| 90 | The max UUID string (all ones).
|
---|
| 91 |
|
---|
| 92 | Example:
|
---|
| 93 |
|
---|
| 94 | ```javascript
|
---|
| 95 | import { MAX as MAX_UUID } from 'uuid';
|
---|
| 96 |
|
---|
| 97 | MAX_UUID; // ⇨ 'ffffffff-ffff-ffff-ffff-ffffffffffff'
|
---|
| 98 | ```
|
---|
| 99 |
|
---|
| 100 | ### uuid.parse(str)
|
---|
| 101 |
|
---|
| 102 | Convert UUID string to array of bytes
|
---|
| 103 |
|
---|
| 104 | | | |
|
---|
| 105 | | --------- | ---------------------------------------- |
|
---|
| 106 | | `str` | A valid UUID `String` |
|
---|
| 107 | | _returns_ | `Uint8Array[16]` |
|
---|
| 108 | | _throws_ | `TypeError` if `str` is not a valid UUID |
|
---|
| 109 |
|
---|
| 110 | <!-- prettier-ignore -->
|
---|
| 111 | > [!NOTE]
|
---|
| 112 | > Ordering of values in the byte arrays used by `parse()` and `stringify()` follows the left ↠ right order of hex-pairs in UUID strings. As shown in the example below.
|
---|
| 113 |
|
---|
| 114 | Example:
|
---|
| 115 |
|
---|
| 116 | ```javascript
|
---|
| 117 | import { parse as uuidParse } from 'uuid';
|
---|
| 118 |
|
---|
| 119 | // Parse a UUID
|
---|
| 120 | uuidParse('6ec0bd7f-11c0-43da-975e-2a8ad9ebae0b'); // ⇨
|
---|
| 121 | // Uint8Array(16) [
|
---|
| 122 | // 110, 192, 189, 127, 17,
|
---|
| 123 | // 192, 67, 218, 151, 94,
|
---|
| 124 | // 42, 138, 217, 235, 174,
|
---|
| 125 | // 11
|
---|
| 126 | // ]
|
---|
| 127 | ```
|
---|
| 128 |
|
---|
| 129 | ### uuid.stringify(arr[, offset])
|
---|
| 130 |
|
---|
| 131 | Convert array of bytes to UUID string
|
---|
| 132 |
|
---|
| 133 | | | |
|
---|
| 134 | | -------------- | ---------------------------------------------------------------------------- |
|
---|
| 135 | | `arr` | `Array`-like collection of 16 values (starting from `offset`) between 0-255. |
|
---|
| 136 | | [`offset` = 0] | `Number` Starting index in the Array |
|
---|
| 137 | | _returns_ | `String` |
|
---|
| 138 | | _throws_ | `TypeError` if a valid UUID string cannot be generated |
|
---|
| 139 |
|
---|
| 140 | <!-- prettier-ignore -->
|
---|
| 141 | > [!NOTE]
|
---|
| 142 | > Ordering of values in the byte arrays used by `parse()` and `stringify()` follows the left ↠ right order of hex-pairs in UUID strings. As shown in the example below.
|
---|
| 143 |
|
---|
| 144 | Example:
|
---|
| 145 |
|
---|
| 146 | ```javascript
|
---|
| 147 | import { stringify as uuidStringify } from 'uuid';
|
---|
| 148 |
|
---|
| 149 | const uuidBytes = Uint8Array.of(
|
---|
| 150 | 0x6e,
|
---|
| 151 | 0xc0,
|
---|
| 152 | 0xbd,
|
---|
| 153 | 0x7f,
|
---|
| 154 | 0x11,
|
---|
| 155 | 0xc0,
|
---|
| 156 | 0x43,
|
---|
| 157 | 0xda,
|
---|
| 158 | 0x97,
|
---|
| 159 | 0x5e,
|
---|
| 160 | 0x2a,
|
---|
| 161 | 0x8a,
|
---|
| 162 | 0xd9,
|
---|
| 163 | 0xeb,
|
---|
| 164 | 0xae,
|
---|
| 165 | 0x0b
|
---|
| 166 | );
|
---|
| 167 |
|
---|
| 168 | uuidStringify(uuidBytes); // ⇨ '6ec0bd7f-11c0-43da-975e-2a8ad9ebae0b'
|
---|
| 169 | ```
|
---|
| 170 |
|
---|
| 171 | ### uuid.v1([options[, buffer[, offset]]])
|
---|
| 172 |
|
---|
| 173 | Create an RFC version 1 (timestamp) UUID
|
---|
| 174 |
|
---|
| 175 | | | |
|
---|
| 176 | | --- | --- |
|
---|
| 177 | | [`options`] | `Object` with one or more of the following properties: |
|
---|
| 178 | | [`options.node = (random)` ] | RFC "node" field as an `Array[6]` of byte values (per 4.1.6) |
|
---|
| 179 | | [`options.clockseq = (random)`] | RFC "clock sequence" as a `Number` between 0 - 0x3fff |
|
---|
| 180 | | [`options.msecs = (current time)`] | RFC "timestamp" field (`Number` of milliseconds, unix epoch) |
|
---|
| 181 | | [`options.nsecs = 0`] | RFC "timestamp" field (`Number` of nanoseconds to add to `msecs`, should be 0-10,000) |
|
---|
| 182 | | [`options.random = (random)`] | `Array` of 16 random bytes (0-255) used to generate other fields, above |
|
---|
| 183 | | [`options.rng`] | Alternative to `options.random`, a `Function` that returns an `Array` of 16 random bytes (0-255) |
|
---|
| 184 | | [`buffer`] | `Array \| Buffer` If specified, uuid will be written here in byte-form, starting at `offset` |
|
---|
| 185 | | [`offset` = 0] | `Number` Index to start writing UUID bytes in `buffer` |
|
---|
| 186 | | _returns_ | UUID `String` if no `buffer` is specified, otherwise returns `buffer` |
|
---|
| 187 | | _throws_ | `Error` if more than 10M UUIDs/sec are requested |
|
---|
| 188 |
|
---|
| 189 | <!-- prettier-ignore -->
|
---|
| 190 | > [!NOTE]
|
---|
| 191 | > The default [node id](https://datatracker.ietf.org/doc/html/rfc9562#section-5.1) (the last 12 digits in the UUID) is generated once, randomly, on process startup, and then remains unchanged for the duration of the process.
|
---|
| 192 |
|
---|
| 193 | <!-- prettier-ignore -->
|
---|
| 194 | > [!NOTE]
|
---|
| 195 | > `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.
|
---|
| 196 |
|
---|
| 197 | Example:
|
---|
| 198 |
|
---|
| 199 | ```javascript
|
---|
| 200 | import { v1 as uuidv1 } from 'uuid';
|
---|
| 201 |
|
---|
| 202 | uuidv1(); // ⇨ '2c5ea4c0-4067-11e9-9bdd-2b0d7b3dcb6d'
|
---|
| 203 | ```
|
---|
| 204 |
|
---|
| 205 | Example using `options`:
|
---|
| 206 |
|
---|
| 207 | ```javascript
|
---|
| 208 | import { v1 as uuidv1 } from 'uuid';
|
---|
| 209 |
|
---|
| 210 | const options = {
|
---|
| 211 | node: Uint8Array.of(0x01, 0x23, 0x45, 0x67, 0x89, 0xab),
|
---|
| 212 | clockseq: 0x1234,
|
---|
| 213 | msecs: new Date('2011-11-01').getTime(),
|
---|
| 214 | nsecs: 5678,
|
---|
| 215 | };
|
---|
| 216 | uuidv1(options); // ⇨ '710b962e-041c-11e1-9234-0123456789ab'
|
---|
| 217 | ```
|
---|
| 218 |
|
---|
| 219 | ### uuid.v1ToV6(uuid)
|
---|
| 220 |
|
---|
| 221 | Convert a UUID from version 1 to version 6
|
---|
| 222 |
|
---|
| 223 | ```javascript
|
---|
| 224 | import { v1ToV6 } from 'uuid';
|
---|
| 225 |
|
---|
| 226 | v1ToV6('92f62d9e-22c4-11ef-97e9-325096b39f47'); // ⇨ '1ef22c49-2f62-6d9e-97e9-325096b39f47'
|
---|
| 227 | ```
|
---|
| 228 |
|
---|
| 229 | ### uuid.v3(name, namespace[, buffer[, offset]])
|
---|
| 230 |
|
---|
| 231 | Create an RFC version 3 (namespace w/ MD5) UUID
|
---|
| 232 |
|
---|
| 233 | API is identical to `v5()`, but uses "v3" instead.
|
---|
| 234 |
|
---|
| 235 | <!-- prettier-ignore -->
|
---|
| 236 | > [!IMPORTANT]
|
---|
| 237 | > Per the RFC, "_If backward compatibility is not an issue, SHA-1 [Version 5] is preferred_."
|
---|
| 238 |
|
---|
| 239 | ### uuid.v4([options[, buffer[, offset]]])
|
---|
| 240 |
|
---|
| 241 | Create an RFC version 4 (random) UUID
|
---|
| 242 |
|
---|
| 243 | | | |
|
---|
| 244 | | --- | --- |
|
---|
| 245 | | [`options`] | `Object` with one or more of the following properties: |
|
---|
| 246 | | [`options.random`] | `Array` of 16 random bytes (0-255) |
|
---|
| 247 | | [`options.rng`] | Alternative to `options.random`, a `Function` that returns an `Array` of 16 random bytes (0-255) |
|
---|
| 248 | | [`buffer`] | `Array \| Buffer` If specified, uuid will be written here in byte-form, starting at `offset` |
|
---|
| 249 | | [`offset` = 0] | `Number` Index to start writing UUID bytes in `buffer` |
|
---|
| 250 | | _returns_ | UUID `String` if no `buffer` is specified, otherwise returns `buffer` |
|
---|
| 251 |
|
---|
| 252 | Example:
|
---|
| 253 |
|
---|
| 254 | ```javascript
|
---|
| 255 | import { v4 as uuidv4 } from 'uuid';
|
---|
| 256 |
|
---|
| 257 | uuidv4(); // ⇨ '9b1deb4d-3b7d-4bad-9bdd-2b0d7b3dcb6d'
|
---|
| 258 | ```
|
---|
| 259 |
|
---|
| 260 | Example using predefined `random` values:
|
---|
| 261 |
|
---|
| 262 | ```javascript
|
---|
| 263 | import { v4 as uuidv4 } from 'uuid';
|
---|
| 264 |
|
---|
| 265 | const v4options = {
|
---|
| 266 | random: Uint8Array.of(
|
---|
| 267 | 0x10,
|
---|
| 268 | 0x91,
|
---|
| 269 | 0x56,
|
---|
| 270 | 0xbe,
|
---|
| 271 | 0xc4,
|
---|
| 272 | 0xfb,
|
---|
| 273 | 0xc1,
|
---|
| 274 | 0xea,
|
---|
| 275 | 0x71,
|
---|
| 276 | 0xb4,
|
---|
| 277 | 0xef,
|
---|
| 278 | 0xe1,
|
---|
| 279 | 0x67,
|
---|
| 280 | 0x1c,
|
---|
| 281 | 0x58,
|
---|
| 282 | 0x36
|
---|
| 283 | ),
|
---|
| 284 | };
|
---|
| 285 | uuidv4(v4options); // ⇨ '109156be-c4fb-41ea-b1b4-efe1671c5836'
|
---|
| 286 | ```
|
---|
| 287 |
|
---|
| 288 | ### uuid.v5(name, namespace[, buffer[, offset]])
|
---|
| 289 |
|
---|
| 290 | Create an RFC version 5 (namespace w/ SHA-1) UUID
|
---|
| 291 |
|
---|
| 292 | | | |
|
---|
| 293 | | --- | --- |
|
---|
| 294 | | `name` | `String \| Array` |
|
---|
| 295 | | `namespace` | `String \| Array[16]` Namespace UUID |
|
---|
| 296 | | [`buffer`] | `Array \| Buffer` If specified, uuid will be written here in byte-form, starting at `offset` |
|
---|
| 297 | | [`offset` = 0] | `Number` Index to start writing UUID bytes in `buffer` |
|
---|
| 298 | | _returns_ | UUID `String` if no `buffer` is specified, otherwise returns `buffer` |
|
---|
| 299 |
|
---|
| 300 | <!-- prettier-ignore -->
|
---|
| 301 | > [!NOTE]
|
---|
| 302 | > The RFC `DNS` and `URL` namespaces are available as `v5.DNS` and `v5.URL`.
|
---|
| 303 |
|
---|
| 304 | Example with custom namespace:
|
---|
| 305 |
|
---|
| 306 | ```javascript
|
---|
| 307 | import { v5 as uuidv5 } from 'uuid';
|
---|
| 308 |
|
---|
| 309 | // Define a custom namespace. Readers, create your own using something like
|
---|
| 310 | // https://www.uuidgenerator.net/
|
---|
| 311 | const MY_NAMESPACE = '1b671a64-40d5-491e-99b0-da01ff1f3341';
|
---|
| 312 |
|
---|
| 313 | uuidv5('Hello, World!', MY_NAMESPACE); // ⇨ '630eb68f-e0fa-5ecc-887a-7c7a62614681'
|
---|
| 314 | ```
|
---|
| 315 |
|
---|
| 316 | Example with RFC `URL` namespace:
|
---|
| 317 |
|
---|
| 318 | ```javascript
|
---|
| 319 | import { v5 as uuidv5 } from 'uuid';
|
---|
| 320 |
|
---|
| 321 | uuidv5('https://www.w3.org/', uuidv5.URL); // ⇨ 'c106a26a-21bb-5538-8bf2-57095d1976c1'
|
---|
| 322 | ```
|
---|
| 323 |
|
---|
| 324 | ### uuid.v6([options[, buffer[, offset]]])
|
---|
| 325 |
|
---|
| 326 | Create an RFC version 6 (timestamp, reordered) UUID
|
---|
| 327 |
|
---|
| 328 | This method takes the same arguments as uuid.v1().
|
---|
| 329 |
|
---|
| 330 | ```javascript
|
---|
| 331 | import { v6 as uuidv6 } from 'uuid';
|
---|
| 332 |
|
---|
| 333 | uuidv6(); // ⇨ '1e940672-c5ea-64c0-9b5d-ab8dfbbd4bed'
|
---|
| 334 | ```
|
---|
| 335 |
|
---|
| 336 | Example using `options`:
|
---|
| 337 |
|
---|
| 338 | ```javascript
|
---|
| 339 | import { v6 as uuidv6 } from 'uuid';
|
---|
| 340 |
|
---|
| 341 | const options = {
|
---|
| 342 | node: [0x01, 0x23, 0x45, 0x67, 0x89, 0xab],
|
---|
| 343 | clockseq: 0x1234,
|
---|
| 344 | msecs: new Date('2011-11-01').getTime(),
|
---|
| 345 | nsecs: 5678,
|
---|
| 346 | };
|
---|
| 347 | uuidv6(options); // ⇨ '1e1041c7-10b9-662e-9234-0123456789ab'
|
---|
| 348 | ```
|
---|
| 349 |
|
---|
| 350 | ### uuid.v6ToV1(uuid)
|
---|
| 351 |
|
---|
| 352 | Convert a UUID from version 6 to version 1
|
---|
| 353 |
|
---|
| 354 | ```javascript
|
---|
| 355 | import { v6ToV1 } from 'uuid';
|
---|
| 356 |
|
---|
| 357 | v6ToV1('1ef22c49-2f62-6d9e-97e9-325096b39f47'); // ⇨ '92f62d9e-22c4-11ef-97e9-325096b39f47'
|
---|
| 358 | ```
|
---|
| 359 |
|
---|
| 360 | ### uuid.v7([options[, buffer[, offset]]])
|
---|
| 361 |
|
---|
| 362 | Create an RFC version 7 (random) UUID
|
---|
| 363 |
|
---|
| 364 | | | |
|
---|
| 365 | | --- | --- |
|
---|
| 366 | | [`options`] | `Object` with one or more of the following properties: |
|
---|
| 367 | | [`options.msecs = (current time)`] | RFC "timestamp" field (`Number` of milliseconds, unix epoch) |
|
---|
| 368 | | [`options.random = (random)`] | `Array` of 16 random bytes (0-255) used to generate other fields, above |
|
---|
| 369 | | [`options.rng`] | Alternative to `options.random`, a `Function` that returns an `Array` of 16 random bytes (0-255) |
|
---|
| 370 | | [`options.seq = (random)`] | 32-bit sequence `Number` between 0 - 0xffffffff. This may be provided to help insure uniqueness for UUIDs generated within the same millisecond time interval. Default = random value. |
|
---|
| 371 | | [`buffer`] | `Array \| Buffer` If specified, uuid will be written here in byte-form, starting at `offset` |
|
---|
| 372 | | [`offset` = 0] | `Number` Index to start writing UUID bytes in `buffer` |
|
---|
| 373 | | _returns_ | UUID `String` if no `buffer` is specified, otherwise returns `buffer` |
|
---|
| 374 |
|
---|
| 375 | Example:
|
---|
| 376 |
|
---|
| 377 | ```javascript
|
---|
| 378 | import { v7 as uuidv7 } from 'uuid';
|
---|
| 379 |
|
---|
| 380 | uuidv7(); // ⇨ '01695553-c90c-705a-b56d-778dfbbd4bed'
|
---|
| 381 | ```
|
---|
| 382 |
|
---|
| 383 | ### ~~uuid.v8()~~
|
---|
| 384 |
|
---|
| 385 | **_"Intentionally left blank"_**
|
---|
| 386 |
|
---|
| 387 | <!-- prettier-ignore -->
|
---|
| 388 | > [!NOTE]
|
---|
| 389 | > Version 8 (experimental) UUIDs are "[for experimental or vendor-specific use cases](https://www.rfc-editor.org/rfc/rfc9562.html#name-uuid-version-8)". The RFC does not define a creation algorithm for them, which is why this package does not offer a `v8()` method. The `validate()` and `version()` methods do work with such UUIDs, however.
|
---|
| 390 |
|
---|
| 391 | ### uuid.validate(str)
|
---|
| 392 |
|
---|
| 393 | Test a string to see if it is a valid UUID
|
---|
| 394 |
|
---|
| 395 | | | |
|
---|
| 396 | | --------- | --------------------------------------------------- |
|
---|
| 397 | | `str` | `String` to validate |
|
---|
| 398 | | _returns_ | `true` if string is a valid UUID, `false` otherwise |
|
---|
| 399 |
|
---|
| 400 | Example:
|
---|
| 401 |
|
---|
| 402 | ```javascript
|
---|
| 403 | import { validate as uuidValidate } from 'uuid';
|
---|
| 404 |
|
---|
| 405 | uuidValidate('not a UUID'); // ⇨ false
|
---|
| 406 | uuidValidate('6ec0bd7f-11c0-43da-975e-2a8ad9ebae0b'); // ⇨ true
|
---|
| 407 | ```
|
---|
| 408 |
|
---|
| 409 | Using `validate` and `version` together it is possible to do per-version validation, e.g. validate for only v4 UUIds.
|
---|
| 410 |
|
---|
| 411 | ```javascript
|
---|
| 412 | import { version as uuidVersion } from 'uuid';
|
---|
| 413 | import { validate as uuidValidate } from 'uuid';
|
---|
| 414 |
|
---|
| 415 | function uuidValidateV4(uuid) {
|
---|
| 416 | return uuidValidate(uuid) && uuidVersion(uuid) === 4;
|
---|
| 417 | }
|
---|
| 418 |
|
---|
| 419 | const v1Uuid = 'd9428888-122b-11e1-b85c-61cd3cbb3210';
|
---|
| 420 | const v4Uuid = '109156be-c4fb-41ea-b1b4-efe1671c5836';
|
---|
| 421 |
|
---|
| 422 | uuidValidateV4(v4Uuid); // ⇨ true
|
---|
| 423 | uuidValidateV4(v1Uuid); // ⇨ false
|
---|
| 424 | ```
|
---|
| 425 |
|
---|
| 426 | ### uuid.version(str)
|
---|
| 427 |
|
---|
| 428 | Detect RFC version of a UUID
|
---|
| 429 |
|
---|
| 430 | | | |
|
---|
| 431 | | --------- | ---------------------------------------- |
|
---|
| 432 | | `str` | A valid UUID `String` |
|
---|
| 433 | | _returns_ | `Number` The RFC version of the UUID |
|
---|
| 434 | | _throws_ | `TypeError` if `str` is not a valid UUID |
|
---|
| 435 |
|
---|
| 436 | Example:
|
---|
| 437 |
|
---|
| 438 | ```javascript
|
---|
| 439 | import { version as uuidVersion } from 'uuid';
|
---|
| 440 |
|
---|
| 441 | uuidVersion('45637ec4-c85f-11ea-87d0-0242ac130003'); // ⇨ 1
|
---|
| 442 | uuidVersion('6ec0bd7f-11c0-43da-975e-2a8ad9ebae0b'); // ⇨ 4
|
---|
| 443 | ```
|
---|
| 444 |
|
---|
| 445 | <!-- prettier-ignore -->
|
---|
| 446 | > [!NOTE]
|
---|
| 447 | > This method returns `0` for the `NIL` UUID, and `15` for the `MAX` UUID.
|
---|
| 448 |
|
---|
| 449 | ## Command Line
|
---|
| 450 |
|
---|
| 451 | UUIDs can be generated from the command line using `uuid`.
|
---|
| 452 |
|
---|
| 453 | ```shell
|
---|
| 454 | $ npx uuid
|
---|
| 455 | ddeb27fb-d9a0-4624-be4d-4615062daed4
|
---|
| 456 | ```
|
---|
| 457 |
|
---|
| 458 | The default is to generate version 4 UUIDS, however the other versions are supported. Type `uuid --help` for details:
|
---|
| 459 |
|
---|
| 460 | ```shell
|
---|
| 461 | $ npx uuid --help
|
---|
| 462 |
|
---|
| 463 | Usage:
|
---|
| 464 | uuid
|
---|
| 465 | uuid v1
|
---|
| 466 | uuid v3 <name> <namespace uuid>
|
---|
| 467 | uuid v4
|
---|
| 468 | uuid v5 <name> <namespace uuid>
|
---|
| 469 | uuid v7
|
---|
| 470 | uuid --help
|
---|
| 471 |
|
---|
| 472 | Note: <namespace uuid> may be "URL" or "DNS" to use the corresponding UUIDs
|
---|
| 473 | defined by RFC9562
|
---|
| 474 | ```
|
---|
| 475 |
|
---|
| 476 | ## `options` Handling for Timestamp UUIDs
|
---|
| 477 |
|
---|
| 478 | Prior to `uuid@11`, it was possible for `options` state to interfere with the internal state used to insure uniqueness of timestamp-based UUIDs (the `v1()`, `v6()`, and `v7()` methods). Starting with `uuid@11`, this issue has been addressed by using the presence of the `options` argument as a flag to select between two possible behaviors:
|
---|
| 479 |
|
---|
| 480 | - Without `options`: Internal state is utilized to improve UUID uniqueness.
|
---|
| 481 | - With `options`: Internal state is **NOT** used and, instead, appropriate defaults are applied as needed.
|
---|
| 482 |
|
---|
| 483 | ## Known issues
|
---|
| 484 |
|
---|
| 485 | ### React Native / Expo
|
---|
| 486 |
|
---|
| 487 | 1. Install [`react-native-get-random-values`](https://github.com/LinusU/react-native-get-random-values#readme)
|
---|
| 488 | 1. Import it _before_ `uuid`. Since `uuid` might also appear as a transitive dependency of some other imports it's safest to just import `react-native-get-random-values` as the very first thing in your entry point:
|
---|
| 489 |
|
---|
| 490 | ```javascript
|
---|
| 491 | import 'react-native-get-random-values';
|
---|
| 492 | import { v4 as uuidv4 } from 'uuid';
|
---|
| 493 | ```
|
---|
| 494 |
|
---|
| 495 | ---
|
---|
| 496 |
|
---|
| 497 | Markdown generated from [README_js.md](README_js.md) by <a href="https://github.com/broofa/runmd"><image height="12px" src="https://camo.githubusercontent.com/5c7c603cd1e6a43370b0a5063d457e0dabb74cf317adc7baba183acb686ee8d0/687474703a2f2f692e696d6775722e636f6d2f634a4b6f3662552e706e67" /></a>
|
---|