包详细信息

uuidjs

LiosK66.4kApache-2.05.1.0

RFC-compliant UUID Generator for JavaScript

library, uuid, guid, rfc9562

自述文件

UUID.js - RFC-compliant UUID Generator for JavaScript

npm License

Synopsis

<!-- HTML5 -->
<script type="module">
  import { UUID } from "https://unpkg.com/uuidjs@^5";
  const uuid = UUID.generate();
</script>
// Node.js
import { UUID } from "uuidjs";
const uuid = UUID.generate();
// TypeScript
import { UUID } from "uuidjs";
const str: string = UUID.generate();
const obj: UUID = UUID.genV4();
# Command-line
npx uuidjs

Description

UUID.js is a JavaScript/ECMAScript library to generate RFC 9562 compliant Universally Unique IDentifiers (UUIDs). This library supports UUIDv4 (random number-based UUIDs), UUIDv1 (Gregorian time-based UUIDs), and UUIDv6 (Reordered Gregorian time-based UUIDs). It also provides an object-oriented interface to print a generated or parsed UUID in a variety of forms.

Features

  • Generates UUIDv4 (random number-based UUIDs), UUIDv1 (Gregorian time-based UUIDs), and UUIDv6 (Reordered Gregorian time-based UUIDs)
  • Provides an object-oriented interface to print various string representations of a generated or parsed UUID
  • Utilizes a cryptographically secure pseudo-random number generator if available, whereas falling back to Math.random() otherwise
  • Appends extra random bits to compensate for the lower timestamp resolution of JavaScript than that required for UUIDv1 and UUIDv6
  • Comes with a lot of test cases including format checks and statistical tests to maintain a high-quality code base

Usage Examples

Import UUID class:

import { UUID } from "uuidjs";
// or on browsers:
// import { UUID } from "https://unpkg.com/uuidjs@^5";

UUID.generate() returns a UUIDv4 as a hexadecimal string.

// Create a UUIDv4 as a hexadecimal string
console.log(UUID.generate());   // fa84cf42-ffdf-4975-b42b-31ab5fb983eb

UUID.genV4(), UUID.genV1(), UUID.genV6(), and UUID.parse() return a UUID object that has various fields and methods.

// Create a UUIDv4 (random number-based UUID) object
const objV4 = UUID.genV4();

// Create a UUIDv1 (Gregorian time-based UUID) object
const objV1 = UUID.genV1();

// Create a UUIDv6 (Reordered Gregorian time-based UUID) object
const objV6 = UUID.genV6();

// Create a UUID object from a hexadecimal string
const uuid = UUID.parse("a0e0f130-8c21-11df-92d9-95795a3bcd40");

// Get string representations of a UUID object
console.log(uuid.toString());   // "a0e0f130-8c21-11df-92d9-95795a3bcd40"
console.log(uuid.hexString);    // "a0e0f130-8c21-11df-92d9-95795a3bcd40"
console.log(uuid.hexNoDelim);   // "a0e0f1308c2111df92d995795a3bcd40"
console.log(uuid.bitString);    // "101000001110000 ... 1100110101000000"
console.log(uuid.urn);          // "urn:uuid:a0e0f130-8c21-11df-92d9-95795a3bcd40"

// Compare UUID objects
console.log(objV4.equals(objV1));   // false

// Get UUID version numbers
console.log(objV4.version); // 4
console.log(objV1.version); // 1
console.log(objV6.version); // 6

License

Copyright (c) 2010-2024 LiosK

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

Author

LiosK contact@mail.liosk.net

See Also

更新日志

Changelog

v5.1.0 - 2024-05-12

  • Marked UUID.genV6() as stable
  • Updated documents with RFC 9562 verbiage
  • Updated dev dependencies

v5.0.1 - 2023-03-30

Added

  • CHANGELOG.md to NPM package

Maintenance

  • Updated TypeScript to 5.0
  • Refined doc comment style

v5.0.0 - 2023-01-28

Breaking changes

  • Migrated to native ES Modules from global variable and CommonJS export
    • Added named UUID export and removed default export
    • Removed UUID.overwrittenUUID property (a.k.a. no conflict mode)
  • Removed node:crypto module-based CSPRNG implementation
    • Now requires Web Crypto API to utilize cryptographically secure pseudorandom number generators
  • Changed target ECMAScript version from ES3 to ES2016
  • Removed uuid.core.js and bower.json from repository
  • Fixed wrong return type declaration of UUID.parse(): UUID -> UUID | null
  • Placed tighter type constraints on UUID class members
    • Marked constructor() as private
    • Marked UUID.FIELD_NAMES, UUID.FIELD_SIZES, UUID#intFields, UUID#bitFields, and UUID#hexFields as read-only arrays/objects

Dev environment changes

  • Migrated to TypeScript and transpilation from pure JavaScript
    • Replaced manually written type declaration with auto-generated .d.ts
  • Adopted class declaration syntax
  • Applied Prettier style to source code
  • Migrated to TypeDoc from JSDoc for API document generation
  • Updated dev dependencies

Migration notes

Import the UUID class using the ESM syntax:

<!-- HTML5 -->
-<script src="https://unpkg.com/uuidjs@^4"></script>
-<script>
+<script type="module">
+  import { UUID } from "https://unpkg.com/uuidjs@^5";
   const uuid = UUID.generate();
 </script>
// Node.js
-const UUID = require("uuidjs");
+import { UUID } from "uuidjs";
const uuid = UUID.generate();

Call static methods through the UUID class, rather than importing them directly:

// Node.js
-const { generate } = require("uuidjs");
-const uuid = generate();
+import { UUID } from "uuidjs";
+const uuid = UUID.generate();

Run type checking relating to the following items, as these items have different type declarations than those in v4:

  • UUID.parse()
  • UUID.FIELD_NAMES
  • UUID.FIELD_SIZES
  • new UUID()
  • UUID#intFields
  • UUID#bitFields
  • UUID#hexFields

v4.2.14

Last version that:

  • Retains ECMAScript 3 compatibility
  • Supports Internet Explorer 6
  • Registers UUID in global scope
  • Exports CommonJS entry point
  • Accompanies uuid.core.js variant
  • Ships with bower.json
  • Uses node:crypto-based cryptographically secure pseudorandom number generator on Node.js