Last active
May 24, 2017 10:41
-
-
Save 100ideas/912eced936c08276b5ff42239b9d1c15 to your computer and use it in GitHub Desktop.
hook.io microservice for generating fake data for 100ideas/storyprinter
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
// Simple microservice for generating fake data for 100ideas/storyprinter | |
// https://hook.io/100ideas/fakerjs-storyprinter?numstories=5 | |
// | |
// faker.js docs - http://github.com/marak/faker.js | |
module['exports'] = function fakeData (hook) { | |
let faker = require('faker'); | |
let params = hook.params; | |
let numStories = hook.params.numstories || 15; | |
// Get random number from Poisson distribution (lambda=1). Handy for mocking | |
// user activity volume. | |
// - https://stackoverflow.com/a/1241605 | |
const simplePoisson = function() { | |
let L = Math.exp(-1); | |
let p = 1.0; | |
let k = 0; | |
do { | |
console.log(`L: ${L}, p: ${p}, k: ${k}`) | |
k++; | |
p *= Math.random(); | |
} while (p > L); | |
console.log(`DONE. L: ${L}, p: ${p}, k: ${k}`) | |
return k -1 | |
} | |
const randImgH = () => Math.floor(Math.random() * (800-384) + 384) | |
// supports multiple locales | |
faker.locale = 'en'; // try: 'de', 'es' | |
let result = []; | |
for(let i=numStories; i>0; i--){ | |
result.push({ | |
ID: faker.random.uuid(), | |
author: { | |
name: faker.name.firstName(), | |
avatarImg: faker.internet.avatar(), | |
}, | |
role: `As a ${faker.name.jobTitle()},`, | |
need: `I need ${faker.company.bs()},`, | |
benefit: `so we can ${faker.company.bsBuzz()} our ${faker.company.catchPhrase()}`, | |
category: faker.company.bsAdjective(), | |
comments: new Array(simplePoisson()).fill('').map(x => faker.hacker.phrase()), | |
createdAt: faker.date.past(), | |
updatedAt: faker.date.recent(), | |
// rasterizedImg: faker.image.business(384, 700, true), | |
rasterizedImg: faker.image.business(384, randImgH(), true), | |
printed: faker.random.boolean() | |
}) | |
} | |
hook.res.end(JSON.stringify(result, true, 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
{ | |
"numstories": { | |
"type": "number", | |
"min": 0, | |
"max": 64 | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment