index.cjs 1.1 KB

12345678910111213141516171819202122232425262728293031323334
  1. // This alphabet uses `A-Za-z0-9_-` symbols.
  2. // The order of characters is optimized for better gzip and brotli compression.
  3. // References to the same file (works both for gzip and brotli):
  4. // `'use`, `andom`, and `rict'`
  5. // References to the brotli default dictionary:
  6. // `-26T`, `1983`, `40px`, `75px`, `bush`, `jack`, `mind`, `very`, and `wolf`
  7. let urlAlphabet =
  8. 'useandom-26T198340PX75pxJACKVERYMINDBUSHWOLF_GQZbfghjklqvwyzrict'
  9. let customAlphabet = (alphabet, defaultSize = 21) => {
  10. return (size = defaultSize) => {
  11. let id = ''
  12. // A compact alternative for `for (var i = 0; i < step; i++)`.
  13. let i = size | 0
  14. while (i--) {
  15. // `| 0` is more compact and faster than `Math.floor()`.
  16. id += alphabet[(Math.random() * alphabet.length) | 0]
  17. }
  18. return id
  19. }
  20. }
  21. let nanoid = (size = 21) => {
  22. let id = ''
  23. // A compact alternative for `for (var i = 0; i < step; i++)`.
  24. let i = size | 0
  25. while (i--) {
  26. // `| 0` is more compact and faster than `Math.floor()`.
  27. id += urlAlphabet[(Math.random() * 64) | 0]
  28. }
  29. return id
  30. }
  31. module.exports = { nanoid, customAlphabet }