From dacf406dbdad92a56e68f658af73a5c9b6cad15f Mon Sep 17 00:00:00 2001 From: belfz Date: Wed, 27 Jun 2018 13:45:47 +0200 Subject: [PATCH] implements Array.prototype.every() --- src/js.rs | 7 +++++++ tests/all/js_globals/Array.rs | 32 ++++++++++++++++++++++++++++++++ 2 files changed, 39 insertions(+) diff --git a/src/js.rs b/src/js.rs index 6ece49ac..49c5e565 100644 --- a/src/js.rs +++ b/src/js.rs @@ -103,6 +103,13 @@ extern { #[wasm_bindgen(method)] pub fn concat(this: &Array, array: &Array) -> Array; + /// The every() method tests whether all elements in the array pass the test + /// implemented by the provided function. + /// + /// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/every + #[wasm_bindgen(method)] + pub fn every(this: &Array, predicate: &mut FnMut(JsValue, u32, Array) -> bool) -> bool; + /// The fill() method fills all the elements of an array from a start index /// to an end index with a static value. The end index is not included. /// diff --git a/tests/all/js_globals/Array.rs b/tests/all/js_globals/Array.rs index c2569cfe..8e8173de 100755 --- a/tests/all/js_globals/Array.rs +++ b/tests/all/js_globals/Array.rs @@ -589,3 +589,35 @@ fn length() { "#) .test() } + +#[test] +fn every() { + 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_every_number_is_even(array: &js::Array) -> bool { + array.every(&mut |el, _, _| el.as_f64().unwrap() % 2f64 == 0f64) + } + "#) + .file("test.ts", 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 = [2, 3, 4, 5]; + + assert(wasm.array_every_number_is_even(arrayEven)); + assert(!wasm.array_every_number_is_even(arrayOdd)); + assert(!wasm.array_every_number_is_even(arrayMixed)); + } + "#) + .test() +}