2018-05-15 16:30:17 +01:00
/**
* @author n1474335 [n1474335@gmail.com]
* @copyright Crown Copyright 2016
* @license Apache-2.0
*/
2019-07-09 12:23:59 +01:00
import Operation from "../Operation.mjs" ;
import { search , URL_REGEX } from "../lib/Extract.mjs" ;
2022-04-14 18:08:16 +01:00
import { caseInsensitiveSort } from "../lib/Sort.mjs" ;
2018-05-15 16:30:17 +01:00
/**
* Extract URLs operation
*/
class ExtractURLs extends Operation {
/**
* ExtractURLs constructor
*/
constructor () {
super ();
this . name = "Extract URLs" ;
this . module = "Regex" ;
this . description = "Extracts Uniform Resource Locators (URLs) from the input. The protocol (http, ftp etc.) is required otherwise there will be far too many false positives." ;
this . inputType = "string" ;
this . outputType = "string" ;
this . args = [
{
2022-04-14 18:08:16 +01:00
name : "Display total" ,
type : "boolean" ,
value : false
},
{
name : "Sort" ,
type : "boolean" ,
value : false
},
{
name : "Unique" ,
type : "boolean" ,
value : false
2018-05-15 16:30:17 +01:00
}
];
}
/**
* @param {string} input
* @param {Object[]} args
* @returns {string}
*/
run ( input , args ) {
2022-04-14 18:08:16 +01:00
const [ displayTotal , sort , unique ] = args ;
const results = search (
input ,
URL_REGEX ,
null ,
sort ? caseInsensitiveSort : null ,
unique
);
if ( displayTotal ) {
return `Total found: ${ results . length } \n\n ${ results . join ( "\n" ) } ` ;
} else {
return results . join ( "\n" );
}
2018-05-15 16:30:17 +01:00
}
}
export default ExtractURLs ;