// Thrown when random bytes array is too short (< 16 bytes)const shortBytes = new Uint8Array(10)try { uuidv4({ random: shortBytes })} catch (error) { console.log(error.code) // => "UUID_RANDOM_BYTES_TOO_SHORT" console.log(error.message) // => "Random bytes length must be >= 16"}
// Thrown when random bytes array is too short (< 10 bytes)const shortBytes = new Uint8Array(5)try { ulid({ random: shortBytes })} catch (error) { console.log(error.code) // => "ULID_RANDOM_BYTES_TOO_SHORT" console.log(error.message) // => "Random bytes length must be >= 10 for ULID"}
// Thrown when length is not between 2 and 32try { cuid2({ length: 50 })} catch (error) { console.log(error.code) // => "CUID2_LENGTH_OUT_OF_RANGE" console.log(error.message) // => "CUID2 length must be between 2 and 32. Received: 50"}
// Thrown when size is not a non-negative integertry { nanoid(-5)} catch (error) { console.log(error.code) // => "NANOID_SIZE_INVALID" console.log(error.message) // => "Size must be a non-negative integer"}
// Thrown when random bytes array is too short (< 16 bytes)try { ksuid({ random: new Uint8Array(10) })} catch (error) { console.log(error.code) // => "KSUID_RANDOM_BYTES_TOO_SHORT" console.log(error.message) // => "Random bytes length must be >= 16 for KSUID"}
import { nanoid } from 'uniku/nanoid'function generateSafeNanoid(size: number, alphabet?: string): string { // Validate size if (!Number.isInteger(size) || size < 0) { throw new Error('Size must be a non-negative integer') } if (size > 2048) { throw new Error('Size must not exceed 2048') } // Validate alphabet if (alphabet !== undefined) { if (alphabet.length < 2) { throw new Error('Alphabet must contain at least 2 characters') } if (new Set(alphabet).size !== alphabet.length) { throw new Error('Alphabet contains duplicate characters') } } return nanoid({ size, alphabet })}