mirror of
https://github.com/fluencelabs/wasm-bindgen
synced 2025-03-16 10:10:52 +00:00
Add toPrecision to Number
This commit is contained in:
parent
bf56d5815b
commit
f636f7b28d
@ -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.
|
||||
///
|
||||
|
@ -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() {
|
||||
|
Loading…
x
Reference in New Issue
Block a user