src/string.util.js
function randomString(length, charSet = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789') {
    let str = '',
        isAllNumeric = false,
        isNegative = false,
        useCharSet = charSet;

    if (+length) {
        if (!isString(charSet)) {
            if (isNumber(charSet)) {
                if (+charSet) {
                    isAllNumeric = true;
                    isNegative = +charSet < 0;
                    useCharSet = `${Math.abs(+charSet)}`;
                } else {
                    useCharSet = ALPHANUMERIC_CHARS;
                }
            } else {
                useCharSet = ALPHANUMERIC_CHARS;
            }
        }
      
        const generateChar = function gc(len) {
            return Math.round(Math.random() * (len - 1));
        }.bind(null, useCharSet.length);
      
        str = Array(+length).fill().map((v, index) => {
            const newChar = generateChar();
            /* If we are generating a random number, make sure the first digit is not zero */
            if (!index && isAllNumeric && !newChar) {
                return useCharSet.charAt(newChar + 1);
            }

            return useCharSet.charAt(newChar);
        }).join('');
    }

    if (isAllNumeric) {
        return isNegative ? -+str : +str;
    }

    return str;
}
function randomString(length, charSet = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789') {
    let str = '',
        isAllNumeric = false,
        isNegative = false,
        useCharSet = charSet;

    if (+length) {
        if (!isString(charSet)) {
            if (isNumber(charSet)) {
                if (+charSet) {
                    isAllNumeric = true;
                    isNegative = +charSet < 0;
                    useCharSet = `${Math.abs(+charSet)}`;
                } else {
                    useCharSet = ALPHANUMERIC_CHARS;
                }
            } else {
                useCharSet = ALPHANUMERIC_CHARS;
            }
        }
      
        const generateChar = function gc(len) {
            return Math.round(Math.random() * (len - 1));
        }.bind(null, useCharSet.length);
      
        str = Array(+length).fill().map((v, index) => {
            const newChar = generateChar();
            /* If we are generating a random number, make sure the first digit is not zero */
            if (!index && isAllNumeric && !newChar) {
                return useCharSet.charAt(newChar + 1);
            }

            return useCharSet.charAt(newChar);
        }).join('');
    }

    if (isAllNumeric) {
        return isNegative ? -+str : +str;
    }

    return str;
}
function randomString ( length , charSet = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789' ) { let str = '' , isAllNumeric = false , isNegative = false , useCharSet = charSet ; if ( + length ) { if ( ! isString ( charSet ) ) { if ( isNumber ( charSet ) ) { if ( + charSet ) { isAllNumeric = true ; isNegative = + charSet < 0 ; useCharSet = ` ${ Math . abs ( + charSet ) } ` ; } else { useCharSet = ALPHANUMERIC_CHARS ; } } else { useCharSet = ALPHANUMERIC_CHARS ; } } const generateChar = function gc ( len ) { return Math . round ( Math . random ( ) * ( len - 1 ) ) ; } . bind ( null , useCharSet . length ) ; str = Array ( + length ) . fill ( ) . map ( ( v , index ) => { const newChar = generateChar ( ) ; /* If we are generating a random number, make sure the first digit is not zero */ if ( ! index && isAllNumeric && ! newChar ) { return useCharSet . charAt ( newChar + 1 ) ; } return useCharSet . charAt ( newChar ) ; } ) . join ( '' ) ; } if ( isAllNumeric ) { return isNegative ? - + str : + str ; } return str ; }
Generates a random string of text of a given length.
Will generate an alpha-numeric string unless you specify a different character set as the second argument
@method utilitarian.string.util#randomString
@param {number} length The length of the string to generate
@param {string|number} [charSet] optional set of characters to use when generating a random string
(defaults to full alpha-numeric set)
@returns {string|number} A randomly generated string or number
Generates a random string of text of a given length . Will generate an alpha - numeric string unless you specify a different character set as the second argument