浏览代码

Merge branch 'port-arithmetic' of https://github.com/d98762625/CyberChef into esm

n1474335 7 年之前
父节点
当前提交
e41eb3d8a2

文件差异内容过多而无法显示
+ 257 - 257
package-lock.json


+ 139 - 0
src/core/lib/Arithmetic.mjs

@@ -0,0 +1,139 @@
+/**
+ * @author bwhitn [brian.m.whitney@outlook.com]
+ * @author d98762625 [d98762625@gmailcom]
+ * @copyright Crown Copyright 2018
+ * @license Apache-2.0
+ */
+
+import Utils from "../Utils";
+import BigNumber from "bignumber.js";
+
+
+/**
+ * Converts a string array to a number array.
+ *
+ * @param {string[]} input
+ * @param {string} delim
+ * @returns {BigNumber[]}
+ */
+export function createNumArray(input, delim) {
+    delim = Utils.charRep(delim || "Space");
+    const splitNumbers = input.split(delim);
+    const numbers = [];
+    let num;
+
+    splitNumbers.map((number) => {
+        try {
+            num = BigNumber(number.trim());
+            if (!num.isNaN()) {
+                numbers.push(num);
+            }
+        } catch (err) {
+            // This line is not a valid number
+        }
+    });
+    return numbers;
+}
+
+
+/**
+ * Adds an array of numbers and returns the value.
+ *
+ * @param {BigNumber[]} data
+ * @returns {BigNumber}
+ */
+export function sum(data) {
+    if (data.length > 0) {
+        return data.reduce((acc, curr) => acc.plus(curr));
+    }
+}
+
+
+/**
+ * Subtracts an array of numbers and returns the value.
+ *
+ * @param {BigNumber[]} data
+ * @returns {BigNumber}
+ */
+export function sub(data) {
+    if (data.length > 0) {
+        return data.reduce((acc, curr) => acc.minus(curr));
+    }
+}
+
+
+/**
+ * Multiplies an array of numbers and returns the value.
+ *
+ * @param {BigNumber[]} data
+ * @returns {BigNumber}
+ */
+export function multi(data) {
+    if (data.length > 0) {
+        return data.reduce((acc, curr) => acc.times(curr));
+    }
+}
+
+
+/**
+ * Divides an array of numbers and returns the value.
+ *
+ * @param {BigNumber[]} data
+ * @returns {BigNumber}
+ */
+export function div(data) {
+    if (data.length > 0) {
+        return data.reduce((acc, curr) => acc.div(curr));
+    }
+}
+
+
+/**
+ * Computes mean of a number array and returns the value.
+ *
+ * @param {BigNumber[]} data
+ * @returns {BigNumber}
+ */
+export function mean(data) {
+    if (data.length > 0) {
+        return sum(data).div(data.length);
+    }
+}
+
+
+/**
+ * Computes median of a number array and returns the value.
+ *
+ * @param {BigNumber[]} data
+ * @returns {BigNumber}
+ */
+export function median(data) {
+    if ((data.length % 2) === 0 && data.length > 0) {
+        data.sort(function(a, b){
+            return a.minus(b);
+        });
+        const first = data[Math.floor(data.length / 2)];
+        const second = data[Math.floor(data.length / 2) - 1];
+        return mean([first, second]);
+    } else {
+        return data[Math.floor(data.length / 2)];
+    }
+}
+
+
+/**
+ * Computes standard deviation of a number array and returns the value.
+ *
+ * @param {BigNumber[]} data
+ * @returns {BigNumber}
+ */
+export function stdDev(data) {
+    if (data.length > 0) {
+        const avg = mean(data);
+        let devSum = new BigNumber(0);
+        data.map((datum) => {
+            devSum = devSum.plus(datum.minus(avg).pow(2));
+        });
+        return devSum.div(data.length).sqrt();
+    }
+}

+ 5 - 0
src/core/lib/Delim.mjs

@@ -31,6 +31,11 @@ export const WORD_DELIM_OPTIONS = ["Line feed", "CRLF", "Forward slash", "Backsl
  */
 export const INPUT_DELIM_OPTIONS = ["Line feed", "CRLF", "Space", "Comma", "Semi-colon", "Colon", "Nothing (separate chars)"];
 
+/**
+ * Arithmetic sequence delimiters
+ */
+export const ARITHMETIC_DELIM_OPTIONS = ["Line feed", "Space", "Comma", "Semi-colon", "Colon", "CRLF"];
+
 /**
  * Split delimiters.
  */

+ 51 - 0
src/core/operations/Divide.mjs

@@ -0,0 +1,51 @@
+/**
+ * @author bwhitn [brian.m.whitney@outlook.com]
+ * @author d98762625 [d98762625@gmail.com]
+ * @copyright Crown Copyright 2018
+ * @license Apache-2.0
+ */
+
+import BigNumber from "bignumber.js";
+import Operation from "../Operation";
+import { div, createNumArray } from "../lib/Arithmetic";
+import { ARITHMETIC_DELIM_OPTIONS } from "../lib/Delim";
+
+
+/**
+ * Divide operation
+ */
+class Divide extends Operation {
+
+    /**
+     * Divide constructor
+     */
+    constructor() {
+        super();
+
+        this.name = "Divide";
+        this.module = "Default";
+        this.description = "Divides a list of numbers. If an item in the string is not a number it is excluded from the list.<br><br>e.g. <code>0x0a 8 .5</code> becomes <code>2.5</code>";
+        this.inputType = "string";
+        this.outputType = "BigNumber";
+        this.args = [
+            {
+                "name": "Delimiter",
+                "type": "option",
+                "value": ARITHMETIC_DELIM_OPTIONS,
+            }
+        ];
+    }
+
+    /**
+     * @param {string} input
+     * @param {Object[]} args
+     * @returns {BigNumber}
+     */
+    run(input, args) {
+        const val = div(createNumArray(input, args[0]));
+        return val instanceof BigNumber ? val : new BigNumber(NaN);
+    }
+
+}
+
+export default Divide;

+ 50 - 0
src/core/operations/Mean.mjs

@@ -0,0 +1,50 @@
+/**
+ * @author bwhitn [brian.m.whitney@outlook.com]
+ * @author d98762625 [d98762625@gmail.com]
+ * @copyright Crown Copyright 2018
+ * @license Apache-2.0
+ */
+
+import Operation from "../Operation";
+import { mean, createNumArray } from "../lib/Arithmetic";
+import { ARITHMETIC_DELIM_OPTIONS } from "../lib/Delim";
+import BigNumber from "bignumber.js";
+
+/**
+ * Mean operation
+ */
+class Mean extends Operation {
+
+    /**
+     * Mean constructor
+     */
+    constructor() {
+        super();
+
+        this.name = "Mean";
+        this.module = "Default";
+        this.description = "Computes the mean (average) of a number list. If an item in the string is not a number it is excluded from the list.<br><br>e.g. <code>0x0a 8 .5 .5</code> becomes <code>4.75</code>";
+        this.inputType = "string";
+        this.outputType = "BigNumber";
+        this.args = [
+            {
+                "name": "Delimiter",
+                "type": "option",
+                "value": ARITHMETIC_DELIM_OPTIONS,
+            }
+        ];
+    }
+
+    /**
+     * @param {string} input
+     * @param {Object[]} args
+     * @returns {BigNumber}
+     */
+    run(input, args) {
+        const val = mean(createNumArray(input, args[0]));
+        return val instanceof BigNumber ? val : new BigNumber(NaN);
+    }
+
+}
+
+export default Mean;

+ 50 - 0
src/core/operations/Median.mjs

@@ -0,0 +1,50 @@
+/**
+ * @author bwhitn [brian.m.whitney@outlook.com]
+ * @author d98762625 [d98762625@gmail.com]
+ * @copyright Crown Copyright 2018
+ * @license Apache-2.0
+ */
+
+import BigNumber from "bignumber.js";
+import Operation from "../Operation";
+import { median, createNumArray } from "../lib/Arithmetic";
+import { ARITHMETIC_DELIM_OPTIONS } from "../lib/Delim";
+
+/**
+ * Median operation
+ */
+class Median extends Operation {
+
+    /**
+     * Median constructor
+     */
+    constructor() {
+        super();
+
+        this.name = "Median";
+        this.module = "Default";
+        this.description = "Computes the median of a number list. If an item in the string is not a number it is excluded from the list.<br><br>e.g. <code>0x0a 8 1 .5</code> becomes <code>4.5</code>";
+        this.inputType = "string";
+        this.outputType = "BigNumber";
+        this.args = [
+            {
+                "name": "Delimiter",
+                "type": "option",
+                "value": ARITHMETIC_DELIM_OPTIONS,
+            }
+        ];
+    }
+
+    /**
+     * @param {string} input
+     * @param {Object[]} args
+     * @returns {BigNumber}
+     */
+    run(input, args) {
+        const val = median(createNumArray(input, args[0]));
+        return val instanceof BigNumber ? val : new BigNumber(NaN);
+    }
+
+}
+
+export default Median;

+ 51 - 0
src/core/operations/Multiply.mjs

@@ -0,0 +1,51 @@
+/**
+ * @author bwhitn [brian.m.whitney@outlook.com]
+ * @author d98762625 [d98762625@gmail.com]
+ * @copyright Crown Copyright 2018
+ * @license Apache-2.0
+ */
+
+import BigNumber from "bignumber.js";
+import Operation from "../Operation";
+import { multi, createNumArray } from "../lib/Arithmetic";
+import { ARITHMETIC_DELIM_OPTIONS } from "../lib/Delim";
+
+
+/**
+ * Multiply operation
+ */
+class Multiply extends Operation {
+
+    /**
+     * Multiply constructor
+     */
+    constructor() {
+        super();
+
+        this.name = "Multiply";
+        this.module = "Default";
+        this.description = "Multiplies a list of numbers. If an item in the string is not a number it is excluded from the list.<br><br>e.g. <code>0x0a 8 .5</code> becomes <code>40</code>";
+        this.inputType = "string";
+        this.outputType = "BigNumber";
+        this.args = [
+            {
+                "name": "Delimiter",
+                "type": "option",
+                "value": ARITHMETIC_DELIM_OPTIONS,
+            }
+        ];
+    }
+
+    /**
+     * @param {string} input
+     * @param {Object[]} args
+     * @returns {BigNumber}
+     */
+    run(input, args) {
+        const val = multi(createNumArray(input, args[0]));
+        return val instanceof BigNumber ? val : new BigNumber(NaN);
+    }
+
+}
+
+export default Multiply;

+ 52 - 0
src/core/operations/StandardDeviation.mjs

@@ -0,0 +1,52 @@
+/**
+ * @author bwhitn [brian.m.whitney@outlook.com]
+ * @author d98762625 [d98762625@gmail.com]
+ * @copyright Crown Copyright 2018
+ * @license Apache-2.0
+ */
+
+import BigNumber from "bignumber.js";
+import Operation from "../Operation";
+import { stdDev, createNumArray } from "../lib/Arithmetic";
+import { ARITHMETIC_DELIM_OPTIONS } from "../lib/Delim";
+
+
+/**
+ * Standard Deviation operation
+ */
+class StandardDeviation extends Operation {
+
+    /**
+     * StandardDeviation constructor
+     */
+    constructor() {
+        super();
+
+        this.name = "Standard Deviation";
+        this.module = "Default";
+        this.description = "Computes the standard deviation of a number list. If an item in the string is not a number it is excluded from the list.<br><br>e.g. <code>0x0a 8 .5</code> becomes <code>4.089281382128433</code>";
+        this.inputType = "string";
+        this.outputType = "BigNumber";
+        this.args = [
+            {
+                "name": "Delimiter",
+                "type": "option",
+                "value": ARITHMETIC_DELIM_OPTIONS,
+            }
+        ];
+    }
+
+    /**
+     * @param {string} input
+     * @param {Object[]} args
+     * @returns {BigNumber}
+     */
+    run(input, args) {
+        const val = stdDev(createNumArray(input, args[0]));
+        return val instanceof BigNumber ? val : new BigNumber(NaN);
+
+    }
+
+}
+
+export default StandardDeviation;

+ 51 - 0
src/core/operations/Subtract.mjs

@@ -0,0 +1,51 @@
+/**
+ * @author bwhitn [brian.m.whitney@outlook.com]
+ * @author d98762625 [d98762625@gmail.com]
+ * @copyright Crown Copyright 2018
+ * @license Apache-2.0
+ */
+
+import BigNumber from "bignumber.js";
+import Operation from "../Operation";
+import { sub, createNumArray } from "../lib/Arithmetic";
+import { ARITHMETIC_DELIM_OPTIONS } from "../lib/Delim";
+
+
+/**
+ * Subtract operation
+ */
+class Subtract extends Operation {
+
+    /**
+     * Subtract constructor
+     */
+    constructor() {
+        super();
+
+        this.name = "Subtract";
+        this.module = "Default";
+        this.description = "Subtracts a list of numbers. If an item in the string is not a number it is excluded from the list.<br><br>e.g. <code>0x0a 8 .5</code> becomes <code>1.5</code>";
+        this.inputType = "string";
+        this.outputType = "BigNumber";
+        this.args = [
+            {
+                "name": "Delimiter",
+                "type": "option",
+                "value": ARITHMETIC_DELIM_OPTIONS,
+            }
+        ];
+    }
+
+    /**
+     * @param {string} input
+     * @param {Object[]} args
+     * @returns {BigNumber}
+     */
+    run(input, args) {
+        const val = sub(createNumArray(input, args[0]));
+        return val instanceof BigNumber ? val : new BigNumber(NaN);
+    }
+
+}
+
+export default Subtract;

+ 50 - 0
src/core/operations/Sum.mjs

@@ -0,0 +1,50 @@
+/**
+ * @author bwhitn [brian.m.whitney@outlook.com]
+ * @copyright Crown Copyright 2016
+ * @license Apache-2.0
+ */
+
+import BigNumber from "bignumber.js";
+import Operation from "../Operation";
+import { sum, createNumArray } from "../lib/Arithmetic";
+import { ARITHMETIC_DELIM_OPTIONS } from "../lib/Delim";
+
+
+/**
+ * Sum operation
+ */
+class Sum extends Operation {
+
+    /**
+     * Sum constructor
+     */
+    constructor() {
+        super();
+
+        this.name = "Sum";
+        this.module = "Default";
+        this.description = "Adds together a list of numbers. If an item in the string is not a number it is excluded from the list.<br><br>e.g. <code>0x0a 8 .5</code> becomes <code>18.5</code>";
+        this.inputType = "string";
+        this.outputType = "BigNumber";
+        this.args = [
+            {
+                "name": "Delimiter",
+                "type": "option",
+                "value": ARITHMETIC_DELIM_OPTIONS,
+            }
+        ];
+    }
+
+    /**
+     * @param {string} input
+     * @param {Object[]} args
+     * @returns {BigNumber}
+     */
+    run(input, args) {
+        const val = sum(createNumArray(input, args[0]));
+        return val instanceof BigNumber ? val : new BigNumber(NaN);
+    }
+
+}
+
+export default Sum;

+ 6 - 0
src/core/operations/legacy/Arithmetic.js

@@ -1,3 +1,9 @@
+/**
+ * @author d98762625 [d98762625@gmail.com]
+ * @copyright Crown Copyright 2018
+ * @license Apache-2.0
+ */
+
 import Utils from "../Utils.js";
 import BigNumber from "bignumber.js";
 

部分文件因为文件数量过多而无法显示