I've made this function for to be used while testing.
// ----- THE ACTUAL FUNCTION -------------------------
// Returns a string consisting of random alphabetic
// characters.
// @param { string } separator - Char which shall be
// used to separate the single chars.
// @param { number } min - Minimal length of the string.
// @param { number } max - Maximal length of the string.
// @param { string } inCase - Case of characters to use.
// - Use 'both' for getting a string consisting of
// lower- and upper-case character.
// - Use 'upper' for getting a string consisting of
// only upper-case characters.
// - Leave the parameter out, pass an empty string or
// any other string for getting a string consisting
// of only lower characters.
// @return { string }
// @throws { object } in case of error.
function getRandomString(separator, min, max, inCase) {
var getPartRandomString = function() {
return Math.random()
.toString(36)
.replace(/[^a-z]+/g, '');
};
var setReturnString = function() {
ret += getPartRandomString();
if (ret.length < min) {
setReturnString();
}
}
var ret = '';
if (min >= max) {
throw {
message: 'Minimum can not be greater or equal Maximum.'
}
}
min = min || 5;
max = max || 8;
separator = separator || '';
setReturnString();
ret = ret.split('');
if (inCase === 'both') {
ret = ret.map(function(currentChar) {
if (Math.random() >= 0.5) {
currentChar = currentChar.toUpperCase();
}
return currentChar;
});
}
ret = ret.join(separator);
if (inCase === 'upper') {
ret = ret.toUpperCase();
}
ret = ret.slice(0, ret.length <= max ? ret.length : max);
if (separator && ret.charAt(ret.length - 1) === separator) {
ret = ret.slice(0, ret.length - 1);
}
return ret;
}
// ----- THE ACTUAL FUNCTION ---- End ----------------------
function testGetRandomString(min, max, caseToTest, limit, regExp) {
var logMessage = function(message) {
console.log('%s%s => %s',
receivedResult,
new Array(max - receivedResult.length + 1)
.join(' '),
message
);
}
var receivedResult;
min = min || 6;
max = max || 12;
limit = limit || 10;
regExp = regExp || /[^a-z]/g;
for (let i = 0; i < limit; i++) {
receivedResult = getRandomString('', min, max, caseToTest);
if (receivedResult.length < min) {
logMessage('Less then min.');
} else if (receivedResult.length > max) {
logMessage('Larger or equal max.');
} else if (receivedResult.search(regExp) !== -1) {
logMessage('Unallowed chars.');
} else {
logMessage('Test passed.');
}
}
}
testGetRandomString(6, 12, '', 10);
console.log('\n');
testGetRandomString(6, 12, 'both', 10, /[^a-zA-Z]/g);
console.log('\n');
testGetRandomString(10, 18, 'upper', 10, /[^A-Z]/g);
console.log('\n');
console.log(getRandomString('-', 1, 2, 'both'));
console.log('\n');
console.log(getRandomString('-', 10, 20, 'both'));
It seem to work alright. But frankly: With all the parameter-checking and taking different possibilities into account it has become a lot of code.
If someone knows a possibility to write it more elegant then I would appreciate his or her answer. Any other hints welcomed too.
Should mention: I can't use ES 6-syntax because we got only Internet Explorer 11 here and there won't be change in any nearer future.