npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

uuid-tool

v2.0.3

Published

Lightweight UUID for JavaScript. Parse and generate UUIDs. Convert between string and byte array.

Downloads

5,889

Readme

UUID Tool

Version MIT License

Lightweight UUID library for JavaScript, written in TypeScript. (Types declarations included.) Simple to use, simply works. Well tested and no dependencies. For NodeJS and Browser.

Parse and generate UUIDs. Convert between UUID string (36 byte) to 16 byte array. UUID RFC 4122 Version 4

Install

npm i uuid-tool

NPM Package

Example

let uuid = UuidTool.newUuid();
let bytes = UuidTool.toBytes(uuid);
let strAgain = UuidTool.toString(bytes);
let isValid = UuidTool.isUuid(strAgain);
let isEqual = UuidTool.compare(uuid1, uuid2);

You can also use the Uuid class.

let uuid = new Uuid(byteOrString);
let str = uuid.toString();
let bytes = uuid.toBytes();
// Generate a new UUID
let uuid = new Uuid();
// Compare (case insensitive)
let uuid1 = new Uuid('3C09B262-49C7-466F-8B4F-626BCA1EC9BC');
let uuid2 = new Uuid('3c09b262-49c7-466f-8b4f-626bca1ec9bc');

if (uuid1.equals(uuid2)) {
  console.log('The IDs match.');
} else {
  console.log('The IDs do not match.');
}

// Result: The IDs match.

In NodeJS e.g.

const Uuid = require('uuid-tool').Uuid;
console.log(new Uuid().toString());

From Json:

const jsonStr = '{ "id": "3c09b262-49c7-466f-8b4f-626bca1ec9bc" }';
const uuid = Uuid.fromJson(jsonStr);
// or as already parsed json object:
const jsonObj = { id: '3c09b262-49c7-466f-8b4f-626bca1ec9bc' };
const uuid = Uuid.fromJson(jsonObj);
// otherwise it throws an error on invalid inputs.

API

UuidTool


toBytes (uuid)

Converts an UUID string to an UUID byte array.

| Param | Type | Description | | ----- | ------ | ------------------------------------------------------- | | uuid | string | UUID string e.g. '1FBD384C-B2A1-41C6-84AF-43CABDF44124' |

Returns number[] UUID byte array (length: 16)


toString (bytes)

Converts an UUID byte array to an UUID string.

| Param | Type | Description | | ----- | -------- | ------------------------------------------ | | bytes | number[] | UUID Byte array (length 16 bytes of bytes) |

Returns string UUID string. (length 36)


newUuid ()

Generates a new (pseudo) UUID RFC 4122 Version 4.

Returns string UUID e.g. '1FBD384C-B2A1-41C6-84AF-43CABDF44124'


isUuid (uuid)

Converts an UUID byte array to an UUID string.

| Param | Type | Description | | ----- | ------ | ------------------------------------------------------- | | uuid | string | UUID string e.g. '1FBD384C-B2A1-41C6-84AF-43CABDF44124' |

Returns boolean True if valid, otherwise false.


compare (uuid1, uuid2)

Compares two UUIDs.

| Param | Type | Description | | ----- | ------ | ----------- | | uuid1 | string | UUID string | | uuid2 | string | UUID string |

Returns boolean True if equal, otherwise false.


Uuid

generate ()

Re-generates a new UUID for this instance.

Returns this The same UUID instance.


fromBytes (bytes)

Converts an UUID byte array to an UUID string. (length: 36) Note that the internal UUID has not yet been validated. Use the method isValid.

| Param | Type | Description | | ----- | -------- | ------------------------------------------ | | bytes | number[] | UUID Byte array (length 16 bytes of bytes) |

Returns this The same UUID instance.


toBytes ()

Converts this UUID to an UUID byte array. (length: 16)

Returns number[] The UUID as byte array.


fromString (uuid)

Converts an UUID byte array to an UUID string. (length: 36) Note that the internal UUID has not yet been validated. Use the method isValid.

| Param | Type | Description | | ----- | ------ | -------------------------------------------------- | | uuid | string | string e.g. '1FBD384C-B2A1-41C6-84AF-43CABDF44124' |

Returns this The same UUID instance.


toString ()

To UUID string. HINT Set case mode by Uuid.stringExportFormat = 'uppercase' | 'lowercase'; This also affects the result of JSON.stringify(...);

Returns string UUID string e.g. '1FBD384C-B2A1-41C6-84AF-43CABDF44124'


isValid ()

Validate this UUID.

Returns boolean True if valid, otherwise false.


equals (uuid)

Compares this UUID with another UUID.

| Param | Type | Description | | ----- | -------------- | ------------------------------------- | | uuid | Uuid | string | Another UUID instance or uuid-string. |

Returns boolean True if equal, otherwise false.


static fromJson (json)

Converts the JSON representation of this class to this class.

| Param | Type | Description | | ----- | ------------------ | ---------------------------------------- | | uuid | UuidLike | string | The string representation of this class. |

Returns Uuid The Uuid class with the parsed input object.


License

MIT © Copyright 2018 - 2021 Dominik-Andreas Geng (@domske)

UUID logo © Copyright 2021 Dominik Geng