Skip to content

Commit 7ff46af

Browse files
committed
refactor: simplify Option match expressions with guard syntax in examples
1 parent 324eecc commit 7ff46af

7 files changed

Lines changed: 54 additions & 85 deletions

File tree

examples/calculator/main.mbt

Lines changed: 8 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -130,13 +130,9 @@ fn main {
130130
let button = make_button(label)
131131
let key = label
132132
button.add_event_listener("click", fn(_event) {
133-
match process_key(state, key) {
134-
Some(new_state) => {
135-
state = new_state
136-
render_ui(display, status, state)
137-
}
138-
None => ()
139-
}
133+
guard process_key(state, key) is Some(new_state) else { () }
134+
state = new_state
135+
render_ui(display, status, state)
140136
})
141137
row_el.append_child(button) |> ignore
142138
}
@@ -146,15 +142,11 @@ fn main {
146142
// Keyboard support
147143
document.add_event_listener("keydown", fn(event) {
148144
let kb_event : KeyboardEvent = event.into()
149-
match map_keyboard_key(kb_event.key()) {
150-
Some(calc_key) => {
151-
guard process_key(state, calc_key) is Some(new_state) else { () }
152-
state = new_state
153-
render_ui(display, status, state)
154-
kb_event.prevent_default()
155-
}
156-
None => ()
157-
}
145+
guard map_keyboard_key(kb_event.key()) is Some(calc_key) else { () }
146+
guard process_key(state, calc_key) is Some(new_state) else { () }
147+
state = new_state
148+
render_ui(display, status, state)
149+
kb_event.prevent_default()
158150
})
159151

160152
let children : Array[&@webapi.TNode] = [

examples/dom/main.mbt

Lines changed: 21 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -101,33 +101,27 @@ fn main {
101101

102102
// Demonstrate getAttribute
103103
let heading_id = heading.get_attribute("id")
104-
match heading_id {
105-
Some(id) => {
106-
let info : @webapi.HTMLDivElement = @webapi.document
107-
.create_element("div")
108-
.into()
109-
info
110-
..set_attribute(
111-
"style", "margin: 0.5em 0; color: gray; font-size: 0.9em;",
112-
)
113-
.set_text_content("Heading id attribute: " + id)
104+
guard heading_id is Some(id) else { () }
105+
let info : @webapi.HTMLDivElement = @webapi.document
106+
.create_element("div")
107+
.into()
108+
info
109+
..set_attribute("style", "margin: 0.5em 0; color: gray; font-size: 0.9em;")
110+
.set_text_content("Heading id attribute: " + id)
114111

115-
// Build the DOM tree
116-
let app = @webapi.document.get_element_by_id("app").unwrap()
117-
let container : @webapi.HTMLDivElement = @webapi.document
118-
.create_element("div")
119-
.into()
120-
container.set_attribute(
121-
"style", "font-family: system-ui; max-width: 600px; margin: 2em auto;",
122-
)
123-
let children : Array[&@webapi.TNode] = [
124-
heading, info, list, status, add_btn, remove_btn, clone_btn,
125-
]
126-
for child in children {
127-
container.append_child(child) |> ignore
128-
}
129-
let _ = app.append_child(container)
130-
}
131-
None => ()
112+
// Build the DOM tree
113+
let app = @webapi.document.get_element_by_id("app").unwrap()
114+
let container : @webapi.HTMLDivElement = @webapi.document
115+
.create_element("div")
116+
.into()
117+
container.set_attribute(
118+
"style", "font-family: system-ui; max-width: 600px; margin: 2em auto;",
119+
)
120+
let children : Array[&@webapi.TNode] = [
121+
heading, info, list, status, add_btn, remove_btn, clone_btn,
122+
]
123+
for child in children {
124+
container.append_child(child) |> ignore
132125
}
126+
let _ = app.append_child(container)
133127
}

examples/intersection-observer/main.mbt

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -75,13 +75,9 @@ fn main {
7575
// Observe all boxes
7676
let boxes = container.query_selector_all(".box")
7777
for i = 0U; i < boxes.length(); i = i + 1 {
78-
match boxes.item(i) {
79-
Some(node) => {
80-
let el : @webapi.Element = @webapi.TJsValue::to_js(node).into()
81-
observer.observe(el)
82-
}
83-
None => ()
84-
}
78+
guard boxes.item(i) is Some(node) else { continue }
79+
let el : @webapi.Element = @webapi.TJsValue::to_js(node).into()
80+
observer.observe(el)
8581
}
8682

8783
let app = doc.get_element_by_id("app").unwrap()

examples/main.mbt

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -174,9 +174,8 @@ fn main {
174174
iframe.set_src(name + "/" + name + "." + target + ".html")
175175

176176
// Remove active class from previous link
177-
match prev_active[0] {
178-
Some(el) => el.class_list().remove(["active"])
179-
None => ()
177+
if prev_active[0] is Some(el) {
178+
el.class_list().remove(["active"])
180179
}
181180

182181
// Add active class to current link

examples/selection-api/main.mbt

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -63,13 +63,9 @@ fn main {
6363
doc.add_event_listener("selectionchange", fn(_event) { update_info() })
6464

6565
select_all_btn.add_event_listener("click", fn(_event) {
66-
match @webapi.window.get_selection() {
67-
Some(sel) => {
68-
sel.select_all_children(text_content)
69-
update_info()
70-
}
71-
None => ()
72-
}
66+
guard @webapi.window.get_selection() is Some(sel) else { () }
67+
sel.select_all_children(text_content)
68+
update_info()
7369
})
7470

7571
let app = doc.get_element_by_id("app").unwrap()

examples/storage/main.mbt

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -50,13 +50,9 @@ fn main {
5050
let len = storage.length()
5151
let mut text = ""
5252
for i in 0U..<len {
53-
match storage.key(i) {
54-
Some(k) => {
55-
let val = storage.get_item(k).unwrap_or("")
56-
text = text + k + " = " + val + "\n"
57-
}
58-
None => ()
59-
}
53+
guard storage.key(i) is Some(k) else { continue }
54+
let val = storage.get_item(k).unwrap_or("")
55+
text = text + k + " = " + val + "\n"
6056
}
6157
if text == "" {
6258
text = "(empty)"

examples/touch-events/main.mbt

Lines changed: 14 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -36,24 +36,20 @@ fn main {
3636
} else {
3737
te.changed_touches().item(0U)
3838
}
39-
match touch {
40-
Some(t) => {
41-
let x = t.client_x().to_int()
42-
let y = t.client_y().to_int()
43-
touch_info.set_text_content(
44-
"Event: " +
45-
event_type +
46-
"\nTouches: " +
47-
count.to_string() +
48-
"\nPosition: (" +
49-
x.to_string() +
50-
", " +
51-
y.to_string() +
52-
")",
53-
)
54-
}
55-
None => ()
56-
}
39+
guard touch is Some(t) else { () }
40+
let x = t.client_x().to_int()
41+
let y = t.client_y().to_int()
42+
touch_info.set_text_content(
43+
"Event: " +
44+
event_type +
45+
"\nTouches: " +
46+
count.to_string() +
47+
"\nPosition: (" +
48+
x.to_string() +
49+
", " +
50+
y.to_string() +
51+
")",
52+
)
5753
}
5854

5955
let types = ["touchstart", "touchmove", "touchend"]

0 commit comments

Comments
 (0)