mirror of
https://github.com/fluencelabs/wasm-bindgen
synced 2025-03-31 01:11:06 +00:00
Add the binding of to_string to Number
This commit is contained in:
parent
a7f8e071fe
commit
bf56d5815b
@ -213,6 +213,13 @@ extern {
|
|||||||
extern {
|
extern {
|
||||||
pub type Number;
|
pub type Number;
|
||||||
|
|
||||||
|
/// The toString() method returns a string representing the
|
||||||
|
/// specified Number object.
|
||||||
|
///
|
||||||
|
/// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/toString
|
||||||
|
#[wasm_bindgen(catch, method, js_name = toString)]
|
||||||
|
pub fn to_string(this: &Number, radix: u8) -> Result<String, JsValue>;
|
||||||
|
|
||||||
/// The valueOf() method returns the wrapped primitive value of
|
/// The valueOf() method returns the wrapped primitive value of
|
||||||
/// a Number object.
|
/// a Number object.
|
||||||
///
|
///
|
||||||
|
@ -3,6 +3,40 @@
|
|||||||
use super::project;
|
use super::project;
|
||||||
|
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn to_string() {
|
||||||
|
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_string(this: &js::Number, radix: u8) -> String {
|
||||||
|
let result = this.to_string(radix);
|
||||||
|
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() {
|
||||||
|
let number = 42;
|
||||||
|
assert.equal(wasm.to_string(number, 10), "42");
|
||||||
|
assert.equal(wasm.to_string(233, 16), "e9");
|
||||||
|
assert.equal(wasm.to_string(number, 100), "RangeError");
|
||||||
|
}
|
||||||
|
"#)
|
||||||
|
.test()
|
||||||
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn value_of() {
|
fn value_of() {
|
||||||
project()
|
project()
|
||||||
|
Loading…
x
Reference in New Issue
Block a user