2018-05-14 16:31:04 +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" ;
import Utils from "../Utils.mjs" ;
import { INPUT _DELIM _OPTIONS } from "../lib/Delim.mjs" ;
2018-05-14 16:31:04 +02:00
/ * *
* Tail operation
* /
class Tail extends Operation {
/ * *
* Tail constructor
* /
constructor ( ) {
super ( ) ;
this . name = "Tail" ;
this . module = "Default" ;
this . description = "Like the UNIX tail utility.<br>Gets the last n lines.<br>Optionally you can select all lines after line n by entering a negative value for n.<br>The delimiter can be changed so that instead of lines, fields (i.e. commas) are selected instead." ;
2018-08-21 20:07:13 +02:00
this . infoURL = "https://wikipedia.org/wiki/Tail_(Unix)" ;
2018-05-14 16:31:04 +02:00
this . inputType = "string" ;
this . outputType = "string" ;
this . args = [
{
"name" : "Delimiter" ,
"type" : "option" ,
"value" : INPUT _DELIM _OPTIONS
} ,
{
"name" : "Number" ,
"type" : "number" ,
"value" : 10
}
] ;
}
/ * *
* @ param { string } input
* @ param { Object [ ] } args
* @ returns { string }
* /
run ( input , args ) {
let delimiter = args [ 0 ] ;
const number = args [ 1 ] ;
delimiter = Utils . charRep ( delimiter ) ;
const splitInput = input . split ( delimiter ) ;
return splitInput
. filter ( ( line , lineIndex ) => {
lineIndex += 1 ;
if ( number < 0 ) {
return lineIndex > - number ;
} else {
return lineIndex > splitInput . length - number ;
}
} )
. join ( delimiter ) ;
}
}
export default Tail ;