@shopascart/uuid
v1.2.4
Published
A uuid generator
Downloads
13
Readme
Unique ID
Description
The UniqueID algorithm is designed to generate unique identifiers for various purposes, such as user IDs, product IDs, order IDs, and so on. The algorithm generates IDs of two types: string and number. The string IDs consist of a prefix (optional), a UUID (Universal Unique Identifier), a timestamp, a random number, and a random bit. The number IDs are generated based on a random number, a timestamp, and a random bit.
How It Works
It generates a single random bit using the
RandomBits
function.It gets the current timestamp using new Date().getTime().
It creates an array of random characters from the alphanumeric character set "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789".
It generates a random 64-bit integer using
Math.random() * 100000000 * 0x100000000
It generates a UUID using crypto.randomBytes.
It concatenates the prefix (if present), the UUID, the timestamp, the random number, and the random bit to create a unique string.
If the requested type is "string", it returns the unique string truncated to the specified length.
If the requested type is "number", it returns a unique number generated by concatenating the timestamp, the random number, and the random bit, and truncating to the specified length.
For string identifiers, the elements are combined into a single string in the following format: [prefix]_[uuid][timestamp][randomNumber][randomBit]. The prefix is optional. The resulting string is then truncated to the desired length specified in the constructor.
For number identifiers, the elements are concatenated into a single string in the following format: [randomNumber][timestamp][randomBit]. The resulting string is then converted to a number and truncated to the desired length specified in the constructor.
The UniqueID algorithm provides a UUID
wrapper function that exposes two methods: generate and test. The UUID function is a wrapper function to the UniqueID class. It provides a more user-friendly interface for generating and testing unique IDs by exposing two methods: generate and test. These methods internally call the corresponding methods of the UniqueID class and provide the necessary options and parameters. The function takes an optional options parameter that can be used to configure the prefix, length, and type of the unique ID.
The generate
method generates a unique ID based on the options provided, or by default, generates a unique ID with a length of 16 characters and a type of "string". It returns the generated unique ID.
The test
method tests the uniqueness of the unique ID algorithm by generating a large number of unique IDs and checking for duplicates. It returns a boolean that indicates whether the list of generated unique IDs are unique or not.
Installation
npm install @shopascart/uuid
Usage
import UUID from '@shopascart/uuid';
const uuid = new UUID();
const uniqueID = uuid.generate();
Limitations
The algorithm may have collisions (i.e., generate the same ID twice) in extremely rare cases. The probability of a collision is extremely low, but it is not zero. The algorithm is still in development and may be improved in the future.