diff --git a/index.js b/index.js index 0c388d4..0efce8b 100644 --- a/index.js +++ b/index.js @@ -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(""); + }; diff --git a/tests/test.js b/tests/test.js index d38d90f..6bbe086 100644 --- a/tests/test.js +++ b/tests/test.js @@ -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); + }); });