import { sliceWrap } from './sliceWrap.ts' /** * Generates lorem ipsum text. * * @param {number} [count=4] - The number of words, sentences, or paragraphs to generate. Default is * `4` * @param {'words' | 'sentences' | 'paragraphs'} [units='sentences'] - The units to generate. * Default is `'sentences'` * @returns {string} The generated text. */ export function loremIpsum( count: number = 4, units: 'words' | 'sentences' | 'paragraphs' = 'sentences', ): string { switch (units) { case 'words': return wordsToSentence(sliceWrap(WORDS, 0, count)) case 'sentences': return sliceWrap(SENTENCES, 0, count) .map((sentence) => wordsToSentence(sentence.words)) .join(' ') case 'paragraphs': return sliceWrap(PARAGRAPHS, 0, count) .map((paragraph) => paragraph.sentences.map((sentence) => wordsToSentence(sentence.words)).join(' '), ) .join('\n') } } function wordsToSentence(words: string[]) { let sentence = '' for (const word of words) { if (sentence.length > 0) sentence += ' ' sentence += word } if (sentence.endsWith(',')) { sentence = sentence.slice(0, -1) } if (sentence.length > 0 && !sentence.endsWith('.')) { sentence += '.' } return sentence } const LOREM_IPSUM = { paragraphs: [ { sentences: [ { words: [ 'Lorem', 'ipsum', 'dolor', 'sit', 'amet,', 'consectetur', 'adipiscing', 'elit,', 'sed', 'do', 'eiusmod', 'tempor', 'incididunt', 'ut', 'labore', 'et', 'dolore', 'magna', 'aliqua.', ], }, { words: [ 'Ut', 'enim', 'ad', 'minim', 'veniam,', 'quis', 'nostrud', 'exercitation', 'ullamco', 'laboris', 'nisi', 'ut', 'aliquip', 'ex', 'ea', 'commodo', 'consequat.', ], }, { words: [ 'Duis', 'aute', 'irure', 'dolor', 'in', 'reprehenderit', 'in', 'voluptate', 'velit', 'esse', 'cillum', 'dolore', 'eu', 'fugiat', 'nulla', 'pariatur.', ], }, { words: [ 'Excepteur', 'sint', 'occaecat', 'cupidatat', 'non', 'proident,', 'sunt', 'in', 'culpa', 'qui', 'officia', 'deserunt', 'mollit', 'anim', 'id', 'est', 'laborum.', ], }, ], }, ], } const PARAGRAPHS = LOREM_IPSUM.paragraphs const SENTENCES = PARAGRAPHS.flatMap((paragraph) => paragraph.sentences) const WORDS = SENTENCES.flatMap((sentence) => sentence.words)