1.2.4 • Published 8 months ago

@shopascart/uuid v1.2.4

Weekly downloads
-
License
MIT
Repository
github
Last release
8 months ago

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_uuidrandomNumber. 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: randomNumberrandomBit. 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.

1.2.0

9 months ago

1.2.4

8 months ago

1.2.3

8 months ago

1.2.2

8 months ago

1.2.1

8 months ago

1.1.2

12 months ago

1.1.1

1 year ago

1.1.0

1 year ago

1.0.5

1 year ago

1.0.4

1 year ago

1.0.3

1 year ago

1.0.2

1 year ago

1.0.1

1 year ago

1.0.0

1 year ago