UNPKG

3.88 kBMarkdownView Raw
1# uuidv4
2
3uuidv4 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 | ![GitHub Actions](https://github.com/thenativeweb/uuidv4/workflows/Release/badge.svg?branch=main) |
13| License | ![GitHub](https://img.shields.io/github/license/thenativeweb/uuidv4) |
14
15## Please note
16
17This module will be deprecated in the future in favour of module [uuid](https://www.npmjs.com/package/uuid). Most of the functionality of this module is already included in `uuid` since version `8.3.0`, so most of the functions of this module have already been marked as deprecated.
18
19## Installation
20
21```shell
22$ npm install uuidv4
23```
24
25## Quick start
26
27First you need to integrate uuidv4 into your project by using the `require` function:
28
29```javascript
30const { uuid } = require('uuidv4');
31```
32
33If you use TypeScript, use the following code instead:
34
35```typescript
36import { uuid } from 'uuidv4';
37```
38
39Then you can create UUIDs. To do so simply call the `uuid` function:
40
41```javascript
42console.log(uuid());
43// => '11bf5b37-e0b8-42e0-8dcf-dc8c4aefc000'
44```
45
46### Verifying a UUID
47
48To verify whether a given value is a UUID, use the `isUuid` function:
49
50```javascript
51import { isUuid } from 'uuidv4';
52
53console.log(isUuid('75442486-0878-440c-9db1-a7006c25a39f'));
54// => true
55```
56
57_Please note that the `isUuid` function returns `true` for both, `v4` and `v5` UUIDs. In addition, `isUuid` returns `true` for `empty()`._
58
59#### Using a regular expression
60
61If you want to perform the verification on your own using a regular expression, use the `regex` property, and access its `v4` or `v5` property, depending on what you need:
62
63```javascript
64import { regex } from 'uuidv4';
65
66console.log(regex.v4);
67console.log(regex.v5);
68```
69
70_Please note that the regular expressions also consider `empty()` to be a valid UUID._
71
72#### Using a JSON schema
73
74If you want to perform the verification on your own using a JSON schema, use the `jsonSchema` property, and access its `v4` or `v5` property, depending on what you need:
75
76```javascript
77import { jsonSchema } from 'uuidv4';
78
79console.log(jsonSchema.v4);
80console.log(jsonSchema.v5);
81```
82
83_Please note that the JSON schemas also consider `empty()` to be a valid UUID._
84
85### Getting a UUID from a string
86
87From 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`:
88
89```javascript
90import { fromString } from 'uuidv4';
91
92console.log(fromString('the native web'));
93// => 'cdb63720-9628-5ef6-bbca-2e5ce6094f3c'
94```
95
96By default, the `fromString` function uses a pre-configured namespace. If you want to use your own namespace, provide a UUID as second parameter:
97
98```javascript
99import { fromString } from 'uuidv4';
100
101console.log(fromString('the native web', '004aadf4-8e1a-4450-905b-6039179f52da'));
102// => 'b1c4a89e-4905-5e3c-b57f-dc92627d011e'
103```
104
105### Getting the empty UUID
106
107If you need a UUID that consists only of zeros, use the `empty` function:
108
109```javascript
110import { empty } from 'uuidv4';
111
112console.log(empty());
113// => '00000000-0000-0000-0000-000000000000'
114```
115
116## Running quality assurance
117
118To run quality assurance for this module use [roboter](https://www.npmjs.com/package/roboter):
119
120```shell
121$ npx roboter
122```