Skip to content

Instantly share code, notes, and snippets.

@eddieh
Created March 21, 2021 19:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save eddieh/e10d5981ddf7e3d666a73af00d721c12 to your computer and use it in GitHub Desktop.
Save eddieh/e10d5981ddf7e3d666a73af00d721c12 to your computer and use it in GitHub Desktop.
Turn an ASCII string into a Mathematical Monospaced String in JS
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