Browse Source

Tidied up Lorem Ipsum op

n1474335 6 years ago
parent
commit
c49a770c59

+ 5 - 0
CHANGELOG.md

@@ -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).
 
 
+### [8.20.0] - 2019-01-09
+- 'Generate Lorem Ipsum' operation added [@klaxon1] | [#455]
+
 ### [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]
 
@@ -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.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
@@ -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
 [#448]: https://github.com/gchq/CyberChef/pull/448
 [#449]: https://github.com/gchq/CyberChef/pull/449
+[#455]: https://github.com/gchq/CyberChef/pull/455
 [#458]: https://github.com/gchq/CyberChef/pull/458

+ 2 - 2
src/core/config/Categories.json

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

+ 45 - 36
src/core/lib/LoremIpsum.mjs

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

+ 6 - 6
src/core/operations/LoremIpsumGenerator.mjs → src/core/operations/GenerateLoremIpsum.mjs

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