A library for generating uniqe random alphabetical strings based on the current timestamp. It also provides a method for retrieving the timestamp from a generated string.
npm install dcsm-random-timestamp-based-string-generator

A library for generating uniqe random alphabetical strings based on the current timestamp. It also provides a method for retrieving the timestamp from a generated string.
This module can be used to generate unique identifiers that are based on the current timestamp. These identifiers can be used to track events, generate unique filenames, or create unique IDs for database records that are sortable in alphabetical order based on the time they were created.
The generated strings only contain the characters A-Z and a-z, and can be sorted chronologically based on the alphabetical order of the strings. Strings generated at the same millisecond are sortable based on the order in which they were generated.
- Generates a random string based on the current timestamp
- The strings are alphabetical and only contain the characters A-Z and a-z
- The timestamp when the string was generated is retrievable from generated strings
- The generated strings are unique and can be sorted in alphabetical order.
- Strings generated at the same millisecond are sortable based on the order in which they were generated.
- Installation
- Usage
- RandomTBS
- Utility Functions
- randomTBS
- timestampFromRandomTBS
- generateRandomTBS
- API
- License
To install this module, run the following command:
``bash`
npm install dcsm-random-timestamp-based-string-generator
This class provides methods to generate a random string based on the current timestamp and a method to retrieve the timestamp from a generated string.
`javascript
import { RandomTBS } from 'dcsm-random-timestamp-based-string-generator';
const generator = new RandomTBS();
const randomString = generator.generate();// Returns: "GCXlRnAzajcXUddQxJxtxnFGLJm"
const timestamp = generator.retriveTimestamp(randomString);// Returns: 1696339642840
`
To ensure that the generated strings are unique, the class uses a counter that is incremented every time a new string is generated at the same time (millisecond) the previous string was generated. The counter is reset to zero when the timestamp changes. This ensures that the generated strings are unique and sortable in alphabetical order based on the time they were generated.
NOTE: Use the same instance of RandomTBS to generate strings. If you create a new instance of RandomTBS every time you generate a string, the counter will be reset to zero every time. This will result in strings that are not sortable in alphabetical order based on the time they were generated if you generate multiple strings at the same time (millisecond).
These functions are provided for convenience.
This function returns a new instance of RandomTBS.
`javascript
import { randomTBS } from 'dcsm-random-timestamp-based-string-generator';
const generator = randomTBS();
const randomString = generator.generate();// Returns: "GCXlRnAzajcXUddQxJxtxnFGLJm"
const timestamp = generator.retriveTimestamp(randomString);// Returns: 1696339642840
`
This function retrieves the timestamp from a generated random timestamp-based string.
`javascript
import { timestampFromRandomTBS } from 'dcsm-random-timestamp-based-string-generator';
const randomString = "GCXlRnAzajcXUddQxJxtxnFGLJm";
const timestamp = timestampFromRandomTBS(randomString);// Returns: 1696339642840
`
This function creates a new instance of RandomTBS and generates a random timestamp-based string.
NOTE!
It is recommended to use the RandomTBS class instead and generate strings using the generate() method on the same instance of RandomTBS.
`javascript
import generateRandomTBS from 'dcsm-random-timestamp-based-string-generator';
const randomString = generateRandomTBS();// Returns: "GCXlRnAzajcXUddQxJxtxnFGLJm"
`
#### generate(): string
Generates a random string based on the current timestamp.
#### retriveTimestamp(stringGeneratedByRandomTimestampBasedString: string): number
Retrieves the timestamp from a generated random timestamp-based string.
Returns a new instance of RandomTBS`.
Retrieves the timestamp from a generated random timestamp-based string.
Generates a random timestamp-based string.
This module is licensed under the Mozilla Public License 2.0. You can find the full license text in the LICENSE file.