This commit is contained in:
n1474335 2023-03-23 17:46:47 +00:00
commit 1bba10e7c2
No known key found for this signature in database
GPG key ID: D15457B7B4AF3F37
4 changed files with 111 additions and 0 deletions

View file

@ -247,6 +247,7 @@
"Remove null bytes", "Remove null bytes",
"To Upper case", "To Upper case",
"To Lower case", "To Lower case",
"Swap case",
"To Case Insensitive Regex", "To Case Insensitive Regex",
"From Case Insensitive Regex", "From Case Insensitive Regex",
"Add line numbers", "Add line numbers",

View file

@ -0,0 +1,76 @@
/**
* @author mikecat
* @copyright Crown Copyright 2023
* @license Apache-2.0
*/
import Operation from "../Operation.mjs";
/**
* Swap case operation
*/
class SwapCase extends Operation {
/**
* SwapCase constructor
*/
constructor() {
super();
this.name = "Swap case";
this.module = "Default";
this.description = "Converts uppercase letters to lowercase ones, and lowercase ones to uppercase ones.";
this.infoURL = "";
this.inputType = "string";
this.outputType = "string";
this.args = [];
}
/**
* @param {string} input
* @param {Object[]} args
* @returns {string}
*/
run(input, args) {
let result = "";
for (let i = 0; i < input.length; i++) {
const c = input.charAt(i);
const upper = c.toUpperCase();
if (c === upper) {
result += c.toLowerCase();
} else {
result += upper;
}
}
return result;
}
/**
* Highlight Swap case
*
* @param {Object[]} pos
* @param {number} pos[].start
* @param {number} pos[].end
* @param {Object[]} args
* @returns {Object[]} pos
*/
highlight(pos, args) {
return pos;
}
/**
* Highlight Swap case in reverse
*
* @param {Object[]} pos
* @param {number} pos[].start
* @param {number} pos[].end
* @param {Object[]} args
* @returns {Object[]} pos
*/
highlightReverse(pos, args) {
return pos;
}
}
export default SwapCase;

View file

@ -131,6 +131,7 @@ import "./tests/CMAC.mjs";
import "./tests/AESKeyWrap.mjs"; import "./tests/AESKeyWrap.mjs";
import "./tests/Rabbit.mjs"; import "./tests/Rabbit.mjs";
import "./tests/LevenshteinDistance.mjs"; import "./tests/LevenshteinDistance.mjs";
import "./tests/SwapCase.mjs";
// Cannot test operations that use the File type yet // Cannot test operations that use the File type yet
// import "./tests/SplitColourChannels.mjs"; // import "./tests/SplitColourChannels.mjs";

View file

@ -0,0 +1,33 @@
/**
* @author mikecat
* @copyright Crown Copyright 2023
* @license Apache-2.0
*/
import TestRegister from "../../lib/TestRegister.mjs";
TestRegister.addTests([
{
"name": "Swap Case: basic example",
"input": "Hello, World!",
"expectedOutput": "hELLO, wORLD!",
"recipeConfig": [
{
"op": "Swap case",
"args": [
],
},
],
},
{
"name": "Swap Case: empty input",
"input": "",
"expectedOutput": "",
"recipeConfig": [
{
"op": "Swap case",
"args": [
],
},
],
},
]);