Created
September 4, 2017 16:01
-
-
Save rshk/d705cbf1fa12e8a8c7fe45be15289bda to your computer and use it in GitHub Desktop.
Simple Lorem Ipsum generation in Javascript.
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
// Some stuff from https://github.com/knicklabs/lorem-ipsum.js | |
const WORDS = [ | |
'ad', 'adipisicing', 'aliqua', 'aliquip', 'amet', 'anim', 'aute', | |
'cillum', 'commodo', 'consectetur', 'consequat', 'culpa', | |
'cupidatat', 'deserunt', 'do', 'dolor', 'dolore', 'duis', 'ea', | |
'eiusmod', 'elit', 'enim', 'esse', 'est', 'et', 'eu', 'ex', | |
'excepteur', 'exercitation', 'fugiat', 'id', 'in', 'incididunt', | |
'ipsum', 'irure', 'labore', 'laboris', 'laborum', 'Lorem', | |
'magna', 'minim', 'mollit', 'nisi', 'non', 'nostrud', 'nulla', | |
'occaecat', 'officia', 'pariatur', 'proident', 'qui', 'quis', | |
'reprehenderit', 'sint', 'sit', 'sunt', 'tempor', 'ullamco', 'ut', | |
'velit', 'veniam', 'voluptate']; | |
function randomInteger(min, max) { | |
return Math.floor(Math.random() * (max - min + 1) + min); | |
} | |
function randomChoice(array) { | |
let idx = randomInteger(0, array.length - 1); | |
return array[idx]; | |
} | |
export function randomWord() { | |
return randomChoice(WORDS); | |
} | |
function capitalize(word) { | |
if (!word.length) { | |
return word; | |
} | |
return word.charAt(0).toUpperCase() + word.slice(1); | |
} | |
export function randomSentence(wordsCount) { | |
let words = []; | |
for (let i = 0; i < wordsCount; i++) { | |
let word = randomWord(); | |
if (i == 0) { | |
word = capitalize(word); | |
} | |
words.push(word); | |
} | |
return words.join(' ') + (wordsCount > 0 ? '.' : ''); | |
} | |
export function randomParagraph(wordsCount) { | |
let sentences = []; | |
while (wordsCount > 0) { | |
let sentenceWordsCount = randomInteger(3, 15); | |
sentenceWordsCount = Math.min(sentenceWordsCount, wordsCount); | |
wordsCount -= sentenceWordsCount; | |
sentences.push(randomSentence(sentenceWordsCount)); | |
} | |
return {sentences.join(' ')}; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment