UNPKG

ssid

Version:

Generate secure and unique short IDs in JavaScript, tested for reliability and compatibility with browsers

53 lines (48 loc) 2.11 kB
// Default length for the generated ID export const DEFAULT_LENGTH: number; // Default alphabet (set of characters) used for generating the ID export const DEFAULT_ALPHABET: string; /** * Generates a unique ID with a specified length and custom alphabet. * * @param length (optional) - The length of the generated ID. Defaults to `DEFAULT_LENGTH` (8). * @param customAlphabet (optional) - A custom set of characters to use for generating the ID. Defaults to `DEFAULT_ALPHABET`. * @returns The generated ID as a string. * * @throws Error - Throws an error if length is less than 4. */ export function ssid( length?: number, customAlphabet?: string ): string; /** * Generates a unique ID with optional prefix and suffix, and a specified length and custom alphabet. * * @param length (optional) - The length of the generated ID. Defaults to `DEFAULT_LENGTH` (8). * @param prefix (optional) - A string to prepend to the generated ID. * @param suffix (optional) - A string to append to the generated ID. * @param customAlphabet (optional) - A custom set of characters to use for generating the ID. Defaults to `DEFAULT_ALPHABET`. * @returns The generated ID with affixes as a string. * * @throws Error - Throws an error if length is less than 4. * @throws Error - Throws an error if neither `prefix` nor `suffix` is provided. */ export function ssidWithAffixes( length?: number, prefix?: string, suffix?: string, customAlphabet?: string ): string; /** * Generates a unique ID with timestamp and a short id with specified length and custom alphabet. * * @param length (optional) - The length of the generated ID. Defaults to `DEFAULT_LENGTH` (8). * @param customAlphabet (optional) - A custom set of characters to use for generating the ID. Defaults to `DEFAULT_ALPHABET`. * @returns The generated ID with timestamp and a short id as a string. * * @throws Error - Throws an error if length is less than 4. */ export function ssidWithTimestamp( length?: number, customAlphabet?: string ): string;