- Notifications
You must be signed in to change notification settings - Fork16
Pure JavaScript Based Universally Unique Identifiers (UUID)
rse/pure-uuid
Folders and files
Name | Name | Last commit message | Last commit date | |
---|---|---|---|---|
Repository files navigation
Pure JavaScript Based Universally Unique Identifier (UUID)
This is a pure JavaScript and dependency-free library for the generationof DCE 1.1, ISO/IEC 11578:1996 and IETF RFC-4122 compliant UniversallyUnique Identifier (UUID). It supports DCE 1.1 variant UUIDs of version1 (time and node based), version 3 (name based, MD5), version 4 (randomnumber based) and version 5 (name based, SHA-1). It can be used in bothNode.js based server and browser based clientenvironments.
The essential points of this implementation (in contrast to the manyothers) are: First, although internally 32/64 bit unsigned integerarithmentic and MD5/SHA-1 digest algorithmic is required, this UUIDimplementation is fully self-contained and dependency-free. Second,this implementation wraps around eitherUint8Array
,Buffer
orArray
standard classes and this way tries to represent UUIDs as bestas possible in the particular environment. Third, thanks to a UniversalModule Definition (UMD) wrapper, this library works out-of-the-box inall important JavaScript run-time environments.
UUIDs are 128 bit numbers which are intended to have a high likelihoodof uniqueness over space and time and are computationally difficult toguess. They are globally unique identifiers which can be locallygenerated without contacting a global registration authority. UUIDs areintended as unique identifiers for both mass tagging objects with anextremely short lifetime and to reliably identifying very persistentobjects across a network.
According to the DCE 1.1, ISO/IEC 11578:1996 and IETF RFC-4122standards, a DCE 1.1 variant UUID is a 128 bit number defined out of 7fields, each field a multiple of an octet in size and stored in networkbyte order:
[4] version -->| |<-- || | | [16] [32] [16] | |time_hi time_low time_mid | _and_version |<---------------------------->||<------------>||<------------>| | MSB || || || | / || || || |/ || || || +------++------++------++------++------++------++------++------+~~ | 15 || 14 || 13 || 12 || 11 || 10 |####9 || 8 | | MSO || || || || || |#### || | +------++------++------++------++------++------++------++------+~~ 7654321076543210765432107654321076543210765432107654321076543210 ~~+------++------++------++------++------++------++------++------+##* 7 || 6 || 5 || 4 || 3 || 2 || 1 || 0 |##* || || || || || || || LSO | ~~+------++------++------++------++------++------++------++------+ 7654321076543210765432107654321076543210765432107654321076543210 | | || || /| | | || || /| | | || || LSB| |<---->||<---->||<-------------------------------------------->| |clk_seq clk_seq node |_hi_res _low [48] |[5-6] [8] || -->| |<-- variant [2-3]
An example of a UUID binary representation is the octet stream 0xF80x1D 0x4F 0xAE 0x7D 0xEC 0x11 0xD0 0xA7 0x65 0x00 0xA0 0xC9 0x1E 0x6B0xF6.
According to the DCE 1.1, ISO/IEC 11578:1996 and IETF RFC-4122standards, a DCE 1.1 variant UUID is represented as an ASCII stringconsisting of 8 hexadecimal digits followed by a hyphen, then threegroups of 4 hexadecimal digits each followed by a hyphen, then 12hexadecimal digits.
$ npm install pure-uuid
- global environment:
varuuid=newUUID(3,"ns:URL","http://example.com/");
- CommonJS environment:
varUUID=require("pure-uuid");varuuid=newUUID(3,"ns:URL","http://example.com/");
- AMD environment:
define(["pure-uuid"],function(UUID){varuuid=newUUID(3,"ns:URL","http://example.com/");});
interfaceUUID{/* making */make(version:number, ...params:any[]):UUID;/* parsing */parse(str:string):UUID;/* formatting */format(type?:string):string;/* formatting (alias) */toString(type?:string):string;/* sensible JSON serialization */toJSON():string;/* importing */import(arr:number[]):UUID;/* exporting */export():number[];/* byte-wise comparison */compare(other:UUID):boolean;/* equal check */equal(other:UUID):boolean;/* fold 1-4 times */fold(k:number):number[];}exportinterfaceUUIDConstructor{/* default construction */new():UUID;/* parsing construction */new(uuid:string):UUID;/* making construction */new(version:number):UUID;new(version:number,ns:string,data:string):UUID;}declarevarUUID:UUIDConstructor;exportdefaultUUID;
// load a UUIDuuid=newUUID("0a300ee9-f9e4-5697-a51a-efc7fafaba67");// make a UUID version 1 (time and node based)uuid=newUUID(1);// make a UUID version 3 (name-based, MD5)uuid=newUUID(3,"ns:URL","http://example.com/");// make a UUID version 4 (random number based)uuid=newUUID(4);// make a UUID version 5 (name-based, SHA-1)uuid=newUUID(5,"ns:URL","http://example.com/");// format a UUID in standard formatstr=uuid.format()str=uuid.format("std")// format a UUID in Base16 formatstr=uuid.format("b16")// format a UUID in ZeroMQ-Base85 formatstr=uuid.format("z85")
Copyright © 2004-2023 Dr. Ralf S. Engelschall (http://engelschall.com/)
Permission is hereby granted, free of charge, to any person obtaininga copy of this software and associated documentation files (the"Software"), to deal in the Software without restriction, includingwithout limitation the rights to use, copy, modify, merge, publish,distribute, sublicense, and/or sell copies of the Software, and topermit persons to whom the Software is furnished to do so, subject tothe following conditions:
The above copyright notice and this permission notice shall be includedin all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OFMERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANYCLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THESOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
About
Pure JavaScript Based Universally Unique Identifiers (UUID)