mirror of
https://github.com/fluencelabs/wasm-bindgen
synced 2025-06-12 12:31:22 +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:
committed by
Alex Crichton
parent
d6a0b34480
commit
bfec9e6401
@ -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();
|
||||
}
|
||||
|
Reference in New Issue
Block a user