morse-key-converter
v1.0.0
Published
morse key to text converter and vice versa
Downloads
6
Readme
Table of Contents
Features
Converts a text or string to Morse code
Converts a morse code string to its corresponding text
Can convert Latin and extended Latin alphabet with accented vowels, vowels with umlauts, numbers and punctuation symbols.
Browser Support
| | | | |---------------------------------------------------------------------------------------------------|------------------------------------------------------------------------------------------------------|---------------------------------------------------------------------------------------------| | Latest ✔ | Latest ✔ | Latest ✔ |
Installing
Read the followings subtitles.
Package manager
Using npm:
$ npm install morse-key-converter
Using bower:
$ bower install morse-key-converter
Using yarn:
$ yarn add morse-key-converter
Using pnpm:
$ pnpm add morse-key-converter
Once the package is installed, you can import the library using the CommonJS require
approach:
const MorseConverter = require('morse-key-converter');
For cases where something went wrong when trying to import a module into a custom or legacy environment,
you can try importing the module package directly:
const MorseConverter = require('morse-key-converter/src/index');
Run test file
once the package is installed, if you want to test the package's default test file, enter the following command:
$ cd node_modules/morse-key-converter
$ npm run start test
and if all is well you will observe the following :
$ npm run start-test
> [email protected] start-test D:\User\Documents\DIGITALHARBOR\06WEBFrontendTechnologies\parte4\1221WS\morse-key-converter
> node test.js
===========ALPHABETICO=============
Input text compare with expected...
Encoded successful
Text decoded compare with input...
Decoded successful
===========NUMERIC=============
Input text compare with expected...
Encoded successful
Text decoded compare with input...
Decoded successful
===========PUNCTUATION=============
Input text compare with expected...
Encoded successful
Text decoded compare with input...
Decoded successful
Input text compare with expected...
Encoded successful
Text decoded compare with input...
Decoded successful
===========LATIN EXTENDED ENCODE=============
Input text compare with expected...
Encoded successful
Input text compare with expected...
Encoded successful
Input text compare with expected...
Encoded successful
Input text compare with expected...
Encoded successful
Input text compare with expected...
Encoded successful
Input text compare with expected...
Encoded successful
Input text compare with expected...
Encoded successful
Input text compare with expected...
Encoded successful
===========LATIN EXTENDED DECODE=============
Input text compare with expected...
Decoded successful
Input text compare with expected...
Decoded successful
Input text compare with expected...
Decoded successful
Input text compare with expected...
Decoded successful
Input text compare with expected...
Decoded successful
Input text compare with expected...
Decoded successful
Input text compare with expected...
Decoded successful
===========CUSTOM TEST=============
Input text compare with expected...
Encoded successful
Input text compare with expected...
Encoded successful
Input text compare with expected...
Decoded successful
Input text compare with expected...
Encoded successful
Input text compare with expected...
Encoded successful
Input text compare with expected...
Encoded successful
Input text compare with expected...
Decoded successful
Input text compare with expected...
Encoded successful
Input text compare with expected...
Decoded successful
Example
Note CommonJS usage
The first step would be to import the MorseConverter class and instantiate it as shown below:
const MorseConverter = require('morse-key-converter');
// MorseConverter is a nodejs class so the first thing to do is to instantiate it using new as in the following example:
const morseConverter = new MorseConverter()
Available methods
Second step, use available methods.
The following methods are available : encodeText(inputString) //returns the string entered as a parameter encoded in Morse code. decodeMorseText(inputString) //returns the string entered as a parameter decoded into its corresponding Latin alphabet meaning.
Example of invocation :
//using the above variable containing the instantiated class
let textEncoded = morseConverter.encodeText('Hello world'); // output .... . .-.. .-.. --- / .-- --- .-. .-.. -..
let textEncoded = morseConverter.decodeMorseText('.... . .-.. .-.. --- / .-- --- .-. .-.. -..');// output HELLO WORLD
Encode example
The following is an example of converting some text to morse code:
const MorseConverter = require('morse-key-converter');
const morseConverter = new MorseConverter()
let textEncoded = morseConverter.encodeText('ABCDEFGHIJKLMNÑOPQRSTUVWXYZ');
console.log(textEncoded)
//output => .- -... -.-. -.. . ..-. --. .... .. .--- -.- .-.. -- -. --.-- --- .--. --.- .-. ... - ..- ...- .-- -..- -.-- --..
Decode example
The following is an example of converting some Morse code text into natural language:
const MorseConverter = require('morse-key-converter');
const morseConverter = new MorseConverter()
let textEncoded = morseConverter.decodeMorseText('.- -... -.-. -.. . ..-. --. .... .. .--- -.- .-.. -- -. --.-- --- .--. --.- .-. ... - ..- ...- .-- -..- -.-- --..');
console.log(textEncoded)
//output => ABCDEFGHIJKLMNÑOPQRSTUVWXYZ
And that's how our package works.
Resources
the alphabet used for the conversion is as follows:
const latin = {
"A": ".-",
"B": "-...",
"C": "-.-.",
"D": "-..",
"E": ".",
"F": "..-.",
"G": "--.",
"H": "....",
"I": "..",
"J": ".---",
"K": "-.-",
"L": ".-..",
"M": "--",
"N": "-.",
"Ñ": "--.--",
"O": "---",
"P": ".--.",
"Q": "--.-",
"R": ".-.",
"S": "...",
"T": "-",
"U": "..-",
"V": "...-",
"W": ".--",
"X": "-..-",
"Y": "-.--",
"Z": "--..",
"1": ".----",
"2": "..---",
"3": "...--",
"4": "....-",
"5": ".....",
"6": "-....",
"7": "--...",
"8": "---..",
"9": "----.",
"0": "-----",
".": ".-.-.-",
",": "--..--",
"?": "..--..",
"'": ".----.",
"!": "-.-.--",
"/": "-..-.",
"(": "-.--.",
")": "-.--.-",
"&": ".-...",
":": "---...",
";": "-.-.-.",
"=": "-...-",
"+": ".-.-.",
"-": "-....-",
"_": "..--.-",
"\"": ".-..-.",
"$": "...-..-",
"@": ".--.-.",
"¿": "..-.-",
"¡": "--...-",
" ": "/"
}
const latinExtended = {
'Ã': '.--.-',
'Á': '.--.-',
'Å': '.--.-',
'À': '.--.-',
'Â': '.--.-',
'Ä': '.-.-',
'Ą': '.-.-',
'Æ': '.-.-',
'Ç': '-.-..',
'Ć': '-.-..',
'Ĉ': '-.-..',
'Č': '-.-..',
'Ę': '..-..',
'Ð': '..--.',
'È': '.-..-',
'Ë': '..-..',
'É': '..-..',
'Ê': '..-..',
'Ğ': '--.-.',
'Ĝ': '--.-.',
'Ĥ': '----',
'İ': '.-..-',
'Ï': '-..--',
'Ì': '.---.',
'Ĵ': '.---.',
'Ł': '.-..-',
'Ń': '--.--',
'Ñ': '--.--',
'Ó': '---.',
'Ò': '---.',
'Ö': '---.',
'Ô': '---.',
'Ø': '---.',
'Ś': '...-...',
'Ş': '.--..',
'Ș': '----',
'Š': '----',
'Ŝ': '...-.',
'ß': '...',
'Þ': '.--..',
'Ü': '..--',
'Ý': '..--',
'Ù': '..--',
'Ŭ': '..--',
'Ž': '--..-',
'Ź': '--..-.',
'Ż': '--..-'
}
Because the extended Latin can be handled in different ways the following JSON was used to decode this can be easily updated: Reference link : https://ham.stackexchange.com/questions/1379/international-characters-in-morse-code
const toDecodeLatinExtended = {
'.--.-': 'Ã',
'.-.-': 'Ä',
'-.-..': 'Ç',
'..-..': 'Ę',
'..--.': 'Ð',
'.-..-': 'È',
"--.-.": "Ğ",
"----": "Ĥ",
"-..--": "Ï",
".---.": "Ì",
"---.": "Ó",
"...-.": "Ŝ",
".--..": "Ş",
"..--": "Ü",
"--..-": "Ž",
"--..-.": "Ź",
}
Credits
The library is based on the following web pages:
- https://morsedecoder.com/
- https://ham.stackexchange.com/questions/1379/international-characters-in-morse-code
License
[ISC]