To help in development

While developing website or app, many times when we are starting from scratch, when there is not yet any data yet, we would like to fill some texts into something. In design, Lorem Ipsum is widely used as the filler text. It is very helpful, while designing.
In development, sometimes we need a dummy text. Yet copying an and pasting the same text make it less interesting. So, in my case, I have created this very simple javascript function to generate the dummy text from a set of lorem ipsum words.
function lipsum(n=10) {
if (typeof(n)!=='number'||(typeof(n)==='number'&&n<1)) {
n=10
}
const words = ['lorem', 'ipsum', 'dolor', 'sit', 'amet', 'consectetur', 'adipiscing', 'elit', 'vivamus', 'et', 'accumsan', 'augue', 'duis', 'eget', 'nunc', 'id', 'sodales', 'finibus', 'vestibulum', 'sagittis', 'magna', 'nec', 'rutrum', 'volutpat', 'risus', 'tincidunt', 'justo', 'non', 'gravida', 'tortor', 'enim', 'in', 'urna', 'ut', 'vel', 'metus', 'pellentesque', 'porttitor', 'vitae', 'nisi', 'nullam', 'faucibus', 'condimentum', 'quam', 'imperdiet', 'class', 'aptent', 'taciti', 'sociosqu', 'ad', 'litora', 'torquent', 'per', 'conubia', 'nostra', 'inceptos', 'himenaeos', 'interdum', 'malesuada', 'fames', 'ac', 'ante', 'primis', 'curabitur', 'nibh', 'quis', 'iaculis', 'cras', 'mollis', 'eu', 'congue', 'leo']
const count = Math.floor(Math.random() * n + 1)
const sentence = []
const indexes = (new Array(count)).fill(0).map(index=>{
return Math.floor(Math.random()*words.length)
})
indexes.forEach((index,i)=>{
const word = words[index]
if (i===0)
sentence.push(word.charAt(0).toUpperCase()+word.substr(1))
else
sentence.push(word)
})
return sentence.join(' ').concat('.')
}
To use it, simply call lipsum()
. By default, if no parameter is provides, it will generate a sentence with 10 random words. Or we may specify how long we want it to be by simply calling lipsum(250)
or lipsum(1024)
.
To demostrate it, I have created a CodePen page for it. Here it is; https://codepen.io/mjunaidi/pen/rdVvOz. I hope it might be useful.