Skip to content

Instantly share code, notes, and snippets.

@bhubr
Created May 7, 2019 09:14
Show Gist options
  • Select an option

  • Save bhubr/0628badd522c0beb640a3ac66b681890 to your computer and use it in GitHub Desktop.

Select an option

Save bhubr/0628badd522c0beb640a3ac66b681890 to your computer and use it in GitHub Desktop.
[Generate fake emails] Generate fake emails using randomuser.me and chancejs
const Chance = require('chance');
const fetch = require('node-fetch');
const fs = require('fs');
const chance = new Chance();
let users;
const getUsers = async () => fetch('https://randomuser.me/api/?results=8&nat=gb')
.then(r => r.json())
.then(({ results }) => results.map(
({
name: { first, last },
picture: { thumbnail }
}, index) => ({ id: index + 1, name: `${first} ${last}`, avatar: thumbnail })
))
.then(data => {
users = data;
});
// .then(users => JSON.stringify(users, null, 2))
// .then(console.log)
const pickUser = () => {
const idx = Math.floor(users.length * Math.random());
return users[idx];
}
const genMessage = () => {
const id = chance.guid();
const user = pickUser();
const message = chance.sentence();
const read = Math.random() > 0.5;
return { id, user, message, read };
};
const genMessages = (len = 10) => {
return new Array(len)
.fill(0)
.map(genMessage)
};
getUsers()
.then(users => {
const messages = genMessages();
fs.writeFileSync('messages.json', JSON.stringify(messages, null, 2));
})
{
"name": "generate-fake-emails",
"version": "1.0.0",
"description": "",
"main": "generateFakeEmails.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC",
"dependencies": {
"chance": "^1.0.18",
"node-fetch": "^2.5.0"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment