瀏覽代碼

Added operation 'filter'

Mike Schwörer 8 年之前
父節點
當前提交
4b5210585a
共有 3 個文件被更改,包括 50 次插入0 次删除
  1. 1 0
      src/js/config/Categories.js
  2. 23 0
      src/js/config/OperationConfig.js
  3. 26 0
      src/js/operations/StrUtils.js

+ 1 - 0
src/js/config/Categories.js

@@ -148,6 +148,7 @@ const Categories = [
             "Sort",
             "Unique",
             "Split",
+            "Filter",
             "Count occurrences",
             "Expand alphabet range",
             "Parse escaped string",

+ 23 - 0
src/js/config/OperationConfig.js

@@ -1764,6 +1764,29 @@ const OperationConfig = {
             }
         ]
     },
+    "Filter": {
+        description: "Filter the string with an regular expression",
+        run: StrUtils.run_filter,
+        input_type: "string",
+        output_type: "string",
+        args: [
+            {
+                name: "Delimiter",
+                type: "option",
+                value: StrUtils.DELIMITER_OPTIONS
+            },
+            {
+                name: "Regex",
+                type: "string",
+                value: ""
+            },
+            {
+                name: "Invert condition",
+                type: "boolean",
+                value: SeqUtils.SORT_REVERSE
+            },
+        ]
+    },
     "Strings": {
         description: "Extracts all strings from the input.",
         run: Extract.run_strings,

+ 26 - 0
src/js/operations/StrUtils.js

@@ -261,6 +261,32 @@ var StrUtils = {
             
         return sections.join(join_delim);
     },
+
+    /**
+     * Filter by regex operation.
+     *
+     * @author Mikescher (https://github.com/Mikescher | https://mikescher.com)
+     *
+     * @param {string} input
+     * @param {Object[]} args
+     * @returns {string}
+     */
+    run_filter: function(input, args) {
+        var delim = Utils.char_rep[args[0]];
+        var reverse = args[2];
+
+        try {
+            var regex = new RegExp(args[1]);
+        } catch (err) {
+            return "Invalid regex. Details: " + err.message;
+        }
+
+        const regex_filter = function(value) {
+            return reverse ^ regex.test(value);
+        }
+
+        return input.split(delim).filter(regex_filter).join(delim);
+    },
     
     
     /**