Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat(InsertDelimiter) #1885

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/core/config/Categories.json
Original file line number Diff line number Diff line change
Expand Up @@ -320,7 +320,8 @@
"Unescape string",
"Pseudo-Random Number Generator",
"Sleep",
"File Tree"
"File Tree",
"Insert Delimiter"
]
},
{
Expand Down
54 changes: 54 additions & 0 deletions src/core/operations/InsertDelimiter.mjs
Original file line number Diff line number Diff line change
@@ -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], 10)}}`, "g")).join(args[1]);
}
}

export default InsertDelimiter;
1 change: 1 addition & 0 deletions tests/operations/index.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ import "./tests/Hexdump.mjs";
import "./tests/HKDF.mjs";
import "./tests/Image.mjs";
import "./tests/IndexOfCoincidence.mjs";
import "./tests/InsertDelimiter.mjs";
import "./tests/JA3Fingerprint.mjs";
import "./tests/JA4.mjs";
import "./tests/JA3SFingerprint.mjs";
Expand Down
65 changes: 65 additions & 0 deletions tests/operations/tests/InsertDelimiter.mjs
Original file line number Diff line number Diff line change
@@ -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