Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Static data generation for Skypey.
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));
}
@jonathanestabillo
Copy link

jonathanestabillo commented Feb 4, 2019

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.

@hallucinogenizer
Copy link

hallucinogenizer commented Aug 23, 2022

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