random-input-generator
v1.0.0
Published
Random JavaScript value generator for testing or seed data.
Downloads
6
Maintainers
Readme
Introduction
random-input-generator is a simple, easily-configurable module that generates common JavaScript values for quick and easy seed or testing data.
Issues
If you happen to find any bugs or would like to recommend suggestions for future updates, please feel free to do so in the github issue tracker.
Usage
To install the module, you can run npm install random-input-generator
from your command line or you can add "random-input-generator" to your package.json
and run npm install
.
Documentation
For your generation pleasure, this module provides methods for generating randomized strings, booleans, numbers, objects, and arrays. Primitive-generating methods accept multiple arguments, whereas objects accept configuration objects.
To get started, simply require in the module:
var randomInput = require('random-input-generator');
If you're using ES6/ES2015, feel free to use destructuring syntax:
const { generateNumber, generateString, generateBoolean, generateObject, generateArray, defaultGenerator } = require('random-input-generator');
generateNumber
Parameters
min
number? Optional number representing the minimum (inclusive) randomly generated number. (optional, default0
)max
number? Optional number representing the maximum (inclusive) randomly generated number. Must be greater thanmin
. (optional, default10000
)rounded
boolean? Optional true/false determining whether to return an integer or a number with a decimal value. (optional, defaulttrue
)
Examples
var randomInput = require('random-input-generator');
var generateNumber = randomInput.generateNumber;
//with default parameters, method will return integer between 0 and 10000
generateNumber(); //1538
//can return a number within a specific range, or decimal value
generateNumber(0,5,false); //4.633396897573343
generateString
Parameters
minLength
number? Optional number representing the minimum (inclusive) length of randomly generated string. (optional, default4
)maxLength
number? Optional number representing the maximum (inclusive) length of a randomly generated string. (optional, default12
)nonLetters
boolean? Optional true/false determining whether to include non-letter characters between Unicode values 32-127. (optional, defaulttrue
)casing
string? Optional string, either"upper"
or"lower"
, provided if either uppercase or lowercase string is preferred. (optional)
Examples
var randomInput = require('random-input-generator');
var generateString = randomInput.generateString;
//with default parameters, method will return string between 4-12 characters with non-letters
generateString(); //'XnB%tudFH>'
//can return a string within a specific length range
generateString(1,4,false); //'"SX'
//can return a string with only letters
generateString(6,10,false); //'SHqIHoF'
//can enforce casing
generateString(4,8,false,"upper"); //'CTEQTUE'
generateBoolean
Parameters
weightPercentage
number? Optional decimal value between0
and1
representing a weight towards either a return value offalse
ortrue
. (optional, default.5
)
Examples
var randomInput = require('random-input-generator');
var generateBoolean = randomInput.generateBoolean;
//with default parameters, theoretically equally likely to return true or false
generateBoolean(); //true
//with weight of .3, theoretically <30% likely to return true
generateBoolean(.3); //false
For non-primitive values, each generator function accepts a configuration object as its sole argument. While each specification is optional, if provided, each must conform to a specific type.
generateObject
Parameters
options
Object? configuration Objectoptions.keyValPairs
number? A specific number of key-value pairs of which the object will be comprised. (optional)options.optionalSkeleton
(Array<string>|Object)? An object on which to generate new Object. If Array is provided, each element will be used as a key in the generated Object and array's length will set the total number of key-value pairs in the generated Object. If Object is provided, each key-value pair will be used to extend an empty object, with any additional key-value pairs added according tooptions.keyValPairs
orminKeyValPairs, maxKeyValPairs
. (optional)options.valPreference
Array<string>? An array containing desired string value types for the generated Object. All strings invalPreference
array must conform to one of the following value types["string", "number", "boolean", "object", "array"]
. Strings contained invalPreference
will be chosen at random to generate values. Duplicate types are permitted and will effectively weight random selection towards one of these value types. (optional, default[]
)options.minKeyValPairs
number? Number representing the minimum (inclusive) possible number of key-value pairs populated in generated Object. Ifoptions.optionalSkeleton
is provided,minKeyValPairs
must be greater than or equal to the length of skeleton array or number of key-value pairs in skeleton Object. (optional, default2
)options.maxKeyValPairs
number? Number representing the maximum (inclusive) possible number of key-value pairs populated in generated Object. (optional, default6
)options.maxDepth
number? As randomly generating objects can potentially exceed the maximum call stack, amaxDepth
option is provided to prevent object spelunking.maxDepth
will dictate the depth of the deepest non-object value, at which point the generator will only be allowed to produce primitive values for key-value pairs. (optional, default3
)
Examples
var randomInput = require('random-input-generator');
var generateObject = randomInput.generateObject;
//with default parameters, will return object with 2-5 randomized key-value pairs
generateObject();
/* {
kzypax: true,
jqbf: { xjjwy: '0>>+1 i5F', wncre: ' o"HYUp>`}' },
lllhs: [ { hmdrx: false, cyflbq: '3=HQ3' }, 2398, 1877 ],
rcauk: true
} */
//with optionalSkeleton array and valPreference
generateObject({optionalSkeleton: ["dogs", "cats"], valPreference: ["number"]});
/* {
dogs: 6816,
cats: 7775
} */
//with optionalSkeleton object and keyValuePairs
var generateString = randomInput.generateString;
var userConfig = {username: generateString(8,20,false,"lower"), password: generateString(15,20,true)}
generateObject({keyValPairs: 4, optionalSkeleton: userConfig});
/* {
username: 'igsxcjotuqcqenebp',
password: 'Iq}H^H?}BS[JB$rdjiAj',
zfzrwv: 3609,
gptci: { crcebn: [ 'Tlg>o:uW' ], ayly: 4987 }
} */
generateArray
Parameters
options
Object? configuration Object (optional)options.setLength
number? Optional number that will set a mandatory length for generated Array. (optional)options.minLength
number? Optional number representing the minimum (inclusive) length for a generated Array of random length. (optional, default0
)options.maxLength
number? Optional number representing the maximum (inclusive) length for a generated Array of random length. (optional, default5
)options.valTypes
Array<string>? An array containing desired string value types for the generated Array. All strings invalTypes
array must conform to one of the following value types["string", "number", "boolean", "object", "array"]
. (optional, default[]
)options.templateArray
Array<string>? An optional base array to whichgenerateArray
will add values according to eitheroptions.setLength
oroptions.minLength, options.maxLength
options.valueGenerator
(Function|truthyValue)? Function or value to be used to populate the generated array. If an Array of identical truthy values is desired,options.valueGenerator
should be assigned a truthyValue. If an Array of randomized values or identical falsey values is desired, aoptions.valueGenerator
should be assigned a callback that returns a randomized value. Will take precedence overoptions.valTypes
array. (optional)
Examples
var randomInput = require('random-input-generator');
var generateArray = randomInput.generateArray;
//with setLength and valueGenerator as
generateArray({setLength: 2, valueGenerator: true});
// [ true, true ]
//with minLength, maxLength, templateArray, valTypes
generateArray({minLength: 4, maxLength: 6, valTypes: ['number'], templateArray:['r', 2, 'd']});
//[ 'r', 2, 'd', 2516, 8386, 7019 ]
//with valueGenerator as a function
var generateString = randomInput.generateString;
var contactGenerator = function() {
return {
firstName: generateString(1, 1, false, "upper") + generateString(5, 8,false, "lower"),
lastName: generateString(1, 1, false, "upper") + generateString(4, 12,false, "lower")
}
};
generateArray({setLength: 3, valueGenerator: contactGenerator});
/* [ { firstName: 'Jvocobshd', lastName: 'Fhigzrfyio' },
{ firstName: 'Kapplphvx', lastName: 'Xmvamyoshcm' },
{ firstName: 'Hzmgbllq', lastName: 'Qyirmdecur' } ] */
License
Licensed under the MIT license.