Skip to content

Commit 947dfbe

Browse files
authored
Merge pull request #314 from fitzgen/Array.prototype.filter
Array.prototype.filter
2 parents 5adda0d + 0851025 commit 947dfbe

File tree

2 files changed

+41
-1
lines changed

2 files changed

+41
-1
lines changed

src/js.rs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ extern {
7777
#[wasm_bindgen(method, js_name = copyWithin)]
7878
pub fn copy_within(this: &Array, target: i32, start: i32, end: i32) -> Array;
7979

80-
///The concat() method is used to merge two or more arrays. This method
80+
/// The concat() method is used to merge two or more arrays. This method
8181
/// does not change the existing arrays, but instead returns a new array.
8282
///
8383
/// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/concat
@@ -91,6 +91,13 @@ extern {
9191
#[wasm_bindgen(method)]
9292
pub fn fill(this: &Array, value: JsValue, start: u32, end: u32) -> Array;
9393

94+
/// The `filter()` method creates a new array with all elements that pass the
95+
/// test implemented by the provided function.
96+
///
97+
/// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/filter
98+
#[wasm_bindgen(method)]
99+
pub fn filter(this: &Array, predicate: &mut FnMut(JsValue, u32, Array) -> bool) -> Array;
100+
94101
/// The length property of an object which is an instance of type Array
95102
/// sets or returns the number of elements in that array. The value is an
96103
/// unsigned, 32-bit integer that is always numerically greater than the

tests/all/js_globals/Array.rs

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,39 @@
22

33
use project;
44

5+
#[test]
6+
fn filter() {
7+
project()
8+
.file("src/lib.rs", r#"
9+
#![feature(proc_macro, wasm_custom_section)]
10+
11+
extern crate wasm_bindgen;
12+
use wasm_bindgen::prelude::*;
13+
use wasm_bindgen::js;
14+
15+
#[wasm_bindgen]
16+
pub fn keep_numbers(array: &js::Array) -> js::Array {
17+
array.filter(&mut |x, _, _| x.as_f64().is_some())
18+
}
19+
20+
"#)
21+
.file("test.ts", r#"
22+
import * as assert from "assert";
23+
import * as wasm from "./out";
24+
25+
export function test() {
26+
let characters = ["a", "c", "x", "n"];
27+
let numbers = [1, 2, 3, 4];
28+
let mixed = ["a", 1, "b", 2];
29+
30+
assert.deepStrictEqual(wasm.keep_numbers(characters), []);
31+
assert.deepStrictEqual(wasm.keep_numbers(numbers), numbers);
32+
assert.deepStrictEqual(wasm.keep_numbers(mixed), [1, 2]);
33+
}
34+
"#)
35+
.test()
36+
}
37+
538
#[test]
639
fn index_of() {
740
project()

0 commit comments

Comments
 (0)