Skip to content

Instantly share code, notes, and snippets.

@Saturate
Last active May 15, 2025 13:15
Show Gist options
  • Save Saturate/129b4387babcfa0cb2e88bb7c0200a3f to your computer and use it in GitHub Desktop.
Save Saturate/129b4387babcfa0cb2e88bb7c0200a3f to your computer and use it in GitHub Desktop.
(function () {
function decodeMorse(morseCode) {
const r = {
".-": "a",
"-...": "b",
"-.-.": "c",
"-..": "d",
".": "e",
"..-.": "f",
"--.": "g",
"....": "h",
"..": "i",
".---": "j",
"-.-": "k",
".-..": "l",
"--": "m",
"-.": "n",
"---": "o",
".--.": "p",
"--.-": "q",
".-.": "r",
"...": "s",
"-": "t",
"..-": "u",
"...-": "v",
".--": "w",
"-..-": "x",
"-.--": "y",
"--..": "z",
".-.-": "æ",
"---.": "ø",
".--.-": "å",
".----": "1",
"..---": "2",
"...--": "3",
"....-": "4",
".....": "5",
"-....": "6",
"--...": "7",
"---..": "8",
"----.": "9",
"-----": "0",
};
return morseCode
.split(" ")
.map((a) =>
a
.split(" ")
.map((b) => r[b])
.join("")
)
.join(" ");
}
function convertToMorse(input) {
return input
.replace(/1/g, "-")
.replace(/0/g, ".")
.replace(/\/\//g, " ")
.replace(/\//g, "");
}
console.log(
decodeMorse(
convertToMorse(
"0001 / 00 / 100 / 0 / 10 // 111 / 110 // 000 / 00 / 101 / 101 / 0 / 010 / 0000 / 0 / 100"
)
)
);
})();
(function () {
function encodeToMorseNumbers(text) {
const morseCodeMap = {
a: ".-",
b: "-...",
c: "-.-.",
d: "-..",
e: ".",
f: "..-.",
g: "--.",
h: "....",
i: "..",
j: ".---",
k: "-.-",
l: ".-..",
m: "--",
n: "-.",
o: "---",
p: ".--.",
q: "--.-",
r: ".-.",
s: "...",
t: "-",
u: "..-",
v: "...-",
w: ".--",
x: "-..-",
y: "-.--",
z: "--..",
æ: ".-.-",
ø: "---.",
å: ".--.-",
1: ".----",
2: "..---",
3: "...--",
4: "....-",
5: ".....",
6: "-....",
7: "--...",
8: "---..",
9: "----.",
0: "-----",
};
const morseToNumbers = (morse) => {
return morse
.replace(/\./g, "0")
.replace(/-/g, "1")
.replace(/ /g, "//")
.replace(/z/g, " / ")
.replace(/\/ \/\/ \//g, " // ");
};
const morseCode = text
.toLowerCase()
.split("")
.map((char) => morseCodeMap[char] || char)
.join("z");
return morseToNumbers(morseCode);
}
console.log(encodeToMorseNumbers("hello world"));
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment