From b7acb0785de1076b9f2f764cba9beed5b2763c27 Mon Sep 17 00:00:00 2001 From: Tyler Wilcock Date: Sat, 21 Jul 2018 15:19:30 -0500 Subject: [PATCH] Add Number.isNaN() binding (#532) --- crates/js-sys/src/lib.rs | 7 +++++++ crates/js-sys/tests/wasm/Number.rs | 18 ++++++++++++++++++ 2 files changed, 25 insertions(+) diff --git a/crates/js-sys/src/lib.rs b/crates/js-sys/src/lib.rs index ba21a468b..43e2efb21 100644 --- a/crates/js-sys/src/lib.rs +++ b/crates/js-sys/src/lib.rs @@ -1263,6 +1263,13 @@ extern "C" { #[wasm_bindgen(static_method_of = Number, js_name = isInteger)] pub fn is_integer(value: &JsValue) -> bool; + /// The Number.isNaN() method determines whether the passed value is NaN and its type is Number. + /// It is a more robust version of the original, global isNaN(). + /// + /// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/isNaN + #[wasm_bindgen(static_method_of = Number, js_name = isNaN)] + pub fn is_nan(value: &JsValue) -> bool; + /// The Number.isSafeInteger() method determines whether the provided value is a number /// that is a safe integer. /// diff --git a/crates/js-sys/tests/wasm/Number.rs b/crates/js-sys/tests/wasm/Number.rs index fdd646092..93e5b0124 100644 --- a/crates/js-sys/tests/wasm/Number.rs +++ b/crates/js-sys/tests/wasm/Number.rs @@ -19,6 +19,24 @@ fn is_integer() { assert!(!Number::is_integer(&42.1.into())); } +#[wasm_bindgen_test] +fn is_nan() { + assert!(Number::is_nan(&NAN.into())); + + assert!(!Number::is_nan(&JsValue::TRUE)); + assert!(!Number::is_nan(&JsValue::NULL)); + assert!(!Number::is_nan(&37.into())); + assert!(!Number::is_nan(&"37".into())); + assert!(!Number::is_nan(&"37.37".into())); + assert!(!Number::is_nan(&"".into())); + assert!(!Number::is_nan(&" ".into())); + + // These would all return true with the global isNaN() + assert!(!Number::is_nan(&"NaN".into())); + assert!(!Number::is_nan(&JsValue::UNDEFINED)); + assert!(!Number::is_nan(&"blabla".into())); +} + #[wasm_bindgen_test] fn is_safe_integer() { assert_eq!(Number::is_safe_integer(&42.into()), true);