/** * Creates a generator that produces categorical values. * @param optionCount: The multiple in which the optional index starts back over at zero. * @param offset: indicates the multiple of the index. */ export function createCategoryGenerator(optionCount = -1, offset?: number){ const i = 0, optionArray = []; let count = 0, oCount = 0, category ; return (index: number) => { let optionIndex; if (optionCount < 0){ category = `Category ${index} ${count}`; } else { if (offset === undefined || (count % offset) === 0){ optionIndex = oCount % optionCount; category = `Category ${index} ${optionIndex}`; oCount = oCount + 1; } } count = count + 1; return category; }; } /** * Creates a generator that produces datum objects based on provided generator arguments. * @numericalGenerators - The result of calling createRandomNumberGenerator(...), * createRandomIntegerGenerator(...) * @deprecated by createDatumMapGenerator */ export function createDatumGenerator(...numericalGenerators){ /** * @categoryGenerators - The result of calling createCategoryGenerator(...) */ return (...categoryGenerators) => { return () => { const datum = {}; let i = 0; for (const numericalGenerator of numericalGenerators) { datum[`value${i}`] = numericalGenerator(); i++; } i = 0; for (const categoryGenerator of categoryGenerators) { datum[`category${i}`] = categoryGenerator(i); i++; } return datum; }; }; } /** * Creates a generator that produces datum objects based on provided generatorMap. * A data object consists of attributes * @param generatorMap: object of generator functions. */ export function createDatumMapGenerator(generatorMap: any): any { return () => { const datum = {}; for (const generatorKey in generatorMap){ if (generatorMap.hasOwnProperty(generatorKey)){ datum[generatorKey] = generatorMap[generatorKey](); } } return datum; }; } /** * Generates a list of datum objects of dataLength consisting of attributes and values provided by the generatorMap. * @param dataLength: Length of data array to produce. * @param generatorMap: Map of value generators. */ export function generateMapData(dataLength, generatorMap){ const dataArray = [], datumGenerator = createDatumMapGenerator(generatorMap) ; for (let i = 0; i < dataLength; i++){ dataArray.push(datumGenerator()); } return dataArray; } /** * Creates a generator that produces random numbers that fall with the provided minmum to maximum range. * @param minmum * @param maximum */ export function createRandomNumberGenerator(minimum: number, maximum: number){ return () => { return Math.random() * (maximum - minimum) + minimum; }; } /** * Creates a generator that produces random integers that fall with the provided minimum to maximum range. * @param minmum * @param maximum */ export function createRandomIntegerGenerator(minimum: number, maximum: number){ const min = Math.ceil(minimum), max = Math.floor(maximum); return () => { return Math.floor(Math.random() * (max - min)) + min; }; } /** * Creates a generator that produces random characters selected based * on character codes that falls within the minCode and maxCode Unicode range. * @param minCode: lower bound of character unicode selection range * @param maxCode: upper bound of character unicode selection range */ export function createRandomCodeCharacterGenerator(minCode: number, maxCode: number){ const randomIntegerGenerator = createRandomIntegerGenerator(minCode, maxCode); return () => { const code = randomIntegerGenerator(); return String.fromCharCode(code); }; } /** * Creates a generator that produces random characters selected from domain of characters provided in the charStr. * Selection with replacement from the domain of characters. * @param charStr: string of characters to use as selection domain. */ export function createRandomCharacterGenerator(charStr: string){ const randomIntegerGenerator = createRandomIntegerGenerator(0, charStr.length - 1); return () => { const index = randomIntegerGenerator(); return charStr.charAt(index); }; } /** * Creates a generator that produces random lower alpha characters */ export function createRandomAlphaLowerCharacterGenerator(){ return createRandomCharacterGenerator("abcdefghijklmnopqrstuvwxyz"); } /** * Creates a generator that produces random upper alpha characters */ export function createRandomAlphaUpperCharacterGenerator(){ return createRandomCharacterGenerator("ABCDEFGHIJKLMNOPQRSTUVWXYZ"); } /** * Creates a generator that produces random number characters */ export function createRandomNumberCharacterGenerator(){ return createRandomCharacterGenerator("0123456789"); } /** * Creates a generator that produces values from a randomly selected generator in the generator list. * @param generatorList: List of generators */ export function createRandomSelectGenerator(generatorList: any[]){ const randomIntegerGenerator = createRandomIntegerGenerator(0, generatorList.length); return () => { const index = randomIntegerGenerator(); return generatorList[index](index); }; } /** * Creates a generator that produces random string, between minLength and maxLength in length. * @param minLength: minimum length of random string produced * @param maxLength: maximum length of random string produced * @param generatorList: List of generators that produce random characters. */ export function createRandomStringGenerator(minLength: number, maxLength: number, generatorList = []) { const randomIntegerGenerator = createRandomIntegerGenerator(minLength, maxLength), randomSelectGenerator = createRandomSelectGenerator(generatorList) ; if (generatorList.length <= 0){ generatorList.push(createRandomAlphaLowerCharacterGenerator()); } return () => { const strArray = [], randomLength = randomIntegerGenerator() ; for (let i = 0; i < randomLength; i++){ strArray.push(randomSelectGenerator()); } return strArray.join(""); }; }