Updated April 6, 2023
Introduction to TypeScript UUID
A Universal Unique Identifier is called UUID in TypeScript. They are also known as Globally Unique Identifiers represented as GUID and is a Microsoft version of UUID. A UUID is 16 bytes value or 128 bits long used to guarantee uniqueness across time and space. A UUID is represented by hexadecimal value, which can be used to identify the rows uniquely in a database table by acting as a primary key, and UUID’s were originally used in Apollo Network Computing System, after which it was used in Distributed Computing Environment’s Open Software Foundation and later in the platform of Microsoft Windows.
Syntax to define UUID in TypeScript class:
function getuuid()
{
return ([1e7]+-1e3+-4e3+-8e3+-1e11).replace(/[018]/g, variable_name =>
(variable_name ^ crypto.getRandomValues(new Uint8Array(1))[0] & 15 >> variable_name / 4).toString(16)
);
}
- Where ([1e7]+-1e3+-4e3+-8e3+-1e11).replace(/[018]/g is equivalent to replacing the value 10000000-1000-4000-8000-100000000000 to hexadecimal characters,
- crypto.getRandomValues is used to generate random numbers cryptographically which are stored in the array UintArray passed as parameter to the crypto.getRandomValues function.
Working of UUID in TypeScript
- A universal unique identifier is a hexadecimal value created to uniquely identify the rows in a database table which acts as a primary key is called UUID in TypeScript.
- The Microsoft version of UUID is called Globally Unique Identifier, represented as GUID.
- A UUID is 16 bytes value or 128 buts long and guarantees uniqueness across space and time.
- UUID’s were originally used in Apollo Network Computing System, after which it was used in Distributed Computing Environment’s Open Software Foundation and later in the platform of Microsoft Windows.
Examples of TypeScript UUID
Given below are the examples of TypeScript UUID:
Example #1
TypeScript program generates a Universally Unique Identifier or UUID by replacing the standard value with hexadecimal characters and generating random numbers cryptographically and displaying the generated UUID as the output of the screen.
Code:
function getuuid()
{
return ([1e7]+-1e3+-4e3+-8e3+-1e11).replace(/[018]/g, fun =>
(fun ^ crypto.getRandomValues(new Uint8Array(1))[0] & 15 >> fun / 4).toString(16)
);
}
console.log('The UUID generated using the standard function is', getuuid());
Output:
In the above program, ([1e7]+-1e3+-4e3+-8e3+-1e11).replace(/[018]/g is equivalent to replacing the value 10000000-1000-4000-8000-100000000000 to hexadecimal characters, crypto.getRandomValues is used to generate random numbers cryptographically which are stored in the array UintArray passed as parameter to the crypto.getRandomValues function and that is how the UUID is generated, which is displayed as the output on the screen.
Example #2
TypeScript program generates a Universally Unique Identifier or UUID by replacing the standard value with hexadecimal characters and generating random numbers cryptographically and displaying the generated UUID as the output of the screen.
Code:
function getuuid()
{
return ([1e7]+-1e3+-4e3+-8e3+-1e11).replace(/[018]/g, fun =>
(fun ^ crypto.getRandomValues(new Uint8Array(1))[0] & 15 >> fun / 4).toString(16)
);
}
console.log('The UUID generated using the standard function is', getuuid());
Output:
In the above program, ([1e7]+-1e3+-4e3+-8e3+-1e11).replace(/[018]/g is equivalent to replacing the value 10000000-1000-4000-8000-100000000000 to hexadecimal characters, crypto.getRandomValues is used to generate random numbers cryptographically which are stored in the array UintArray passed as parameter to the crypto.getRandomValues function and that is how the UUID is generated, which is displayed as the output on the screen.
Example #3
TypeScript program generates a Universally Unique Identifier or UUID by replacing the standard value with hexadecimal characters and generating random numbers cryptographically and displaying the generated UUID as the output of the screen.
Code:
function getuuid()
{
return ([1e7]+-1e3+-4e3+-8e3+-1e11).replace(/[018]/g, fun =>
(fun ^ crypto.getRandomValues(new Uint8Array(1))[0] & 15 >> fun / 4).toString(16)
);
}
console.log('The UUID generated using the standard function is', getuuid());
Output:
In the above program, ([1e7]+-1e3+-4e3+-8e3+-1e11).replace(/[018]/g is equivalent to replacing the value 10000000-1000-4000-8000-100000000000 to hexadecimal characters, crypto.getRandomValues is used to generate random numbers cryptographically which are stored in the array UintArray passed as parameter to the crypto.getRandomValues function and that is how the UUID is generated, which is displayed as the output on the screen.
Example #4
TypeScript program generates a Universally Unique Identifier or UUID by replacing the standard value with hexadecimal characters and generating random numbers cryptographically and displaying the generated UUID as the output of the screen.
Code:
function getuuid()
{
return ([1e7]+-1e3+-4e3+-8e3+-1e11).replace(/[018]/g, fun =>
(fun ^ crypto.getRandomValues(new Uint8Array(1))[0] & 15 >> fun / 4).toString(16)
);
}
console.log('The UUID generated using the standard function is', getuuid());
Output:
In the above program, ([1e7]+-1e3+-4e3+-8e3+-1e11).replace(/[018]/g is equivalent to replacing the value 10000000-1000-4000-8000-100000000000 to hexadecimal characters, crypto.getRandomValues is used to generate random numbers cryptographically which are stored in the array UintArray passed as parameter to the crypto.getRandomValues function and that is how the UUID is generated, which is displayed as the output on the screen.
Example #5
TypeScript program generates a Universally Unique Identifier or UUID by replacing the standard value with hexadecimal characters and generating random numbers cryptographically and displaying the generated UUID as the output of the screen.
Code:
function getuuid()
{
return ([1e7]+-1e3+-4e3+-8e3+-1e11).replace(/[018]/g, fun =>
(fun ^ crypto.getRandomValues(new Uint8Array(1))[0] & 15 >> fun / 4).toString(16)
);
}
console.log('The UUID generated using the standard function is', getuuid());
Output:
In the above program, ([1e7]+-1e3+-4e3+-8e3+-1e11).replace(/[018]/g is equivalent to replacing the value 10000000-1000-4000-8000-100000000000 to hexadecimal characters, crypto.getRandomValues is used to generate random numbers cryptographically which are stored in the array UintArray passed as parameter to the crypto.getRandomValues function and that is how the UUID is generated, which is displayed as the output on the screen.
Rules and Regulations for Generating UUID
- A UUID must be 16 bytes long or 128 bits value.
- A UUID must be a unique value uniquely identifying the rows in a database table.
- A UUID must be made the primary key to identify the rows in the database table uniquely.
- A UUID can be generated only after it is converted to 16 bytes long hexadecimal value.
- The scope of UUID is universal, and the generated UUID is 16 bytes long, separated by a hypen to 5 different groups.
Recommended Articles
We hope that this EDUCBA information on “TypeScript UUID” was beneficial to you. You can view EDUCBA’s recommended articles for more information.