From d29b17f15846939770d1436b3a86dd599d166a99 Mon Sep 17 00:00:00 2001 From: Tyler Laing Date: Thu, 5 Jul 2018 15:05:23 -0700 Subject: [PATCH 1/2] Issue #275: Added Array.prototype.find binding --- src/js.rs | 8 ++++++++ tests/all/js_globals/Array.rs | 38 +++++++++++++++++++++++++++++++++++ 2 files changed, 46 insertions(+) diff --git a/src/js.rs b/src/js.rs index 8db2d1a1..ef60a95c 100644 --- a/src/js.rs +++ b/src/js.rs @@ -134,6 +134,7 @@ extern "C" { #[wasm_bindgen(method)] pub fn fill(this: &Array, value: JsValue, start: u32, end: u32) -> Array; + /// The `filter()` method creates a new array with all elements that pass the /// test implemented by the provided function. /// @@ -141,6 +142,13 @@ extern "C" { #[wasm_bindgen(method)] pub fn filter(this: &Array, predicate: &mut FnMut(JsValue, u32, Array) -> bool) -> Array; + /// The `find()` method returns the value of the first element in the array that satisfies + /// the provided testing function. Otherwise `undefined` is returned. + /// + /// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/find + #[wasm_bindgen(method)] + pub fn find(this: &Array, predicate: &mut FnMut(JsValue, u32, Array) -> bool) -> JsValue; + /// The includes() method determines whether an array includes a certain /// element, returning true or false as appropriate. /// diff --git a/tests/all/js_globals/Array.rs b/tests/all/js_globals/Array.rs index e8ce5119..33b01723 100644 --- a/tests/all/js_globals/Array.rs +++ b/tests/all/js_globals/Array.rs @@ -758,3 +758,41 @@ fn every() { ) .test() } + +#[test] +fn find() { + project() + .file( + "src/lib.rs", + r#" + #![feature(proc_macro, wasm_custom_section)] + + extern crate wasm_bindgen; + use wasm_bindgen::prelude::*; + use wasm_bindgen::js; + + #[wasm_bindgen] + pub fn array_find_first_even_number(array: &js::Array) -> JsValue { + array.find(&mut |el, _, _| el.as_f64().unwrap() % 2f64 == 0f64) + } + "#, + ) + .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(arrayEven), 2); + assert.equal(wasm.array_find_first_even_number(arrayOdd), undefined); + assert.equal(wasm.array_find_first_even_number(arrayMixed), 10); + } + "#, + ) + .test() +} From baf76431bcdff0a40ed7317d1c20655f743c4d49 Mon Sep 17 00:00:00 2001 From: Tyler Laing Date: Thu, 5 Jul 2018 16:02:51 -0700 Subject: [PATCH 2/2] Issue #275: Adding Array.prototype.find. Fixing newline. --- src/js.rs | 1 - 1 file changed, 1 deletion(-) diff --git a/src/js.rs b/src/js.rs index ef60a95c..36f338eb 100644 --- a/src/js.rs +++ b/src/js.rs @@ -134,7 +134,6 @@ extern "C" { #[wasm_bindgen(method)] pub fn fill(this: &Array, value: JsValue, start: u32, end: u32) -> Array; - /// The `filter()` method creates a new array with all elements that pass the /// test implemented by the provided function. ///