Skip to content

Commit f2d115e

Browse files
committed
add lorem ipsum generator
1 parent a1b1614 commit f2d115e

File tree

3 files changed

+293
-1
lines changed

3 files changed

+293
-1
lines changed

src/core/config/Categories.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -375,7 +375,8 @@
375375
"Parse QR Code",
376376
"Haversine distance",
377377
"Numberwang",
378-
"XKCD Random Number"
378+
"XKCD Random Number",
379+
"Lorem Ipsum Generator"
379380
]
380381
},
381382
{

src/core/lib/LoremIpsum.mjs

+221
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,221 @@
1+
/**
2+
* Lorem Ipsum generator.
3+
*
4+
* @author Klaxon [[email protected]]
5+
* @copyright Crown Copyright 2016
6+
* @license Apache-2.0
7+
*/
8+
9+
/**
10+
* generate lorem ipsum paragraphs.
11+
*
12+
* @param {number} length
13+
* @returns {string}
14+
*/
15+
export function GenerateParagraphs(length=3) {
16+
const paragraphs = [];
17+
while (paragraphs.length < length) {
18+
const paragraphLength = getRandomLength(PARAGRAPH_LENGTH_MEAN, PARAGRAPH_LENGTH_STD_DEV);
19+
const sentences = [];
20+
while (sentences.length < paragraphLength) {
21+
const sentenceLength = getRandomLength(SENTENCE_LENGTH_MEAN, SENTENCE_LENGTH_STD_DEV);
22+
const sentence = getWords(sentenceLength);
23+
sentences.push(formatSentence(sentence));
24+
}
25+
paragraphs.push(formatParagraph(sentences));
26+
}
27+
paragraphs[paragraphs.length-1] = paragraphs[paragraphs.length-1].slice(0, -2);
28+
paragraphs[0] = replaceStart(paragraphs[0]);
29+
return paragraphs.join("");
30+
}
31+
32+
/**
33+
* generate lorem ipsum sentences.
34+
*
35+
* @param {number} length
36+
* @returns {string}
37+
*/
38+
export function GenerateSentences(length=3) {
39+
const sentences = [];
40+
while (sentences.length < length) {
41+
const sentenceLength = getRandomLength(SENTENCE_LENGTH_MEAN, SENTENCE_LENGTH_STD_DEV);
42+
const sentence = getWords(sentenceLength);
43+
sentences.push(formatSentence(sentence));
44+
}
45+
const paragraphs = sentencesToParagraphs(sentences);
46+
return paragraphs.join("");
47+
}
48+
49+
/**
50+
* generate lorem ipsum words.
51+
*
52+
* @param {number} length
53+
* @returns {string}
54+
*/
55+
export function GenerateWords(length=3) {
56+
const words = getWords(length);
57+
const sentences = wordsToSentences(words);
58+
const paragraphs = sentencesToParagraphs(sentences);
59+
return paragraphs.join("");
60+
}
61+
62+
/**
63+
* generate lorem ipsum bytes.
64+
*
65+
* @param {number} length
66+
* @returns {string}
67+
*/
68+
export function GenerateBytes(length=3) {
69+
const str = GenerateWords(length/3);
70+
return str.slice(0, length);
71+
}
72+
73+
/**
74+
* get array of randomly selected words from the lorem ipsum wordList.
75+
*
76+
* @param {number} length
77+
* @returns {string[]}
78+
* @private
79+
*/
80+
function getWords(length=3) {
81+
const words = [];
82+
let word;
83+
let previousWord;
84+
while (words.length < length){
85+
do {
86+
word = wordList[Math.floor(Math.random() * wordList.length)];
87+
}
88+
while (previousWord === word);
89+
words.push(word);
90+
previousWord = word;
91+
}
92+
return words;
93+
}
94+
95+
/**
96+
* convert an array or words into an array of sentences"
97+
*
98+
* @param {string[]} words
99+
* @returns {string[]}
100+
* @private
101+
*/
102+
function wordsToSentences(words) {
103+
const sentences = [];
104+
while (words.length > 0) {
105+
const sentenceLength = getRandomLength(SENTENCE_LENGTH_MEAN, SENTENCE_LENGTH_STD_DEV);
106+
if (sentenceLength <= words.length) {
107+
sentences.push(formatSentence(words.splice(0, sentenceLength)));
108+
} else {
109+
sentences.push(formatSentence(words.splice(0, words.length)));
110+
}
111+
}
112+
return sentences;
113+
}
114+
115+
/**
116+
* convert an array or sentences into an array of paragraphs"
117+
*
118+
* @param {string[]} sentences
119+
* @returns {string[]}
120+
* @private
121+
*/
122+
function sentencesToParagraphs(sentences) {
123+
const paragraphs = [];
124+
while (sentences.length > 0) {
125+
const paragraphLength = getRandomLength(PARAGRAPH_LENGTH_MEAN, PARAGRAPH_LENGTH_STD_DEV);
126+
paragraphs.push(formatParagraph(sentences.splice(0, paragraphLength)));
127+
}
128+
paragraphs[paragraphs.length-1] = paragraphs[paragraphs.length-1].slice(0, -1);
129+
paragraphs[0] = replaceStart(paragraphs[0]);
130+
return paragraphs;
131+
}
132+
133+
/**
134+
* format an array of words into a sentence.
135+
*
136+
* @param {string[]} words
137+
* @returns {string}
138+
* @private
139+
*/
140+
function formatSentence(words) {
141+
//0.35 chance of a comma being added randomly to the sentence.
142+
if (Math.random() < PROBABILITY_OF_A_COMMA) {
143+
const pos = Math.round(Math.random()*(words.length-1));
144+
words[pos] +=",";
145+
}
146+
let sentence = words.join(" ");
147+
sentence = sentence.charAt(0).toUpperCase() + sentence.slice(1);
148+
sentence += ".";
149+
return sentence;
150+
}
151+
152+
/**
153+
* format an array of sentences into a paragraph
154+
*
155+
* @param {string[]} sentences
156+
* @returns {string}
157+
* @private
158+
*/
159+
function formatParagraph(sentences) {
160+
let paragraph = sentences.join(" ");
161+
paragraph += "\n\n";
162+
return paragraph;
163+
}
164+
165+
/**
166+
* get a random number based on a mean and standard deviation.
167+
*
168+
* @param {number} Mean
169+
* @param {number} stdDev
170+
* @returns {number}
171+
* @private
172+
*/
173+
function getRandomLength(mean, stdDev) {
174+
let length;
175+
do {
176+
length = Math.round((Math.random()*2-1)+(Math.random()*2-1)+(Math.random()*2-1)*stdDev+mean);
177+
}
178+
while (length <= 0);
179+
return length;
180+
}
181+
182+
/**
183+
* replace first 5 words with "Lorem ipsum dolor sit amet"
184+
*
185+
* @param {string[]} str
186+
* @returns {string[]}
187+
* @private
188+
*/
189+
function replaceStart(str) {
190+
let words = str.split(" ");
191+
if (words.length > 5) {
192+
words.splice(0, 5, "Lorem", "ipsum", "dolor", "sit", "amet");
193+
return words.join(" ");
194+
} else {
195+
const lorem = ["Lorem", "ipsum", "dolor", "sit", "amet"];
196+
words = lorem.slice(0, words.length);
197+
str = words.join(" ");
198+
str += ".";
199+
return str;
200+
}
201+
}
202+
203+
const SENTENCE_LENGTH_MEAN = 15;
204+
const SENTENCE_LENGTH_STD_DEV = 9;
205+
const PARAGRAPH_LENGTH_MEAN = 5;
206+
const PARAGRAPH_LENGTH_STD_DEV = 2;
207+
const PROBABILITY_OF_A_COMMA = 0.35;
208+
209+
const wordList = [
210+
"ad", "adipisicing", "aliqua", "aliquip", "amet", "anim",
211+
"aute", "cillum", "commodo", "consectetur", "consequat", "culpa",
212+
"cupidatat", "deserunt", "do", "dolor", "dolore", "duis",
213+
"ea", "eiusmod", "elit", "enim", "esse", "est",
214+
"et", "eu", "ex", "excepteur", "exercitation", "fugiat",
215+
"id", "in", "incididunt", "ipsum", "irure", "labore",
216+
"laboris", "laborum", "Lorem", "magna", "minim", "mollit",
217+
"nisi", "non", "nostrud", "nulla", "occaecat", "officia",
218+
"pariatur", "proident", "qui", "quis", "reprehenderit", "sint",
219+
"sit", "sunt", "tempor", "ullamco", "ut", "velit",
220+
"veniam", "voluptate",
221+
];
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
/**
2+
* @author klaxon [[email protected]]
3+
* @copyright Crown Copyright 2018
4+
* @license Apache-2.0
5+
*/
6+
7+
import Operation from "../Operation";
8+
import OperationError from "../errors/OperationError";
9+
import { GenerateParagraphs, GenerateSentences, GenerateWords, GenerateBytes } from "../lib/LoremIpsum";
10+
11+
/**
12+
* Lorem Ipsum Generator operation
13+
*/
14+
class LoremIpsumGenerator extends Operation {
15+
16+
/**
17+
* LoremIpsumGenerator constructor
18+
*/
19+
constructor() {
20+
super();
21+
22+
this.name = "Lorem Ipsum Generator";
23+
this.module = "Default";
24+
this.description = "Generate varying length lorem ipsum placeholder text.";
25+
this.infoURL = "https://wikipedia.org/wiki/Lorem_ipsum";
26+
this.inputType = "string";
27+
this.outputType = "string";
28+
this.args = [
29+
{
30+
"name": "Length",
31+
"type": "number",
32+
"value": "3"
33+
},
34+
{
35+
"name": "Length in",
36+
"type": "option",
37+
"value": ["Paragraphs", "Sentences", "Words", "Bytes"]
38+
}
39+
40+
];
41+
}
42+
43+
/**
44+
* @param {string} input
45+
* @param {Object[]} args
46+
* @returns {string}
47+
*/
48+
run(input, args) {
49+
const [length, lengthType] = args;
50+
if (length < 1){
51+
throw new OperationError("Length must be greater than 0");
52+
}
53+
switch (lengthType) {
54+
case "Paragraphs":
55+
return GenerateParagraphs(length);
56+
case "Sentences":
57+
return GenerateSentences(length);
58+
case "Words":
59+
return GenerateWords(length);
60+
case "Bytes":
61+
return GenerateBytes(length);
62+
default:
63+
throw new OperationError("invalid lengthType");
64+
65+
}
66+
}
67+
68+
}
69+
70+
export default LoremIpsumGenerator;

0 commit comments

Comments
 (0)