2018-05-14 16:31:04 +02:00
|
|
|
/**
|
|
|
|
* @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";
|
2018-05-15 19:01:04 +02:00
|
|
|
import OperationError from "../errors/OperationError";
|
2018-08-23 22:41:57 +02:00
|
|
|
import XRegExp from "xregexp";
|
2018-05-14 16:31:04 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Filter operation
|
|
|
|
*/
|
|
|
|
class Filter extends Operation {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Filter constructor
|
|
|
|
*/
|
|
|
|
constructor() {
|
|
|
|
super();
|
|
|
|
|
|
|
|
this.name = "Filter";
|
2018-08-23 22:41:57 +02:00
|
|
|
this.module = "Regex";
|
2018-05-14 16:31:04 +02:00
|
|
|
this.description = "Splits up the input using the specified delimiter and then filters each branch based on a regular expression.";
|
|
|
|
this.inputType = "string";
|
|
|
|
this.outputType = "string";
|
|
|
|
this.args = [
|
|
|
|
{
|
|
|
|
"name": "Delimiter",
|
|
|
|
"type": "option",
|
|
|
|
"value": INPUT_DELIM_OPTIONS
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"name": "Regex",
|
|
|
|
"type": "string",
|
|
|
|
"value": ""
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"name": "Invert condition",
|
|
|
|
"type": "boolean",
|
|
|
|
"value": false
|
|
|
|
}
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param {string} input
|
|
|
|
* @param {Object[]} args
|
|
|
|
* @returns {string}
|
|
|
|
*/
|
|
|
|
run(input, args) {
|
|
|
|
const delim = Utils.charRep(args[0]),
|
|
|
|
reverse = args[2];
|
|
|
|
let regex;
|
|
|
|
|
|
|
|
try {
|
2018-08-23 22:41:57 +02:00
|
|
|
regex = new XRegExp(args[1]);
|
2018-05-14 16:31:04 +02:00
|
|
|
} catch (err) {
|
2018-05-15 19:01:04 +02:00
|
|
|
throw new OperationError(`Invalid regex. Details: ${err.message}`);
|
2018-05-14 16:31:04 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
const regexFilter = function(value) {
|
|
|
|
return reverse ^ regex.test(value);
|
|
|
|
};
|
|
|
|
|
|
|
|
return input.split(delim).filter(regexFilter).join(delim);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
export default Filter;
|