Skip to content

Commit

Permalink
Merge branch 'main' into gh-pages
Browse files Browse the repository at this point in the history
  • Loading branch information
salif committed Mar 18, 2024
2 parents beb5dee + 1f5f73e commit 24b3f55
Show file tree
Hide file tree
Showing 6 changed files with 1,054 additions and 1,014 deletions.
24 changes: 14 additions & 10 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
import * as mc_characters from "./src_gleam/build/dev/javascript/morse_code_translator/characters.mjs"
import * as morse_code from "./src_gleam/build/dev/javascript/morse_code_translator/morse_code_translator.mjs"

// button.onclick
function set_lang(mc_lang) {
window.mc_lang = mc_lang
oninp(window.inputs[0])
}

// button.onclick
function add_row(dot, dash, sp, sep) {
if (dot.value.length == 0) { dot.focus(); return; }
Expand All @@ -20,10 +25,9 @@ function oninp(this_inp, skip_abc) {
if (this_inp.str_dot === "abc" && this_inp.str_dash === "") {
// first non abc textarea
const t_inp = window.inputs[0]
// TODO: language
const result = morse_code.encode(
window.characters_dict.get("1"), this_inp.el.value, new morse_code.EncodeOptions(
t_inp.str_dot, t_inp.str_dash, t_inp.str_sp, t_inp.str_sep, false))
this_inp.el.value, window.characters_list, new morse_code.EncodeOptions(
t_inp.str_dot, t_inp.str_dash, t_inp.str_sp, t_inp.str_sep, false, window.mc_lang))
// TODO: improve
if (result.isOk()) {
t_inp.el.value = result["0"]
Expand All @@ -39,8 +43,8 @@ function oninp(this_inp, skip_abc) {
// skip because abc is input
if (!skip_abc) {
window.input_abc.el.value = morse_code.decode_to_string(
window.characters_list.get("1"), this_inp.el.value, new morse_code.DecodeOptions(
this_inp.str_dot, this_inp.str_dash, this_inp.str_sp, this_inp.str_sep, false))
this_inp.el.value, window.characters_list, new morse_code.DecodeOptions(
this_inp.str_dot, this_inp.str_dash, this_inp.str_sp, this_inp.str_sep, false, window.mc_lang))

}
window.inputs.forEach(inp => {
Expand Down Expand Up @@ -89,14 +93,14 @@ function main_table_add_row(fn_oninput, options) {
document.body.onload = function () {
window.inputs = []
window.input_abc = {}
// for encoding
window.characters_dict = mc_characters.base_characters_dict()
// for decoding
window.characters_list = mc_characters.base_characters_list()
window.characters_list = morse_code.morse_code_list
window.mc_lang = "1"
// html table
window.el_main_table = document.getElementById("main_table")
// used in html
window.set_lang = set_lang
window.add_row = add_row

main_table_add_row(oninp, { str_dot: "o", str_dash: "i", str_sp: "/", str_sep: " " })
main_table_add_row(oninp, { str_dot: ".", str_dash: ",", str_sp: "/", str_sep: " " })
main_table_add_row(oninp, { str_dot: "•", str_dash: "−", str_sp: "/", str_sep: " " })
Expand Down
69 changes: 67 additions & 2 deletions src_gleam/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,75 @@
gleam add morse_code_translator
```
```gleam
import morse_code_translator
import gleam/io
import morse_code_translator as mct
pub fn main() {
// TODO: An example of the project in use
let demo_encode_options =
mct.EncodeOptions(
mct.default_dot,
mct.default_dash,
mct.default_space,
mct.default_separator,
mct.default_is_uppercase,
mct.default_language_num,
)
let demo_decode_options =
mct.DecodeOptions(
input_dot: mct.default_dot,
input_dash: mct.default_dash,
input_space: mct.default_space,
input_separator: mct.default_separator,
to_uppercase: mct.default_to_uppercase,
language_num: mct.default_language_num,
)
let demo_encode: Result(String, mct.MorseCodeError) =
mct.encode(" Test ", mct.morse_code_list, demo_encode_options)
io.debug(demo_encode)
// Ok("/ - . ... - /")
let demo_decode: Result(String, mct.MorseCodeError) =
mct.decode("/ - . ... - /", mct.morse_code_list, demo_decode_options)
io.debug(demo_decode)
// Ok(" test ")
"demo"
|> mct.encode_to_string(mct.morse_code_list, demo_encode_options)
|> mct.decode_to_string(mct.morse_code_list, demo_decode_options)
|> io.debug
// "demo"
"-.. . -- ---"
|> mct.decode_to_string(
mct.morse_code_list,
mct.DecodeOptions(..demo_decode_options, language_num: "5"),
)
|> io.debug
// "демо"
let demo_convert_options =
mct.ConvertOptions(
input_dot: mct.default_dot,
output_dot: "0",
input_dash: mct.default_dash,
output_dash: "1",
input_space: mct.default_space,
output_space: mct.default_space,
input_separator: mct.default_separator,
output_separator: mct.default_separator,
)
"-.. . -- ---"
|> mct.convert(demo_convert_options)
|> io.debug
// Ok("100 0 11 111")
"="
|> mct.convert_to_string(demo_convert_options)
|> io.debug
// "Invalid morse code symbol: ="
}
```

Expand Down
Loading

0 comments on commit 24b3f55

Please sign in to comment.