mirror of
https://github.com/gchq/CyberChef.git
synced 2025-04-20 14:56:19 -04:00
Adding operation to insert a delimiter at a given interval
This commit is contained in:
parent
d635cca210
commit
5cf7f823e6
4 changed files with 122 additions and 1 deletions
|
@ -320,7 +320,8 @@
|
||||||
"Unescape string",
|
"Unescape string",
|
||||||
"Pseudo-Random Number Generator",
|
"Pseudo-Random Number Generator",
|
||||||
"Sleep",
|
"Sleep",
|
||||||
"File Tree"
|
"File Tree",
|
||||||
|
"Insert Delimiter"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
|
|
54
src/core/operations/InsertDelimiter.mjs
Normal file
54
src/core/operations/InsertDelimiter.mjs
Normal file
|
@ -0,0 +1,54 @@
|
||||||
|
/**
|
||||||
|
* @author 0xff1ce [github.com/0xff1ce]
|
||||||
|
* @copyright Crown Copyright 2024
|
||||||
|
* @license Apache-2.0
|
||||||
|
*/
|
||||||
|
|
||||||
|
import Operation from "../Operation.mjs";
|
||||||
|
|
||||||
|
/**
|
||||||
|
* InsertDelimiter operation
|
||||||
|
*/
|
||||||
|
class InsertDelimiter extends Operation {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* InsertDelimiter constructor
|
||||||
|
*/
|
||||||
|
constructor() {
|
||||||
|
super();
|
||||||
|
this.name = "Insert Delimiter";
|
||||||
|
this.module = "Default";
|
||||||
|
this.description = "Inserts a given delimiter at regular intervals within the input string.";
|
||||||
|
this.inputType = "string";
|
||||||
|
this.outputType = "string";
|
||||||
|
this.args = [
|
||||||
|
{
|
||||||
|
"name": "Interval",
|
||||||
|
"type": "number",
|
||||||
|
"value": 8, // Default interval is 8
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "Delimiter",
|
||||||
|
"type": "string",
|
||||||
|
"value": " ", // Default delimiter is a space
|
||||||
|
}
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param {string} input
|
||||||
|
* @param {Object[]} args
|
||||||
|
* @returns {string}
|
||||||
|
*/
|
||||||
|
run(input, args) {
|
||||||
|
if (!input) return ""
|
||||||
|
|
||||||
|
if (isNaN(args[0]) || args[0] <= 0) {
|
||||||
|
return "Invalid interval: must be a positive integer.";
|
||||||
|
}
|
||||||
|
|
||||||
|
return input.match(new RegExp(`.{1,${parseInt(args[0])}}`, 'g')).join(args[1]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export default InsertDelimiter;
|
|
@ -83,6 +83,7 @@ import "./tests/Hexdump.mjs";
|
||||||
import "./tests/HKDF.mjs";
|
import "./tests/HKDF.mjs";
|
||||||
import "./tests/Image.mjs";
|
import "./tests/Image.mjs";
|
||||||
import "./tests/IndexOfCoincidence.mjs";
|
import "./tests/IndexOfCoincidence.mjs";
|
||||||
|
import "./tests/InsertDelimiter.mjs";
|
||||||
import "./tests/JA3Fingerprint.mjs";
|
import "./tests/JA3Fingerprint.mjs";
|
||||||
import "./tests/JA4.mjs";
|
import "./tests/JA4.mjs";
|
||||||
import "./tests/JA3SFingerprint.mjs";
|
import "./tests/JA3SFingerprint.mjs";
|
||||||
|
|
65
tests/operations/tests/InsertDelimiter.mjs
Normal file
65
tests/operations/tests/InsertDelimiter.mjs
Normal file
|
@ -0,0 +1,65 @@
|
||||||
|
/**
|
||||||
|
* @author 0xff1ce [github.com/0xff1ce]
|
||||||
|
* @copyright Crown Copyright 2024
|
||||||
|
* @license Apache-2.0
|
||||||
|
*/
|
||||||
|
|
||||||
|
import TestRegister from "../../lib/TestRegister.mjs";
|
||||||
|
|
||||||
|
TestRegister.addTests([
|
||||||
|
{
|
||||||
|
name: "Insert space every 8 characters",
|
||||||
|
input: "010010000110010101101100011011000110111100100000010101110110111101110010011011000110010000100001",
|
||||||
|
expectedOutput: "01001000 01100101 01101100 01101100 01101111 00100000 01010111 01101111 01110010 01101100 01100100 00100001",
|
||||||
|
recipeConfig: [
|
||||||
|
{
|
||||||
|
"op": "Insert Delimiter",
|
||||||
|
"args": [8, " "]
|
||||||
|
},
|
||||||
|
],
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "Insert newline every 4 characters",
|
||||||
|
input: "ABCDEFGHIJKL",
|
||||||
|
expectedOutput: "ABCD\nEFGH\nIJKL",
|
||||||
|
recipeConfig: [
|
||||||
|
{
|
||||||
|
"op": "Insert Delimiter",
|
||||||
|
"args": [4, "\n"]
|
||||||
|
},
|
||||||
|
],
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "Insert hyphen every 3 characters",
|
||||||
|
input: "1234567890",
|
||||||
|
expectedOutput: "123-456-789-0",
|
||||||
|
recipeConfig: [
|
||||||
|
{
|
||||||
|
"op": "Insert Delimiter",
|
||||||
|
"args": [3, "-"]
|
||||||
|
},
|
||||||
|
],
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "Use a float as delimiter",
|
||||||
|
input: "1234567890",
|
||||||
|
expectedOutput: "123-456-789-0",
|
||||||
|
recipeConfig: [
|
||||||
|
{
|
||||||
|
"op": "Insert Delimiter",
|
||||||
|
"args": [3.4, "-"]
|
||||||
|
},
|
||||||
|
],
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "Handle empty input gracefully",
|
||||||
|
input: "",
|
||||||
|
expectedOutput: "",
|
||||||
|
recipeConfig: [
|
||||||
|
{
|
||||||
|
"op": "Insert Delimiter",
|
||||||
|
"args": [8, " "]
|
||||||
|
},
|
||||||
|
],
|
||||||
|
}
|
||||||
|
]);
|
Loading…
Add table
Add a link
Reference in a new issue