Created
May 7, 2019 09:14
-
-
Save bhubr/0628badd522c0beb640a3ac66b681890 to your computer and use it in GitHub Desktop.
[Generate fake emails] Generate fake emails using randomuser.me and chancejs
This file contains hidden or 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
| 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)); | |
| }) |
This file contains hidden or 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
| { | |
| "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