Skip to content

Instantly share code, notes, and snippets.

@jens1101
Last active June 26, 2018 05:59
Show Gist options
  • Save jens1101/a9e6b0cac0d901d8b9388eda3d8eb7db to your computer and use it in GitHub Desktop.
Save jens1101/a9e6b0cac0d901d8b9388eda3d8eb7db to your computer and use it in GitHub Desktop.
Generate test data from the bacon ipsum API
async function generateTestData (paragraphs = null, columns = 10, addBlanks = true) {
let numParagraphs = parseInt(paragraphs)
if (Number.isNaN(numParagraphs)) {
numParagraphs = Math.floor(Math.random() * 6) + 1
}
const testData = await fetch(`https://baconipsum.com/api/?type=meat-and-filler&paras=${numParagraphs}`)
.then(response => response.json())
.then(data => data.join(' ').split(' ').filter(Boolean))
if (addBlanks) {
new Array(testData.length).fill(0)
.map((value, index) => Math.random() > 0.8 ? index : null)
.filter(val => val !== null)
.reverse()
.forEach(index => testData.splice(index, 0, ''))
}
const testDataLines = []
while (testData.length > 0) {
testDataLines.push(testData.splice(0, columns))
}
return testDataLines.map(values => values.join('\t')).join('\n')
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment