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
The walk stopped after 200 commits: lines older than cdc3edc point at it.
Update size limitAndrey Sitnikbefore adoptioncdc3edc
line 1why, one step back1/**Move module docs to .d.tsAndrey Sitnikbefore adoptiond4fb377
lines 2–13why, one step back2 * A tiny, secure, URL-friendly, unique string ID generator for JavaScript3 * with hardware random generator.4 *5 * ```js6 * import { nanoid } from 'nanoid'7 * model.id = nanoid() //=> "V1StGXR8_Z5jdHi6B-myT"8 * ```9 *10 * @module11 */1213/**Update size limitAndrey Sitnikbefore adoptioncdc3edc
lines 14–24why, one step back14 * Generate secure URL-friendly unique ID.15 *16 * By default, the ID will have 21 symbols to have a collision probability17 * similar to UUID v4.18 *19 * ```js20 * import { nanoid } from 'nanoid'21 * model.id = nanoid() //=> "Uakgb_J5m9g-0JDMbcJqL"22 * ```23 *24 * @param size Size of the ID. The default size is 21.Add opaque type for non-secure generatorAndrey Sitnikbefore adoption1e24963
line 25why, one step back25 * @typeparam Type The ID type to replace `string` with some opaque type.Update size limitAndrey Sitnikbefore adoptioncdc3edc
lines 26–27why, one step back26 * @returns A random string.27 */Make functions generic so it can cast to opaque (branded) types (#521)Sasha Kossbefore adoptionea64dc7
line 28why, one step back28export function nanoid<Type extends string>(size?: number): TypeUpdate size limitAndrey Sitnikbefore adoptioncdc3edc
lines 29–37why, one step back2930/**31 * Generate secure unique ID with custom alphabet.32 *33 * Alphabet must contain 256 symbols or less. Otherwise, the generator34 * will not be secure.35 *36 * @param alphabet Alphabet used to generate the ID.37 * @param defaultSize Size of the ID. The default size is 21.Add opaque type for non-secure generatorAndrey Sitnikbefore adoption1e24963
line 38why, one step back38 * @typeparam Type The ID type to replace `string` with some opaque type.Update size limitAndrey Sitnikbefore adoptioncdc3edc
lines 39–41why, one step back39 * @returns A random string generator.40 *41 * ```jsFix incorrect import paths in JSDoc examples (#567)Mahmoud Hamdybefore adoptiona1873f7
line 42why, one step back42 * import { customAlphabet } from 'nanoid'Update size limitAndrey Sitnikbefore adoptioncdc3edc
lines 43–46why, one step back43 * const nanoid = customAlphabet('0123456789абвгдеё', 5)44 * nanoid() //=> "8ё56а"45 * ```46 */Make functions generic so it can cast to opaque (branded) types (#521)Sasha Kossbefore adoptionea64dc7
line 47why, one step back47export function customAlphabet<Type extends string>(Update size limitAndrey Sitnikbefore adoptioncdc3edc
lines 48–49why, one step back48 alphabet: string,49 defaultSize?: numberMake functions generic so it can cast to opaque (branded) types (#521)Sasha Kossbefore adoptionea64dc7
line 50why, one step back50): (size?: number) => TypeUpdate size limitAndrey Sitnikbefore adoptioncdc3edc
lines 51–58why, one step back5152/**53 * Generate unique ID with custom random generator and alphabet.54 *55 * Alphabet must contain 256 symbols or less. Otherwise, the generator56 * will not be secure.57 *58 * ```jsFix incorrect import paths in JSDoc examples (#567)Mahmoud Hamdybefore adoptiona1873f7
line 59why, one step back59 * import { customRandom } from 'nanoid'Update size limitAndrey Sitnikbefore adoptioncdc3edc
lines 60–61why, one step back60 *61 * const nanoid = customRandom('abcdef', 5, size => {docs: fix custom generator JSDoc examples (#610)Alexbefore adoptionbb68abc
line 62why, one step back62 * const random = new Uint8Array(size)Update size limitAndrey Sitnikbefore adoptioncdc3edc
line 63why, one step back63 * for (let i = 0; i < size; i++) {docs: fix custom generator JSDoc examples (#610)Alexbefore adoptionbb68abc
line 64why, one step back64 * random[i] = randomByte()Update size limitAndrey Sitnikbefore adoptioncdc3edc
lines 65–72why, one step back65 * }66 * return random67 * })68 *69 * nanoid() //=> "fbaef"70 * ```71 *72 * @param alphabet Alphabet used to generate a random string.Port amazing nope-id optimizations (#602)Andrey Sitnikbefore adoptionb1bb75f
line 73why, one step back73 * @param defaultSize Size of the random string.Update size limitAndrey Sitnikbefore adoptioncdc3edc
line 74why, one step back74 * @param random A random bytes generator.Add opaque type for non-secure generatorAndrey Sitnikbefore adoption1e24963
line 75why, one step back75 * @typeparam Type The ID type to replace `string` with some opaque type.Update size limitAndrey Sitnikbefore adoptioncdc3edc
lines 76–77why, one step back76 * @returns A random string generator.77 */Make functions generic so it can cast to opaque (branded) types (#521)Sasha Kossbefore adoptionea64dc7
line 78why, one step back78export function customRandom<Type extends string>(Update size limitAndrey Sitnikbefore adoptioncdc3edc
line 79why, one step back79 alphabet: string,Port amazing nope-id optimizations (#602)Andrey Sitnikbefore adoptionb1bb75f
line 80why, one step back80 defaultSize: number,Update size limitAndrey Sitnikbefore adoptioncdc3edc
line 81why, one step back81 random: (bytes: number) => Uint8ArrayChange customRandom signature to accept optional size (#551)Olivier Guimbalbefore adoption787a511
line 82why, one step back82): (size?: number) => TypeUpdate size limitAndrey Sitnikbefore adoptioncdc3edc
lines 83–87why, one step back8384/**85 * URL safe symbols.86 *87 * ```jsdocs: fix custom generator JSDoc examples (#610)Alexbefore adoptionbb68abc
line 88why, one step back88 * import { customAlphabet, urlAlphabet } from 'nanoid'Update size limitAndrey Sitnikbefore adoptioncdc3edc
lines 89–106why, one step back89 * const nanoid = customAlphabet(urlAlphabet, 10)90 * nanoid() //=> "Uakgb_J5m9"91 * ```92 */93export const urlAlphabet: string9495/**96 * Generate an array of random bytes collected from hardware noise.97 *98 * ```js99 * import { customRandom, random } from 'nanoid'100 * const nanoid = customRandom("abcdef", 5, random)101 * ```102 *103 * @param bytes Size of the array.104 * @returns An array of random bytes.105 */106export function random(bytes: number): Uint8Array