2018-05-14 23:15:28 +02:00
/ * *
* @ author n1474335 [ n1474335 @ gmail . com ]
* @ copyright Crown Copyright 2016
* @ license Apache - 2.0
* /
2019-07-09 13:23:59 +02:00
import Operation from "../Operation.mjs" ;
2018-05-14 23:15:28 +02:00
import url from "url" ;
/ * *
* Parse URI operation
* /
class ParseURI extends Operation {
/ * *
* ParseURI constructor
* /
constructor ( ) {
super ( ) ;
this . name = "Parse URI" ;
this . module = "URL" ;
this . description = "Pretty prints complicated Uniform Resource Identifier (URI) strings for ease of reading. Particularly useful for Uniform Resource Locators (URLs) with a lot of arguments." ;
2018-08-21 20:07:13 +02:00
this . infoURL = "https://wikipedia.org/wiki/Uniform_Resource_Identifier" ;
2018-05-14 23:15:28 +02:00
this . inputType = "string" ;
this . outputType = "string" ;
this . args = [ ] ;
}
/ * *
* @ param { string } input
* @ param { Object [ ] } args
* @ returns { string }
* /
run ( input , args ) {
const uri = url . parse ( input , true ) ;
let output = "" ;
if ( uri . protocol ) output += "Protocol:\t" + uri . protocol + "\n" ;
if ( uri . auth ) output += "Auth:\t\t" + uri . auth + "\n" ;
if ( uri . hostname ) output += "Hostname:\t" + uri . hostname + "\n" ;
if ( uri . port ) output += "Port:\t\t" + uri . port + "\n" ;
if ( uri . pathname ) output += "Path name:\t" + uri . pathname + "\n" ;
if ( uri . query ) {
const keys = Object . keys ( uri . query ) ;
let padding = 0 ;
keys . forEach ( k => {
padding = ( k . length > padding ) ? k . length : padding ;
} ) ;
output += "Arguments:\n" ;
for ( const key in uri . query ) {
output += "\t" + key . padEnd ( padding , " " ) ;
if ( uri . query [ key ] . length ) {
output += " = " + uri . query [ key ] + "\n" ;
} else {
output += "\n" ;
}
}
}
if ( uri . hash ) output += "Hash:\t\t" + uri . hash + "\n" ;
return output ;
}
}
export default ParseURI ;