Skip to content

Bindings for Aate.prototype.findIndex(), toLocaleString() #475

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jul 14, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions src/js.rs
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,13 @@ extern "C" {
#[wasm_bindgen(method)]
pub fn find(this: &Array, predicate: &mut FnMut(JsValue, u32, Array) -> bool) -> JsValue;

/// The findIndex() method returns the index of the first element in the array that
/// satisfies the provided testing function. Otherwise -1 is returned.
///
/// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/findIndex
#[wasm_bindgen(method, js_name = findIndex)]
pub fn find_index(this: &Array, predicate: &mut FnMut(JsValue, u32, Array) -> bool, this_arg: &JsValue) -> u32;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think we need this_arg because our closure can't access it. Want to do a follow up to remove it?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for your Advice.
I'll try to remove it 😃


/// The includes() method determines whether an array includes a certain
/// element, returning true or false as appropriate.
///
Expand Down Expand Up @@ -294,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.
///
Expand Down
72 changes: 72 additions & 0 deletions tests/all/js_globals/Array.rs
Original file line number Diff line number Diff line change
Expand Up @@ -895,4 +895,76 @@ fn reduce_right() {
"#,
)
.test()
}

#[test]
fn find_index() {
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_find_first_even_number_index(array: &js::Array) -> u32 {
array.find_index(&mut |el, _, _| el.as_f64().unwrap() % 2. == 0., &JsValue::undefined())
}
"#,
)
.file(
"test.js",
r#"
import * as assert from "assert";
import * as wasm from "./out";

export function test() {
const arrayEven = [2, 4, 6, 8];
const arrayOdd = [1, 3, 5, 7];
const arrayMixed = [3, 5, 7, 10];

assert.equal(wasm.array_find_first_even_number_index(arrayEven), 0);
assert.equal(wasm.array_find_first_even_number_index(arrayOdd), -1);
assert.equal(wasm.array_find_first_even_number_index(arrayMixed), 3);
}
"#,
)
.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()
}