2.4.0 • Published 1 year ago

code-morse-by-bruno-ramirez v2.4.0

Weekly downloads
-
License
ISC
Repository
-
Last release
1 year ago

➡️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

🔗 https://www.npmjs.com/package/code-morse-by-bruno-ramirez


➡️More information

In the following link you can more information about Morse code 👉 MORSE DECODER

2.4.0

1 year ago

2.3.0

1 year ago

2.2.0

1 year ago

2.1.0

1 year ago

2.0.0

1 year ago

1.3.0

1 year ago

1.2.1

1 year ago

1.2.0

1 year ago

1.1.0

1 year ago

1.0.0

1 year ago