UNPKG

2.77 kBMarkdownView Raw
1# uuid
2
3uuid 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=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
23First you need to integrate uuidv4 into your project by using the `require` function:
24
25```javascript
26const { uuid } = require('uuidv4');
27```
28
29If you use TypeScript, use the following code instead:
30
31```typescript
32import { uuid } from 'uuidv4';
33```
34
35Then you can create UUIDs. To do so simply call the `uuid` function:
36
37```javascript
38console.log(uuid());
39// => '11bf5b37-e0b8-42e0-8dcf-dc8c4aefc000'
40```
41
42### Verifying a UUID
43
44To verify whether a given value is a UUID, use the `isUuid` function:
45
46```javascript
47import { isUuid } from 'uuidv4';
48
49console.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
55If 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
58import { regex } from 'uuidv4';
59
60console.log(regex.v4);
61console.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
68From 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
71import { fromString } from 'uuidv4';
72
73console.log(fromString('the native web'));
74// => 'cdb63720-9628-5ef6-bbca-2e5ce6094f3c'
75```
76
77### Getting the empty UUID
78
79If you need a UUID that consists only of zeros, use the `empty` function:
80
81```javascript
82import { empty } from 'uuidv4';
83
84console.log(empty());
85// => '00000000-0000-0000-0000-000000000000'
86```
87
88## Running the build
89
90To build this module use [roboter](https://www.npmjs.com/package/roboter).
91
92```shell
93$ npx roboter
94```