/** * A blindtext generator written in JavaScript. It uses a set of latin words to create either a given amount * of words or sentences. * * @author mightyplow */ const defaultOptions = { minWords: 3, maxWords: 9 }; const availableWords = require('./words'); const punctuationMarks = ['.', '!', '?']; const { randomInt, capitalize, getRandomArrayValue } = require('./utils'); const getRandomPunctuationMark = getRandomArrayValue.bind(null, punctuationMarks); const getRandomWord = getRandomArrayValue.bind(null, availableWords); function createSentence ({ minWords, maxWords }) { const numWords = randomInt(minWords, maxWords); const sentenceWords = []; for (let i = 0, randomWord = ''; i < numWords; i += 1) { do { randomWord = getRandomWord(); } while(sentenceWords.includes(randomWord)); // capitalize first letter when first word of sentence // also capitalize when a random number between 0 and 3 is 0 -> just to get more capital letters sentenceWords.push( (i !== 0 && randomInt(0, 3) > 0) ? randomWord : capitalize(randomWord) ); } return sentenceWords.join(' ') + getRandomPunctuationMark(); } const Unit = { SENTENCE: 'sentence', WORD: 'word' }; const UnitFunction = { [Unit.SENTENCE]: createSentence, [Unit.WORD]: getRandomWord }; function createText (num, unit = Unit.WORD, options) { if (!UnitFunction.hasOwnProperty(unit)) { throw Error('Invalid unit parameter'); } const creatorOptions = Object.assign({}, options, defaultOptions); const unitCreator = UnitFunction[unit]; const texts = []; for (let i = 0; i < num; i += 1) { texts.push(unitCreator(creatorOptions)); } return texts.join(' '); } export { createText, Unit };