Skip to content

Commit

Permalink
Refactor type check
Browse files Browse the repository at this point in the history
  • Loading branch information
nabetti1720 committed Nov 9, 2024
1 parent c984521 commit 58fc2b6
Showing 1 changed file with 23 additions and 14 deletions.
37 changes: 23 additions & 14 deletions modules/llrt_assert/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,30 @@ use llrt_utils::module::{export_default, ModuleInfo};
use rquickjs::{
module::{Declarations, Exports, ModuleDef},
prelude::{Func, Opt},
Ctx, Exception, Result, Value,
Ctx, Exception, Result, Type, Value,
};

fn ok(ctx: Ctx, value: Value, message: Opt<Value>) -> Result<()> {
if value.as_bool().unwrap_or(false) {
return Ok(());
}
if value.as_number().unwrap_or(0.0) != 0.0 {
return Ok(());
}
if value.is_string() && !value.as_string().unwrap().to_string().unwrap().is_empty() {
return Ok(());
}
if value.is_array() || value.is_object() {
return Ok(());
fn assert(ctx: Ctx, value: Value, message: Opt<Value>) -> Result<()> {
match value.type_of() {
Type::Bool => {
if value.as_bool().unwrap() {
return Ok(());
}
},
Type::Float | Type::Int => {
if value.as_number().unwrap() != 0.0 {
return Ok(());
}
},
Type::String => {
if !value.as_string().unwrap().to_string().unwrap().is_empty() {
return Ok(());
}
},
Type::Array | Type::Object => {
return Ok(());
},
_ => {},
}

if let Some(obj) = message.0 {
Expand Down Expand Up @@ -49,7 +58,7 @@ impl ModuleDef for AssertModule {

fn evaluate<'js>(ctx: &Ctx<'js>, exports: &Exports<'js>) -> Result<()> {
export_default(ctx, exports, |default| {
default.set("ok", Func::from(ok))?;
default.set("ok", Func::from(assert))?;

Ok(())
})
Expand Down

0 comments on commit 58fc2b6

Please sign in to comment.