From 7a7bc6d22e87e823de1b8fa04cca2f633bcb2e08 Mon Sep 17 00:00:00 2001 From: Tomohide Takao Date: Sat, 14 Jul 2018 19:39:15 +0900 Subject: [PATCH] Bindings for Array.prototype.toLocaleString() --- src/js.rs | 8 ++++++++ tests/all/js_globals/Array.rs | 33 +++++++++++++++++++++++++++++++++ 2 files changed, 41 insertions(+) diff --git a/src/js.rs b/src/js.rs index 973b03e0..e9212e80 100644 --- a/src/js.rs +++ b/src/js.rs @@ -301,6 +301,14 @@ extern "C" { #[wasm_bindgen(method)] pub fn sort(this: &Array) -> Array; + /// The toLocaleString() method returns a string representing the elements of the array. + /// The elements are converted to Strings using their toLocaleString methods and these + /// Strings are separated by a locale-specific String (such as a comma “,”). + /// + /// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/toLocaleString + #[wasm_bindgen(method, js_name = toLocaleString)] + pub fn to_locale_string(this: &Array, locales: &JsValue, options: &JsValue) -> JsString; + /// The toString() method returns a string representing the specified array /// and its elements. /// diff --git a/tests/all/js_globals/Array.rs b/tests/all/js_globals/Array.rs index aedb0aa0..47979d73 100644 --- a/tests/all/js_globals/Array.rs +++ b/tests/all/js_globals/Array.rs @@ -934,4 +934,37 @@ fn find_index() { "#, ) .test() +} + +#[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; + use JsValue; + + #[wasm_bindgen] + pub fn array_to_locale_string(array: &js::Array, locale: &JsValue, options: &JsValue) -> js::JsString { + array.to_locale_string(locale, options) + } + "#, + ) + .file( + "test.js", + r#" + import * as assert from "assert"; + import * as wasm from "./out"; + + export function test() { + assert.equal(wasm.array_to_locale_string([1, 'a', new Date('21 Dec 1997 14:12:00 UTC')], 'en', {timeZone: 'UTC'}), '1,a,12/21/1997, 2:12:00 PM'); + } + "#, + ) + .test() } \ No newline at end of file