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:
- Let’s generate a new Guid instance.
- 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 :
- A handy bit of functionality is that its toString method returns the string value.
- If we need a placeholder Guid, or a value to represent a non-GUID, use the static EMPTY property:
- Once we have a Guid object, we can’t change its value:
- To instantiate an Guid object using an existing GUID string, use the constructor:
- we can check the equality of two different Guid objects using the equals instance method.
- Compare a Guid object to a GUID string:
- Compare two Guid objects:
CommonJS Module:
[pastacode lang=”javascript” manual=”exports.generate%20%3D%20function()%0A%7B%0A%20%20%20%20var%20guid%20%3D%20’xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx’.replace(%2F%5Bxy%5D%2Fg%2C%20function(c)%20%0A%7B%0A%20%20%20%20%20%20%20%20var%20r%20%3D%20Math.random()*16%7C0%2C%20v%20%3D%20c%20%3D%3D%20’x’%20%3F%20r%20%3A%20(r%260×3%7C0x8)%3B%0A%20%20%20%20%20%20%20%20%20return%20v.toString(16)%3B%0A%20%20%20%20%7D)%3B%20%20%20%20%20%0A%20%20%20%20return%20guid%3B%0A%7D%3B%0A” message=”JAVASCRIPT CODE” highlight=”” provider=”manual”/] [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
[pastacode lang=”javascript” manual=”%2F%2F%20Generate%20a%20v1%20UUID%20(time-based)%20%0Aconst%20uuidV1%20%3D%20require(‘uuid%2Fv1’)%3B%0AuuidV1()%3B%20%2F%2F%20-%3E%20’6c84fb90-12c4-11e1-840d-7b25c5ee775a’%20%0A%20%2F%2F%20Generate%20a%20v4%20UUID%20(random)%20%0Aconst%20uuidV4%20%3D%20require(‘uuid%2Fv4’)%3B%0AuuidV4()%3B%20%2F%2F%20-%3E%20’110ec58a-a0f2-4ac4-8393-c866d813b8d1’%20%0A” message=”JAVASCRIPT CODE” highlight=”” provider=”manual”/] [pastacode lang=”javascript” manual=”uuid.v1(%5Boptions%20%5B%2C%20buffer%20%5B%2C%20offset%5D%5D%5D)%0A” message=”JAVASCRIPT CODE” highlight=”” provider=”manual”/]- 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
[pastacode lang=”javascript” manual=”uuid.v1(%0A%7B%0A%20%20node%3A%20%5B0x01%2C%200×23%2C%200×45%2C%200×67%2C%200×89%2C%200xab%5D%2C%0A%20%20clockseq%3A%200×1234%2C%0A%20%20msecs%3A%20new%20Date(‘2011-11-01’).getTime()%2C%0A%20%20nsecs%3A%205678%0A%7D%0A)%3B%20%20%20%2F%2F%20-%3E%20%22710b962e-041c-11e1-9234-0123456789ab%22%20%0A” message=”JAVASCRIPT CODE” highlight=”” provider=”manual”/]Example2: In-place generation of two binary IDs
[pastacode lang=”javascript” manual=”%2F%2F%20Generate%20two%20ids%20in%20an%20array%20%0Aconst%20arr%20%3D%20new%20Array(32)%3B%20%2F%2F%20-%3E%20%5B%5D%20%0Auuid.v1(null%2C%20arr%2C%200)%3B%20%20%20%2F%2F%20-%3E%20%5B02%20a2%20ce%2090%2014%2032%2011%20e1%2085%2058%200b%2048%208e%204f%20c1%2015%5D%20%0Auuid.v1(null%2C%20arr%2C%2016)%3B%20%20%2F%2F%20-%3E%20%5B02%20a2%20ce%2090%2014%2032%2011%20e1%2085%2058%200b%2048%208e%204f%20c1%2015%2002%20a3%201c%20b0%2014%2032%2011%20e1%2085%2058%200b%2048%208e%204f%20c1%2015%5D%20%0A” message=”JAVASCRIPT CODE” highlight=”” provider=”manual”/] [pastacode lang=”javascript” manual=”uuid.v4(%5Boptions%20%5B%2C%20buffer%20%5B%2C%20offset%5D%5D%5D)%0A” message=”JAVASCRIPT CODE” highlight=”” provider=”manual”/]- 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
[pastacode lang=”javascript” manual=”uuid.v4(%0A%7B%0A%20%20random%3A%20%5B%0A%20%20%20%200×10%2C%200×91%2C%200×56%2C%200xbe%2C%200xc4%2C%200xfb%2C%200xc1%2C%200xea%2C%0A%20%20%20%200×71%2C%200xb4%2C%200xef%2C%200xe1%2C%200×67%2C%200x1c%2C%200×58%2C%200×36%0A%20%20%5D%0A%7D%0A)%3B%0A%2F%2F%20-%3E%20%22109156be-c4fb-41ea-b1b4-efe1671c5836%22%20%0A” message=”javascript code” highlight=”” provider=”manual”/]Example2: Generate two IDs in a single buffer
[pastacode lang=”javascript” manual=”const%20buffer%20%3D%20new%20Array(32)%3B%20%2F%2F%20(or%20’new%20Buffer’%20in%20node.js)%20%0Auuid.v4(null%2C%20buffer%2C%200)%3B%0Auuid.v4(null%2C%20buffer%2C%2016)%3B%0A” message=”javascript code” highlight=”” provider=”manual”/]How to Create a GUID / UUID in Javascript
1.To create an rfc4122 version 4 compliant guid
[pastacode lang=”javascript” manual=”function%20createGuid()%0A%7B%0A%20%20%20%20return%20’xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx’.replace(%2F%5Bxy%5D%2Fg%2C%20function(c)%0A%20%7B%0A%20%20%20%20%20%20%20%20var%20r%20%3D%20Math.random()*16%7C0%2C%20v%20%3D%20c%20%3D%3D%3D%20’x’%20%3F%20r%20%3A%20(r%260×3%7C0x8)%3B%0A%20%20%20%20%20%20%20%20return%20v.toString(16)%3B%0A%20%20%20%20%7D)%3B%0A%7D%0A” message=”javascript code” highlight=”” provider=”manual”/]