Home > Net >  convert string to braille
convert string to braille

Time:10-29

I'm working on a discord command that converts text to braille. I've found a few web-based examples of converting to other things such as morse code and tried to modify them but nothing seems to work.

The basic idea is that it would take in a string convert each character in the string to its braille equivalent and send that as an output.

Any help would be wonderful thanks <3

CodePudding user response:

1 line in JavaScript.

string.toUpperCase().split("").map(c => "⠀⠁⠂⠃⠄⠅⠆⠇⠈⠉⠊⠋⠌⠍⠎⠏⠐⠑⠒⠓⠔⠕⠖⠗⠘⠙⠚⠛⠜⠝⠞⠟⠠⠡⠢⠣⠤⠥⠦⠧⠨⠩⠪⠫⠬⠭⠮⠯⠰⠱⠲⠳⠴⠵⠶⠷⠸⠹⠺⠻⠼⠽⠾⠿"[" A1B'K2L@CIF/MSP\"E3H9O6R^DJG>NTQ,*5<-U8V.%[$ X!&;:4\\0Z7(_?W]#Y)=".indexOf(c)]).join("");

One liners are cool, but here is a slightly better variation of the same code.

let map = " A1B'K2L@CIF/MSP\"E3H9O6R^DJG>NTQ,*5<-U8V.%[$ X!&;:4\\0Z7(_?W]#Y)=".split("").reduce((o, n, i) => {
  return o[n] = "⠀⠁⠂⠃⠄⠅⠆⠇⠈⠉⠊⠋⠌⠍⠎⠏⠐⠑⠒⠓⠔⠕⠖⠗⠘⠙⠚⠛⠜⠝⠞⠟⠠⠡⠢⠣⠤⠥⠦⠧⠨⠩⠪⠫⠬⠭⠮⠯⠰⠱⠲⠳⠴⠵⠶⠷⠸⠹⠺⠻⠼⠽⠾⠿"[i],
    o[n.toLowerCase()] = o[n], o;
}, {});

function toBraile(string) {
    return string.split("").map(c => map[c]).join("");
}

console.log(toBraile("test="));

Perhaps the conversion of split("") and join("") can also be improved, I'd be happy if someone has something to say on that.

  • Related