Javascript library that lets you generate and manage unique identifiers GUIDs
npm install js-guidjs-guid is a javascript library that lets you generate and manage unique identifiers GUIDs written with TypeScript.
``bash
npm install js-guid
yarn add js-guid
`
In order to start using the library use the following statement:
`js
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. |
#### Guid.EMPTY
The Empty Guid string (all zeros).
example
`js
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
`js
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. |true
| _returns_ | if valid, false otherwise. |Error
| _throws_ | if value is not a valid Guid. |
Example
`js
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. |Uint8Array(16)
| _returns_ | if value is string. Or, returns string if value is Uint8Array(16). |Error
| _throws_ | if value is not a valid Guid, or type is not supported. |
Example
`js
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. |Error
| _returns_ | new Guid instance. |
| _throws_ | if value is not a valid Guid, or type is not supported. |
Example
`js
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
`js
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
`js
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. |true
| _returns_ | if equals, false otherwise. |Error
| _throws_ | if value is not a valid Guid. or type not spported. |
Example
`js
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
`js
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
``