diff --git a/src/core/config/Categories.json b/src/core/config/Categories.json index 6164ef8b..9edd76a2 100644 --- a/src/core/config/Categories.json +++ b/src/core/config/Categories.json @@ -294,6 +294,7 @@ "To Upper case", "To Lower case", "Swap case", + "Alternating Caps", "To Case Insensitive Regex", "From Case Insensitive Regex", "Add line numbers", diff --git a/src/core/operations/AlternatingCaps.mjs b/src/core/operations/AlternatingCaps.mjs new file mode 100644 index 00000000..2d54867c --- /dev/null +++ b/src/core/operations/AlternatingCaps.mjs @@ -0,0 +1,53 @@ +/** + * @author sw5678 + * @copyright Crown Copyright 2023 + * @license Apache-2.0 + */ + +import Operation from "../Operation.mjs"; + +/** + * Alternating caps operation + */ +class AlternatingCaps extends Operation { + + /** + * AlternatingCaps constructor + */ + constructor() { + super(); + + this.name = "Alternating Caps"; + this.module = "Default"; + this.description = "Alternating caps, also known as studly caps, sticky caps, or spongecase is a form of text notation in which the capitalization of letters varies by some pattern, or arbitrarily. An example of this would be spelling 'alternative caps' as 'aLtErNaTiNg CaPs'."; + this.infoURL = "https://en.wikipedia.org/wiki/Alternating_caps"; + this.inputType = "string"; + this.outputType = "string"; + this.args= []; + } + + /** + * @param {string} input + * @param {Object[]} args + * @returns {string} + */ + run(input, args) { + let output = ""; + let previousCaps = true; + for (let i = 0; i < input.length; i++) { + // Check if the element is a letter + if (!RegExp(/^\p{L}/, "u").test(input[i])) { + output += input[i]; + } else if (previousCaps) { + output += input[i].toLowerCase(); + previousCaps = false; + } else { + output += input[i].toUpperCase(); + previousCaps = true; + } + } + return output; + } +} + +export default AlternatingCaps; diff --git a/tests/operations/index.mjs b/tests/operations/index.mjs index 7d93c2f5..0923cb46 100644 --- a/tests/operations/index.mjs +++ b/tests/operations/index.mjs @@ -15,6 +15,7 @@ import { setLongTestFailure, logTestReport } from "../lib/utils.mjs"; import TestRegister from "../lib/TestRegister.mjs"; import "./tests/AESKeyWrap.mjs"; +import "./tests/AlternatingCaps.mjs"; import "./tests/AvroToJSON.mjs"; import "./tests/BaconCipher.mjs"; import "./tests/Base32.mjs"; diff --git a/tests/operations/tests/AlternatingCaps.mjs b/tests/operations/tests/AlternatingCaps.mjs new file mode 100644 index 00000000..b23768eb --- /dev/null +++ b/tests/operations/tests/AlternatingCaps.mjs @@ -0,0 +1,19 @@ +/* @author sw5678 + * @copyright Crown Copyright 2024 + * @license Apache-2.0 + */ +import TestRegister from "../../lib/TestRegister.mjs"; + +TestRegister.addTests([ + { + "name": "AlternatingCaps: Basic Example", + "input": "Hello, world!", + "expectedOutput": "hElLo, WoRlD!", + "recipeConfig": [ + { + "op": "Alternating Caps", + "args": [] + }, + ], + } +]);