73 lines
1.6 KiB
JavaScript
73 lines
1.6 KiB
JavaScript
/**
|
|
* @author n1474335 [n1474335@gmail.com]
|
|
* @copyright Crown Copyright 2016
|
|
* @license Apache-2.0
|
|
*/
|
|
|
|
import Operation from "../Operation.mjs";
|
|
import { EMAIL_REGEX, search } from "../lib/Extract.mjs";
|
|
import { caseInsensitiveSort } from "../lib/Sort.mjs";
|
|
|
|
/**
|
|
* Extract email addresses operation
|
|
*/
|
|
class ExtractEmailAddresses extends Operation {
|
|
|
|
/**
|
|
* ExtractEmailAddresses constructor
|
|
*/
|
|
constructor() {
|
|
super();
|
|
|
|
this.name = "Extract email addresses";
|
|
this.module = "Regex";
|
|
this.description = "Extracts all email addresses from the input.";
|
|
this.inputType = "string";
|
|
this.outputType = "string";
|
|
this.args = [
|
|
{
|
|
name: "Display total",
|
|
type: "boolean",
|
|
value: false
|
|
},
|
|
{
|
|
name: "Sort",
|
|
type: "boolean",
|
|
value: false
|
|
},
|
|
{
|
|
name: "Unique",
|
|
type: "boolean",
|
|
value: false
|
|
}
|
|
];
|
|
}
|
|
|
|
/**
|
|
* @param {string} input
|
|
* @param {Object[]} args
|
|
* @returns {string}
|
|
*/
|
|
run(input, args) {
|
|
const [displayTotal, sort, unique] = args,
|
|
regex = EMAIL_REGEX;
|
|
|
|
const results = search(
|
|
input,
|
|
regex,
|
|
null,
|
|
sort ? caseInsensitiveSort : null,
|
|
unique
|
|
);
|
|
|
|
if (displayTotal) {
|
|
return `Total found: ${results.length}\n\n${results.join("\n")}`;
|
|
} else {
|
|
return results.join("\n");
|
|
}
|
|
}
|
|
|
|
}
|
|
|
|
export default ExtractEmailAddresses;
|