mirror of
https://github.com/gchq/CyberChef.git
synced 2025-06-13 17:55:27 -04:00
Create run implementation
This commit is contained in:
parent
3cd45c5f68
commit
81e2b5f9ac
1 changed files with 38 additions and 0 deletions
|
@ -5,6 +5,7 @@
|
|||
*/
|
||||
|
||||
import Operation from "../Operation.mjs";
|
||||
import Utils from "../Utils.mjs";
|
||||
|
||||
|
||||
/**
|
||||
|
@ -36,6 +37,43 @@ class LineBreak extends Operation {
|
|||
}
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {ArrayBuffer} input
|
||||
* @param {Object[]} args
|
||||
* @returns {string}
|
||||
*/
|
||||
run(input, args) {
|
||||
const lines = [];
|
||||
const lineWidth = args[0];
|
||||
const removeLeading = args[1];
|
||||
const msg = Utils.arrayBufferToStr(input, false);
|
||||
let i = 0;
|
||||
|
||||
while (i < msg.length) {
|
||||
let slice = msg.slice(i, i + lineWidth);
|
||||
let leadingWhitespace = 0;
|
||||
|
||||
if (removeLeading) {
|
||||
const match = slice.match(/^\s*/);
|
||||
leadingWhitespace = match ? match[0].length : 0;
|
||||
slice = slice.trimStart();
|
||||
}
|
||||
|
||||
slice = msg.slice(i, i + lineWidth + leadingWhitespace);
|
||||
|
||||
if (removeLeading) {
|
||||
slice = slice.trimStart();
|
||||
}
|
||||
|
||||
i += lineWidth + leadingWhitespace;
|
||||
|
||||
if (slice.length === 0) continue;
|
||||
lines.push(slice);
|
||||
}
|
||||
|
||||
return lines.join("\n");
|
||||
}
|
||||
}
|
||||
|
||||
export default LineBreak;
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue