提交学习笔记专用
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.

48 lines
1.5 KiB

3 weeks ago
3 weeks ago
3 weeks ago
3 weeks ago
3 weeks ago
3 weeks ago
3 weeks ago
  1. /**
  2. * By default, Nano ID uses hardware random bytes generation for security
  3. * and low collision probability. If you are not so concerned with security,
  4. * you can use it for environments without hardware random generators.
  5. *
  6. * ```js
  7. * import { nanoid } from 'nanoid/non-secure'
  8. * const id = nanoid() //=> "Uakgb_J5m9g-0JDMbcJqLJ"
  9. * ```
  10. *
  11. * @module
  12. */
  13. /**
  14. * Generate URL-friendly unique ID. This method uses the non-secure
  15. * predictable random generator with bigger collision probability.
  16. *
  17. * ```js
  18. * import { nanoid } from 'nanoid/non-secure'
  19. * model.id = nanoid() //=> "Uakgb_J5m9g-0JDMbcJqL"
  20. * ```
  21. *
  22. * @param size Size of the ID. The default size is 21.
  23. * @typeparam Type The ID type to replace `string` with some opaque type.
  24. * @returns A random string.
  25. */
  26. export function nanoid<Type extends string>(size?: number): Type
  27. /**
  28. * Generate a unique ID based on a custom alphabet.
  29. * This method uses the non-secure predictable random generator
  30. * with bigger collision probability.
  31. *
  32. * @param alphabet Alphabet used to generate the ID.
  33. * @param defaultSize Size of the ID. The default size is 21.
  34. * @typeparam Type The ID type to replace `string` with some opaque type.
  35. * @returns A random string generator.
  36. *
  37. * ```js
  38. * import { customAlphabet } from 'nanoid/non-secure'
  39. * const nanoid = customAlphabet('0123456789абвгдеё', 5)
  40. * model.id = nanoid() //=> "8ё56а"
  41. * ```
  42. */
  43. export function customAlphabet<Type extends string>(
  44. alphabet: string,
  45. defaultSize?: number
  46. ): (size?: number) => Type