GUID :

A GUID (Global Unique Identifier) is Microsoft’s implementation of a unique identifier (UUID).

  • In its simplest form, Guid lets we generate raw GUID formatted strings:
JAVASCRIPT CODE
Guid.raw();
// -> '6fdf6ffc-ed77-94fa-407e-a7b86ed9e59d'
  • Let’s generate a new Guid instance.
JAVASCRIPT CODE
var guid = Guid.create();
[ad type=”banner”]
  • We’ve now got an object which we can work with programmatic manner. Lets check the validity of our Guid using the built-in validator :
JAVASCRIPT CODE
Guid.isGuid(guid);
// -> true
Guid.value;
// -> '6fdf6ffc-ed77-94fa-407e-a7b86ed9e59d'
  • A handy bit of functionality is that its toString method returns the string value.
JAVASCRIPT CODE
var itemUrl = "http://whatever.com/items/" + guid;
// -> 'http://whatever.com/items/6fdf6ffc-ed77-94fa-407e-a7b86ed9e59d'
  • If we need a placeholder Guid, or a value to represent a non-GUID, use the static EMPTY property:
JAVASCRIPT CODE
Guid.EMPTY;
// -> '00000000-0000-0000-0000-000000000000'
  • Once we have a Guid object, we can’t change its value:
JAVASCRIPT CODE
guid.value = "go suck it, guid!"
guid.value;
// -> '6fdf6ffc-ed77-94fa-407e-a7b86ed9e59d‘
  • To instantiate an Guid object using an existing GUID string, use the constructor:
JAVASCRIPT CODE
var guid = new Guid('6fdf6ffc-ed77-94fa-407e-a7b86ed9e59d');
  • we can check the equality of two different Guid objects using the equals instance method.
  • Compare a Guid object to a GUID string:
JAVASCRIPT CODE
guid.equals('6fdf6ffc-ed77-94fa-407e-a7b86ed9e59d');
// -> true
  • Compare two Guid objects:
JAVASCRIPT CODE
guid.equals(new Guid('6fdf6ffc-ed77-94fa-407e-a7b86ed9e59d'));
// -> true

CommonJS Module:

 

JAVASCRIPT CODE
exports.generate = function()
{
var guid = 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c)
{
var r = Math.random()*16|0, v = c == 'x' ? r : (r&0x3|0x8);
return v.toString(16);
});
return guid;
};
[ad type=”banner”]

UUID:

  • Universally Unique Identifier (UUID) is an identifier standard used in software construction, standardized by the Open Software Foundation (OSF) as part of the Distributed Computing Environment (DCE).”
  • A UUID is a 16-octet (128-bit) number.
  • A UUID is represented by 32 lowercase hexadecimal digits and it displayed in 5 groups separated by hyphens(-).
  • This represent in form of 8-4-4-4-12 for a total of 36 characters (32 alphanumeric characters and four hyphens).
  • Simple, fast generation of RFC4122 UUIDS.

CommonJS Module:

npm install uuid

JAVASCRIPT CODE
// Generate a v1 UUID (time-based) 
const uuidV1 = require('uuid/v1');
uuidV1(); // -> '6c84fb90-12c4-11e1-840d-7b25c5ee775a'
// Generate a v4 UUID (random)
const uuidV4 = require('uuid/v4');
uuidV4(); // -> '110ec58a-a0f2-4ac4-8393-c866d813b8d1'
JAVASCRIPT CODE
uuid.v1([options [, buffer [, offset]]])
  • To Generate and return a RFC4122 v1 (timestamp-based) UUID.
  • options – (Object) Optional uuid state to apply.

Properties may include the following:

  • node (Array) Node id as Array of 6 bytes . Default: Randomly generated ID.
  • clockseq – (Number between 0 – 0x3fff) RFC clock sequence. Default: An internally maintained clockseq is used.
  • msecs – (Number | Date) Time in milliseconds since unix Epoch. Default: The current time is used.
  • nsecs – (Number between 0-9999) additional time, in 100-nanosecond units. Ignored if msecs is unspecified.
  • Default: internal uuid counter is used.
  • buffer – (Array | Buffer) Array or buffer where UUID bytes are to be written.
  • offset – (Number) Starting index in buffer at which to begin writing.

Example1: Generate string UUID with fully-specified options

JAVASCRIPT CODE
uuid.v1(
{
node: [0x01, 0x23, 0x45, 0x67, 0x89, 0xab],
clockseq: 0x1234,
msecs: new Date('2011-11-01').getTime(),
nsecs: 5678
}
); // -> "710b962e-041c-11e1-9234-0123456789ab"

Example2: In-place generation of two binary IDs

JAVASCRIPT CODE
// Generate two ids in an array 
const arr = new Array(32); // -> []
uuid.v1(null, arr, 0); // -> [02 a2 ce 90 14 32 11 e1 85 58 0b 48 8e 4f c1 15]
uuid.v1(null, arr, 16); // -> [02 a2 ce 90 14 32 11 e1 85 58 0b 48 8e 4f c1 15 02 a3 1c b0 14 32 11 e1 85 58 0b 48 8e 4f c1 15]
JAVASCRIPT CODE
uuid.v4([options [, buffer [, offset]]])
  • To Generate and return a RFC4122 v4 UUID.
  • options – (Object) Optional uuid state to apply.

Properties:

  • random – (Number[16]) Array of 16 numbers (0-255) to use in place of randomly generated values
  • rng
    • (Function) Random # generator to use.
    • Set to one of the built-in generators – uuid.mathRNG (all platforms), uuid.nodeRNG (node.js only), uuid.whatwgRNG (WebKit only) – or a custom function that returns an array[16] of byte values.
  • buffer – (Array | Buffer) Array or buffer where UUID bytes are to be written.
  • offset – (Number) Starting index in buffer at which to begin writing.

Example1: Generate string UUID with fully-specified options

javascript code
uuid.v4(
{
random: [
0x10, 0x91, 0x56, 0xbe, 0xc4, 0xfb, 0xc1, 0xea,
0x71, 0xb4, 0xef, 0xe1, 0x67, 0x1c, 0x58, 0x36
]
}
);
// -> "109156be-c4fb-41ea-b1b4-efe1671c5836"

Example2: Generate two IDs in a single buffer

javascript code
const buffer = new Array(32); // (or 'new Buffer' in node.js) 
uuid.v4(null, buffer, 0);
uuid.v4(null, buffer, 16);

How to Create a GUID / UUID in Javascript

 1.To create an rfc4122 version 4 compliant guid

javascript code
function createGuid()
{
return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c)
{
var r = Math.random()*16|0, v = c === 'x' ? r : (r&0x3|0x8);
return v.toString(16);
});
}

Usage:

javascript code
var uuid = createGuid();
>>> "e2ece964-0315-4b91-b411-20f9868ce7d4"
[ad type=”banner”]

Categorized in: