File tree Expand file tree Collapse file tree 4 files changed +59
-0
lines changed Expand file tree Collapse file tree 4 files changed +59
-0
lines changed Original file line number Diff line number Diff line change @@ -85,6 +85,12 @@ intrinsics! {
85
85
#[ symbol = "__wbindgen_is_string" ]
86
86
#[ signature = fn ( ref_anyref( ) ) -> Boolean ]
87
87
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 ,
88
94
#[ symbol = "__wbindgen_object_clone_ref" ]
89
95
#[ signature = fn ( ref_anyref( ) ) -> Anyref ]
90
96
ObjectCloneRef ,
Original file line number Diff line number Diff line change @@ -2299,6 +2299,16 @@ impl<'a> Context<'a> {
2299
2299
format ! ( "typeof({}) === 'string'" , args[ 0 ] )
2300
2300
}
2301
2301
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
+
2302
2312
Intrinsic :: ObjectCloneRef => {
2303
2313
assert_eq ! ( args. len( ) , 1 ) ;
2304
2314
args[ 0 ] . clone ( )
Original file line number Diff line number Diff line change @@ -332,6 +332,22 @@ impl JsValue {
332
332
unsafe { __wbindgen_is_function ( self . idx ) == 1 }
333
333
}
334
334
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
+
335
351
/// Get a string representation of the JavaScript object for debugging
336
352
#[ cfg( feature = "std" ) ]
337
353
fn as_debug_string ( & self ) -> String {
@@ -502,6 +518,8 @@ externs! {
502
518
fn __wbindgen_is_object( idx: u32 ) -> u32 ;
503
519
fn __wbindgen_is_function( idx: u32 ) -> u32 ;
504
520
fn __wbindgen_is_string( idx: u32 ) -> u32 ;
521
+ fn __wbindgen_is_truthy( idx: u32 ) -> u32 ;
522
+ fn __wbindgen_is_falsy( idx: u32 ) -> u32 ;
505
523
506
524
fn __wbindgen_number_get( idx: u32 , invalid: * mut u8 ) -> f64 ;
507
525
fn __wbindgen_boolean_get( idx: u32 ) -> u32 ;
Original file line number Diff line number Diff line change
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
+ }
You can’t perform that action at this time.
0 commit comments