1. const uuid = (n = 6) => {
    2. if (n < 2) {
    3. throw new RangeError('n is not less than 2');
    4. }
    5. const CHARS = 'abcdefghigklmnopqrstuvwxyz';
    6. const NUMS = '0123456789';
    7. const ALL = CHARS + NUMS;
    8. let repeatStr = '';
    9. for (let i = 0; i < n - 2; i++) {
    10. repeatStr += 'z';
    11. }
    12. return `xx${repeatStr}`.replace(/[xz]/g, (c) =>
    13. c === 'x' ? CHARS[(Math.random() * 26) | 0] : ALL[(Math.random() * 36) | 0],
    14. );
    15. };