mirror of
https://github.com/fluencelabs/wasm-bindgen
synced 2025-03-16 02:00:51 +00:00
bindings for parseInt/parseFloat (#384)
* parseInt, parseFloat, JsValue::is_nan * Number.parseInt, Number.parseFloat * remove `JsValue::is_nan` * parse_int/float returns f64
This commit is contained in:
parent
d6a0b34480
commit
bfec9e6401
28
src/js.rs
28
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.
|
||||
///
|
||||
|
@ -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()
|
||||
|
@ -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();
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user