0.4.1 • Published 1 year ago

@chronark/prefixed-id v0.4.1

Weekly downloads
-
License
ISC
Repository
-
Last release
1 year ago

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;
};
0.4.1

1 year ago

0.4.0

1 year ago

0.3.6

2 years ago

0.3.5

2 years ago

0.3.8

2 years ago

0.3.7

2 years ago

0.3.4

2 years ago

0.1.0

3 years ago

0.2.0

2 years ago

0.3.2

2 years ago

0.3.1

2 years ago

0.3.3

2 years ago

0.0.1

3 years ago