diff --git a/index.js b/index.js index b06ac2b..6c2d483 100644 --- a/index.js +++ b/index.js @@ -22,35 +22,49 @@ assert(tokenChars.length < 256); } - function buildGenerator(options) { - assert(!options || typeof(options) == 'object'); - options = options || {}; - options.chars = options.chars || defaults.chars; - options.source = options.source || defaults.source; - - // Allowed characters - switch( options.chars ) { + function getChars (chars) { + var result; + switch( chars ) { case 'default': - options.chars = alphaNumeric; + result = alphaNumeric; break; case 'a-z': case 'alpha': - options.chars = alphaLower; + result = alphaLower; break; case 'A-Z': case 'ALPHA': - options.chars = alphaUpper; + result = alphaUpper; break; case '0-9': case 'numeric': - options.chars = numeric; + result = numeric; break; case 'base32': - options.chars = alphaUpper + "234567"; + result = alphaUpper + "234567"; break; default: // use the characters as is } + return result; + } + + function buildGenerator(options) { + assert(!options || typeof(options) == 'object'); + options = options || {}; + options.chars = options.chars || defaults.chars; + options.source = options.source || defaults.source; + + // Allowed characters + if (Array.isArray(options.chars)) { + var charsSet = ''; + options.chars.forEach(function(item) { + charsSet += getChars(item); + }); + options.chars = charsSet; + } else { + options.chars = getChars(options.chars); + } validateTokenChars(options.chars); // Source of randomness: diff --git a/test/test.js b/test/test.js index a621710..c0c14d7 100644 --- a/test/test.js +++ b/test/test.js @@ -49,6 +49,8 @@ var tokenCharsToTest = [ {chars: 'alpha', regex: /^[a-z]{16}$/ }, {chars: 'ALPHA', regex: /^[A-Z]{16}$/ }, {chars: 'numeric', regex: /^[0-9]{16}$/ }, + {chars: ['ALPHA', 'numeric'], regex: /^[a-z0-9]{16}$/}, + {chars: ['alpha', 'ALPHA', 'numeric'], regex: /^[a-zA-Z0-9]{16}$/}, ]; _.each(randSourceToTest, function(randSourceTest) {