Skip to content

Commit b808086

Browse files
author
Marc Lipovsky
authored
Merge pull request #55 from marclipovsky/feature/random-case
Add random case command fixes #54
2 parents 3810fdd + a1ad9b4 commit b808086

File tree

3 files changed

+78
-1
lines changed

3 files changed

+78
-1
lines changed

package.json

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,8 @@
4545
"onCommand:string-manipulation.decrement",
4646
"onCommand:string-manipulation.duplicateAndIncrement",
4747
"onCommand:string-manipulation.duplicateAndDecrement",
48-
"onCommand:string-manipulation.sequence"
48+
"onCommand:string-manipulation.sequence",
49+
"onCommand:string-manipulation.randomCase"
4950
],
5051
"contributes": {
5152
"commands": [
@@ -193,6 +194,11 @@
193194
"title": "Convert UTF8 to char",
194195
"category": "String Manipulation",
195196
"command": "string-manipulation.utf8ToChar"
197+
},
198+
{
199+
"title": "Random Case",
200+
"category": "String Manipulation",
201+
"command": "string-manipulation.randomCase"
196202
}
197203
],
198204
"submenus": [
@@ -324,6 +330,10 @@
324330
{
325331
"command": "string-manipulation.charToUtf8",
326332
"group": "7_modification"
333+
},
334+
{
335+
"command": "string-manipulation.randomCase",
336+
"group": "7_modification"
327337
}
328338
]
329339
}

src/extension.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,18 @@ const increment = (str: string) =>
2727
const decrement = (str: string) =>
2828
str.replace(/-?\d+/g, (n) => String(Number(n) - 1));
2929

30+
const randomCase = (input: string): string => {
31+
let result = "";
32+
for (const char of input) {
33+
if (Math.random() < 0.5) {
34+
result += char.toLowerCase();
35+
} else {
36+
result += char.toUpperCase();
37+
}
38+
}
39+
return result;
40+
};
41+
3042
export type StringFunction = (
3143
str: string,
3244
multiselectData?: MultiSelectData
@@ -88,6 +100,7 @@ const commandNameFunctionMap: { [key: string]: CommandFunction } = {
88100
.split("")
89101
.map((x) => `\\u${x.charCodeAt(0).toString(16).padStart(4, "0")}`)
90102
.join(""),
103+
randomCase,
91104
};
92105

93106
const numberFunctionNames = [

src/test/extension.test.ts

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,5 +146,59 @@ suite("Extension Test Suite", () => {
146146
assert.equal(func(originalString, multiselectData), expectedString);
147147
});
148148
});
149+
150+
suite("randomCase", () => {
151+
const input = "Hello, World!";
152+
153+
test("returns a string of the same length", () => {
154+
const output = myExtension.commandNameFunctionMap["randomCase"](
155+
input
156+
) as string;
157+
assert.equal(output.length, input.length);
158+
});
159+
160+
test("contains the same characters ignoring case", () => {
161+
const output = myExtension.commandNameFunctionMap["randomCase"](
162+
input
163+
) as string;
164+
assert.equal(output.toLowerCase(), input.toLowerCase());
165+
});
166+
167+
test("changes the case of at least one character (statistically)", () => {
168+
let changed = false;
169+
for (let i = 0; i < 10; i++) {
170+
const output = myExtension.commandNameFunctionMap["randomCase"](
171+
input
172+
) as string;
173+
if (output !== input && output.toLowerCase() === input.toLowerCase()) {
174+
changed = true;
175+
break;
176+
}
177+
}
178+
assert.equal(changed, true);
179+
});
180+
181+
test("handles empty strings", () => {
182+
const output = myExtension.commandNameFunctionMap.randomCase("");
183+
assert.equal(output, "");
184+
});
185+
186+
test("preserves non-alphabetic characters", () => {
187+
const specialChars = "12345!@#$%";
188+
const output =
189+
myExtension.commandNameFunctionMap.randomCase(specialChars);
190+
assert.equal(output, specialChars);
191+
});
192+
193+
test("handles strings with mixed content", () => {
194+
const mixedInput = "Test123!";
195+
const output = myExtension.commandNameFunctionMap.randomCase(
196+
mixedInput
197+
) as string;
198+
assert.equal(output.length, mixedInput.length);
199+
assert.notEqual(output.replace(/[^a-zA-Z]/g, ""), "");
200+
});
201+
202+
});
149203
});
150204
});

0 commit comments

Comments
 (0)