Skip to content

Instantly share code, notes, and snippets.

@ohansemmanuel
Created May 18, 2018 14:00
Show Gist options
  • Save ohansemmanuel/d940d45c541ae8bc49e16b2fe0a55a2d to your computer and use it in GitHub Desktop.
Save ohansemmanuel/d940d45c541ae8bc49e16b2fe0a55a2d to your computer and use it in GitHub Desktop.
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));
}
@cicciogeppo88
Copy link

cicciogeppo88 commented Aug 20, 2018

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 !

@poonia
Copy link

poonia commented Jan 9, 2019

use yarn add shortid txtgen faker lodash

@jonathanestabillo
Copy link

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

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