Add Number.isNaN() binding (#532)

This commit is contained in:
Tyler Wilcock 2018-07-21 15:19:30 -05:00 committed by Alex Crichton
parent c174262cf0
commit b7acb0785d
2 changed files with 25 additions and 0 deletions

View File

@ -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.
///

View File

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