Created
March 21, 2021 19:56
-
-
Save eddieh/e10d5981ddf7e3d666a73af00d721c12 to your computer and use it in GitHub Desktop.
Turn an ASCII string into a Mathematical Monospaced String in JS
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
function mono(str) { | |
const ASCII_A = 'A'.codePointAt(0) | |
const ASCII_Z = 'Z'.codePointAt(0) | |
const ASCII_a = 'a'.codePointAt(0) | |
const ASCII_z = 'z'.codePointAt(0) | |
const ASCII_0 = '0'.codePointAt(0) | |
const ASCII_9 = '9'.codePointAt(0) | |
const MONO_Alpha_offset = '𝙰'.codePointAt(0) - ASCII_A | |
const MONO_alpha_offset = '𝚊'.codePointAt(0) - ASCII_a | |
const MONO_num_offset = '𝟶'.codePointAt(0) - ASCII_0 | |
let ret = '' | |
for (let c of str) { | |
let cp = c.codePointAt(0) | |
if (cp >= ASCII_A && cp <= ASCII_Z) | |
ret += String.fromCodePoint(cp + MONO_Alpha_offset) | |
else if (cp >= ASCII_a && cp <= ASCII_z) | |
ret += String.fromCodePoint(cp + MONO_alpha_offset) | |
else if (cp >= ASCII_0 && cp <= ASCII_9) | |
ret += String.fromCodePoint(cp + MONO_num_offset) | |
else | |
ret += String.fromCodePoint(cp) | |
} | |
return ret | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment