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

snowuuid

v1.0.0

Published

A distributed Snowflake UUID generator for creating unique identifiers with high scalability and low latency. Ideal for applications requiring unique IDs across distributed systems.

Downloads

64

Readme

Banner

SnowUUID

SnowUUID is a distributed UUID generator that implements the Snowflake algorithm, designed to create unique identifiers that can be generated across multiple nodes without collision. Each generated ID contains timestamp, worker ID, datacenter ID, and a sequence number.

Features

  • Unique ID Generation: Ensures that IDs are unique across distributed systems.
  • Timestamp Included: Each ID contains a timestamp, allowing for chronological sorting.
  • Configurable: Custom configurations for epoch, worker ID, and datacenter ID.
  • Scalable: Supports multiple workers and datacenters, making it suitable for large applications.

Usage

To use SnowUUID in your TypeScript project, follow these steps:

Installation

npm install snowuuid

Example

Here's a simple example demonstrating how to use the SnowUUID class:

import { SnowUUID } from 'snowuuid';

// Create a new SnowUUID instance with default settings
const uuidGenerator = new SnowUUID();

// Generate unique IDs
const id1 = uuidGenerator.nextId();
console.log(`Generated ID 1: ${id1}`);

const id2 = uuidGenerator.nextId();
console.log(`Generated ID 2: ${id2}`);

Custom Configuration

You can create a SnowUUID instance with custom settings for epoch, worker ID, and datacenter ID:

const customUUIDGenerator = new SnowUUID({
    epoch: 1672531200000n, // Custom epoch (January 1, 2023)
    workerId: 1n,         // Worker ID
    datacenterId: 1n      // Datacenter ID
});

const customId = customUUIDGenerator.nextId();
console.log(`Custom Generated ID: ${customId}`);

Error Handling

The SnowUUID class throws errors in specific scenarios, such as:

  • If the system clock moves backwards.
  • If the worker ID or datacenter ID exceeds their maximum limits.

Example Tests

You can also include tests using Jest to ensure that your implementation works correctly. Here is a sample test suite:

import { SnowUUID } from '../src/index';

describe('SnowUUID Generator', () => {
    let uuidGenerator: SnowUUID;

    beforeEach(() => {
        uuidGenerator = new SnowUUID();
    });

    test('generates a unique ID each time', () => {
        const id1 = uuidGenerator.nextId();
        const id2 = uuidGenerator.nextId();
        expect(id1).not.toBe(id2);  // IDs should be unique
    });

    // Additional tests can be added here...
});

Contributing

Contributions are welcome! Please feel free to submit a pull request or open an issue for any suggestions or improvements.

License

This project is licensed under the MIT License.

Author