Add toPrecision to Number

This commit is contained in:
Jonathan Sundqvist 2018-06-23 18:18:58 +02:00
parent bf56d5815b
commit f636f7b28d
2 changed files with 38 additions and 0 deletions

View File

@ -213,6 +213,13 @@ extern {
extern {
pub type Number;
/// The toPrecision() method returns a string representing the Number
/// object to the specified precision.
///
/// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/toPrecision
#[wasm_bindgen(catch, method, js_name = toPrecision)]
pub fn to_precision(this: &Number, precision: u8) -> Result<String, JsValue>;
/// The toString() method returns a string representing the
/// specified Number object.
///

View File

@ -2,6 +2,37 @@
use super::project;
#[test]
fn to_precision() {
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 to_precision(this: &js::Number, precision: u8) -> String {
let result = this.to_precision(precision);
let result = match result {
Ok(num) => num,
Err(_err) => "RangeError".to_string()
};
result
}
"#)
.file("test.ts", r#"
import * as assert from "assert";
import * as wasm from "./out";
export function test() {
assert.equal(wasm.to_precision(0.1, 3), "0.100");
assert.equal(wasm.to_precision(10, 101), "RangeError");
}
"#)
.test()
}
#[test]
fn to_string() {