darcien / morse

TS utility functions for morse code (WIP)

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Morse

JSR

Encode text in morse code.

This work is in very early stages.

  • Only supports encoding, no decoding yet
  • Built-in encodings only supports 26 basic latin letters (a-z) and and Arabic numerals (0-9)
  • Support specifying custom encoding
  • Unsupported characters are skipped

Usage

Using built-in encoding

import { encodeMorse } from "@darcien/morse";

const input = "SOS SOS";

const simple = encodeMorse(input, { variant: "simple" });
// "... --- ...       ... --- ..."

const compact = encodeMorse(input, { variant: "compact" });
// "... --- ... / ... --- ..."

const spoken = encodeMorse(input, { variant: "spoken" });
// "di di dit   dah dah dah   di di dit,       di di dit   dah dah dah   di di dit"

Using custom encoding

import {
  encodeMorse,
  ENCODING_BY_VARIANT,
  transformSimpleMorseCodeValues,
} from "@darcien/morse";

const myEncoding: MorseEncoding = {
  variant: "my cool encoding",
  shortGap: " ",
  mediumGap: " | ",
  // The map keys should be in lower case
  codeByLetter: {
    s: "dot_dot_lastdot",
    o: "dash_dash_dash",
    // ...other letters
  },
  // A transform function is exported to make it easier
  // writing a custom encoding in simple morse code.
  // This is equivalent to the example above,
  // which uses simple encoding as base template.
  // codeByLetter: transformSimpleMorseCodeValues(
  //   ENCODING_BY_VARIANT.simple.codeByLetter,
  //   {
  //     shortMark: "dot",
  //     lastShortMark: "lastdot",
  //     longMark: "dash",
  //     interMarkGap: "_",
  //   },
  // ),
};

const input = "SOS SOS";

const encoded = encodeMorse(input, { encoding: myEncoding });
// dot_dot_lastdot dash_dash_dash dot_dot_lastdot | dot_dot_lastdot dash_dash_dash dot_dot_lastdot

About

TS utility functions for morse code (WIP)

License:MIT License


Languages

Language:TypeScript 100.0%