浏览代码

ESM: Ported SeqUtils operations

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

+ 46 - 0
src/core/operations/AddLineNumbers.mjs

@@ -0,0 +1,46 @@
+/**
+ * @author n1474335 [n1474335@gmail.com]
+ * @copyright Crown Copyright 2016
+ * @license Apache-2.0
+ */
+
+import Operation from "../Operation";
+
+/**
+ * Add line numbers operation
+ */
+class AddLineNumbers extends Operation {
+
+    /**
+     * AddLineNumbers constructor
+     */
+    constructor() {
+        super();
+
+        this.name = "Add line numbers";
+        this.module = "Default";
+        this.description = "Adds line numbers to the output.";
+        this.inputType = "string";
+        this.outputType = "string";
+        this.args = [];
+    }
+
+    /**
+     * @param {string} input
+     * @param {Object[]} args
+     * @returns {string}
+     */
+    run(input, args) {
+        const lines = input.split("\n"),
+            width = lines.length.toString().length;
+        let output = "";
+
+        for (let n = 0; n < lines.length; n++) {
+            output += (n+1).toString().padStart(width, " ") + " " + lines[n] + "\n";
+        }
+        return output.slice(0, output.length-1);
+    }
+
+}
+
+export default AddLineNumbers;

+ 65 - 0
src/core/operations/CountOccurrences.mjs

@@ -0,0 +1,65 @@
+/**
+ * @author n1474335 [n1474335@gmail.com]
+ * @copyright Crown Copyright 2016
+ * @license Apache-2.0
+ */
+
+import Operation from "../Operation";
+import Utils from "../Utils";
+
+/**
+ * Count occurrences operation
+ */
+class CountOccurrences extends Operation {
+
+    /**
+     * CountOccurrences constructor
+     */
+    constructor() {
+        super();
+
+        this.name = "Count occurrences";
+        this.module = "Default";
+        this.description = "Counts the number of times the provided string occurs in the input.";
+        this.inputType = "string";
+        this.outputType = "number";
+        this.args = [
+            {
+                "name": "Search string",
+                "type": "toggleString",
+                "value": "",
+                "toggleValues": ["Regex", "Extended (\\n, \\t, \\x...)", "Simple string"]
+            }
+        ];
+    }
+
+    /**
+     * @param {string} input
+     * @param {Object[]} args
+     * @returns {number}
+     */
+    run(input, args) {
+        let search = args[0].string;
+        const type = args[0].option;
+
+        if (type === "Regex" && search) {
+            try {
+                const regex = new RegExp(search, "gi"),
+                    matches = input.match(regex);
+                return matches.length;
+            } catch (err) {
+                return 0;
+            }
+        } else if (search) {
+            if (type.indexOf("Extended") === 0) {
+                search = Utils.parseEscapedChars(search);
+            }
+            return input.count(search);
+        } else {
+            return 0;
+        }
+    }
+
+}
+
+export default CountOccurrences;

+ 46 - 0
src/core/operations/ExpandAlphabetRange.mjs

@@ -0,0 +1,46 @@
+/**
+ * @author n1474335 [n1474335@gmail.com]
+ * @copyright Crown Copyright 2016
+ * @license Apache-2.0
+ */
+
+import Operation from "../Operation";
+import Utils from "../Utils";
+
+/**
+ * Expand alphabet range operation
+ */
+class ExpandAlphabetRange extends Operation {
+
+    /**
+     * ExpandAlphabetRange constructor
+     */
+    constructor() {
+        super();
+
+        this.name = "Expand alphabet range";
+        this.module = "Default";
+        this.description = "Expand an alphabet range string into a list of the characters in that range.<br><br>e.g. <code>a-z</code> becomes <code>abcdefghijklmnopqrstuvwxyz</code>.";
+        this.inputType = "string";
+        this.outputType = "string";
+        this.args = [
+            {
+                "name": "Delimiter",
+                "type": "binaryString",
+                "value": ""
+            }
+        ];
+    }
+
+    /**
+     * @param {string} input
+     * @param {Object[]} args
+     * @returns {string}
+     */
+    run(input, args) {
+        return Utils.expandAlphRange(input).join(args[0]);
+    }
+
+}
+
+export default ExpandAlphabetRange;

+ 39 - 0
src/core/operations/RemoveLineNumbers.mjs

@@ -0,0 +1,39 @@
+/**
+ * @author n1474335 [n1474335@gmail.com]
+ * @copyright Crown Copyright 2016
+ * @license Apache-2.0
+ */
+
+import Operation from "../Operation";
+
+/**
+ * Remove line numbers operation
+ */
+class RemoveLineNumbers extends Operation {
+
+    /**
+     * RemoveLineNumbers constructor
+     */
+    constructor() {
+        super();
+
+        this.name = "Remove line numbers";
+        this.module = "Default";
+        this.description = "Removes line numbers from the output if they can be trivially detected.";
+        this.inputType = "string";
+        this.outputType = "string";
+        this.args = [];
+    }
+
+    /**
+     * @param {string} input
+     * @param {Object[]} args
+     * @returns {string}
+     */
+    run(input, args) {
+        return input.replace(/^[ \t]{0,5}\d+[\s:|\-,.)\]]/gm, "");
+    }
+
+}
+
+export default RemoveLineNumbers;

+ 67 - 0
src/core/operations/Reverse.mjs

@@ -0,0 +1,67 @@
+/**
+ * @author n1474335 [n1474335@gmail.com]
+ * @copyright Crown Copyright 2016
+ * @license Apache-2.0
+ */
+
+import Operation from "../Operation";
+
+/**
+ * Reverse operation
+ */
+class Reverse extends Operation {
+
+    /**
+     * Reverse constructor
+     */
+    constructor() {
+        super();
+
+        this.name = "Reverse";
+        this.module = "Default";
+        this.description = "Reverses the input string.";
+        this.inputType = "byteArray";
+        this.outputType = "byteArray";
+        this.args = [
+            {
+                "name": "By",
+                "type": "option",
+                "value": ["Character", "Line"]
+            }
+        ];
+    }
+
+    /**
+     * @param {byteArray} input
+     * @param {Object[]} args
+     * @returns {byteArray}
+     */
+    run(input, args) {
+        let i;
+        if (args[0] === "Line") {
+            const lines = [];
+            let line = [],
+                result = [];
+            for (i = 0; i < input.length; i++) {
+                if (input[i] === 0x0a) {
+                    lines.push(line);
+                    line = [];
+                } else {
+                    line.push(input[i]);
+                }
+            }
+            lines.push(line);
+            lines.reverse();
+            for (i = 0; i < lines.length; i++) {
+                result = result.concat(lines[i]);
+                result.push(0x0a);
+            }
+            return result.slice(0, input.length);
+        } else {
+            return input.reverse();
+        }
+    }
+
+}
+
+export default Reverse;

+ 136 - 0
src/core/operations/Sort.mjs

@@ -0,0 +1,136 @@
+/**
+ * @author n1474335 [n1474335@gmail.com]
+ * @copyright Crown Copyright 2016
+ * @license Apache-2.0
+ */
+
+import Operation from "../Operation";
+import Utils from "../Utils";
+import {INPUT_DELIM_OPTIONS} from "../lib/Delim";
+
+/**
+ * Sort operation
+ */
+class Sort extends Operation {
+
+    /**
+     * Sort constructor
+     */
+    constructor() {
+        super();
+
+        this.name = "Sort";
+        this.module = "Default";
+        this.description = "Alphabetically sorts strings separated by the specified delimiter.<br><br>The IP address option supports IPv4 only.";
+        this.inputType = "string";
+        this.outputType = "string";
+        this.args = [
+            {
+                "name": "Delimiter",
+                "type": "option",
+                "value": INPUT_DELIM_OPTIONS
+            },
+            {
+                "name": "Reverse",
+                "type": "boolean",
+                "value": false
+            },
+            {
+                "name": "Order",
+                "type": "option",
+                "value": ["Alphabetical (case sensitive)", "Alphabetical (case insensitive)", "IP address", "Numeric"]
+            }
+        ];
+    }
+
+    /**
+     * @param {string} input
+     * @param {Object[]} args
+     * @returns {string}
+     */
+    run(input, args) {
+        const delim = Utils.charRep(args[0]),
+            sortReverse = args[1],
+            order = args[2];
+        let sorted = input.split(delim);
+
+        if (order === "Alphabetical (case sensitive)") {
+            sorted = sorted.sort();
+        } else if (order === "Alphabetical (case insensitive)") {
+            sorted = sorted.sort(Sort._caseInsensitiveSort);
+        } else if (order === "IP address") {
+            sorted = sorted.sort(Sort._ipSort);
+        } else if (order === "Numeric") {
+            sorted = sorted.sort(Sort._numericSort);
+        }
+
+        if (sortReverse) sorted.reverse();
+        return sorted.join(delim);
+    }
+
+    /**
+     * Comparison operation for sorting of strings ignoring case.
+     *
+     * @private
+     * @param {string} a
+     * @param {string} b
+     * @returns {number}
+     */
+    static _caseInsensitiveSort(a, b) {
+        return a.toLowerCase().localeCompare(b.toLowerCase());
+    }
+
+
+    /**
+     * Comparison operation for sorting of IPv4 addresses.
+     *
+     * @private
+     * @param {string} a
+     * @param {string} b
+     * @returns {number}
+     */
+    static _ipSort(a, b) {
+        let a_ = a.split("."),
+            b_ = b.split(".");
+
+        a_ = a_[0] * 0x1000000 + a_[1] * 0x10000 + a_[2] * 0x100 + a_[3] * 1;
+        b_ = b_[0] * 0x1000000 + b_[1] * 0x10000 + b_[2] * 0x100 + b_[3] * 1;
+
+        if (isNaN(a_) && !isNaN(b_)) return 1;
+        if (!isNaN(a_) && isNaN(b_)) return -1;
+        if (isNaN(a_) && isNaN(b_)) return a.localeCompare(b);
+
+        return a_ - b_;
+    }
+
+    /**
+     * Comparison operation for sorting of numeric values.
+     *
+     * @author Chris van Marle
+     * @private
+     * @param {string} a
+     * @param {string} b
+     * @returns {number}
+     */
+    static _numericSort(a, b) {
+        const a_ = a.split(/([^\d]+)/),
+            b_ = b.split(/([^\d]+)/);
+
+        for (let i = 0; i < a_.length && i < b.length; ++i) {
+            if (isNaN(a_[i]) && !isNaN(b_[i])) return 1; // Numbers after non-numbers
+            if (!isNaN(a_[i]) && isNaN(b_[i])) return -1;
+            if (isNaN(a_[i]) && isNaN(b_[i])) {
+                const ret = a_[i].localeCompare(b_[i]); // Compare strings
+                if (ret !== 0) return ret;
+            }
+            if (!isNaN(a_[i]) && !isNaN(a_[i])) { // Compare numbers
+                if (a_[i] - b_[i] !== 0) return a_[i] - b_[i];
+            }
+        }
+
+        return a.localeCompare(b);
+    }
+
+}
+
+export default Sort;

+ 48 - 0
src/core/operations/Unique.mjs

@@ -0,0 +1,48 @@
+/**
+ * @author n1474335 [n1474335@gmail.com]
+ * @copyright Crown Copyright 2016
+ * @license Apache-2.0
+ */
+
+import Operation from "../Operation";
+import Utils from "../Utils";
+import {INPUT_DELIM_OPTIONS} from "../lib/Delim";
+
+/**
+ * Unique operation
+ */
+class Unique extends Operation {
+
+    /**
+     * Unique constructor
+     */
+    constructor() {
+        super();
+
+        this.name = "Unique";
+        this.module = "Default";
+        this.description = "Removes duplicate strings from the input.";
+        this.inputType = "string";
+        this.outputType = "string";
+        this.args = [
+            {
+                "name": "Delimiter",
+                "type": "option",
+                "value": INPUT_DELIM_OPTIONS
+            }
+        ];
+    }
+
+    /**
+     * @param {string} input
+     * @param {Object[]} args
+     * @returns {string}
+     */
+    run(input, args) {
+        const delim = Utils.charRep(args[0]);
+        return input.split(delim).unique().join(delim);
+    }
+
+}
+
+export default Unique;