Merge pull request #299 from jonathan-s/number

Adds valueOf and toString to Number
This commit is contained in:
Nick Fitzgerald 2018-06-23 16:04:35 -07:00 committed by GitHub
commit 0f5badf95e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 164 additions and 1 deletions

View File

@ -208,6 +208,41 @@ extern {
pub fn entries(this: &Array) -> ArrayIterator;
}
// Number.
#[wasm_bindgen]
extern {
pub type Number;
/// The toLocaleString() method returns a string with a language sensitive
/// representation of this number.
///
/// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/toLocaleString
#[wasm_bindgen(method, js_name = toLocaleString)]
pub fn to_locale_string(this: &Number, locale: String) -> String;
/// 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.
///
/// 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
/// a Number object.
///
/// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/valueOf
#[wasm_bindgen(method, js_name = valueOf)]
pub fn value_of(this: &Number) -> Number;
}
// Object.
#[wasm_bindgen]
extern {

View File

@ -0,0 +1,127 @@
#![allow(non_snake_case)]
use super::project;
#[test]
fn to_locale_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_locale_string(this: &js::Number, locale: String) -> String {
this.to_locale_string(locale)
}
"#)
.file("test.ts", r#"
import * as assert from "assert";
import * as wasm from "./out";
export function test() {
let number = 1234.45;
assert.equal(wasm.to_locale_string(number, "de-DE"), "1,234.45");
assert.equal(wasm.to_locale_string(number, "en-US"), "1,234.45");
assert.equal(wasm.to_locale_string(number, "zh-Hans-CN-u-nu-hanidec"), "1,234.45");
}
"#)
.test()
}
#[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() {
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]
fn value_of() {
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 js_value_of(this: &js::Number) -> js::Number {
this.value_of()
}
"#)
.file("test.ts", r#"
import * as assert from "assert";
import * as wasm from "./out";
export function test() {
let number = 42;
assert.equal(wasm.js_value_of(number), 42);
assert.equal(typeof wasm.js_value_of(number), "number");
}
"#)
.test()
}

View File

@ -6,6 +6,7 @@ mod Object;
mod Array;
mod ArrayIterator;
mod JsString;
mod Number;
#[test]
#[cfg(feature = "std")]