mirror of
https://github.com/gchq/CyberChef.git
synced 2024-11-16 08:58:30 +01:00
71 lines
1.7 KiB
JavaScript
71 lines
1.7 KiB
JavaScript
|
/**
|
||
|
* @author n1474335 [n1474335@gmail.com]
|
||
|
* @copyright Crown Copyright 2016
|
||
|
* @license Apache-2.0
|
||
|
*/
|
||
|
|
||
|
import Operation from "../Operation";
|
||
|
|
||
|
/**
|
||
|
* Pad lines operation
|
||
|
*/
|
||
|
class PadLines extends Operation {
|
||
|
|
||
|
/**
|
||
|
* PadLines constructor
|
||
|
*/
|
||
|
constructor() {
|
||
|
super();
|
||
|
|
||
|
this.name = "Pad lines";
|
||
|
this.module = "Default";
|
||
|
this.description = "Add the specified number of the specified character to the beginning or end of each line";
|
||
|
this.inputType = "string";
|
||
|
this.outputType = "string";
|
||
|
this.args = [
|
||
|
{
|
||
|
"name": "Position",
|
||
|
"type": "option",
|
||
|
"value": ["Start", "End"]
|
||
|
},
|
||
|
{
|
||
|
"name": "Length",
|
||
|
"type": "number",
|
||
|
"value": 5
|
||
|
},
|
||
|
{
|
||
|
"name": "Character",
|
||
|
"type": "binaryShortString",
|
||
|
"value": " "
|
||
|
}
|
||
|
];
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* @param {string} input
|
||
|
* @param {Object[]} args
|
||
|
* @returns {string}
|
||
|
*/
|
||
|
run(input, args) {
|
||
|
const [position, len, chr] = args,
|
||
|
lines = input.split("\n");
|
||
|
let output = "",
|
||
|
i = 0;
|
||
|
|
||
|
if (position === "Start") {
|
||
|
for (i = 0; i < lines.length; i++) {
|
||
|
output += lines[i].padStart(lines[i].length+len, chr) + "\n";
|
||
|
}
|
||
|
} else if (position === "End") {
|
||
|
for (i = 0; i < lines.length; i++) {
|
||
|
output += lines[i].padEnd(lines[i].length+len, chr) + "\n";
|
||
|
}
|
||
|
}
|
||
|
|
||
|
return output.slice(0, output.length-1);
|
||
|
}
|
||
|
|
||
|
}
|
||
|
|
||
|
export default PadLines;
|