Skip to content

Commit

Permalink
Merge pull request #1638 from ThomasdenH/master
Browse files Browse the repository at this point in the history
Add is_truthy, is_falsy
  • Loading branch information
alexcrichton authored Aug 8, 2019
2 parents 49acddf + 8b99fdc commit 67e858e
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 0 deletions.
3 changes: 3 additions & 0 deletions crates/cli-support/src/intrinsic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,9 @@ intrinsics! {
#[symbol = "__wbindgen_is_string"]
#[signature = fn(ref_anyref()) -> Boolean]
IsString,
#[symbol = "__wbindgen_is_falsy"]
#[signature = fn(ref_anyref()) -> Boolean]
IsFalsy,
#[symbol = "__wbindgen_object_clone_ref"]
#[signature = fn(ref_anyref()) -> Anyref]
ObjectCloneRef,
Expand Down
5 changes: 5 additions & 0 deletions crates/cli-support/src/js/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2464,6 +2464,11 @@ impl<'a> Context<'a> {
format!("typeof({}) === 'string'", args[0])
}

Intrinsic::IsFalsy => {
assert_eq!(args.len(), 1);
format!("!{}", args[0])
}

Intrinsic::ObjectCloneRef => {
assert_eq!(args.len(), 1);
args[0].clone()
Expand Down
17 changes: 17 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -336,6 +336,22 @@ impl JsValue {
unsafe { __wbindgen_is_function(self.idx) == 1 }
}

/// Tests whether the value is ["truthy"].
///
/// ["truthy"]: https://developer.mozilla.org/en-US/docs/Glossary/Truthy
#[inline]
pub fn is_truthy(&self) -> bool {
!self.is_falsy()
}

/// Tests whether the value is ["falsy"].
///
/// ["falsy"]: https://developer.mozilla.org/en-US/docs/Glossary/Falsy
#[inline]
pub fn is_falsy(&self) -> bool {
unsafe { __wbindgen_is_falsy(self.idx) == 1 }
}

/// Get a string representation of the JavaScript object for debugging
#[cfg(feature = "std")]
fn as_debug_string(&self) -> String {
Expand Down Expand Up @@ -506,6 +522,7 @@ externs! {
fn __wbindgen_is_object(idx: u32) -> u32;
fn __wbindgen_is_function(idx: u32) -> u32;
fn __wbindgen_is_string(idx: u32) -> u32;
fn __wbindgen_is_falsy(idx: u32) -> u32;

fn __wbindgen_number_get(idx: u32, invalid: *mut u8) -> f64;
fn __wbindgen_boolean_get(idx: u32) -> u32;
Expand Down
1 change: 1 addition & 0 deletions tests/wasm/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ pub mod rethrow;
pub mod simple;
pub mod slice;
pub mod structural;
pub mod truthy_falsy;
pub mod u64;
pub mod validate_prt;
pub mod variadic;
Expand Down
28 changes: 28 additions & 0 deletions tests/wasm/truthy_falsy.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
use wasm_bindgen::prelude::*;
use wasm_bindgen_test::*;

#[wasm_bindgen_test]
fn test_is_truthy() {
assert_eq!(JsValue::from(0).is_truthy(), false);
assert_eq!(JsValue::from("".to_string()).is_truthy(), false);
assert_eq!(JsValue::from(false).is_truthy(), false);
assert_eq!(JsValue::NULL.is_truthy(), false);
assert_eq!(JsValue::UNDEFINED.is_truthy(), false);

assert_eq!(JsValue::from(10).is_truthy(), true);
assert_eq!(JsValue::from("null".to_string()).is_truthy(), true);
assert_eq!(JsValue::from(true).is_truthy(), true);
}

#[wasm_bindgen_test]
fn test_is_falsy() {
assert_eq!(JsValue::from(0).is_falsy(), true);
assert_eq!(JsValue::from("".to_string()).is_falsy(), true);
assert_eq!(JsValue::from(false).is_falsy(), true);
assert_eq!(JsValue::NULL.is_falsy(), true);
assert_eq!(JsValue::UNDEFINED.is_falsy(), true);

assert_eq!(JsValue::from(10).is_falsy(), false);
assert_eq!(JsValue::from("null".to_string()).is_falsy(), false);
assert_eq!(JsValue::from(true).is_falsy(), false);
}

0 comments on commit 67e858e

Please sign in to comment.