Skip to content

Shopascart/uuid

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

19 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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

  1. It generates a single random bit using the RandomBits function.

  2. It gets the current timestamp using new Date().getTime().

  3. It creates an array of random characters from the alphanumeric character set "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789".

  4. It generates a random 64-bit integer using Math.random() * 100000000 * 0x100000000

  5. It generates a UUID using crypto.randomBytes.

  6. It concatenates the prefix (if present), the UUID, the timestamp, the random number, and the random bit to create a unique string.

  7. If the requested type is "string", it returns the unique string truncated to the specified length.

  8. 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.

ord_5BOElwUNzRQjm3AC9ks9H                     260147348
└──┘└───────────────────┘                     └───────┘
 └─ Prefix └─ Generated Random Characters         └─ Generated Random Number

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.

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published