From 6b5974d1bda6a8a7fe878f1a3f7b8bae97a9fc92 Mon Sep 17 00:00:00 2001 From: Jonathan Sundqvist Date: Sat, 23 Jun 2018 19:03:55 +0200 Subject: [PATCH] Add toLocaleString to Number --- src/js.rs | 7 +++++++ tests/all/js_globals/Number.rs | 30 ++++++++++++++++++++++++++++++ 2 files changed, 37 insertions(+) diff --git a/src/js.rs b/src/js.rs index 4acf60f0..44e41147 100644 --- a/src/js.rs +++ b/src/js.rs @@ -213,6 +213,13 @@ extern { 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. /// diff --git a/tests/all/js_globals/Number.rs b/tests/all/js_globals/Number.rs index fa215fc6..da308342 100644 --- a/tests/all/js_globals/Number.rs +++ b/tests/all/js_globals/Number.rs @@ -2,6 +2,36 @@ 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()