Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

make the readibilty more #10

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
320 changes: 149 additions & 171 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,174 +6,152 @@
* Licensed under the MIT license.
*/

var BRAILLE = {
' ': '⠀', // space bar to dot-0
'_': '⠸',
'-': '⠤',
',': '⠠',
';': '⠰',
':': '⠱',
'!': '⠮',
'?': '⠹',
'.': '⠨',
'(': '⠷',
'[': '⠪',
'@': '⠈',
'*': '⠡',
'/': '⠌',
'\'': '⠄',
'\"': '⠐',
'\\': '⠳',
'&': '⠯',
'%': '⠩',
'^': '⠘',
'+': '⠬',
'<': '⠣',
'>': '⠜',
'$': '⠫',
'0': '⠴',
'1': '⠂',
'2': '⠆',
'3': '⠒',
'4': '⠲',
'5': '⠢',
'6': '⠖',
'7': '⠶',
'8': '⠦',
'9': '⠔',
'A': '⠁',
'B': '⠃',
'C': '⠉',
'D': '⠙',
'E': '⠑',
'F': '⠋',
'G': '⠛',
'H': '⠓',
'I': '⠊',
'J': '⠚',
'K': '⠅',
'L': '⠇',
'M': '⠍',
'N': '⠝',
'O': '⠕',
'P': '⠏',
'Q': '⠟',
'R': '⠗',
'S': '⠎',
'T': '⠞',
'U': '⠥',
'V': '⠧',
'W': '⠺',
'X': '⠭',
'Z': '⠵',
']': '⠻',
'#': '⠼',
'Y': '⠽',
')': '⠾',
'=': '⠿'
},

ASCII = {
' ': ' ', // space bar to space bar
'⠀': ' ', // dot-0 to space bar
'⠸': '_',
'⠤': '-',
'⠠': ',',
'⠰': ';',
'⠱': ':',
'⠮': '!',
'⠹': '?',
'⠨': '.',
'⠷': '(',
'⠪': '[',
'⠈': '@',
'⠡': '*',
'⠌': '/',
'⠄': '\'',
'⠐': '\"',
'⠳': '\\',
'⠯': '&',
'⠩': '%',
'⠘': '^',
'⠬': '+',
'⠣': '<',
'⠜': '>',
'⠫': '$',
'⠴': '0',
'⠂': '1',
'⠆': '2',
'⠒': '3',
'⠲': '4',
'⠢': '5',
'⠖': '6',
'⠶': '7',
'⠦': '8',
'⠔': '9',
'⠁': 'A',
'⠃': 'B',
'⠉': 'C',
'⠙': 'D',
'⠑': 'E',
'⠋': 'F',
'⠛': 'G',
'⠓': 'H',
'⠊': 'I',
'⠚': 'J',
'⠅': 'K',
'⠇': 'L',
'⠍': 'M',
'⠝': 'N',
'⠕': 'O',
'⠏': 'P',
'⠟': 'Q',
'⠗': 'R',
'⠎': 'S',
'⠞': 'T',
'⠥': 'U',
'⠧': 'V',
'⠺': 'W',
'⠭': 'X',
'⠵': 'Z',
'⠻': ']',
'⠼': '#',
'⠽': 'Y',
'⠾': ')',
'⠿': '='
};

module.exports = {
convert: function (character) {
return !!BRAILLE[character] ? BRAILLE[character] : '?';
},

read: function (symbol) {
return !!ASCII[symbol] ? ASCII[symbol] : '?';
},

toBraille: function (text) {
var upperText, upperTextLength, brailleText, i;

upperText = text.toUpperCase();
upperTextLength = upperText.length;
brailleText = '';

for (i = 0; i < upperTextLength; i++) {
brailleText += this.convert(upperText[i]);
}

return brailleText;
},

toText: function (code) {
var codeLength, asciiText, i;

codeLength = code.length;
asciiText = '';

for (i = 0; i < codeLength; i++) {
asciiText += this.read(code[i]);
}

return asciiText;
}
};
const BRAILLE = {
" ": "⠀",
_: "⠸",
"-": "⠤",
",": "⠠",
";": "⠰",
":": "⠱",
"!": "⠮",
"?": "⠹",
".": "⠨",
"(": "⠷",
"[": "⠪",
"@": "⠈",
"*": "⠡",
"/": "⠌",
"'": "⠄",
'"': "⠐",
"\\": "⠳",
"&": "⠯",
"%": "⠩",
"^": "⠘",
"+": "⠬",
"<": "⠣",
">": "⠜",
$: "⠫",
"0": "⠴",
"1": "⠂",
"2": "⠆",
"3": "⠒",
"4": "⠲",
"5": "⠢",
"6": "⠖",
"7": "⠶",
"8": "⠦",
"9": "⠔",
A: "⠁",
B: "⠃",
C: "⠉",
D: "⠙",
E: "⠑",
F: "⠋",
G: "⠛",
H: "⠓",
I: "⠊",
J: "⠚",
K: "⠅",
L: "⠇",
M: "⠍",
N: "⠝",
O: "⠕",
P: "⠏",
Q: "⠟",
R: "⠗",
S: "⠎",
T: "⠞",
U: "⠥",
V: "⠧",
W: "⠺",
X: "⠭",
Z: "⠵",
"]": "⠻",
"#": "⠼",
Y: "⠽",
")": "⠾",
"=": "⠿"
};

const ASCII = {
" ": " ",
"⠀": " ",
"⠸": "_",
"⠤": "-",
"⠠": ",",
"⠰": ";",
"⠱": ":",
"⠮": "!",
"⠹": "?",
"⠨": ".",
"⠷": "(",
"⠪": "[",
"⠈": "@",
"⠡": "*",
"⠌": "/",
"⠄": "'",
"⠐": '"',
"⠳": "\\",
"⠯": "&",
"⠩": "%",
"⠘": "^",
"⠬": "+",
"⠣": "<",
"⠜": ">",
"⠫": "$",
"⠴": "0",
"⠂": "1",
"⠆": "2",
"⠒": "3",
"⠲": "4",
"⠢": "5",
"⠖": "6",
"⠶": "7",
"⠦": "8",
"⠔": "9",
"⠁": "A",
"⠃": "B",
"⠉": "C",
"⠙": "D",
"⠑": "E",
"⠋": "F",
"⠛": "G",
"⠓": "H",
"⠊": "I",
"⠚": "J",
"⠅": "K",
"⠇": "L",
"⠍": "M",
"⠝": "N",
"⠕": "O",
"⠏": "P",
"⠟": "Q",
"⠗": "R",
"⠎": "S",
"⠞": "T",
"⠥": "U",
"⠧": "V",
"⠺": "W",
"⠭": "X",
"⠵": "Z",
"⠻": "]",
"⠼": "#",
"⠽": "Y",
"⠾": ")",
"⠿": "="
};

module.exports.toBraille = input => {
return String(input)
.toUpperCase()
.split("")
.map(char => (char in BRAILLE ? BRAILLE[char] : "?"))
.join("");
};

module.exports.toText = input => {
return String(input)
.split("")
.map(code => (code in ASCII ? ASCII[code] : "?"))
.join("");
};
60 changes: 22 additions & 38 deletions tests/test.js
Original file line number Diff line number Diff line change
@@ -1,46 +1,30 @@
var should, Braille;
const should = require("should");
const Braille = require("../index.js");

should = require('should');
Braille = require('../index.js');
describe("ASCII to braille: ", function() {
it("should convert an ASCII string into a braille unicode string", function() {
Braille.toBraille("HELLO WORLD").should.equal("⠓⠑⠇⠇⠕⠀⠺⠕⠗⠇⠙");
});

describe('ASCII to braille: ', function () {
it('should return a blank space if it does not found an ASCII character', function () {
Braille.convert('º').should.equal('?');
});
it("full coverage test", function() {
var ascii =
" A1B'K2L@CIF/MSP\"E3H9O6R^DJG>NTQ,*5<-U8V.%[$+X!&;:4\\0Z7(_?W]#Y)=",
dot6 = "⠀⠁⠂⠃⠄⠅⠆⠇⠈⠉⠊⠋⠌⠍⠎⠏⠐⠑⠒⠓⠔⠕⠖⠗⠘⠙⠚⠛⠜⠝⠞⠟⠠⠡⠢⠣⠤⠥⠦⠧⠨⠩⠪⠫⠬⠭⠮⠯⠰⠱⠲⠳⠴⠵⠶⠷⠸⠹⠺⠻⠼⠽⠾⠿";

it('should return the braille unicode char if the ASCII char exists', function () {
Braille.convert('A').should.equal('⠁');
});

it('should convert an ASCII string into a braille unicode string', function () {
Braille.toBraille('HELLO WORLD').should.equal('⠓⠑⠇⠇⠕⠀⠺⠕⠗⠇⠙');
});

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

Braille.toBraille(ascii).should.equal(dot6);
});
Braille.toBraille(ascii).should.equal(dot6);
});
});

describe('braille to ASCII: ', function () {
it('should return a blank space if it does not found a braille unicode char', function () {
Braille.read('A').should.equal('?');
});

it('should return the ASCII char if the braille unicode char exists', function () {
Braille.read('⠁').should.equal('A');
});

it('should convert a Braille string into an ASCII string', function () {
Braille.toText('⠓⠑⠇⠇⠕ ⠺⠕⠗⠇⠙').should.equal('HELLO WORLD');
});
describe("braille to ASCII: ", function() {
it("should convert a Braille string into an ASCII string", function() {
Braille.toText("⠓⠑⠇⠇⠕ ⠺⠕⠗⠇⠙").should.equal("HELLO WORLD");
});

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

Braille.toText(dot6).should.equal(ascii);
});
Braille.toText(dot6).should.equal(ascii);
});
});