1 | # uuid
|
2 |
|
3 | uuid creates v4 UUIDs.
|
4 |
|
5 | ## Status
|
6 |
|
7 | | Category | Status |
|
8 | | ---------------- | ---------------------------------------------------------------------------------------------------------------------------------------- |
|
9 | | Version | [![npm](https://img.shields.io/npm/v/uuidv4)](https://www.npmjs.com/package/uuidv4) |
|
10 | | Dependencies | ![David](https://img.shields.io/david/thenativeweb/uuidv4) |
|
11 | | Dev dependencies | ![David](https://img.shields.io/david/dev/thenativeweb/uuidv4) |
|
12 | | Build | [![CircleCI](https://img.shields.io/circleci/build/github/thenativeweb/uuidv4)](https://circleci.com/gh/thenativeweb/uuidv4/tree/master) |
|
13 | | License | ![GitHub](https://img.shields.io/github/license/thenativeweb/uuidv4) |
|
14 |
|
15 | ## Installation
|
16 |
|
17 | ```shell
|
18 | $ npm install uuidv4
|
19 | ```
|
20 |
|
21 | ## Quick start
|
22 |
|
23 | First you need to integrate uuidv4 into your project by using the `require` function:
|
24 |
|
25 | ```javascript
|
26 | const { uuid } = require('uuidv4');
|
27 | ```
|
28 |
|
29 | If you use TypeScript, use the following code instead:
|
30 |
|
31 | ```typescript
|
32 | import { uuid } from 'uuidv4';
|
33 | ```
|
34 |
|
35 | Then you can create UUIDs. To do so simply call the `uuid` function:
|
36 |
|
37 | ```javascript
|
38 | console.log(uuid());
|
39 | // => '11bf5b37-e0b8-42e0-8dcf-dc8c4aefc000'
|
40 | ```
|
41 |
|
42 | ### Verifying a UUID
|
43 |
|
44 | To verify whether a given value is a UUID, use the `isUuid` function:
|
45 |
|
46 | ```javascript
|
47 | import { isUuid } from 'uuidv4';
|
48 |
|
49 | console.log(isUuid('75442486-0878-440c-9db1-a7006c25a39f'));
|
50 | // => true
|
51 | ```
|
52 |
|
53 | _Please note that the `isUuid` function returns `true` for both, `v4` and `v5` UUIDs. In addition, `isUuid` returns `true` for `empty()`._
|
54 |
|
55 | If you want to perform the verification on your own, use the `regex` property, and access its `v4` or `v5` property, depending on what you need:
|
56 |
|
57 | ```javascript
|
58 | import { regex } from 'uuidv4';
|
59 |
|
60 | console.log(regex.v4);
|
61 | console.log(regex.v5);
|
62 | ```
|
63 |
|
64 | _Please note that the regular expressions also consider `empty()` to be a valid UUID._
|
65 |
|
66 | ### Getting a UUID from a string
|
67 |
|
68 | 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`:
|
69 |
|
70 | ```javascript
|
71 | import { fromString } from 'uuidv4';
|
72 |
|
73 | console.log(fromString('the native web'));
|
74 | // => 'cdb63720-9628-5ef6-bbca-2e5ce6094f3c'
|
75 | ```
|
76 |
|
77 | ### Getting the empty UUID
|
78 |
|
79 | If you need a UUID that consists only of zeros, use the `empty` function:
|
80 |
|
81 | ```javascript
|
82 | import { empty } from 'uuidv4';
|
83 |
|
84 | console.log(empty());
|
85 | // => '00000000-0000-0000-0000-000000000000'
|
86 | ```
|
87 |
|
88 | ## Running the build
|
89 |
|
90 | To build this module use [roboter](https://www.npmjs.com/package/roboter).
|
91 |
|
92 | ```shell
|
93 | $ npx roboter
|
94 | ```
|