Skip to content

Instantly share code, notes, and snippets.

@Orangetronic
Last active June 22, 2022 22:09
Show Gist options
  • Save Orangetronic/d15b6b6fd54339e291b45f33e9b396e4 to your computer and use it in GitHub Desktop.
Save Orangetronic/d15b6b6fd54339e291b45f33e9b396e4 to your computer and use it in GitHub Desktop.
a crYoTPGRApHicaLly rEVeRsIBle SPoNGebOb-casE alGORiThm
/*
* Cryptographically reversible spongebob-case text transformation
*
* Sometimes you want to encode some text in a way that is not completely
* secret, but is quite hard to read. But what if you need to decode it again?
*
* Now you can!
*
* to De-sponge some text, simply pass the sponged text back through the sponge
* function
*/
function sponge(str) {
return str
.split("")
.map((char, i) => {
let result = i % 2 == 0 ? toggleCase(char) : char
if (i % 7 == 0) result = toggleCase(result)
if (i % 9 == 0) result = toggleCase(result)
return result
})
.join("")
}
const [upp_start, upp_end, low_start, low_end] = "AZaz"
.split("")
.map(x => x.charCodeAt(0));
function toggleCase(char) {
const charCode = char.charCodeAt(0);
let diff = 0;
if (charCode >= upp_start && charCode <= upp_end) {
// change uppercase to lowercase
diff = low_end - upp_end;
} else if (charCode >= low_start && charCode <= low_end) {
// change lowercase to uppercase
diff = upp_end - low_end;
}
return String.fromCharCode(charCode + diff);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment