UNPKG

2.2 kBTypeScriptView Raw
1/**
2 * Generate secure URL-friendly unique ID.
3 *
4 * By default, the ID will have 21 symbols to have a collision probability
5 * similar to UUID v4.
6 *
7 * ```js
8 * import { nanoid } from 'nanoid'
9 * model.id = nanoid() //=> "Uakgb_J5m9g-0JDMbcJqL"
10 * ```
11 *
12 * @param size Size of the ID. The default size is 21.
13 * @returns A random string.
14 */
15export function nanoid (size?: number): string
16
17/**
18 * Generate secure unique ID with custom alphabet.
19 *
20 * Alphabet must contain 256 symbols or less. Otherwise, the generator
21 * will not be secure.
22 *
23 * @param alphabet Alphabet used to generate the ID.
24 * @param size Size of the ID.
25 * @returns A random string generator.
26 *
27 * ```js
28 * const { customAlphabet } = require('nanoid')
29 * const nanoid = customAlphabet('0123456789абвгдеё', 5)
30 * nanoid() //=> "8ё56а"
31 * ```
32 */
33export function customAlphabet (alphabet: string, size: number): () => string
34
35/**
36 * Generate unique ID with custom random generator and alphabet.
37 *
38 * Alphabet must contain 256 symbols or less. Otherwise, the generator
39 * will not be secure.
40 *
41 * ```js
42 * import { customRandom } from 'nanoid/format'
43 *
44 * const nanoid = customRandom('abcdef', 5, size => {
45 * const random = []
46 * for (let i = 0; i < size; i++) {
47 * random.push(randomByte())
48 * }
49 * return random
50 * })
51 *
52 * nanoid() //=> "fbaef"
53 * ```
54 *
55 * @param alphabet Alphabet used to generate a random string.
56 * @param size Size of the random string.
57 * @param random A random bytes generator.
58 * @returns A random string generator.
59 */
60export function customRandom (
61 alphabet: string,
62 size: number,
63 random: (bytes: number) => Uint8Array
64): () => string
65
66/**
67 * URL safe symbols.
68 *
69 * ```js
70 * import { urlAlphabet } from 'nanoid'
71 * const nanoid = customRandom(urlAlphabet, 10)
72 * nanoid() //=> "Uakgb_J5m9"
73 * ```
74 */
75export const urlAlphabet: string
76
77/**
78 * Generate an array of random bytes collected from hardware noise.
79 *
80 * ```js
81 * import { customRandom, random } from 'nanoid'
82 * const nanoid = customRandom("abcdef", 5, random)
83 * ```
84 *
85 * @param bytes Size of the array.
86 * @returns An array of random bytes.
87 */
88export function random (bytes: number): Uint8Array
89
\No newline at end of file