From a1ad9b4f55e6db63351d5e172063429a7c4b3c66 Mon Sep 17 00:00:00 2001 From: Marc Lipovsky Date: Mon, 7 Oct 2024 19:30:38 -0400 Subject: [PATCH] Add random case command fixes #54 --- package.json | 12 ++++++++- src/extension.ts | 13 +++++++++ src/test/extension.test.ts | 54 ++++++++++++++++++++++++++++++++++++++ 3 files changed, 78 insertions(+), 1 deletion(-) diff --git a/package.json b/package.json index 3324c68..41fd419 100644 --- a/package.json +++ b/package.json @@ -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": [ @@ -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": [ @@ -324,6 +330,10 @@ { "command": "string-manipulation.charToUtf8", "group": "7_modification" + }, + { + "command": "string-manipulation.randomCase", + "group": "7_modification" } ] } diff --git a/src/extension.ts b/src/extension.ts index 7946d5a..8c24b60 100644 --- a/src/extension.ts +++ b/src/extension.ts @@ -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 @@ -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 = [ diff --git a/src/test/extension.test.ts b/src/test/extension.test.ts index a601d4c..d00fabc 100644 --- a/src/test/extension.test.ts +++ b/src/test/extension.test.ts @@ -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, ""), ""); + }); + + }); }); });