mirror of
https://github.com/gchq/CyberChef.git
synced 2024-11-16 08:58:30 +01:00
68 lines
1.7 KiB
JavaScript
68 lines
1.7 KiB
JavaScript
/**
|
|
* @author n1474335 [n1474335@gmail.com]
|
|
* @copyright Crown Copyright 2016
|
|
* @license Apache-2.0
|
|
*/
|
|
|
|
import Operation from "../Operation.mjs";
|
|
import Utils from "../Utils.mjs";
|
|
import {INPUT_DELIM_OPTIONS} from "../lib/Delim.mjs";
|
|
|
|
/**
|
|
* Head operation
|
|
*/
|
|
class Head extends Operation {
|
|
|
|
/**
|
|
* Head constructor
|
|
*/
|
|
constructor() {
|
|
super();
|
|
|
|
this.name = "Head";
|
|
this.module = "Default";
|
|
this.description = "Like the UNIX head utility.<br>Gets the first n lines.<br>You can select all but the last n lines 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.";
|
|
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 <= splitInput.length + number;
|
|
} else {
|
|
return lineIndex <= number;
|
|
}
|
|
})
|
|
.join(delimiter);
|
|
}
|
|
|
|
}
|
|
|
|
export default Head;
|