Last active
April 5, 2023 18:41
-
-
Save dancxjo/c673c1eda5cfed33ec585d6f61d44bd7 to your computer and use it in GitHub Desktop.
Displacement chains
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// qwerty to engram | |
const displacements = { | |
"`": "[", | |
"-": "]", | |
"=": "/", | |
q: "b", | |
w: "y", | |
e: "o", | |
r: "u", | |
t: "'", | |
y: '"', | |
u: "l", | |
i: "d", | |
o: "w", | |
p: "v", | |
"[": "z", | |
"]": "#", | |
"|": "@", | |
a: "c", | |
s: "i", | |
d: "e", | |
f: "a", | |
g: ",", | |
h: ".", | |
j: "h", | |
k: "t", | |
l: "s", | |
";": "n", | |
"'": "q", | |
z: "g", | |
x: "x", | |
c: "j", | |
v: "k", | |
b: "-", | |
n: "?", | |
m: "r", | |
",": "m", | |
".": "f", | |
"/": "p", | |
"~": "{", | |
"!": "|", | |
"@": "=", | |
"#": "~", | |
$: "+", | |
"%": "<", | |
"^": ">", | |
"&": "^", | |
"*": "&", | |
"(": "%", | |
")": "*", | |
_: "}", | |
"+": "\\", | |
T: "(", | |
Y: ")", | |
"{": "Z", | |
"}": "$", | |
"\\": "`", | |
G: ";", | |
H: ":", | |
":": "N", | |
'"': "Q", | |
B: "_", | |
N: "!", | |
}; | |
// engram to qwerty | |
const replacements = Object.keys(displacements).reduce((previousValue, currentValue) => { | |
if (currentValue in displacements) { | |
const replacement = displacements[currentValue as keyof typeof displacements]; | |
if (previousValue[replacement]) { | |
throw new Error('Duplicate value found: ' + replacement) | |
} | |
previousValue[replacement] = currentValue; | |
return previousValue | |
} | |
return previousValue | |
}, {} as unknown as any); | |
// console.log({ displacements, replacements }) | |
function confusionChain(qwerty: string): string { | |
if (qwerty.length !== 1) { | |
throw new Error("Only one character is allowed"); | |
} | |
let originKey = qwerty; | |
if (!(originKey in replacements)) { | |
originKey = /[A-Z]/.test(originKey) ? originKey.toLowerCase() : originKey.toUpperCase(); | |
} | |
return displacements[originKey as keyof typeof displacements] + originKey + replacements[originKey]; | |
} | |
const chains = Object.keys(displacements).map(confusionChain); | |
console.log(chains.join(' ')) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment