1.0.2 • Published 1 year ago

js-guid v1.0.2

Weekly downloads
212
License
GPL-3.0-only
Repository
github
Last release
1 year ago

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');
APIDescription
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.

KeyDescription
valueA valid Guid String or Uint8Array.
returnstrue if valid, false otherwise.
throwsError 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.

KeyDescription
valueA valid Guid String or Uint8Array.
returnsUint8Array(16) if value is string. Or, returns string if value is Uint8Array(16).
throwsError 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.

KeyDescription
valueA valid Guid String or Uint8Array.
returnsnew Guid instance.
throwsError 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.

KeyDescription
returnsstring 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.

KeyDescription
returnsUint8Array(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.

KeyDescription
valueA valid Guid String, Uint8Array or Guid object.
returnstrue if equals, false otherwise.
throwsError 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.

KeyDescription
returnstrue 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