Skip to content

Latest commit

 

History

History
47 lines (32 loc) · 1.49 KB

File metadata and controls

47 lines (32 loc) · 1.49 KB
description Modern, secure alternatives to the shortid package for generating URL‑friendly unique IDs

Replacements for shortid

nanoid

nanoid is a tiny, secure, URL‑friendly, unique string ID generator. It’s also faster than shortid.

:::info Good to know before migration

  • shortid.isValid(id): there’s no direct equivalent. Validate with a regex that matches your chosen alphabet and length, e.g. /^[A-Za-z0-9_-]{21}$/.

  • shortid.seed()/shortid.worker(): not needed and not provided by nanoid (it uses a secure random source). Avoid seeded/deterministic IDs for security.

:::

Basic migration

import shortid from 'shortid' // [!code --]
import { nanoid } from 'nanoid' // [!code ++]

const id = shortid.generate() // [!code --]
const id = nanoid() // [!code ++] => "V1StGXR8_Z5jdHi6B-myT"

Control length

// shortid produced ~7-14 chars; with nanoid you pick the size explicitly:
nanoid(10) // e.g., "NG3oYbq9qE"

Custom alphabet (replacement for shortid.characters)

shortid.characters('0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ$@') // [!code --]
import { customAlphabet } from 'nanoid' // [!code ++]

const alphabet = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ$@' // [!code ++]
const makeId = customAlphabet(alphabet, 12) // [!code ++]
const id = makeId() // [!code ++]