提交学习笔记专用
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

106 lines
2.7 KiB

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