morse-node

Encode strings to Morse code, decode strings from Morse code.

Morse Node

A very tiny, dependency-free Morse code encoder and decoder. Currently supports the ITU standard.

npm install morse-node
 var morse = require('morse-node').create("ITU");
  • All alphanumeric characters are accepted, available symbols includes everything inside the brackets [ . , ? ' ! / ( ) & : ; = + - _ " $ @ ].
var msg = morse.encode('The quick brown fox jumped over the lazy dog.');
  • Letters are seperated by a single space.
  • Words are seperated by a forward slash " / ".
var msg = morse.decode("- .... ./--.- ..- .. -.-. -.-/-... .-. --- .-- -./..-. --- -..-/.--- ..- -- .--. . -../--- ...- . .-./- .... ./.-.. .- --.. -.--/-.. --- --. .-.-.-");
  • Check validity of a string by using the isValid function.
  • Encoding and decoding functions do not make this check and will attempt to translate your string regardless of undefined characters.
var morse = require('morse-node').create();
 
var encoded = morse.encode('Pack my box with five dozen liquor jugs!');
console.log(encoded);
 
var decoded = morse.decode(encoded);
console.log(decoded);
 
console.log(morse.isValid(encoded, "morse"));
console.log(morse.isValid(encoded += "asdf", "morse"));
console.log(morse.isValid(decoded, "chars"));
console.log(morse.isValid(decoded += "~", "chars"));
.--. .- -.-. -.- / -- -.-- / -... --- -..- / .-- .. - .... / ..-. .. ...- . / -.. --- --.. . -. / .-.. .. --.- ..- --- .-. / .--- ..- --. ... -.-.--
pack my box with five dozen liquor jugs!
true
false
true
false

Calvin Nichols