Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 27 additions & 13 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
2 changes: 2 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down