uuid

repository·main·Indexed 12 days ago

https://github.com/uuidjs/uuid

A complete, cross-platform library for creating RFC9562 (formerly RFC4122) compliant UUIDs. Version 14.0.1 supports standard versions v1, v3, v4, v5, v6, and v7. It is designed to be tree-shakable, zero-dependency, and secure using modern crypto APIs. Supports Node.js, browsers, and TypeScript. Note: CommonJS support was removed in uuid@12; ECMAScript Modules (ESM) are required.

Tokens
5.2K
Snippets
32
Records
36
Agent score
96%

What's inside uuid

  1. Supported environments for uuid

    main

    The uuid library supports the following environments:

    • Browsers: Tested against the latest versions of desktop Chrome, Safari, Firefox, and Edge. Mobile versions of these browsers are expected to work but are not explicitly tested.
    • Node.js: Supports current LTS releases and the release immediately prior (e.g., node@20 through node@24).
    • TypeScript: Supports TypeScript versions released within the past two years.
  2. Quickstart: Create a version 4 UUID

    main

    To generate a random (version 4) UUID, import the v4 function from the uuid package and call it. This is the most common way to generate unique identifiers.

    import { v4 as uuidv4 } from 'uuid';
    
    uuidv4(); // ⇨ 'b18794e8-5d0d-417c-b361-ba38e78411b4'
  3. Run the uuid browser example with Rollup.js

    main

    To run the browser-based demonstration of uuid using Rollup.js, follow these steps:

    1. Install dependencies using npm.
    2. Start the development server.
    3. Open the generated example-*.html files in your browser.

    This example demonstrates how uuid works in a browser environment and shows that tree-shaking effectively reduces bundle size when only specific UUID versions are imported.

    npm install
    npm start
  4. Run the uuid Benchmark in the Browser

    main

    To run the performance benchmarks in a web browser, use the start script and view the results in the browser's developer console.

    ```bash
    npm install
    npm run start

    After running the command, open benchmark.html in your browser and check the console for the output.

  5. Run the uuid Node.js Webpack example

    main

    To set up and run the uuid example project using Node.js and Webpack, follow these steps:

    1. Install dependencies using npm install.
    2. Run the tests and build process using npm test.

    After running these commands, Webpack will generate bundles located in the ./dist directory, which are then executed.

    npm install
    npm test
  6. Run the uuid Browser with Webpack example

    main

    To run the browser-based demonstration of uuid using Webpack, follow these steps in your terminal:

    1. Install dependencies using npm install.
    2. Start the development server using npm start.
    3. Open the generated example-*.html files in your browser to view the results.

    This example demonstrates how uuid can be bundled for the browser and shows that tree-shaking works as expected, allowing you to only include the specific UUID versions you need (e.g., v1.js or v4.js) in your final bundle.

    npm install
    npm start
  7. Understand the UUIDTypes return type

    main

    The UUIDTypes type represents the possible output formats for UUID generation functions. A UUID can be returned as either a standard string or as a Uint8Array (or a specific subclass of Uint8Array). This allows developers to choose between human-readable strings or high-performance byte buffers.

    // A UUID can be a string or a Uint8Array
    type UUIDTypes<TBuf extends Uint8Array = Uint8Array> = string | TBuf;