JavaScript library for encoding & decoding Morse code
mor.js is a pure JavaScript library for encoding/decoding Morse code messages.
The library provides a very simple API
that exposes the easy encoding/decoding functionality while only exposing a single
global variable (morjs
). The predefined Morse alphabet is based
on the ITU
but mor.js allows this to be easily extended by adding
and/or modifying characters.
The API
allows either a data
object or string message
to be
passed as the first argument. If a string is used all options will use their
default value; otherwise they can be set using the following properties;
"classic"
mode is used by default
morjs.encode([data|message][, callback(error, result)])
This returns the message provided encoded in Morse code.
morjs.encode([data|message][, callback(error, result)])
This returns the human-readable message decoded from the Morse code provided.
In order to get the most out of mor.js the API also exposes two methods which allow you to define (or redefine) supported characters and translation modes.
morjs.defineChar(character, pattern[, callback(error)])
This method maps a Morse alphabet pattern for a supported character (existing/new). When defining a character it's recommended to specify its value using a Unicode escape sequence (codes can easily be found on Wikipedia) and follow these guidelines when specifying the pattern;
S
and L
characters
morjs.defineChar('\u0642', 'SSSLLL') // Arabic Letter Qaf
morjs.encode({message: 'ق'}) // · · · - - -
morjs.defineMode(name, characters[, callback(error)])
This method maps a set of characters to a translation mode (existing/new). When
defining a mode the order of the elements in the characters
array
determines the implementation;
morjs.encode({message: 'Invert', mode: 'simple'}) // 00,10,0001,0,010,1
morjs.defineMode('simple-invert', [
'\u0031' // 1
, '\u0030' // 0
, '', // Nothing
, '\u002C' // Comma
, '\u0020' // Space
])
morjs.encode({message: 'Invert', mode: 'simple-invert'}) // 11,01,1110,1,101,0
The mor.js API exposes some properties and methods which may only be useful to users in special cases.
morjs.chars([callback(error, chars)])
morjs.modes([callback(error, modes)])
Retrieve a read-only version of the currently loaded modes and characters.
morjs.noConflict([callback(error)])
Relinquish mor.js' control of the morjs
global
variable. If another library uses this variable calling this method will reassign
it back to that library.
morjs.VERSION
The current version of mor.js.
encode
and decode
methods
chars
and modes
variables
If you have any problems with this library or would like to see the changes currently in development browse our issues.
Developers should run all tests (locally) and ensure they pass before submitting a pull request.
Take a look at the documentation to get a better understanding of what the code is doing.
If that doesn't help, feel free to follow me on Twitter, @neocotic.