Skip to content

Commit 64a4bfe

Browse files
authored
Merge pull request #1897 from sw5678/master
2 parents da922a4 + bd9e014 commit 64a4bfe

File tree

4 files changed

+74
-0
lines changed

4 files changed

+74
-0
lines changed

src/core/config/Categories.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -294,6 +294,7 @@
294294
"To Upper case",
295295
"To Lower case",
296296
"Swap case",
297+
"Alternating Caps",
297298
"To Case Insensitive Regex",
298299
"From Case Insensitive Regex",
299300
"Add line numbers",
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
/**
2+
* @author sw5678
3+
* @copyright Crown Copyright 2023
4+
* @license Apache-2.0
5+
*/
6+
7+
import Operation from "../Operation.mjs";
8+
9+
/**
10+
* Alternating caps operation
11+
*/
12+
class AlternatingCaps extends Operation {
13+
14+
/**
15+
* AlternatingCaps constructor
16+
*/
17+
constructor() {
18+
super();
19+
20+
this.name = "Alternating Caps";
21+
this.module = "Default";
22+
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'.";
23+
this.infoURL = "https://en.wikipedia.org/wiki/Alternating_caps";
24+
this.inputType = "string";
25+
this.outputType = "string";
26+
this.args= [];
27+
}
28+
29+
/**
30+
* @param {string} input
31+
* @param {Object[]} args
32+
* @returns {string}
33+
*/
34+
run(input, args) {
35+
let output = "";
36+
let previousCaps = true;
37+
for (let i = 0; i < input.length; i++) {
38+
// Check if the element is a letter
39+
if (!RegExp(/^\p{L}/, "u").test(input[i])) {
40+
output += input[i];
41+
} else if (previousCaps) {
42+
output += input[i].toLowerCase();
43+
previousCaps = false;
44+
} else {
45+
output += input[i].toUpperCase();
46+
previousCaps = true;
47+
}
48+
}
49+
return output;
50+
}
51+
}
52+
53+
export default AlternatingCaps;

tests/operations/index.mjs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import { setLongTestFailure, logTestReport } from "../lib/utils.mjs";
1515

1616
import TestRegister from "../lib/TestRegister.mjs";
1717
import "./tests/AESKeyWrap.mjs";
18+
import "./tests/AlternatingCaps.mjs";
1819
import "./tests/AvroToJSON.mjs";
1920
import "./tests/BaconCipher.mjs";
2021
import "./tests/Base32.mjs";
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
/* @author sw5678
2+
* @copyright Crown Copyright 2024
3+
* @license Apache-2.0
4+
*/
5+
import TestRegister from "../../lib/TestRegister.mjs";
6+
7+
TestRegister.addTests([
8+
{
9+
"name": "AlternatingCaps: Basic Example",
10+
"input": "Hello, world!",
11+
"expectedOutput": "hElLo, WoRlD!",
12+
"recipeConfig": [
13+
{
14+
"op": "Alternating Caps",
15+
"args": []
16+
},
17+
],
18+
}
19+
]);

0 commit comments

Comments
 (0)