Skip to content

Commit 0f5badf

Browse files
authored
Merge pull request #299 from jonathan-s/number
Adds valueOf and toString to Number
2 parents de11ec1 + 6b5974d commit 0f5badf

File tree

3 files changed

+164
-1
lines changed

3 files changed

+164
-1
lines changed

src/js.rs

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,41 @@ extern {
208208
pub fn entries(this: &Array) -> ArrayIterator;
209209
}
210210

211+
// Number.
212+
#[wasm_bindgen]
213+
extern {
214+
pub type Number;
215+
216+
/// The toLocaleString() method returns a string with a language sensitive
217+
/// representation of this number.
218+
///
219+
/// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/toLocaleString
220+
#[wasm_bindgen(method, js_name = toLocaleString)]
221+
pub fn to_locale_string(this: &Number, locale: String) -> String;
222+
223+
/// The toPrecision() method returns a string representing the Number
224+
/// object to the specified precision.
225+
///
226+
/// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/toPrecision
227+
#[wasm_bindgen(catch, method, js_name = toPrecision)]
228+
pub fn to_precision(this: &Number, precision: u8) -> Result<String, JsValue>;
229+
230+
/// The toString() method returns a string representing the
231+
/// specified Number object.
232+
///
233+
/// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/toString
234+
#[wasm_bindgen(catch, method, js_name = toString)]
235+
pub fn to_string(this: &Number, radix: u8) -> Result<String, JsValue>;
236+
237+
/// The valueOf() method returns the wrapped primitive value of
238+
/// a Number object.
239+
///
240+
/// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/valueOf
241+
#[wasm_bindgen(method, js_name = valueOf)]
242+
pub fn value_of(this: &Number) -> Number;
243+
244+
}
245+
211246
// Object.
212247
#[wasm_bindgen]
213248
extern {
@@ -275,4 +310,4 @@ extern {
275310
/// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/slice
276311
#[wasm_bindgen(method, js_class = "String")]
277312
pub fn slice(this: &JsString, start: u32, end: u32) -> JsString;
278-
}
313+
}

tests/all/js_globals/Number.rs

Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
#![allow(non_snake_case)]
2+
3+
use super::project;
4+
5+
6+
#[test]
7+
fn to_locale_string() {
8+
project()
9+
.file("src/lib.rs", r#"
10+
#![feature(proc_macro, wasm_custom_section)]
11+
12+
extern crate wasm_bindgen;
13+
use wasm_bindgen::prelude::*;
14+
use wasm_bindgen::js;
15+
16+
#[wasm_bindgen]
17+
pub fn to_locale_string(this: &js::Number, locale: String) -> String {
18+
this.to_locale_string(locale)
19+
}
20+
"#)
21+
.file("test.ts", r#"
22+
import * as assert from "assert";
23+
import * as wasm from "./out";
24+
25+
export function test() {
26+
let number = 1234.45;
27+
assert.equal(wasm.to_locale_string(number, "de-DE"), "1,234.45");
28+
assert.equal(wasm.to_locale_string(number, "en-US"), "1,234.45");
29+
assert.equal(wasm.to_locale_string(number, "zh-Hans-CN-u-nu-hanidec"), "1,234.45");
30+
}
31+
"#)
32+
.test()
33+
}
34+
35+
#[test]
36+
fn to_precision() {
37+
project()
38+
.file("src/lib.rs", r#"
39+
#![feature(proc_macro, wasm_custom_section)]
40+
41+
extern crate wasm_bindgen;
42+
use wasm_bindgen::prelude::*;
43+
use wasm_bindgen::js;
44+
45+
#[wasm_bindgen]
46+
pub fn to_precision(this: &js::Number, precision: u8) -> String {
47+
let result = this.to_precision(precision);
48+
let result = match result {
49+
Ok(num) => num,
50+
Err(_err) => "RangeError".to_string()
51+
};
52+
result
53+
}
54+
"#)
55+
.file("test.ts", r#"
56+
import * as assert from "assert";
57+
import * as wasm from "./out";
58+
59+
export function test() {
60+
assert.equal(wasm.to_precision(0.1, 3), "0.100");
61+
assert.equal(wasm.to_precision(10, 101), "RangeError");
62+
}
63+
"#)
64+
.test()
65+
}
66+
67+
#[test]
68+
fn to_string() {
69+
project()
70+
.file("src/lib.rs", r#"
71+
#![feature(proc_macro, wasm_custom_section)]
72+
73+
extern crate wasm_bindgen;
74+
use wasm_bindgen::prelude::*;
75+
use wasm_bindgen::js;
76+
77+
#[wasm_bindgen]
78+
pub fn to_string(this: &js::Number, radix: u8) -> String {
79+
let result = this.to_string(radix);
80+
let result = match result {
81+
Ok(num) => num,
82+
Err(_err) => "RangeError".to_string()
83+
};
84+
result
85+
}
86+
"#)
87+
.file("test.ts", r#"
88+
import * as assert from "assert";
89+
import * as wasm from "./out";
90+
91+
export function test() {
92+
let number = 42;
93+
assert.equal(wasm.to_string(number, 10), "42");
94+
assert.equal(wasm.to_string(233, 16), "e9");
95+
assert.equal(wasm.to_string(number, 100), "RangeError");
96+
}
97+
"#)
98+
.test()
99+
}
100+
101+
#[test]
102+
fn value_of() {
103+
project()
104+
.file("src/lib.rs", r#"
105+
#![feature(proc_macro, wasm_custom_section)]
106+
107+
extern crate wasm_bindgen;
108+
use wasm_bindgen::prelude::*;
109+
use wasm_bindgen::js;
110+
111+
#[wasm_bindgen]
112+
pub fn js_value_of(this: &js::Number) -> js::Number {
113+
this.value_of()
114+
}
115+
"#)
116+
.file("test.ts", r#"
117+
import * as assert from "assert";
118+
import * as wasm from "./out";
119+
120+
export function test() {
121+
let number = 42;
122+
assert.equal(wasm.js_value_of(number), 42);
123+
assert.equal(typeof wasm.js_value_of(number), "number");
124+
}
125+
"#)
126+
.test()
127+
}

tests/all/js_globals/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ mod Object;
66
mod Array;
77
mod ArrayIterator;
88
mod JsString;
9+
mod Number;
910

1011
#[test]
1112
#[cfg(feature = "std")]

0 commit comments

Comments
 (0)