Merge pull request #1638 from ThomasdenH/master

Add is_truthy, is_falsy
This commit is contained in:
Alex Crichton
2019-08-08 12:35:01 -05:00
committed by GitHub
5 changed files with 54 additions and 0 deletions

View File

@ -85,6 +85,9 @@ intrinsics! {
#[symbol = "__wbindgen_is_string"] #[symbol = "__wbindgen_is_string"]
#[signature = fn(ref_anyref()) -> Boolean] #[signature = fn(ref_anyref()) -> Boolean]
IsString, IsString,
#[symbol = "__wbindgen_is_falsy"]
#[signature = fn(ref_anyref()) -> Boolean]
IsFalsy,
#[symbol = "__wbindgen_object_clone_ref"] #[symbol = "__wbindgen_object_clone_ref"]
#[signature = fn(ref_anyref()) -> Anyref] #[signature = fn(ref_anyref()) -> Anyref]
ObjectCloneRef, ObjectCloneRef,

View File

@ -2464,6 +2464,11 @@ impl<'a> Context<'a> {
format!("typeof({}) === 'string'", args[0]) format!("typeof({}) === 'string'", args[0])
} }
Intrinsic::IsFalsy => {
assert_eq!(args.len(), 1);
format!("!{}", args[0])
}
Intrinsic::ObjectCloneRef => { Intrinsic::ObjectCloneRef => {
assert_eq!(args.len(), 1); assert_eq!(args.len(), 1);
args[0].clone() args[0].clone()

View File

@ -336,6 +336,22 @@ impl JsValue {
unsafe { __wbindgen_is_function(self.idx) == 1 } 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 /// Get a string representation of the JavaScript object for debugging
#[cfg(feature = "std")] #[cfg(feature = "std")]
fn as_debug_string(&self) -> String { fn as_debug_string(&self) -> String {
@ -506,6 +522,7 @@ externs! {
fn __wbindgen_is_object(idx: u32) -> u32; fn __wbindgen_is_object(idx: u32) -> u32;
fn __wbindgen_is_function(idx: u32) -> u32; fn __wbindgen_is_function(idx: u32) -> u32;
fn __wbindgen_is_string(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_number_get(idx: u32, invalid: *mut u8) -> f64;
fn __wbindgen_boolean_get(idx: u32) -> u32; fn __wbindgen_boolean_get(idx: u32) -> u32;

View File

@ -37,6 +37,7 @@ pub mod rethrow;
pub mod simple; pub mod simple;
pub mod slice; pub mod slice;
pub mod structural; pub mod structural;
pub mod truthy_falsy;
pub mod u64; pub mod u64;
pub mod validate_prt; pub mod validate_prt;
pub mod variadic; pub mod variadic;

View File

@ -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);
}