Skip to content

Instantly share code, notes, and snippets.

@rshk
Created September 4, 2017 16:01
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rshk/d705cbf1fa12e8a8c7fe45be15289bda to your computer and use it in GitHub Desktop.
Save rshk/d705cbf1fa12e8a8c7fe45be15289bda to your computer and use it in GitHub Desktop.
Simple Lorem Ipsum generation in Javascript.
// 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