Skip to content
Merged
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
12 changes: 11 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,8 @@
"onCommand:string-manipulation.decrement",
"onCommand:string-manipulation.duplicateAndIncrement",
"onCommand:string-manipulation.duplicateAndDecrement",
"onCommand:string-manipulation.sequence"
"onCommand:string-manipulation.sequence",
"onCommand:string-manipulation.randomCase"
],
"contributes": {
"commands": [
Expand Down Expand Up @@ -193,6 +194,11 @@
"title": "Convert UTF8 to char",
"category": "String Manipulation",
"command": "string-manipulation.utf8ToChar"
},
{
"title": "Random Case",
"category": "String Manipulation",
"command": "string-manipulation.randomCase"
}
],
"submenus": [
Expand Down Expand Up @@ -324,6 +330,10 @@
{
"command": "string-manipulation.charToUtf8",
"group": "7_modification"
},
{
"command": "string-manipulation.randomCase",
"group": "7_modification"
}
]
}
Expand Down
13 changes: 13 additions & 0 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,18 @@ const increment = (str: string) =>
const decrement = (str: string) =>
str.replace(/-?\d+/g, (n) => String(Number(n) - 1));

const randomCase = (input: string): string => {
let result = "";
for (const char of input) {
if (Math.random() < 0.5) {
result += char.toLowerCase();
} else {
result += char.toUpperCase();
}
}
return result;
};

export type StringFunction = (
str: string,
multiselectData?: MultiSelectData
Expand Down Expand Up @@ -88,6 +100,7 @@ const commandNameFunctionMap: { [key: string]: CommandFunction } = {
.split("")
.map((x) => `\\u${x.charCodeAt(0).toString(16).padStart(4, "0")}`)
.join(""),
randomCase,
};

const numberFunctionNames = [
Expand Down
54 changes: 54 additions & 0 deletions src/test/extension.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -146,5 +146,59 @@ suite("Extension Test Suite", () => {
assert.equal(func(originalString, multiselectData), expectedString);
});
});

suite("randomCase", () => {
const input = "Hello, World!";

test("returns a string of the same length", () => {
const output = myExtension.commandNameFunctionMap["randomCase"](
input
) as string;
assert.equal(output.length, input.length);
});

test("contains the same characters ignoring case", () => {
const output = myExtension.commandNameFunctionMap["randomCase"](
input
) as string;
assert.equal(output.toLowerCase(), input.toLowerCase());
});

test("changes the case of at least one character (statistically)", () => {
let changed = false;
for (let i = 0; i < 10; i++) {
const output = myExtension.commandNameFunctionMap["randomCase"](
input
) as string;
if (output !== input && output.toLowerCase() === input.toLowerCase()) {
changed = true;
break;
}
}
assert.equal(changed, true);
});

test("handles empty strings", () => {
const output = myExtension.commandNameFunctionMap.randomCase("");
assert.equal(output, "");
});

test("preserves non-alphabetic characters", () => {
const specialChars = "12345!@#$%";
const output =
myExtension.commandNameFunctionMap.randomCase(specialChars);
assert.equal(output, specialChars);
});

test("handles strings with mixed content", () => {
const mixedInput = "Test123!";
const output = myExtension.commandNameFunctionMap.randomCase(
mixedInput
) as string;
assert.equal(output.length, mixedInput.length);
assert.notEqual(output.replace(/[^a-zA-Z]/g, ""), "");
});

});
});
});
Loading