Skip to content

Commit 6b5974d

Browse files
committed
Add toLocaleString to Number
1 parent f636f7b commit 6b5974d

File tree

2 files changed

+37
-0
lines changed

2 files changed

+37
-0
lines changed

src/js.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,13 @@ extern {
213213
extern {
214214
pub type Number;
215215

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+
216223
/// The toPrecision() method returns a string representing the Number
217224
/// object to the specified precision.
218225
///

tests/all/js_globals/Number.rs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,36 @@
22

33
use super::project;
44

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+
535
#[test]
636
fn to_precision() {
737
project()

0 commit comments

Comments
 (0)