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 | */
|
---|
15 | export 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 defaultSize Size of the ID. The default size is 21.
|
---|
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 | */
|
---|
33 | export function customAlphabet(
|
---|
34 | alphabet: string,
|
---|
35 | defaultSize?: number
|
---|
36 | ): (size?: number) => string
|
---|
37 |
|
---|
38 | /**
|
---|
39 | * Generate unique ID with custom random generator and alphabet.
|
---|
40 | *
|
---|
41 | * Alphabet must contain 256 symbols or less. Otherwise, the generator
|
---|
42 | * will not be secure.
|
---|
43 | *
|
---|
44 | * ```js
|
---|
45 | * import { customRandom } from 'nanoid/format'
|
---|
46 | *
|
---|
47 | * const nanoid = customRandom('abcdef', 5, size => {
|
---|
48 | * const random = []
|
---|
49 | * for (let i = 0; i < size; i++) {
|
---|
50 | * random.push(randomByte())
|
---|
51 | * }
|
---|
52 | * return random
|
---|
53 | * })
|
---|
54 | *
|
---|
55 | * nanoid() //=> "fbaef"
|
---|
56 | * ```
|
---|
57 | *
|
---|
58 | * @param alphabet Alphabet used to generate a random string.
|
---|
59 | * @param size Size of the random string.
|
---|
60 | * @param random A random bytes generator.
|
---|
61 | * @returns A random string generator.
|
---|
62 | */
|
---|
63 | export function customRandom(
|
---|
64 | alphabet: string,
|
---|
65 | size: number,
|
---|
66 | random: (bytes: number) => Uint8Array
|
---|
67 | ): () => string
|
---|
68 |
|
---|
69 | /**
|
---|
70 | * URL safe symbols.
|
---|
71 | *
|
---|
72 | * ```js
|
---|
73 | * import { urlAlphabet } from 'nanoid'
|
---|
74 | * const nanoid = customAlphabet(urlAlphabet, 10)
|
---|
75 | * nanoid() //=> "Uakgb_J5m9"
|
---|
76 | * ```
|
---|
77 | */
|
---|
78 | export const urlAlphabet: string
|
---|
79 |
|
---|
80 | /**
|
---|
81 | * Generate an array of random bytes collected from hardware noise.
|
---|
82 | *
|
---|
83 | * ```js
|
---|
84 | * import { customRandom, random } from 'nanoid'
|
---|
85 | * const nanoid = customRandom("abcdef", 5, random)
|
---|
86 | * ```
|
---|
87 | *
|
---|
88 | * @param bytes Size of the array.
|
---|
89 | * @returns An array of random bytes.
|
---|
90 | */
|
---|
91 | export function random(bytes: number): Uint8Array
|
---|