From cefe1681ab37091ca07080fe1234390e5b5aff47 Mon Sep 17 00:00:00 2001 From: Tyler Wilcock <tyler.l.wilcock@gmail.com> Date: Mon, 23 Jul 2018 23:50:29 -0500 Subject: [PATCH] Implement Math.min() and Math.max() bindings (#542) * Add Number.isNaN() binding * Add binding for Math.hypot() * Implement Math.min() and Math.max() bindings --- crates/js-sys/src/lib.rs | 12 ++++++++++++ crates/js-sys/tests/wasm/Math.rs | 18 ++++++++++++++++++ 2 files changed, 30 insertions(+) diff --git a/crates/js-sys/src/lib.rs b/crates/js-sys/src/lib.rs index fc1e69c3..146110a7 100644 --- a/crates/js-sys/src/lib.rs +++ b/crates/js-sys/src/lib.rs @@ -1191,6 +1191,18 @@ extern "C" { #[wasm_bindgen(static_method_of = Math)] pub fn log2(x: f64) -> f64; + /// The Math.max() function returns the largest of two numbers. + /// + /// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/max + #[wasm_bindgen(static_method_of = Math)] + pub fn max(x: f64, y: f64) -> f64; + + /// The static function Math.min() returns the lowest-valued number passed into it. + /// + /// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/min + #[wasm_bindgen(static_method_of = Math)] + pub fn min(x: f64, y: f64) -> f64; + /// The Math.pow() function returns the base to the exponent power, that is, base^exponent. /// /// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/pow diff --git a/crates/js-sys/tests/wasm/Math.rs b/crates/js-sys/tests/wasm/Math.rs index 8863fa82..a46ded34 100644 --- a/crates/js-sys/tests/wasm/Math.rs +++ b/crates/js-sys/tests/wasm/Math.rs @@ -172,6 +172,24 @@ fn log2() { assert_eq!(Math::log2(0.), NEG_INFINITY); } +#[wasm_bindgen_test] +fn max() { + assert_eq!(Math::max(3., 1.), 3.); + assert_eq!(Math::max(-3., 1.), 1.); + assert_eq!(Math::max(9913., 43.4), 9913.); + assert_eq!(Math::max(-27., -43.), -27.); + assert_eq!(Math::max(-423.27, -43.1), -43.1); +} + +#[wasm_bindgen_test] +fn min() { + assert_eq!(Math::min(3., 1.), 1.); + assert_eq!(Math::min(-3., 1.), -3.); + assert_eq!(Math::min(9913., 43.4), 43.4); + assert_eq!(Math::min(-27., -43.), -43.); + assert_eq!(Math::min(-423.27, -43.1), -423.27); +} + #[wasm_bindgen_test] fn pow() { assert_eq!(Math::pow(7., 2.), 49.);