Skip to content

Commit c6c5afc

Browse files
authored
Merge pull request #475 from dorayakikun/master
Bindings for Aate.prototype.findIndex(), toLocaleString()
2 parents 1e32e91 + 7a7bc6d commit c6c5afc

File tree

2 files changed

+87
-0
lines changed

2 files changed

+87
-0
lines changed

src/js.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,13 @@ extern "C" {
172172
#[wasm_bindgen(method)]
173173
pub fn find(this: &Array, predicate: &mut FnMut(JsValue, u32, Array) -> bool) -> JsValue;
174174

175+
/// The findIndex() method returns the index of the first element in the array that
176+
/// satisfies the provided testing function. Otherwise -1 is returned.
177+
///
178+
/// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/findIndex
179+
#[wasm_bindgen(method, js_name = findIndex)]
180+
pub fn find_index(this: &Array, predicate: &mut FnMut(JsValue, u32, Array) -> bool, this_arg: &JsValue) -> u32;
181+
175182
/// The includes() method determines whether an array includes a certain
176183
/// element, returning true or false as appropriate.
177184
///
@@ -294,6 +301,14 @@ extern "C" {
294301
#[wasm_bindgen(method)]
295302
pub fn sort(this: &Array) -> Array;
296303

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+
297312
/// The toString() method returns a string representing the specified array
298313
/// and its elements.
299314
///

tests/all/js_globals/Array.rs

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -895,4 +895,76 @@ fn reduce_right() {
895895
"#,
896896
)
897897
.test()
898+
}
899+
900+
#[test]
901+
fn find_index() {
902+
project()
903+
.file(
904+
"src/lib.rs",
905+
r#"
906+
#![feature(proc_macro, wasm_custom_section)]
907+
908+
extern crate wasm_bindgen;
909+
use wasm_bindgen::prelude::*;
910+
use wasm_bindgen::js;
911+
use JsValue;
912+
913+
#[wasm_bindgen]
914+
pub fn array_find_first_even_number_index(array: &js::Array) -> u32 {
915+
array.find_index(&mut |el, _, _| el.as_f64().unwrap() % 2. == 0., &JsValue::undefined())
916+
}
917+
"#,
918+
)
919+
.file(
920+
"test.js",
921+
r#"
922+
import * as assert from "assert";
923+
import * as wasm from "./out";
924+
925+
export function test() {
926+
const arrayEven = [2, 4, 6, 8];
927+
const arrayOdd = [1, 3, 5, 7];
928+
const arrayMixed = [3, 5, 7, 10];
929+
930+
assert.equal(wasm.array_find_first_even_number_index(arrayEven), 0);
931+
assert.equal(wasm.array_find_first_even_number_index(arrayOdd), -1);
932+
assert.equal(wasm.array_find_first_even_number_index(arrayMixed), 3);
933+
}
934+
"#,
935+
)
936+
.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()
898970
}

0 commit comments

Comments
 (0)