GithubHelp home page GithubHelp logo

prefixed-id's Introduction

@chronark/prefixed-id

pre_JUbF9zRGz9hrFXUyNJLXcowD9GsqCD

A minimal library to generate Stripe inspired predixed ids for your application. Prefixed ids look like this: pre_JUbF9zRGz9hrFXUyNJLXcowD9GsqCD. They can be very useful if you have different entities and want to quickly identify them.

Generated ids rely on the provided crypto implementation. If you use either window.crypto or crypto from "node:crypto", the ids will be cryptographically secure.

Works in

  • Nodejs
  • Cloudflare Workers
  • Vercel Edge

Install

npm i @chronark/prefixed-id

Usage

Nodejs

import nodeCrypto from "node:crypto"

const idGenerator = new IdGenerator({
    prefixes: {
        "user": "u",
    },
    crypto: nodeCrypto
})

console.log(idGenerator.id("user"))
// u_PtbBA7NGcYYDpae6ULWujk

WebCrypto

  • Cloudflare Workers
  • Vercel Edge
const idGenerator = new IdGenerator({
    prefixes: {
        "user": "u",
    },
    crypto: crypto // will be globally defined
})

console.log(idGenerator.id("user"))
// u_PtbBA7NGcYYDpae6ULWujk

Options

You may pass these options to the constructor to customize the id generation.

{
	/**
	 *
	 * @default "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz"
	 */
	alphabet?: string;

	/**
	 * Either window.crypto or crypto from node
	 *
	 * @default window?.crypto
	 */
	crypto?: Crypto;

	/**
	 * Byte size of the generated id
	 */
	size?: number;
};

prefixed-id's People

Contributors

chronark avatar

Stargazers

Iñigo Taibo avatar Niels Kootstra avatar  avatar  avatar JZinkl avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.