mirror of
https://github.com/rustwasm/wasm-bindgen.git
synced 2024-11-24 06:33:33 +03:00
Set.prototype.forEach
and Array.prototype.forEach
(#504)
* Array.prototype.forEach binding. * Set.prototype.forEach binding.
This commit is contained in:
parent
f0dcdc249c
commit
32fa5724dd
13
src/js.rs
13
src/js.rs
@ -167,6 +167,12 @@ extern "C" {
|
||||
#[wasm_bindgen(method, js_name = findIndex)]
|
||||
pub fn find_index(this: &Array, predicate: &mut FnMut(JsValue, u32, Array) -> bool) -> u32;
|
||||
|
||||
/// The `forEach()` method executes a provided function once for each array element.
|
||||
///
|
||||
/// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/forEach
|
||||
#[wasm_bindgen(method, js_name = forEach)]
|
||||
pub fn for_each(this: &Array, callback: &mut FnMut(JsValue, u32, Array));
|
||||
|
||||
/// The includes() method determines whether an array includes a certain
|
||||
/// element, returning true or false as appropriate.
|
||||
///
|
||||
@ -1784,6 +1790,13 @@ extern {
|
||||
#[wasm_bindgen(method)]
|
||||
pub fn delete(this: &Set, value: &JsValue) -> bool;
|
||||
|
||||
/// The forEach() method executes a provided function once for each value
|
||||
/// in the Set object, in insertion order.
|
||||
///
|
||||
/// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Set/forEach
|
||||
#[wasm_bindgen(method, js_name = forEach)]
|
||||
pub fn for_each(this: &Set, callback: &mut FnMut(JsValue, JsValue, Set));
|
||||
|
||||
/// The `has()` method returns a boolean indicating whether an element with
|
||||
/// the specified value exists in a [`Set`] object or not.
|
||||
///
|
||||
|
@ -969,3 +969,50 @@ fn to_locale_string() {
|
||||
)
|
||||
.test()
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn for_each() {
|
||||
project()
|
||||
.file("src/lib.rs", r#"
|
||||
#![feature(use_extern_macros, wasm_custom_section)]
|
||||
|
||||
extern crate wasm_bindgen;
|
||||
use wasm_bindgen::prelude::*;
|
||||
use wasm_bindgen::js;
|
||||
|
||||
#[wasm_bindgen]
|
||||
pub fn sum_indices_of_evens(array: &js::Array) -> u32 {
|
||||
let mut res = 0;
|
||||
array.for_each(&mut |elem: JsValue, i, _| {
|
||||
match elem.as_f64() {
|
||||
Some(val) if val % 2. == 0. => res += i,
|
||||
_ => { }
|
||||
}
|
||||
});
|
||||
|
||||
res
|
||||
}
|
||||
"#)
|
||||
.file("test.js", r#"
|
||||
import * as assert from "assert";
|
||||
import * as wasm from "./out";
|
||||
|
||||
export function test() {
|
||||
const arrayEven = [2, 4, 6, 8];
|
||||
const arrayEvenExpected = 0 + 1 + 2 + 3;
|
||||
const arrayEvenActual = wasm.sum_indices_of_evens(arrayEven);
|
||||
assert.equal(arrayEvenActual, arrayEvenExpected);
|
||||
|
||||
const arrayOdd = [1, 3, 5, 7];
|
||||
const arrayOddExpected = 0;
|
||||
const arrayOddActual = wasm.sum_indices_of_evens(arrayOdd);
|
||||
assert.equal(arrayOddActual, arrayOddExpected);
|
||||
|
||||
const arrayMixed = [3, 5, 7, 10];
|
||||
const arrayMixedExpected = 3;
|
||||
const arrayMixedActual = wasm.sum_indices_of_evens(arrayMixed);
|
||||
assert.equal(arrayMixedActual, arrayMixedExpected);
|
||||
}
|
||||
"#)
|
||||
.test()
|
||||
}
|
||||
|
@ -95,6 +95,52 @@ fn delete() {
|
||||
.test()
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn for_each() {
|
||||
project()
|
||||
.file("src/lib.rs", r#"
|
||||
#![feature(use_extern_macros, wasm_custom_section)]
|
||||
|
||||
extern crate wasm_bindgen;
|
||||
use wasm_bindgen::prelude::*;
|
||||
use wasm_bindgen::js;
|
||||
|
||||
#[wasm_bindgen]
|
||||
pub fn count_evens(set: &js::Set) -> u32 {
|
||||
let mut res = 0;
|
||||
set.for_each(&mut |value, _, _| {
|
||||
match value.as_f64() {
|
||||
Some(val) if val % 2. == 0. => res += 1,
|
||||
_ => { }
|
||||
}
|
||||
});
|
||||
res
|
||||
}
|
||||
"#)
|
||||
.file("test.js", r#"
|
||||
import * as assert from "assert";
|
||||
import * as wasm from "./out";
|
||||
|
||||
export function test() {
|
||||
let setEven = new Set([2, 4, 6, 8]);
|
||||
let setEvenExpected = 4;
|
||||
let setEvenActual = wasm.count_evens(setEven);
|
||||
assert.equal(setEvenExpected, setEvenActual);
|
||||
|
||||
let setOdd = new Set([1, 3, 5, 7]);
|
||||
let setOddExpected = 0;
|
||||
let setOddActual = wasm.count_evens(setOdd);
|
||||
assert.equal(setOddExpected, setOddActual);
|
||||
|
||||
let setMixed = new Set([3, 5, 7, 10]);
|
||||
let setMixedExpected = 1;
|
||||
let setMixedActual = wasm.count_evens(setMixed);
|
||||
assert.equal(setMixedExpected, setMixedActual);
|
||||
}
|
||||
"#)
|
||||
.test()
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn has() {
|
||||
project()
|
||||
|
Loading…
Reference in New Issue
Block a user