mor.js

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.

Translating messages

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;

  • message - The message to be translated
  • mode - The mode to be used to translate the message
    • Optional: "classic" mode is used by default

Encoding

morjs.encode([data|message][, callback(error, result)])

This returns the message provided encoded in Morse code.

Decoding

morjs.encode([data|message][, callback(error, result)])

This returns the human-readable message decoded from the Morse code provided.

Customizing

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.

Defining characters

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;

  • Must be a combination of S and L characters
    • S - Short mark (dot)
    • L - Longer mark (dash)
  • Must contain a minimum of one valid character
  • May be a duplicate but not recommended as decoding chooses the character that matches the pattern first
  • Use a recognized pattern for that character where possible
morjs.defineChar('\u0642', 'SSSLLL') // Arabic Letter Qaf
morjs.encode({message: 'ق'})         // · · · - - -

Defining modes

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;

  1. Short mark (dot)
  2. Longer mark (dash)
  3. Intra-character gap (between dots and dashes within a character)
  4. Short gap (between letters)
  5. Medium gap (between words)
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

Miscellaneous

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.

Changes

Version 1.0.2

  • #1: Add support for single string or object argument to the encode and decode methods
  • #3: Expose read-only versions of the internal chars and modes variables

View historical changes

Bugs

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.

Questions?

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.