index.d.ts
This file
Path
index.d.ts
106 lines · 2.8 KiB · TypeScript
Last changed by
docs: fix custom generator JSDoc examples (#610)Alexbefore adoptionbb68abc
History of this fileTake it
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107/** * A tiny, secure, URL-friendly, unique string ID generator for JavaScript * with hardware random generator. * * ```js * import { nanoid } from 'nanoid' * model.id = nanoid() //=> "V1StGXR8_Z5jdHi6B-myT" * ``` * * @module */
/** * Generate secure URL-friendly unique ID. * * By default, the ID will have 21 symbols to have a collision probability * similar to UUID v4. * * ```js * import { nanoid } from 'nanoid' * model.id = nanoid() //=> "Uakgb_J5m9g-0JDMbcJqL" * ``` * * @param size Size of the ID. The default size is 21. * @typeparam Type The ID type to replace `string` with some opaque type. * @returns A random string. */export function nanoid<Type extends string>(size?: number): Type
/** * Generate secure unique ID with custom alphabet. * * Alphabet must contain 256 symbols or less. Otherwise, the generator * will not be secure. * * @param alphabet Alphabet used to generate the ID. * @param defaultSize Size of the ID. The default size is 21. * @typeparam Type The ID type to replace `string` with some opaque type. * @returns A random string generator. * * ```js * import { customAlphabet } from 'nanoid' * const nanoid = customAlphabet('0123456789абвгдеё', 5) * nanoid() //=> "8ё56а" * ``` */export function customAlphabet<Type extends string>( alphabet: string, defaultSize?: number): (size?: number) => Type
/** * Generate unique ID with custom random generator and alphabet. * * Alphabet must contain 256 symbols or less. Otherwise, the generator * will not be secure. * * ```js * import { customRandom } from 'nanoid' * * const nanoid = customRandom('abcdef', 5, size => { * const random = new Uint8Array(size) * for (let i = 0; i < size; i++) { * random[i] = randomByte() * } * return random * }) * * nanoid() //=> "fbaef" * ``` * * @param alphabet Alphabet used to generate a random string. * @param defaultSize Size of the random string. * @param random A random bytes generator. * @typeparam Type The ID type to replace `string` with some opaque type. * @returns A random string generator. */export function customRandom<Type extends string>( alphabet: string, defaultSize: number, random: (bytes: number) => Uint8Array): (size?: number) => Type
/** * URL safe symbols. * * ```js * import { customAlphabet, urlAlphabet } from 'nanoid' * const nanoid = customAlphabet(urlAlphabet, 10) * nanoid() //=> "Uakgb_J5m9" * ``` */export const urlAlphabet: string
/** * Generate an array of random bytes collected from hardware noise. * * ```js * import { customRandom, random } from 'nanoid' * const nanoid = customRandom("abcdef", 5, random) * ``` * * @param bytes Size of the array. * @returns An array of random bytes. */export function random(bytes: number): Uint8Array