Skip to content

Implement Array.prototype.filter and Array.prototype.find #300

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

Closed
wants to merge 2 commits into from
Closed
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
15 changes: 15 additions & 0 deletions src/js.rs
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,21 @@ extern {
/// http://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/unshift
#[wasm_bindgen(method)]
pub fn unshift(this: &Array, value: JsValue) -> u32;

/// 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, function: JsValue) -> 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, function: JsValue) -> JsValue;

}

// Array Iterator
Expand Down
84 changes: 84 additions & 0 deletions tests/all/js_globals/Array.rs
Original file line number Diff line number Diff line change
Expand Up @@ -516,3 +516,87 @@ fn length() {
"#)
.test()
}

#[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 array_filter(this: &js::Array, function: JsValue) -> js::Array {
this.filter(function)
}
"#)
.file("test.ts", r#"
import * as assert from "assert";
import * as wasm from "./out";

export function test() {
let characters = [8, 5, 4, 3, 1, 2]

// filters by element
let filteredArray1 = wasm.array_filter(characters, function(element: number) {
return element > 4;
});
assert.equal(filteredArray1.length, 2)
assert.equal(filteredArray1[1], 5)

// filters by index and element
let filteredArray2 = wasm.array_filter(characters, function(element: number, index: number) {
return index < 3 && element > 7;
});
assert.equal(filteredArray2.length, 1)
assert.equal(filteredArray2[0], 8)
}
"#)
.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(this: &js::Array, function: JsValue) -> JsValue {
this.find(function)
}
"#)
.file("test.ts", r#"
import * as assert from "assert";
import * as wasm from "./out";

export function test() {
let characters = [8, 5, 4, 3, 1, 2]

// finds item
let foundElement = wasm.array_find(characters, function(element: number) {
return element > 6;
});
assert.equal(foundElement, 8);

// if unfound returns undefined
let unfoundElement1 = wasm.array_find(characters, function(element: number) {
return element > 8;
});
assert.equal(unfoundElement1, undefined);

// if incorrect type returns undefined
let unfoundElement2 = wasm.array_find(characters, function(element: string) {
return element > "a";
});
assert.equal(unfoundElement2, undefined);
}
"#)
.test()
}