random-object-generator
Version:
Generate random values for javascript objects
170 lines (153 loc) • 4.97 kB
JavaScript
// Random generators
// Generate random string
// prefix: Define prefix for string. "" for empty
// maxLength: maximum number of characters
// useSpecialChars: use special characters
function randomString(prefix, maxLength, useSpecialChars) {
var text ="";
var possible="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-";
if(useSpecialChars) {
possible += "+() $%^,.?=";
}
var length = Math.floor((Math.random() * maxLength) + 1);
for(var i = 0; i < length; ++i) {
text += possible.charAt(Math.floor(Math.random() * possible.length));
}
if(prefix == "")
return text;
return prefix + "-" + text;
}
// generates a number in a range
// minValue: minimum value, inclusive
// maxValue: maximum value, exclusive
// isInt: whether returned value is an integer
function randomNumberInRange(minValue, maxValue, isInt) {
// If wrong use
if(maxValue < minValue){
var tmp = minValue;
minValue = maxValue;
maxValue = minValue;
}
var diff = maxValue - minValue;
var random = Math.floor(Math.random() * diff); // Generate value between 0 and diff;
if(isInt)
return parseInt((minValue + random));
return (minValue + random);
}
// generate random number
// prefix: prefix for number. easy for generating id's (employee-123,..). if prefix is not empty, this function will return a string. use "" as prefix to get a number
// maxValue: maximum value for the random number
// isInt: whether returned value is an integer
function randomNumber(prefix, maxValue, isInt) {
var number = Math.random() * maxValue;
if(isInt)
number = parseInt(number);
if(prefix == "")
return parseFloat(number.toFixed(2));
return prefix + "-" + number;
}
// generate random bool
function randomBool() {
return Math.random() > 0.5;
}
// generate random date for LocalDateTime java class
function randomDate() {
var date = [];
date.push(randomNumberInRange(2000, 2016, true)); // Year
date.push(randomNumberInRange(1, 11, true)); // Month
date.push(randomNumberInRange(1, 31, true)); // Day
date.push(randomNumberInRange(0, 24, true)); // Hour
date.push(randomNumberInRange(0, 60, true)); // Minute
date.push(randomNumberInRange(0, 60, true)); // Seconds
date.push(randomNumberInRange(10000, 10000000, true)); // Nanoseconds
return date;
}
// generate random period for Period java class
function randomPeriod() {
var period = "";
var years = randomNumberInRange(0, 5, true);
var months = randomNumberInRange(0, 12, true);
var days = randomNumberInRange(0, 31, true);
period += "P";
if(years > 0)
period += years + "Y";
if(months > 0)
period += months + "M";
if(days > 0)
period += days + "D";
return period;
}
// generate random array for the given item.
// item: any javascript object
function randomArray(item) {
var type = item[0];
// Generate between 1 and 5 items
var count = Math.floor((Math.random() * 3) + 1);
var array = [];
if(typeof(type) === "object") {
for(var i = 0; i < count; ++i) {
array.push(randomObject(type));
}
}
if(typeof(type) === "string") {
for(var i = 0; i < count; ++i) {
array.push(randomValue(type));
}
}
return array
}
// generate a random value for a property
// property: type of the random value to be generated
// name: name of the property for which a random value is generated (used for generating id's)
function randomValue(property, name) {
var value = null;
switch(property.toLowerCase()) {
case("id"): value = randomNumber(name, 9999, true); break;
case("string"): value = randomString("", 15, true); break;
case("float"): value = randomNumber("", 9999, false); break;
case("int"): value = randomNumber("", 999, true); break;
case("date"): value = randomDate(); break;
case("period"): value = randomPeriod(); break;
case("bool"): value = randomBool(); break;
default: console.log("Error: Cannot generate random value for type \'" + property + "\' on property \'" + name + "\'.");
}
return value;
}
// Recursively generate test objects
// Supports the following types:
// 'id'
// 'string'
// 'float'
// 'int'
// 'date'
// 'period'
// 'boolean'
// To get an array, simply set the property to an array which contains the typename as the first elementFromPoint
// For example: product = ["int"] will generate an array of ints
function randomObject (obj) {
var newObject = new Object();
if(typeof(obj) === "object") {
for(var k in obj) {
//console.log("typeof " + k + ": " + typeof(obj[k]));
if(typeof obj[k] === "object" ) {
if(Array.isArray(obj[k]))
newObject[k] = randomArray(obj[k]);
else
newObject[k] = randomObject(obj[k]);
//console.log(newObject[k]);
continue;
}
if (typeof(obj[k]) === "string") {
newObject[k] = randomValue(obj[k], k);
continue;
}
console.log("Error: Cannot generate random value for type \'" + obj[k] + "\' on property \'" + k + "\'.");
//delete obj[k];
}
return newObject;
}
}
module.exports = {
// Generate an object
randomObject
}