Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

All Case Variations Of String #1065

Merged
merged 2 commits into from
Mar 28, 2022
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
1 change: 1 addition & 0 deletions src/core/config/Categories.json
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,7 @@
"From Case Insensitive Regex",
"Add line numbers",
"Remove line numbers",
"Get All Casings",
"To Table",
"Reverse",
"Sort",
Expand Down
53 changes: 53 additions & 0 deletions src/core/operations/GetAllCasings.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/**
* @author n1073645 [n1073645@gmail.com]
* @copyright Crown Copyright 2020
* @license Apache-2.0
*/

import Operation from "../Operation.mjs";

/**
* Permutate String operation
*/
class GetAllCasings extends Operation {

/**
* GetAllCasings constructor
*/
constructor() {
super();

this.name = "Get All Casings";
this.module = "Default";
this.description = "Outputs all possible casing variations of a string.";
this.infoURL = "";
this.inputType = "string";
this.outputType = "string";
this.args = [];
}

/**
* @param {string} input
* @param {Object[]} args
* @returns {string}
*/
run(input, args) {
const length = input.length;
const max = 1 << length;
input = input.toLowerCase();
let result = "";

for (let i = 0; i < max; i++) {
const temp = input.split("");
for (let j = 0; j < length; j++) {
if (((i >> j) & 1) === 1) {
temp[j] = temp[j].toUpperCase();
}
}
result += temp.join("") + "\n";
}
return result;
}
}

export default GetAllCasings;
2 changes: 1 addition & 1 deletion tests/operations/index.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ import "./tests/LuhnChecksum.mjs";
import "./tests/CipherSaber2.mjs";
import "./tests/Colossus.mjs";
import "./tests/ParseObjectIDTimestamp.mjs";

import "./tests/GetAllCasings.mjs";

// Cannot test operations that use the File type yet
// import "./tests/SplitColourChannels.mjs";
Expand Down
44 changes: 44 additions & 0 deletions tests/operations/tests/GetAllCasings.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/**
* GetAllCasings tests.
*
* @author n1073645 [n1073645@gmail.com]
* @copyright Crown Copyright 2020
* @license Apache-2.0
*/
import TestRegister from "../../lib/TestRegister.mjs";

TestRegister.addTests([
{
name: "All casings of test",
input: "test",
expectedOutput: "test\nTest\ntEst\nTEst\nteSt\nTeSt\ntESt\nTESt\ntesT\nTesT\ntEsT\nTEsT\nteST\nTeST\ntEST\nTEST\n",
recipeConfig: [
{
"op": "Get All Casings",
"args": []
}
]
},
{
name: "All casings of t",
input: "t",
expectedOutput: "t\nT\n",
recipeConfig: [
{
"op": "Get All Casings",
"args": []
}
]
},
{
name: "All casings of null",
input: "",
expectedOutput: "\n",
recipeConfig: [
{
"op": "Get All Casings",
"args": []
}
]
}
]);