Skip to content

Array.prototype.filter #314

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jun 25, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion src/js.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ extern {
#[wasm_bindgen(method, js_name = copyWithin)]
pub fn copy_within(this: &Array, target: i32, start: i32, end: i32) -> Array;

///The concat() method is used to merge two or more arrays. This method
/// The concat() method is used to merge two or more arrays. This method
/// does not change the existing arrays, but instead returns a new array.
///
/// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/concat
Expand All @@ -91,6 +91,13 @@ extern {
#[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.
///
/// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/filter
#[wasm_bindgen(method)]
pub fn filter(this: &Array, predicate: &mut FnMut(JsValue, u32, Array) -> bool) -> Array;

/// The length property of an object which is an instance of type Array
/// sets or returns the number of elements in that array. The value is an
/// unsigned, 32-bit integer that is always numerically greater than the
Expand Down
33 changes: 33 additions & 0 deletions tests/all/js_globals/Array.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,39 @@

use project;

#[test]
fn filter() {
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 keep_numbers(array: &js::Array) -> js::Array {
array.filter(&mut |x, _, _| x.as_f64().is_some())
}

"#)
.file("test.ts", r#"
import * as assert from "assert";
import * as wasm from "./out";

export function test() {
let characters = ["a", "c", "x", "n"];
let numbers = [1, 2, 3, 4];
let mixed = ["a", 1, "b", 2];

assert.deepStrictEqual(wasm.keep_numbers(characters), []);
assert.deepStrictEqual(wasm.keep_numbers(numbers), numbers);
assert.deepStrictEqual(wasm.keep_numbers(mixed), [1, 2]);
}
"#)
.test()
}

#[test]
fn index_of() {
project()
Expand Down