Created
May 18, 2018 14:00
-
-
Save ohansemmanuel/d940d45c541ae8bc49e16b2fe0a55a2d to your computer and use it in GitHub Desktop.
Static data generation for Skypey.
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
const shortid = require("shortid"); // shortid.generate() returns a unique "short" id | |
const txtgen = require("txtgen"); // txtgen.sentence() returns random "readable" sentences | |
const faker = require("faker"); // faker is used for generating random fake data. | |
const _ = require("lodash"); // lodash is a utility lib for Javascript | |
const users = generateUsers(10); | |
export const contacts = _.mapKeys(users, "user_id"); | |
export const getMessages = messagesPerUser => { | |
let messages = {}; | |
_.forEach(users, user => { | |
messages[user.user_id] = { | |
..._.mapKeys(generateMsgs(messagesPerUser), "number") | |
}; | |
}); | |
return messages; | |
}; | |
// just an example of how the state object is structured | |
export const state = { | |
user: generateUser(), | |
messages: getMessages(10), | |
typing: "", | |
contacts, | |
activeUserId: null | |
}; | |
/** | |
* @returns {Object} - a new user object | |
*/ | |
export function generateUser() { | |
return { | |
name: faker.name.findName(), | |
email: faker.internet.email(), | |
profile_pic: faker.internet.avatar(), | |
status: txtgen.sentence(), | |
user_id: shortid.generate() | |
}; | |
} | |
/** | |
* @returns {Object} - a new message object | |
*/ | |
function generateMsg(number) { | |
return { | |
number, | |
text: txtgen.sentence(), | |
is_user_msg: faker.random.boolean() | |
}; | |
} | |
/** | |
* | |
* @param {Number} numberOfUsers - the number of users to be generated | |
* @param {Function} generateUser - function that generates a single user | |
* @returns {Array} - an array of user objects with length n = numberOfUsers | |
*/ | |
function generateUsers(numberOfUsers) { | |
return Array.from({ length: numberOfUsers }, () => generateUser()); | |
} | |
function generateMsgs(numberOfMsgs) { | |
return Array.from({ length: numberOfMsgs }, (v, i) => generateMsg(i)); | |
} |
Hello @kraizt , I sort it just calling from the console (once inside the project folder) 'yarn add shortId', 'yarn add txtgen', 'yarn add faker', 'yarn add lodash'. If you already installed node these packages are already in your environment , hope it will be useful !
use yarn add shortid txtgen faker lodash
Hi Ohan! I'm having a syntax error on line 12 actually. I'm new at using lodash. I tried removing the three dots and I'm still getting the syntax error.
Note that faker js has been discontinued by its creator.
I have written an alternate script that uses only Falso to generate the same content.
https://github.com/hallucinogenizer/skypey-static-data
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
hello ohans, pardon me, but u didn't mention in book for required package need to be installed, I try manually finding it, but little worry installing wrong package 😅 hehe