diff --git a/src/js.rs b/src/js.rs index 6246197f..4385df0c 100644 --- a/src/js.rs +++ b/src/js.rs @@ -87,6 +87,20 @@ extern "C" { /// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/isFinite #[wasm_bindgen(js_name = isFinite)] pub fn is_finite(value: &JsValue) -> bool; + + /// The `parseInt()` function parses a string argument and returns an integer + /// of the specified radix (the base in mathematical numeral systems), or NaN on error. + /// + /// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/parseInt + #[wasm_bindgen(js_name = parseInt)] + pub fn parse_int(text: &str, radix: u8) -> f64; + + /// The parseFloat() function parses an argument and returns a floating point number, + /// or NaN on error. + /// + /// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/parseFloat + #[wasm_bindgen(js_name = parseFloat)] + pub fn parse_float(text: &str) -> f64; } // UInt8Array @@ -780,6 +794,20 @@ extern "C" { #[wasm_bindgen(constructor)] pub fn new(value: JsValue) -> Number; + /// The Number.parseInt() method parses a string argument and returns an + /// integer of the specified radix or base. + /// + /// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/parseInt + #[wasm_bindgen(static_method_of = Number, js_name = parseInt)] + pub fn parse_int(text: &str, radix: u8) -> Number; + + /// The Number.parseFloat() method parses a string argument and returns a + /// floating point number. + /// + /// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/parseFloat + #[wasm_bindgen(static_method_of = Number, js_name = parseFloat)] + pub fn parse_float(text: &str) -> Number; + /// The toLocaleString() method returns a string with a language sensitive /// representation of this number. /// diff --git a/tests/all/js_globals/Number.rs b/tests/all/js_globals/Number.rs index fac4961d..7b32bf16 100755 --- a/tests/all/js_globals/Number.rs +++ b/tests/all/js_globals/Number.rs @@ -118,6 +118,46 @@ fn new() { .test() } +#[test] +fn parse_int_float() { + project() + .file( + "src/lib.rs", r#" + #![feature(proc_macro, wasm_custom_section)] + + extern crate wasm_bindgen; + use wasm_bindgen::prelude::*; + use wasm_bindgen::js::Number; + + #[wasm_bindgen] + pub fn parse_int(text: &str, radix: u8) -> Number { + Number::parse_int(text, radix) + } + + #[wasm_bindgen] + pub fn parse_float(text: &str) -> Number { + Number::parse_float(text) + } + "#, + ) + .file( + "test.js", r#" + import * as assert from "assert"; + import * as wasm from "./out"; + + export function test() { + assert.equal(wasm.parse_int("42", 10), 42); + assert.equal(wasm.parse_int("42", 16), 66); // 0x42 == 66 + assert.ok(Number.isNaN(wasm.parse_int("invalid int", 10)), "should be NaN"); + + assert.equal(wasm.parse_float("123456.789"), 123456.789); + assert.ok(Number.isNaN(wasm.parse_float("invalid float")), "should be NaN"); + } + "#, + ) + .test() +} + #[test] fn to_locale_string() { project() diff --git a/tests/all/js_globals/mod.rs b/tests/all/js_globals/mod.rs index 041fae78..d1206a8b 100644 --- a/tests/all/js_globals/mod.rs +++ b/tests/all/js_globals/mod.rs @@ -186,3 +186,34 @@ fn is_finite() { ) .test(); } + +#[test] +fn parse_int_float() { + project() + .file("src/lib.rs", r#" + #![feature(proc_macro, wasm_custom_section)] + + extern crate wasm_bindgen; + use wasm_bindgen::prelude::*; + use wasm_bindgen::js; + + #[wasm_bindgen] + pub fn test() { + let i = js::parse_int("42", 10); + assert_eq!(i as i64, 42); + + let i = js::parse_int("42", 16); + assert_eq!(i as i64, 66); // 0x42 == 66 + + let i = js::parse_int("invalid int", 10); + assert!(i.is_nan()); + + let f = js::parse_float("123456.789"); + assert_eq!(f, 123456.789); + + let f = js::parse_float("invalid float"); + assert!(f.is_nan()); + } + "#) + .test(); +}