Giter Site home page Giter Site logo

uuid.js's Introduction

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 [email protected]

See Also

uuid.js's People

Contributors

liosk avatar

Stargazers

Greg Konush avatar  avatar idealphase avatar v1r avatar Stéphane Zaryavtre avatar kinlon avatar elyesa avatar 杨川 avatar huuya avatar Seasonley avatar Pedro Duarte avatar  avatar muners avatar Jedi Chou avatar Leandro Araújo avatar Chris Hart avatar  avatar Raffi Minassian avatar Lintao Fan avatar Nathaniel Palmer avatar Peter Mbanugo avatar Abdul Samad Ansari avatar Jake avatar atRonan avatar Andrei Luca avatar Johan Heyvaert avatar Evgeni Nikolov avatar Sasha Beg avatar Mohammed Alshehri avatar Gavin Wong avatar João Valente avatar Petar Kolev avatar David Lorenz avatar Hummel avatar ✅ Daniel Danielecki avatar  avatar Luke Chinworth avatar Pascal Tbf avatar Omar Hossam Eldin Kandil avatar Jakub Syrek avatar Johana Mburu avatar Sakar SR avatar Robert McGuinness avatar  avatar Chauncey McAskill avatar Alex Jennings avatar Alexey Kachalov avatar Ehab Reda avatar Pavel Fomchenkov avatar  avatar  avatar  avatar  avatar Jarvis Semou avatar  avatar wangruifang avatar 王言伟 avatar YuukiTsuchida avatar NULL avatar Fabio Lima avatar Masashi Hasegawa avatar  avatar Cat in the orange box. avatar Arunkumar R R avatar  avatar  avatar Sakamoto, Kazunori avatar mengxianghan avatar zhihaofans avatar D0g4r33 avatar  avatar Jamie Sparks avatar LGD.HuaFEEng avatar  avatar  avatar James avatar xunmeng avatar nebel avatar  avatar kylelin1998 avatar  avatar Alex Lawrence avatar Tag G avatar Echover avatar 伏毅 avatar Bart Wu avatar ferrinweb avatar  avatar cheng avatar Nikita Dobrov avatar  avatar  avatar Hico avatar  avatar  avatar 你好好瞧哦 avatar trackiss (Ryuki Kobayashi) avatar  avatar Roman avatar Hongbo Miao avatar

Watchers

Adeola Awoyemi avatar sepahsalar avatar  avatar Dan Herbert avatar Nihad Abbasov avatar Martin Botťánek avatar John Barounis avatar James Cloos avatar michael van ham avatar Tomoya Yamamoto avatar Sebastian Godelet avatar  avatar kazarus avatar Janvo Aldred avatar  avatar  avatar  avatar  avatar  avatar Ilmaç Pilagila avatar  avatar

uuid.js's Issues

TypeScript Declaration File

I would like to use "UUID.js" in a TypeScript project. Unfortunately this project does not have a TypeScript definiton file (.dts): https://www.npmjs.com/package/@types/uuidjs

I saw that "UUID.js" is well-documented and uses JSDoc. With JSDoc and tsd-jsdoc it's possible to auto-generate a *.d.ts file.

I can create a Pull Request with the generated definition file (includig "tsd-jsdoc" setup) if you would accept it. 😃

add `browser` option to `package.json`

When it's packed with webpack, the bundle is too large because of the polyfills of the crypto module, you can try it with the simple require('uuidjs') and webpack it !

So just adding browser option to package.json can make it used in browser.

I just simply offer a PR for you, it's better to extract cryptoPRNG function out

#10

Error loading uuid.js in Webpack. Can't resolve 'crypto'

I get an error when I am trying to load uuid.js in Webpack. Here is the error:

ERROR in ./build/components/uuid.js/src/uuid.js
Module not found: Error: Can't resolve 'crypto' in '/Users/liechtypo/sandbox/search-artifact/build/components/uuid.js/src'
@ ./build/components/uuid.js/src/uuid.js 102:57-74

npm package

Hey @LiosK, can you publish your package on npm, so that it can be used in Node.js projects? That would be great. 🌞

By the way, we are using your library in Wire's web application: https://wire.com/en/legal/#licenses - Thanks for providing this good piece of software. 👍

Can't load a module from TypeScript using ts-node

got this error:

/home/kuba/projects/jcubic/koduj/akademia/quiz/node_modules/ts-node/dist/index.js:851
            return old(m, filename);
                   ^
Error [ERR_REQUIRE_ESM]: require() of ES Module /home/kuba/projects/jcubic/koduj/akademia/quiz/node_modules/uuidjs/dist/uuid.js from /home/kuba/projects/jcubic/koduj/akademia/quiz/utils.ts not supported.
Instead change the require of uuid.js in /home/kuba/projects/jcubic/koduj/akademia/quiz/utils.ts to a dynamic import() which is available in all CommonJS modules.
    at require.extensions.<computed> [as .js] (/home/kuba/projects/jcubic/koduj/akademia/quiz/node_modules/ts-node/dist/index.js:851:20)                                                           
    at Object.<anonymous> (/home/kuba/projects/jcubic/koduj/akademia/quiz/utils.ts:4:18)
    at m._compile (/home/kuba/projects/jcubic/koduj/akademia/quiz/node_modules/ts-node/dist/index.js:857:29)                                                                               
    at require.extensions.<computed> [as .ts] (/home/kuba/projects/jcubic/koduj/akademia/quiz/node_modules/ts-node/dist/index.js:859:16)                                                           
    at Object.<anonymous> (/home/kuba/projects/jcubic/koduj/akademia/quiz/app.ts:21:17)
    at m._compile (/home/kuba/projects/jcubic/koduj/akademia/quiz/node_modules/ts-node/dist/index.js:857:29)                                                                               
    at require.extensions.<computed> [as .ts] (/home/kuba/projects/jcubic/koduj/akademia/quiz/node_modules/ts-node/dist/index.js:859:16)                                                           
    at phase4 (/home/kuba/projects/jcubic/koduj/akademia/quiz/node_modules/ts-node/dist/bin.js:466:20
)                                                                                                   
    at bootstrap (/home/kuba/projects/jcubic/koduj/akademia/quiz/node_modules/ts-node/dist/bin.js:54:12)                                                                                  
    at main (/home/kuba/projects/jcubic/koduj/akademia/quiz/node_modules/ts-node/dist/bin.js:33:12)
    at Object.<anonymous> (/home/kuba/projects/jcubic/koduj/akademia/quiz/node_modules/ts-node/dist/bin.js:579:5) {                                                                                
  code: 'ERR_REQUIRE_ESM'
}

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.