Skip to content

Adds valueOf and toString to Number #299

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Jun 23, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 36 additions & 1 deletion src/js.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -268,4 +303,4 @@ extern {
/// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/slice
#[wasm_bindgen(method, js_class = "String")]
pub fn slice(this: &JsString, start: u32, end: u32) -> JsString;
}
}
127 changes: 127 additions & 0 deletions tests/all/js_globals/Number.rs
Original file line number Diff line number Diff line change
@@ -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");
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you use this locale string for javascript in the browser you'll get chinese characters. Any idea why that doesn't work here?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I assume it must be an issue with node.js then.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This test fails for me, I get the chinese characters. Also the "de-DE" fails for me, I get "1.234,45".

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hm, I wonder if it depends on some other system-specific things then. Perhaps we should just disable the test.

}
"#)
.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()
}
1 change: 1 addition & 0 deletions tests/all/js_globals/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ mod Object;
mod Array;
mod ArrayIterator;
mod JsString;
mod Number;

#[test]
#[cfg(feature = "std")]
Expand Down