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

29 lines
973 B

3 weeks ago
3 weeks ago
3 weeks ago
  1. /* @ts-self-types="./index.d.ts" */
  2. import { urlAlphabet as scopedUrlAlphabet } from './url-alphabet/index.js'
  3. export { urlAlphabet } from './url-alphabet/index.js'
  4. export let random = bytes => crypto.getRandomValues(new Uint8Array(bytes))
  5. export let customRandom = (alphabet, defaultSize, getRandom) => {
  6. let mask = (2 << Math.log2(alphabet.length - 1)) - 1
  7. let step = -~((1.6 * mask * defaultSize) / alphabet.length)
  8. return (size = defaultSize) => {
  9. let id = ''
  10. while (true) {
  11. let bytes = getRandom(step)
  12. let j = step | 0
  13. while (j--) {
  14. id += alphabet[bytes[j] & mask] || ''
  15. if (id.length >= size) return id
  16. }
  17. }
  18. }
  19. }
  20. export let customAlphabet = (alphabet, size = 21) =>
  21. customRandom(alphabet, size | 0, random)
  22. export let nanoid = (size = 21) => {
  23. let id = ''
  24. let bytes = crypto.getRandomValues(new Uint8Array((size |= 0)))
  25. while (size--) {
  26. id += scopedUrlAlphabet[bytes[size] & 63]
  27. }
  28. return id
  29. }