index.cjs 854 B

123456789101112131415161718192021222324252627282930
  1. // This alphabet uses `A-Za-z0-9_-` symbols. The genetic algorithm helped
  2. // optimize the gzip compression for this alphabet.
  3. let urlAlphabet =
  4. 'ModuleSymbhasOwnPr-0123456789ABCDEFGHNRVfgctiUvz_KqYTJkLxpZXIjQW'
  5. let customAlphabet = (alphabet, size) => {
  6. return () => {
  7. let id = ''
  8. // A compact alternative for `for (var i = 0; i < step; i++)`.
  9. let i = size
  10. while (i--) {
  11. // `| 0` is more compact and faster than `Math.floor()`.
  12. id += alphabet[(Math.random() * alphabet.length) | 0]
  13. }
  14. return id
  15. }
  16. }
  17. let nanoid = (size = 21) => {
  18. let id = ''
  19. // A compact alternative for `for (var i = 0; i < step; i++)`.
  20. let i = size
  21. while (i--) {
  22. // `| 0` is more compact and faster than `Math.floor()`.
  23. id += urlAlphabet[(Math.random() * 64) | 0]
  24. }
  25. return id
  26. }
  27. module.exports = { nanoid, customAlphabet }