Last active
May 15, 2025 13:15
-
-
Save Saturate/129b4387babcfa0cb2e88bb7c0200a3f to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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" | |
) | |
) | |
); | |
})(); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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