randomStr.js 550 B

1234567891011121314
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.randomStr = void 0;
  4. // Default alphabet allows "-" hyphens, because UUIDs have them.
  5. const defaultAlphabet = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-';
  6. function randomStr(length, alphabet = defaultAlphabet) {
  7. let str = '';
  8. const alphabetLength = alphabet.length;
  9. for (let i = 0; i < length; i++) {
  10. str += alphabet.charAt(Math.floor(Math.random() * alphabetLength));
  11. }
  12. return str;
  13. }
  14. exports.randomStr = randomStr;