UUID

The uuid library generates RFC-compliant Universally Unique Identifiers (UUIDs) in JavaScript.

The uuid module is available via the following require() statement:

1const {v3, v4, v5, validate, version} = require('uuid');

Exposed constructors

Use these constructors in PubNub Functions:

  • v3 — Generates a UUID from the MD5 hash of a namespace identifier (for example, a URL or DNS name) and a name.
  • v4 — Generates a random UUID. This is the simplest option because it requires no inputs.
  • v5 — Generates a UUID from a namespace and name using SHA-1 (similar to v3, which uses MD5).

Exposed methods

Use these methods in your Function code:

  • validate — Check whether a string is a valid UUID.
  • version — Detect the RFC version of a UUID.

Examples

const { v3, validate, version } = require('uuid');

export default req => {
const namespace = 'f8435596-ee6a-4a3d-a304-e3f0c7bb5321';
const identifier = 'Hello World';
const uuidV3 = v3(identifier, namespace);

const expectedV3 = '51bfe3fe-6be8-3192-8ff2-b86ffd390fcc';

return req.ok({
v3: {
uuid: uuidV3,
valid: validate(uuidV3),
version: version(uuidV3),
expectedV3: expectedV3,
show all 18 lines

The following response is returned:

{
"v3": {
"uuid": "51bfe3fe-6be8-3192-8ff2-b86ffd390fcc",
"valid": true,
"version": 3,
"expectedV3": "51bfe3fe-6be8-3192-8ff2-b86ffd390fcc"
}
}
Functions support

Functions provides a rich set of tools. For help with situations not covered here, contact PubNub Support.

Last updated on