Skip to content

Commit 7a7bc6d

Browse files
committed
Bindings for Array.prototype.toLocaleString()
1 parent f5035c3 commit 7a7bc6d

File tree

2 files changed

+41
-0
lines changed

2 files changed

+41
-0
lines changed

src/js.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -301,6 +301,14 @@ extern "C" {
301301
#[wasm_bindgen(method)]
302302
pub fn sort(this: &Array) -> Array;
303303

304+
/// The toLocaleString() method returns a string representing the elements of the array.
305+
/// The elements are converted to Strings using their toLocaleString methods and these
306+
/// Strings are separated by a locale-specific String (such as a comma “,”).
307+
///
308+
/// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/toLocaleString
309+
#[wasm_bindgen(method, js_name = toLocaleString)]
310+
pub fn to_locale_string(this: &Array, locales: &JsValue, options: &JsValue) -> JsString;
311+
304312
/// The toString() method returns a string representing the specified array
305313
/// and its elements.
306314
///

tests/all/js_globals/Array.rs

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -934,4 +934,37 @@ fn find_index() {
934934
"#,
935935
)
936936
.test()
937+
}
938+
939+
#[test]
940+
fn to_locale_string() {
941+
project()
942+
.file(
943+
"src/lib.rs",
944+
r#"
945+
#![feature(proc_macro, wasm_custom_section)]
946+
947+
extern crate wasm_bindgen;
948+
use wasm_bindgen::prelude::*;
949+
use wasm_bindgen::js;
950+
use JsValue;
951+
952+
#[wasm_bindgen]
953+
pub fn array_to_locale_string(array: &js::Array, locale: &JsValue, options: &JsValue) -> js::JsString {
954+
array.to_locale_string(locale, options)
955+
}
956+
"#,
957+
)
958+
.file(
959+
"test.js",
960+
r#"
961+
import * as assert from "assert";
962+
import * as wasm from "./out";
963+
964+
export function test() {
965+
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');
966+
}
967+
"#,
968+
)
969+
.test()
937970
}

0 commit comments

Comments
 (0)