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 🙏

© 2025 – Pkg Stats / Ryan Hefner

youid

v1.0.5

Published

A lightweight utility to generate unique identifiers (UIDs) from strings or randomly.

Downloads

23

Readme

youid

youid is a lightweight JavaScript function that generates unique identifiers (UIDs) from strings. It can also generate random UIDs if no string input is provided. youid is flexible, allowing you to specify the length of the UID.

Installation

To install youid as an npm package:

npm install youid

Usage

youid provides a simple API to generate UIDs from strings or create random UIDs. The function takes two optional parameters:

  • str (optional): The input string from which the UID will be generated.
  • length (optional): The desired length of the UID.

Importing the Function

If using a module bundler:

import youid from 'youid';

Generating a UID

1. UID from a String

To generate a UID based on a string:

const uid = youid('HelloWorld');
console.log(uid); // Example output: "j1m3q4v9"

2. Specifying the Length

You can control the length of the generated UID:

const uid = youid('HelloWorld', 10);
console.log(uid); // Example output: "j1m3q4v900"

3. Generating a Random UID

If no string is provided, youid generates a random UID:

const randomUID = youid();
console.log(randomUID); // Example output: "x5y7z1w8a2"

Function Signature

const youid = (str?: string, length?: number) => string;

Parameters

  1. str (optional): A string input to generate a hash-based UID.

    • If omitted, a random UID generator function is returned.
  2. length (optional): The desired length of the UID.

    • Defaults to the full length of the generated hash or random string.

Return Value

  • If str is provided: A string UID based on the input string.
  • If str is omitted: A function that generates random UIDs, optionally constrained by length.

Examples

Basic Examples

Hash-Based UID:

const uid = youid('MyString');
console.log(uid); // Example output: "k9l2m5"

Length-Controlled UID:

const uid = youid('AnotherString', 12);
console.log(uid); // Example output: "k9l2m5n0pqr8"

Random UID:

const randomUID = youid(null, 8);
console.log(randomUID); // Example output: "abc123xy"

Real-World Usage

Generating IDs for HTML Elements:

const elementID = youid('button-submit', 10);
document.getElementById('my-element').id = elementID;
console.log(elementID); // Example output: "b8t9w7z6x1"

Random UIDs for Testing:

const randomIDGenerate = youid();
console.log(randomIDGenerate); // Example output: "a9b3c5d7"

How It Works

  1. If a string is provided, the function computes a hash using bitwise operations and converts it to a base-36 representation.
  2. If length is specified, the UID is padded or truncated to match the desired length.
  3. If no string is provided, the function returns another function that generates random UIDs using Math.random().

License

youid is open-source software licensed under the MIT License.

Contributions

Contributions are welcome! Please open an issue or submit a pull request on GitHub.


For any issues or inquiries, please contact the maintainer.