index.js 767 B

12345678910111213141516171819202122232425
  1. var assert = require('assert')
  2. var base62 = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'
  3. var base36 = 'abcdefghijklmnopqrstuvwxyz0123456789'
  4. var base10 = '0123456789'
  5. exports = module.exports = create(base62)
  6. exports.base62 = exports
  7. exports.base36 = create(base36)
  8. exports.base10 = create(base10)
  9. exports.create = create
  10. function create(chars) {
  11. assert(typeof chars === 'string', 'the list of characters must be a string!')
  12. var length = Buffer.byteLength(chars)
  13. return function rndm(len) {
  14. len = len || 10
  15. assert(typeof len === 'number' && len >= 0, 'the length of the random string must be a number!')
  16. var salt = ''
  17. for (var i = 0; i < len; i++) salt += chars[Math.floor(length * Math.random())]
  18. return salt
  19. }
  20. }