mirror of
https://github.com/rustwasm/wasm-bindgen.git
synced 2024-11-24 14:42:35 +03:00
Bindings for Array.prototype.findIndex()
This commit is contained in:
parent
696678b8cc
commit
f5035c3841
@ -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;
|
||||
|
||||
/// The includes() method determines whether an array includes a certain
|
||||
/// element, returning true or false as appropriate.
|
||||
///
|
||||
|
@ -896,3 +896,42 @@ 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()
|
||||
}
|
Loading…
Reference in New Issue
Block a user