Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion moon.mod.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "rami3l/js-ffi",
"version": "0.1.3",
"version": "0.1.4",
"readme": "README.md",
"repository": "",
"license": "Apache-2.0",
Expand Down
9 changes: 9 additions & 0 deletions src/js/js.mbti
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,15 @@ impl Object {
op_set[K, V](Self, K, V) -> Unit
}

type Optional[_]
impl Optional {
from_option[T](T?) -> Self[T]
get_exn[T](Self[T]) -> T
is_undefined[T](Self[T]) -> Bool
to_option[T](Self[T]) -> T?
undefined[T]() -> Self[T]
}

pub type Promise[_]

type Symbol
Expand Down
32 changes: 32 additions & 0 deletions src/js/optional.mbt
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
///|
type Optional[_]

///|
pub fn Optional::is_undefined[T](self : Optional[T]) -> Bool {
self |> Value::cast_from |> Value::is_undefined
}

///|
pub fn Optional::get_exn[T](self : Optional[T]) -> T = "%identity"

///|
pub fn Optional::to_option[T](self : Optional[T]) -> T? {
if Value::cast_from(self).is_undefined() {
None
} else {
Some(self.get_exn())
}
}

///|
pub fn Optional::undefined[T]() -> Optional[T] {
Value::undefined().cast()
}

///|
pub fn Optional::from_option[T](value : T?) -> Optional[T] {
match value {
Some(v) => Value::cast_from(v).cast()
None => Optional::undefined()
}
}