npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

dcsm-random-timestamp-based-string-generator

v1.1.1

Published

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.

Downloads

3

Readme

Random Timestamp-Based String Generator

npm version License: MPL 2.0

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.

Why use this module?

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.

Features

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

Table of Contents

Installation

To install this module, run the following command:

npm install dcsm-random-timestamp-based-string-generator

Usage

RandomTBS

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.

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

Utility Functions

These functions are provided for convenience.

randomTBS

This function returns a new instance of RandomTBS.

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

timestampFromRandomTBS

This function retrieves the timestamp from a generated random timestamp-based string.

import { timestampFromRandomTBS } from 'dcsm-random-timestamp-based-string-generator';

const randomString = "GCXlRnAzajcXUddQxJxtxnFGLJm";
const timestamp = timestampFromRandomTBS(randomString);// Returns: 1696339642840

generateRandomTBS

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.

import generateRandomTBS from 'dcsm-random-timestamp-based-string-generator';

const randomString = generateRandomTBS();// Returns: "GCXlRnAzajcXUddQxJxtxnFGLJm"

API

RandomTBS

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.

randomTBS(): RandomTBS

Returns a new instance of RandomTBS.

timestampFromRandomTBS(stringGeneratedByRandomTimestampBasedString: string): number

Retrieves the timestamp from a generated random timestamp-based string.

generateRandomTBS(): string

Generates a random timestamp-based string.

License

This module is licensed under the Mozilla Public License 2.0. You can find the full license text in the LICENSE file.