Tidied up Lorem Ipsum op

This commit is contained in:
n1474335 2019-01-09 16:36:34 +00:00
parent 0e601d5b5f
commit c49a770c59
4 changed files with 58 additions and 44 deletions

View File

@ -2,6 +2,9 @@
All major and minor version changes will be documented in this file. Details of patch-level version changes can be found in [commit messages](https://github.com/gchq/CyberChef/commits/master). All major and minor version changes will be documented in this file. Details of patch-level version changes can be found in [commit messages](https://github.com/gchq/CyberChef/commits/master).
### [8.20.0] - 2019-01-09
- 'Generate Lorem Ipsum' operation added [@klaxon1] | [#455]
### [8.19.0] - 2018-12-30 ### [8.19.0] - 2018-12-30
- UI test suite added to confirm that the app loads correctly in a reasonable time and that various operations from each module can be run [@n1474335] | [#458] - UI test suite added to confirm that the app loads correctly in a reasonable time and that various operations from each module can be run [@n1474335] | [#458]
@ -88,6 +91,7 @@ All major and minor version changes will be documented in this file. Details of
[8.20.0]: https://github.com/gchq/CyberChef/releases/tag/v8.20.0
[8.19.0]: https://github.com/gchq/CyberChef/releases/tag/v8.19.0 [8.19.0]: https://github.com/gchq/CyberChef/releases/tag/v8.19.0
[8.18.0]: https://github.com/gchq/CyberChef/releases/tag/v8.18.0 [8.18.0]: https://github.com/gchq/CyberChef/releases/tag/v8.18.0
[8.17.0]: https://github.com/gchq/CyberChef/releases/tag/v8.17.0 [8.17.0]: https://github.com/gchq/CyberChef/releases/tag/v8.17.0
@ -159,4 +163,5 @@ All major and minor version changes will be documented in this file. Details of
[#446]: https://github.com/gchq/CyberChef/pull/446 [#446]: https://github.com/gchq/CyberChef/pull/446
[#448]: https://github.com/gchq/CyberChef/pull/448 [#448]: https://github.com/gchq/CyberChef/pull/448
[#449]: https://github.com/gchq/CyberChef/pull/449 [#449]: https://github.com/gchq/CyberChef/pull/449
[#455]: https://github.com/gchq/CyberChef/pull/455
[#458]: https://github.com/gchq/CyberChef/pull/458 [#458]: https://github.com/gchq/CyberChef/pull/458

View File

@ -374,9 +374,9 @@
"Generate QR Code", "Generate QR Code",
"Parse QR Code", "Parse QR Code",
"Haversine distance", "Haversine distance",
"Generate Lorem Ipsum",
"Numberwang", "Numberwang",
"XKCD Random Number", "XKCD Random Number"
"Lorem Ipsum Generator"
] ]
}, },
{ {

View File

@ -2,16 +2,16 @@
* Lorem Ipsum generator. * Lorem Ipsum generator.
* *
* @author Klaxon [klaxon@veyr.com] * @author Klaxon [klaxon@veyr.com]
* @copyright Crown Copyright 2016 * @copyright Crown Copyright 2018
* @license Apache-2.0 * @license Apache-2.0
*/ */
/** /**
* generate lorem ipsum paragraphs. * Generate lorem ipsum paragraphs.
* *
* @param {number} length * @param {number} length
* @returns {string} * @returns {string}
*/ */
export function GenerateParagraphs(length=3) { export function GenerateParagraphs(length=3) {
const paragraphs = []; const paragraphs = [];
while (paragraphs.length < length) { while (paragraphs.length < length) {
@ -29,12 +29,13 @@ export function GenerateParagraphs(length=3) {
return paragraphs.join(""); return paragraphs.join("");
} }
/** /**
* generate lorem ipsum sentences. * Generate lorem ipsum sentences.
* *
* @param {number} length * @param {number} length
* @returns {string} * @returns {string}
*/ */
export function GenerateSentences(length=3) { export function GenerateSentences(length=3) {
const sentences = []; const sentences = [];
while (sentences.length < length) { while (sentences.length < length) {
@ -46,12 +47,13 @@ export function GenerateSentences(length=3) {
return paragraphs.join(""); return paragraphs.join("");
} }
/** /**
* generate lorem ipsum words. * Generate lorem ipsum words.
* *
* @param {number} length * @param {number} length
* @returns {string} * @returns {string}
*/ */
export function GenerateWords(length=3) { export function GenerateWords(length=3) {
const words = getWords(length); const words = getWords(length);
const sentences = wordsToSentences(words); const sentences = wordsToSentences(words);
@ -59,19 +61,21 @@ export function GenerateWords(length=3) {
return paragraphs.join(""); return paragraphs.join("");
} }
/**
* generate lorem ipsum bytes. /**
* * Generate lorem ipsum bytes.
* @param {number} length *
* @returns {string} * @param {number} length
*/ * @returns {string}
*/
export function GenerateBytes(length=3) { export function GenerateBytes(length=3) {
const str = GenerateWords(length/3); const str = GenerateWords(length/3);
return str.slice(0, length); return str.slice(0, length);
} }
/** /**
* get array of randomly selected words from the lorem ipsum wordList. * Get array of randomly selected words from the lorem ipsum wordList.
* *
* @param {number} length * @param {number} length
* @returns {string[]} * @returns {string[]}
@ -84,16 +88,16 @@ function getWords(length=3) {
while (words.length < length){ while (words.length < length){
do { do {
word = wordList[Math.floor(Math.random() * wordList.length)]; word = wordList[Math.floor(Math.random() * wordList.length)];
} } while (previousWord === word);
while (previousWord === word);
words.push(word); words.push(word);
previousWord = word; previousWord = word;
} }
return words; return words;
} }
/** /**
* convert an array or words into an array of sentences" * Convert an array of words into an array of sentences
* *
* @param {string[]} words * @param {string[]} words
* @returns {string[]} * @returns {string[]}
@ -112,8 +116,9 @@ function wordsToSentences(words) {
return sentences; return sentences;
} }
/** /**
* convert an array or sentences into an array of paragraphs" * Convert an array of sentences into an array of paragraphs
* *
* @param {string[]} sentences * @param {string[]} sentences
* @returns {string[]} * @returns {string[]}
@ -130,15 +135,16 @@ function sentencesToParagraphs(sentences) {
return paragraphs; return paragraphs;
} }
/** /**
* format an array of words into a sentence. * Format an array of words into a sentence.
* *
* @param {string[]} words * @param {string[]} words
* @returns {string} * @returns {string}
* @private * @private
*/ */
function formatSentence(words) { function formatSentence(words) {
//0.35 chance of a comma being added randomly to the sentence. // 0.35 chance of a comma being added randomly to the sentence.
if (Math.random() < PROBABILITY_OF_A_COMMA) { if (Math.random() < PROBABILITY_OF_A_COMMA) {
const pos = Math.round(Math.random()*(words.length-1)); const pos = Math.round(Math.random()*(words.length-1));
words[pos] +=","; words[pos] +=",";
@ -149,8 +155,9 @@ function formatSentence(words) {
return sentence; return sentence;
} }
/** /**
* format an array of sentences into a paragraph * Format an array of sentences into a paragraph.
* *
* @param {string[]} sentences * @param {string[]} sentences
* @returns {string} * @returns {string}
@ -162,10 +169,11 @@ function formatParagraph(sentences) {
return paragraph; return paragraph;
} }
/** /**
* get a random number based on a mean and standard deviation. * Get a random number based on a mean and standard deviation.
* *
* @param {number} Mean * @param {number} mean
* @param {number} stdDev * @param {number} stdDev
* @returns {number} * @returns {number}
* @private * @private
@ -174,13 +182,13 @@ function getRandomLength(mean, stdDev) {
let length; let length;
do { do {
length = Math.round((Math.random()*2-1)+(Math.random()*2-1)+(Math.random()*2-1)*stdDev+mean); length = Math.round((Math.random()*2-1)+(Math.random()*2-1)+(Math.random()*2-1)*stdDev+mean);
} } while (length <= 0);
while (length <= 0);
return length; return length;
} }
/** /**
* replace first 5 words with "Lorem ipsum dolor sit amet" * Replace first 5 words with "Lorem ipsum dolor sit amet"
* *
* @param {string[]} str * @param {string[]} str
* @returns {string[]} * @returns {string[]}
@ -200,6 +208,7 @@ function replaceStart(str) {
} }
} }
const SENTENCE_LENGTH_MEAN = 15; const SENTENCE_LENGTH_MEAN = 15;
const SENTENCE_LENGTH_STD_DEV = 9; const SENTENCE_LENGTH_STD_DEV = 9;
const PARAGRAPH_LENGTH_MEAN = 5; const PARAGRAPH_LENGTH_MEAN = 5;

View File

@ -9,17 +9,17 @@ import OperationError from "../errors/OperationError";
import { GenerateParagraphs, GenerateSentences, GenerateWords, GenerateBytes } from "../lib/LoremIpsum"; import { GenerateParagraphs, GenerateSentences, GenerateWords, GenerateBytes } from "../lib/LoremIpsum";
/** /**
* Lorem Ipsum Generator operation * Generate Lorem Ipsum operation
*/ */
class LoremIpsumGenerator extends Operation { class GenerateLoremIpsum extends Operation {
/** /**
* LoremIpsumGenerator constructor * GenerateLoremIpsum constructor
*/ */
constructor() { constructor() {
super(); super();
this.name = "Lorem Ipsum Generator"; this.name = "Generate Lorem Ipsum";
this.module = "Default"; this.module = "Default";
this.description = "Generate varying length lorem ipsum placeholder text."; this.description = "Generate varying length lorem ipsum placeholder text.";
this.infoURL = "https://wikipedia.org/wiki/Lorem_ipsum"; this.infoURL = "https://wikipedia.org/wiki/Lorem_ipsum";
@ -60,11 +60,11 @@ class LoremIpsumGenerator extends Operation {
case "Bytes": case "Bytes":
return GenerateBytes(length); return GenerateBytes(length);
default: default:
throw new OperationError("invalid lengthType"); throw new OperationError("Invalid length type");
} }
} }
} }
export default LoremIpsumGenerator; export default GenerateLoremIpsum;