Created
January 11, 2018 15:50
-
-
Save MassimoSporchia/70456febddfab7250c0758222f1f113b to your computer and use it in GitHub Desktop.
Snippet that creates random people from a list of names and surname (json arrays in files) and writes it to a file (json array)
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
var fs = require('fs'); | |
var namesIn = "names.json" | |
var surnamesIn = "surnames.json" | |
var peopleOut = "peopleOut.json" | |
var namesFile = fs.readFileSync(namesIn, 'utf8'); | |
var surnamesFile = fs.readFileSync(surnamesIn, 'utf8'); | |
namesJson = JSON.parse(namesFile); | |
surnamesJson = JSON.parse(surnamesFile); | |
people = { person: [] } | |
for (let index = 0; index < 10000; index++) { | |
people.person.push( randomPerson(namesJson, surnamesJson)); | |
} | |
var stringified = JSON.stringify(people); | |
fs.writeFile(peopleOut, stringified, 'utf8'); | |
function randomPerson(names,surnames){ | |
var name = randomName(names); | |
var surname = randomName(surnames); | |
var ppl = { 'name': name, 'surname': surname } | |
return ppl; | |
} | |
function randomName(lst){ | |
if (lst.names != null) | |
return lst.names[randomIntFromInterval(0,lst.names.length)] | |
else | |
return lst.surnames[randomIntFromInterval(0,lst.surnames.length)] | |
} | |
function randomIntFromInterval(min,max) | |
{ | |
return Math.floor(Math.random()*(max-min+1)+min); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment