index.d.ts 2.2 KB

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