Skip to content

Commit e61f691

Browse files
committed
Add is_truthy, is_falsy
1 parent bf631ed commit e61f691

File tree

4 files changed

+59
-0
lines changed

4 files changed

+59
-0
lines changed

crates/cli-support/src/intrinsic.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,12 @@ intrinsics! {
8585
#[symbol = "__wbindgen_is_string"]
8686
#[signature = fn(ref_anyref()) -> Boolean]
8787
IsString,
88+
#[symbol = "__wbindgen_is_truthy"]
89+
#[signature = fn(ref_anyref()) -> Boolean]
90+
IsTruthy,
91+
#[symbol = "__wbindgen_is_falsy"]
92+
#[signature = fn(ref_anyref()) -> Boolean]
93+
IsFalsy,
8894
#[symbol = "__wbindgen_object_clone_ref"]
8995
#[signature = fn(ref_anyref()) -> Anyref]
9096
ObjectCloneRef,

crates/cli-support/src/js/mod.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2299,6 +2299,16 @@ impl<'a> Context<'a> {
22992299
format!("typeof({}) === 'string'", args[0])
23002300
}
23012301

2302+
Intrinsic::IsTruthy => {
2303+
assert_eq!(args.len(), 1);
2304+
format!("!!{}", args[0])
2305+
}
2306+
2307+
Intrinsic::IsFalsy => {
2308+
assert_eq!(args.len(), 1);
2309+
format!("!{}", args[0])
2310+
}
2311+
23022312
Intrinsic::ObjectCloneRef => {
23032313
assert_eq!(args.len(), 1);
23042314
args[0].clone()

src/lib.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -332,6 +332,22 @@ impl JsValue {
332332
unsafe { __wbindgen_is_function(self.idx) == 1 }
333333
}
334334

335+
/// Tests whether the value is ["truthy"].
336+
///
337+
/// ["truthy"]: https://developer.mozilla.org/en-US/docs/Glossary/Truthy
338+
#[inline]
339+
pub fn is_truthy(&self) -> bool {
340+
unsafe { __wbindgen_is_truthy(self.idx) == 1 }
341+
}
342+
343+
/// Tests whether the value is ["falsy"].
344+
///
345+
/// ["falsy"]: https://developer.mozilla.org/en-US/docs/Glossary/Falsy
346+
#[inline]
347+
pub fn is_falsy(&self) -> bool {
348+
unsafe { __wbindgen_is_falsy(self.idx) == 1 }
349+
}
350+
335351
/// Get a string representation of the JavaScript object for debugging
336352
#[cfg(feature = "std")]
337353
fn as_debug_string(&self) -> String {
@@ -502,6 +518,8 @@ externs! {
502518
fn __wbindgen_is_object(idx: u32) -> u32;
503519
fn __wbindgen_is_function(idx: u32) -> u32;
504520
fn __wbindgen_is_string(idx: u32) -> u32;
521+
fn __wbindgen_is_truthy(idx: u32) -> u32;
522+
fn __wbindgen_is_falsy(idx: u32) -> u32;
505523

506524
fn __wbindgen_number_get(idx: u32, invalid: *mut u8) -> f64;
507525
fn __wbindgen_boolean_get(idx: u32) -> u32;

tests/wasm/truthy_falsy.rs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
#[wasm_bindgen_test]
2+
fn test_is_truthy() {
3+
assert_eq!(JsValue::from(0).is_truthy(), false);
4+
assert_eq!(JsValue::from("".to_string()).is_truthy(), false);
5+
assert_eq!(JsValue::from(false).is_truthy(), false);
6+
assert_eq!(JsValue::NULL.is_truthy(), false);
7+
assert_eq!(JsValue::UNDEFINED.is_truthy(), false);
8+
9+
assert_eq!(JsValue::from(10).is_truthy(), true);
10+
assert_eq!(JsValue::from("null".to_string()).is_truthy(), true);
11+
assert_eq!(JsValue::from(true).is_truthy(), true);
12+
}
13+
14+
#[wasm_bindgen_test]
15+
fn test_is_falsy() {
16+
assert_eq!(JsValue::from(0).is_falsy(), true);
17+
assert_eq!(JsValue::from("".to_string()).is_falsy(), true);
18+
assert_eq!(JsValue::from(false).is_falsy(), true);
19+
assert_eq!(JsValue::NULL.is_falsy(), true);
20+
assert_eq!(JsValue::UNDEFINED.is_falsy(), true);
21+
22+
assert_eq!(JsValue::from(10).is_falsy(), false);
23+
assert_eq!(JsValue::from("null".to_string()).is_falsy(), false);
24+
assert_eq!(JsValue::from(true).is_falsy(), false);
25+
}

0 commit comments

Comments
 (0)