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

js-guid

v1.0.2

Published

Javascript library that lets you generate and manage unique identifiers GUIDs

Downloads

55,336

Readme

js-guid

Testing js-guid

js-guid is a javascript library that lets you generate and manage unique identifiers GUIDs written with TypeScript.

Quick start

1. Install

npm install js-guid

# or

yarn add js-guid

2. Library APIs

In order to start using the library use the following statement:

import { Guid } from 'js-guid';

// or

const { Guid } = require('js-guid');

| API | Description | | ---------------------------------------- | ---------------------------------------------------------------------- | | Guid.EMPTY | (static) The Empty Guid string (all zeros). | | Guid.newGuid() | (static) Generate a new v4 Guid and return a new instance of the Guid. | | Guid.isValid(value) | (static) Checks if the given value is a valid GUID. | | Guid.parse(value) | (static) Parse the given value into the opposite type. | | new Guid(value?) | Instantiate a new Guid object. | | guid.toString() | Returns the string format of the Guid. | | guid.toByteArray() | Returns the Uint8Array of the Guid. | | guid.equals(value) | Compare the Given value with the current Guid. | | guid.isEmpty() | Return {True} if the Guid holds an empty value, False otherwise. |

API details

Guid.EMPTY

The Empty Guid string (all zeros).

example

import { Guid } from 'js-guid';

console.log(Guid.Empty);

// Output
// ⇨ '00000000-0000-0000-0000-000000000000'

Guid.newGuid()

Generate a new v4 Guid and return a new instance of the Guid.

Example

import { Guid } from 'js-guid';

console.log(Guid.newGuid());

// Output
// ⇨ '77eb3969-19fd-4223-907a-5749669f1178'

Guid.isValid(value)

Checks if the given value is a valid GUID.

| Key | Description | | --------- | --------------------------------------- | | value | A valid Guid String or Uint8Array. | | returns | true if valid, false otherwise. | | throws | Error if value is not a valid Guid. |

Example

import { Guid } from 'js-guid';

console.log(Guid.isValid('77eb3969-19fd-4223-907a-5749669f1178'));

console.log(Guid.isValid(new Uint8Array([
  105, 57, 235, 119,
  253, 25, 35, 66,
  144, 122, 87, 73,
  102, 159, 17, 120,
]));

// Output
// ⇨ true
// ⇨ true

Guid.parse(value)

Parse the given value into the opposite type.

Note : if value is string the function return a {Uint8Array of 16 elements}, otherwise it return a {string} if the value is a Uint8Array.

| Key | Description | | --------- | ----------------------------------------------------------------------------------------- | | value | A valid Guid String or Uint8Array. | | returns | Uint8Array(16) if value is string. Or, returns string if value is Uint8Array(16). | | throws | Error if value is not a valid Guid, or type is not supported. |

Example

import { Guid } from 'js-guid';

console.log(Guid.parse('77eb3969-19fd-4223-907a-5749669f1178'));

// Output
// ⇨ [
//    105, 57, 235, 119,
//    253, 25, 35, 66,
//    144, 122, 87, 73,
//    102, 159, 17, 120,
//  ]

console.log(Guid.isValid(new Uint8Array([
  105, 57, 235, 119,
  253, 25, 35, 66,
  144, 122, 87, 73,
  102, 159, 17, 120,
]));

// Output
// ⇨ '77eb3969-19fd-4223-907a-5749669f1178'

new Guid(value?)

Create a new instance of the Guid with the given value, or generate a new Guid if no value was given.

| Key | Description | | --------- | ----------------------------------------------------------------- | | value | A valid Guid String or Uint8Array. | | returns | new Guid instance. | | throws | Error if value is not a valid Guid, or type is not supported. |

Example

import { Guid } from 'js-guid';

let guid = new Guid();
console.log(guid.toString());

guid = new Guid('77eb3969-19fd-4223-907a-5749669f1178');
console.log(guid.toString());

guid = new Guid(
  new Uint8Array([
    105, 57, 235, 119, 253, 25, 35, 66, 144, 122, 87, 73, 102, 159, 17, 120,
  ]),
);
console.log(guid.toString());

// Output
// ⇨ '77eb3969-19fd-4223-907a-5749669f1178'
// ⇨ '77eb3969-19fd-4223-907a-5749669f1178'
// ⇨ '77eb3969-19fd-4223-907a-5749669f1178'

Guid.toString()

Returns the string format of the Guid.

| Key | Description | | --------- | ------------------------------------ | | returns | string value of the Guid instance. |

Example

import { Guid } from 'js-guid';

const guid = new Guid();
console.log(guid.toString());

// Output
// ⇨ '77eb3969-19fd-4223-907a-5749669f1178'

Guid.toByteArray()

Returns the Uint8Array of the Guid.

| Key | Description | | --------- | -------------------------------------------- | | returns | Uint8Array(16) value of the Guid instance. |

Example

import { Guid } from 'js-guid';

const guid = new Guid();
console.log(guid.toByteArray());

// Output
// ⇨ [
//    105, 57, 235, 119,
//    253, 25, 35, 66,
//    144, 122, 87, 73,
//    102, 159, 17, 120,
//  ]

Guid.equals(value)

Compare the Given value with the current Guid.

| Key | Description | | --------- | ------------------------------------------------------------- | | value | A valid Guid String, Uint8Array or Guid object. | | returns | true if equals, false otherwise. | | throws | Error if value is not a valid Guid. or type not spported. |

Example

import { Guid } from 'js-guid';

const guid = new Guid('77eb3969-19fd-4223-907a-5749669f1178');
const guid2 = new Guid();

console.log(guid.equals(guid2));
console.log(guid.equals('77eb3969-19fd-4223-907a-5749669f1178'));
console.log(
  guid.equals(
    new Uint8Array([
      105, 57, 235, 119, 253, 25, 35, 66, 144, 122, 87, 73, 102, 159, 17, 120,
    ]),
  ),
);

// Output
// ⇨ false
// ⇨ true
// ⇨ true

Guid.isEmpty()

Return {True} if the Guid holds an empty value, False otherwise.

| Key | Description | | --------- | ------------------------------------------------------ | | returns | true if Guid equals Guid.Empty, false otherwise. |

Example

import { Guid } from 'js-guid';

let guid = new Guid();
console.log(guid.isEmpty());

guid = new Guid(Guid.Empty);
console.log(guid.isEmpty());

// Output
// ⇨ false
// ⇨ true