Skip to content

Commit

Permalink
Morse code
Browse files Browse the repository at this point in the history
  • Loading branch information
ghluka committed Apr 27, 2024
1 parent 89f29ef commit 0a87ede
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 2 deletions.
31 changes: 31 additions & 0 deletions techdemos/encode/js/encoders/morse.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
var morse = {".": ".-.-.-", ",": "--..--", "?": "..--..", "'": ".----.", "/": "-..-.", "(": "-.--.", ")": "-.--.-", "&": ".-...", ":": "---...", ";": "-.-.-.", "=": "-...-", "+": ".-.-.", "-": "-....-", "_": "..--.-", "\"": ".-..-.", "$": "...-..-", "!": "-.-.--", "@": ".--.-.", " ": "/", "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": "-----"};

function encodeMorse(text) {
text = text.toLowerCase();

for (const [key, value] of Object.entries(morse)) {
text = text.replaceAll(key, value + " ");
}

text = text.substring(0, text.length - 1);

return text;
}

function decodeMorse(code) {
var text = ""

for (const sequence of code.split(" ")) {
for (const [key, value] of Object.entries(morse)) {
console.log(value)
console.log(sequence)
if (sequence == value) {
text += key;
}
}
}

return text;
}

export {encodeMorse, decodeMorse}
3 changes: 1 addition & 2 deletions techdemos/encode/js/encoders/url.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@ function encodeURIAggressive(text) {
var encoded_text = "";

const characters = encodeURI(text).split(/(%\w\w)/g).filter(t => t !== "");
for (const i in characters) {
let c = characters[i]
for (const c of characters) {
if (c.startsWith("%")) {
encoded_text += c;
}
Expand Down
5 changes: 5 additions & 0 deletions techdemos/encode/js/main.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import {encodeURIAggressive, encodeURINonDestructive, decodeURINonDestructive} from "./encoders/url.js";
import {encodeMorse, decodeMorse} from "./encoders/morse.js";

const mode = document.getElementById("mode");
const encode = document.getElementById("encode");
Expand All @@ -24,6 +25,10 @@ const codes = {
encode: text => text.split("").map(c => c.charCodeAt(0).toString(2).padStart(8, "0")).join(" "),
decode: text => text.split(" ").filter(p => !!p).map(c => String.fromCharCode(parseInt(c, 2))).join("")
},
"Morse": {
encode: encodeMorse,
decode: decodeMorse
},
"URL": {
encode: encodeURI,
decode: decodeURI
Expand Down

0 comments on commit 0a87ede

Please sign in to comment.