Last active
June 26, 2018 05:59
-
-
Save jens1101/a9e6b0cac0d901d8b9388eda3d8eb7db to your computer and use it in GitHub Desktop.
Generate test data from the bacon ipsum API
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
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¶s=${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