From 4b5210585a8d0bb085b959d3d0ef27cbee48ec3a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mike=20Schw=C3=B6rer?= Date: Sat, 17 Dec 2016 01:53:06 +0100 Subject: [PATCH] Added operation 'filter' --- src/js/config/Categories.js | 1 + src/js/config/OperationConfig.js | 23 +++++++++++++++++++++++ src/js/operations/StrUtils.js | 26 ++++++++++++++++++++++++++ 3 files changed, 50 insertions(+) diff --git a/src/js/config/Categories.js b/src/js/config/Categories.js index f75a5484..304b006c 100755 --- a/src/js/config/Categories.js +++ b/src/js/config/Categories.js @@ -148,6 +148,7 @@ const Categories = [ "Sort", "Unique", "Split", + "Filter", "Count occurrences", "Expand alphabet range", "Parse escaped string", diff --git a/src/js/config/OperationConfig.js b/src/js/config/OperationConfig.js index 5591a43c..52f7a6a9 100755 --- a/src/js/config/OperationConfig.js +++ b/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, diff --git a/src/js/operations/StrUtils.js b/src/js/operations/StrUtils.js index e0d10105..71d3436a 100755 --- a/src/js/operations/StrUtils.js +++ b/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); + }, /**