What is @sanity/uuid?
@sanity/uuid is a utility package for generating and validating UUIDs (Universally Unique Identifiers). It provides simple and efficient methods to create UUIDs and check their validity, making it useful for applications that require unique identifiers for objects, sessions, or other entities.
What are @sanity/uuid's main functionalities?
Generate UUID
This feature allows you to generate a new UUID. The `uuid` function generates a random UUID each time it is called.
const { uuid } = require('@sanity/uuid');
const newUuid = uuid();
console.log(newUuid);
Validate UUID
This feature allows you to validate whether a given string is a valid UUID. The `isUuid` function returns true if the string is a valid UUID, and false otherwise.
const { isUuid } = require('@sanity/uuid');
const valid = isUuid('123e4567-e89b-12d3-a456-426614174000');
console.log(valid);
Other packages similar to @sanity/uuid
uuid
The `uuid` package is one of the most popular libraries for generating UUIDs. It supports multiple versions of UUIDs (v1, v3, v4, v5) and provides a comprehensive API for generating and validating UUIDs. Compared to @sanity/uuid, it offers more flexibility and options for different UUID versions.
shortid
The `shortid` package generates short, unique, non-sequential ids. While it does not generate standard UUIDs, it is useful for cases where shorter unique identifiers are sufficient. It is simpler and more lightweight compared to @sanity/uuid.
nanoid
The `nanoid` package is a tiny, secure, URL-friendly, unique string ID generator. It is smaller and faster than UUID and shortid, making it a good alternative for generating unique identifiers in performance-critical applications. Unlike @sanity/uuid, it does not generate standard UUIDs.
@sanity/uuid
Generate a valid Sanity document ID
Installation
$ npm install @sanity/uuid
// or
$ yarn add @sanity/uuid
Usage
import {uuid} from '@sanity/uuid'
const id = uuid()
console.log(id)
License
MIT © Sanity.io