code-morse-by-bruno-ramirez v2.4.0
➡️Introduction
This library will help you encode a string into morse code and vice versa, it will also allow you to decode morse code. The library functions admit as a parameter a string which can be composed between uppercase and lowercase letters, that is, it treats uppercase and lowercase letters as the same, it even admits special characters.
➡️Table of Contents
🚩 Introduction
🚩 Installation
🚩 Usage
🚩 Library URL
🚩 More information
➡️Installation
npm i code-morse-by-bruno-ramirez
➡️Usage
Encode message example
To encode the message, the following library function is used
function encode(code) {
code = code.toUpperCase();
let response = "";
let currentLetter;
let codeChar;
let letterFound = undefined;
for (let i = 0; i < code.length; i++) {
currentLetter = code.charAt(i);
codeChar = code.charCodeAt(i);
if (codeChar >= 48 && codeChar <= 57) {
response += getByValue(numbers, currentLetter) + " ";
} else if (codeChar >= 65 && codeChar <= 90) {
response += getByValue(alphabet, currentLetter) + " ";
} else {
letterFound = getByValue(specialCharacters, currentLetter);
if (letterFound) {
response += letterFound + " ";
} else if (currentLetter === " ") {
response += "/ ";
} else {
response += "# ";
}
}
}
return response;
}
Use of the library
We will send a string to the function as a parameter to encode the following message "Hello, world"
const codeConverter = require("code-morse-by-bruno-ramirez");
console.log(codeConverter.encode("Hello, world"));
/* Result: ".... . .-.. .-.. --- --..-- / .-- --- .-. .-.. -.." */
Decode message example
To decode a message, the following library function will be used
function decode(code) {
code = code.toUpperCase();
let response = "";
let removingWhitespace = code.split(" ");
let codeFound = undefined;
removingWhitespace.forEach((element) => {
if (alphabet.has(element)) {
response += alphabet.get(element);
} else if (numbers.has(element)) {
response += numbers.get(element);
} else if (specialCharacters.has(element)) {
response += specialCharacters.get(element);
} else {
if (element === "/") {
response += " ";
} else {
response += "#";
}
}
});
return response;
}
Use of the library
The function requires a string as a parameter. In this case, as an example we will use "..-. . .-.. .. --.. / -. .- ...- .. -.. .- -.. -.-.--"
const codeConverter = require("code-morse-by-bruno-ramirez");
console.log(
codeConverter.decode("..-. . .-.. .. --.. / -. .- ...- .. -.. .- -.. -.-.--")
);
/* Result: "FELIZ NAVIDAD!" */
➡️Library URL
➡️More information
In the following link you can more information about Morse code 👉 MORSE DECODER