index.d.ts 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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. * Generate secure unique ID with custom alphabet.
  18. *
  19. * Alphabet must contain 256 symbols or less. Otherwise, the generator
  20. * will not be secure.
  21. *
  22. * @param alphabet Alphabet used to generate the ID.
  23. * @param size Size of the ID.
  24. * @returns A random string generator.
  25. *
  26. * ```js
  27. * const { customAlphabet } = require('nanoid')
  28. * const nanoid = customAlphabet('0123456789абвгдеё', 5)
  29. * nanoid() //=> "8ё56а"
  30. * ```
  31. */
  32. export function customAlphabet(alphabet: string, size: number): () => string
  33. /**
  34. * Generate unique ID with custom random generator and alphabet.
  35. *
  36. * Alphabet must contain 256 symbols or less. Otherwise, the generator
  37. * will not be secure.
  38. *
  39. * ```js
  40. * import { customRandom } from 'nanoid/format'
  41. *
  42. * const nanoid = customRandom('abcdef', 5, size => {
  43. * const random = []
  44. * for (let i = 0; i < size; i++) {
  45. * random.push(randomByte())
  46. * }
  47. * return random
  48. * })
  49. *
  50. * nanoid() //=> "fbaef"
  51. * ```
  52. *
  53. * @param alphabet Alphabet used to generate a random string.
  54. * @param size Size of the random string.
  55. * @param random A random bytes generator.
  56. * @returns A random string generator.
  57. */
  58. export function customRandom(
  59. alphabet: string,
  60. size: number,
  61. random: (bytes: number) => Uint8Array
  62. ): () => string
  63. /**
  64. * URL safe symbols.
  65. *
  66. * ```js
  67. * import { urlAlphabet } from 'nanoid'
  68. * const nanoid = customAlphabet(urlAlphabet, 10)
  69. * nanoid() //=> "Uakgb_J5m9"
  70. * ```
  71. */
  72. export const urlAlphabet: string
  73. /**
  74. * Generate an array of random bytes collected from hardware noise.
  75. *
  76. * ```js
  77. * import { customRandom, random } from 'nanoid'
  78. * const nanoid = customRandom("abcdef", 5, random)
  79. * ```
  80. *
  81. * @param bytes Size of the array.
  82. * @returns An array of random bytes.
  83. */
  84. export function random(bytes: number): Uint8Array