Created
January 25, 2019 21:06
-
-
Save erkobridee/ac14ce7f52f66a05747511adf33bc2fa to your computer and use it in GitHub Desktop.
nodejs script to manipulate and generate random data (uses the lodash and the moment libs)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'use strict'; | |
var _ = require('lodash'), | |
hash = require('./utils_hash') | |
; | |
//----------------------------------------------------------------------------// | |
var SERVER_DATE_FORMAT = 'YYYY-MM-DD'; | |
module.exports.SERVER_DATE_FORMAT = SERVER_DATE_FORMAT; | |
var SERVER_DATETIME_FORMAT = 'YYYY-MM-DD HH:mm:ss'; | |
module.exports.SERVER_DATETIME_FORMAT = SERVER_DATETIME_FORMAT; | |
//----------------------------------------------------------------------------// | |
module.exports.generateHash = hash.generateRandomHash; | |
module.exports.getRandomSizeOfPossible = hash.getRandomSizeOfPossible; | |
module.exports.getRandomSizeOfNumbers = hash.getRandomSizeOfNumbers; | |
//--- | |
function getHashFromValue(value){ | |
return hash.complex(value); | |
} | |
module.exports.getHashFromValue = getHashFromValue; | |
//----------------------------------------------------------------------------// | |
function getRandomFloat(min, max){ | |
return ((Math.random() * (max - min + 1)) + min); | |
} | |
module.exports.getRandomFloat = getRandomFloat; | |
function getRandomDecimal(min, max, fixed){ | |
fixed = fixed || 2; | |
return +getRandomFloat(min, max).toFixed(fixed); | |
} | |
module.exports.getRandomDecimal = getRandomDecimal; | |
// Math.random produces random decimals from [0..1) [inclusive, exclusive) | |
// This function produces an integer at random in the range [min..max] (inclusive, inclusive) | |
function getRandomInt(min, max) { | |
return Math.floor(getRandomFloat(min, max)); | |
}; | |
module.exports.getRandomInt = getRandomInt; | |
function getRandomIntAsString(min, max) { | |
var intValue = getRandomInt(min,max); | |
return ( | |
intValue < 10 ? | |
"0" + intValue : | |
intValue.toString() | |
); | |
}; | |
module.exports.getRandomIntAsString = getRandomIntAsString; | |
function getRandomBoolean(){ | |
return (getRandomInt(0, 1) === 0); | |
} | |
module.exports.getRandomBoolean = getRandomBoolean; | |
//----------------------------------------------------------------------------// | |
function getRandomObjectValue(obj){ | |
return _.sample(obj); | |
} | |
module.exports.getRandomObjectValue = getRandomObjectValue; | |
//----------------------------------------------------------------------------// | |
function getRandomValue(options, inNumber){ | |
if (_.isArray(options)) { | |
inNumber = inNumber || options.length; | |
} | |
else if (_.isObject(options)) { | |
var optionsToArray = []; | |
var length = 0; | |
for (var key in options) { | |
if (options.hasOwnProperty(key)) { | |
optionsToArray.push(options[key]); | |
length++; | |
} | |
} | |
inNumber = Math.min(inNumber || Number.MAX_SAFE_INTEGER, length); | |
options = optionsToArray; | |
} | |
else { | |
return null; | |
} | |
return options[Math.floor(Math.random()*inNumber)]; | |
} | |
module.exports.getRandomValue = getRandomValue; | |
//----------------------------------------------------------------------------// | |
var userNamesData = [ | |
'User Name 1', 'User Name 2', 'User Name 3', 'User Name 4', | |
'Another User Name 1', 'Another User Name 2', | |
'Another User Name 3', 'Another User Name 4' | |
]; | |
var userNamesDataLength = userNamesData.length; | |
function getRandomUserName(){ | |
return getRandomValue(userNamesData, userNamesDataLength); | |
} | |
module.exports.getRandomUserName = getRandomUserName; | |
function getRandomUserEmail(userName){ | |
var endofemail = '@domain.ext'; | |
if(userName) { | |
return userName + endofemail; | |
} else { | |
return getRandomUserName() | |
.split(' ')[0] | |
.toLowerCase() + endofemail; | |
} | |
} | |
module.exports.getRandomUserEmail = getRandomUserEmail; | |
//----------------------------------------------------------------------------// | |
function getRandomNameToObject(nameTo){ | |
nameTo = nameTo || 'mock object'; | |
return getRandomUserName() + ' ' + nameTo; | |
} | |
module.exports.getRandomNameToObject = getRandomNameToObject; | |
function getRandomIdToObject(sequenceTo){ | |
if( !_.isNumber(sequenceTo) ) { | |
throw new Error('getRandomIdToObject - Unable to generate id to value'); | |
} | |
return hash.numbers( sequenceTo ); | |
} | |
module.exports.getRandomIdToObject = getRandomIdToObject; | |
//----------------------------------------------------------------------------// | |
var moment = require('moment'); | |
function getMoment(date){ | |
var output = null; | |
if( _.isUndefined(date) ) { | |
output = moment(); | |
} else if( moment.isMoment(date) ) { | |
output = date; | |
} else if( _.isNumber(date) || _.isString(date) ) { | |
output = moment(date); | |
} | |
return output; | |
} | |
module.exports.getMoment = getMoment; | |
function getNowDate(){ | |
return moment(); | |
} | |
module.exports.getNowDate = getNowDate; | |
function getDateInMonths(inRandomValue, add){ | |
var output = moment(); | |
if(add){ | |
output = output.add((Math.random()*inRandomValue), 'months') | |
}else{ | |
output = output.subtract((Math.random()*inRandomValue), 'months') | |
} | |
return output | |
.set('date', getRandomInt(1, 28)); | |
} | |
module.exports.getDateInMonths = getDateInMonths; | |
//----------------------------------------------------------------------------// | |
function getTonsOfData(func, min, max) { | |
var data = []; | |
var num = min; | |
if(max){ | |
num = getRandomInt(min, max); | |
} | |
for (var i = 0; i < num; i++) { | |
data.push(func()); | |
} | |
return data; | |
} | |
module.exports.getTonsOfData = getTonsOfData; | |
//----------------------------------------------------------------------------// | |
function isArray(data){ | |
return (data && _.isArray(data)); | |
} | |
//--- | |
function paginateData(data, start, pageSize){ | |
if(isArray(data)){ | |
start = start || 0; | |
pageSize = pageSize || 20; | |
var length = data.length; | |
if (start > length) { | |
data = []; | |
} else if (start + pageSize > length) { | |
data = data.slice(start, length); | |
} else { | |
data = data.slice(start, start + pageSize); | |
} | |
return data; | |
} | |
return data; | |
} | |
module.exports.paginateData = paginateData; | |
//--- | |
function calcAvailablePages(data, pageSize){ | |
pageSize = pageSize || 5; | |
if(isArray(data)){ | |
return Math.ceil(data.length/pageSize); | |
} | |
return 1; | |
} | |
module.exports.caclAvailablePages = calcAvailablePages; | |
//--- | |
function getRandomSetOfData(data, amount){ | |
amount = amount || getRandomInt(1,5); | |
if(isArray(data)){ | |
return ( | |
paginateData( | |
data, | |
getRandomInt(1, calcAvailablePages(data, amount)), | |
amount | |
) | |
); | |
} | |
return []; | |
} | |
module.exports.getRandomSetOfData = getRandomSetOfData; | |
//--- | |
function getRandomAmountOfData(data, amount, key){ | |
amount = amount || getRandomInt(5,10); | |
key = key || getHashFromValue; | |
function getKeyValue(value){ | |
if(_.isObject(value)){ | |
if(_.isFunction(key)){ | |
return key(value); | |
} else { | |
return value[key]; | |
} | |
} else { | |
return value; | |
} | |
} | |
if(isArray(data)){ | |
var item; | |
var keyValue; | |
var keysMap = {}; | |
var dataLength = data.length; | |
var output = []; | |
while(output.length < amount){ | |
item = getRandomValue(data, dataLength); | |
keyValue = getKeyValue(item); | |
if(!keysMap.hasOwnProperty(keyValue)){ | |
output.push(item); | |
keysMap[keyValue] = null; | |
} | |
keyValue = null; | |
item = null; | |
} | |
dataLength = null; | |
keysMap = null; | |
keyValue = null; | |
item = null; | |
return output; | |
} | |
return []; | |
} | |
module.exports.getRandomAmountOfData = getRandomAmountOfData; | |
//----------------------------------------------------------------------------// | |
function getCharCodeArray(value){ | |
var i, charArray = []; | |
for(i = value.length-1; i>=0; i--){ | |
charArray.push( value.charCodeAt(i) ); | |
} | |
value = charArray; | |
i = null; | |
charArray = null; | |
return value; | |
} | |
module.exports.getCharCodeArray = getCharCodeArray; | |
function getNumberFromChars(value){ | |
var i, sum = 0; | |
for(i = value.length-1; i>=0; i--){ | |
sum += value.charCodeAt(i); | |
} | |
value = sum; | |
sum = null; | |
i = null; | |
return value; | |
} | |
module.exports.getNumberFromChars = getNumberFromChars; | |
//----------------------------------------------------------------------------// | |
function buildEnum(values){ | |
if(!_.isArray(values)){ | |
return; | |
} | |
var enumMap = {}; | |
values.forEach(function(value){ | |
if(_.isString(value)){ | |
value = value.toUpperCase(); | |
enumMap[value] = value; | |
} | |
}); | |
return enumMap; | |
} | |
module.exports.buildEnum = buildEnum; | |
//----------------------------------------------------------------------------// |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment