Morse code

Convert text into Morse code and back to regular text, with the option to choose from multiple input formats.

Face with waiting expression Nothing to see yet!

Loading takeymakey...
TakeyMakey code
Want this tool to do something else? Edit the code below and make it do whatever you want.
export const take = [
  { type: "text" },
  { type: "dropdown", options: ["Encode", "Decode"], label: "Direction" },
  {
    type: "dropdown",
    options: ["··· ——— ···", ".../---/...", "dot space dash"],
    label: "Format",
  },
  {
    type: "import",
    value: "https://cdn.jsdelivr.net/npm/morse-browser@1.0.1/morse.js",
  },
]

export const make = ([text, dir, format]) => {
  let result, key, mE, mD

  if (format.match("·")) key = ["·", "—", " "]
  if (format.match("d")) key = ["dot ", "dash ", "space "]

  if (key) {
    mE = { ".": key[0], "-": key[1], "/": key[2] }
    mD = Object.fromEntries(Object.entries(mE).map(([k, v]) => [v, k]))
  }

  const fn = dir === "Encode" ? morse.encode : morse.decode

  if (fn === morse.decode && mD) {
    text = [...text].map((c) => mD[c] || c).join("")
  }

  result = fn(text)

  if (fn === morse.encode && mE) {
    result = [...result].map((c) => mE[c] || c).join("")
  }

  return result
}