uuidv4
uuidv4 creates v4 UUIDs.
Status
Category | Status |
---|
Version | |
Dependencies | |
Dev dependencies | |
Build | |
License | |
Please note
This module will be deprecated in the future in favour of module 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.
Installation
$ npm install uuidv4
Quick start
First you need to integrate uuidv4 into your project by using the require
function:
const { uuid } = require('uuidv4');
If you use TypeScript, use the following code instead:
import { uuid } from 'uuidv4';
Then you can create UUIDs. To do so simply call the uuid
function:
console.log(uuid());
Verifying a UUID
To verify whether a given value is a UUID, use the isUuid
function:
import { isUuid } from 'uuidv4';
console.log(isUuid('75442486-0878-440c-9db1-a7006c25a39f'));
Please note that the isUuid
function returns true
for both, v4
and v5
UUIDs. In addition, isUuid
returns true
for empty()
.
Using a regular expression
If 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:
import { regex } from 'uuidv4';
console.log(regex.v4);
console.log(regex.v5);
Please note that the regular expressions also consider empty()
to be a valid UUID.
Using a JSON schema
If 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:
import { jsonSchema } from 'uuidv4';
console.log(jsonSchema.v4);
console.log(jsonSchema.v5);
Please note that the JSON schemas also consider empty()
to be a valid UUID.
Getting a UUID from a string
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
:
import { fromString } from 'uuidv4';
console.log(fromString('the native web'));
By default, the fromString
function uses a pre-configured namespace. If you want to use your own namespace, provide a UUID as second parameter:
import { fromString } from 'uuidv4';
console.log(fromString('the native web', '004aadf4-8e1a-4450-905b-6039179f52da'));
Getting the empty UUID
If you need a UUID that consists only of zeros, use the empty
function:
import { empty } from 'uuidv4';
console.log(empty());
Running quality assurance
To run quality assurance for this module use roboter:
$ npx roboter