Skip to content

add bindings for Array.prototype.some() #341

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 1 commit into from
Jun 28, 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
7 changes: 7 additions & 0 deletions src/js.rs
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,13 @@ extern {
#[wasm_bindgen(method)]
pub fn slice(this: &Array, start: u32, end: u32) -> Array;

/// The some() method tests whether at least one element in the array passes the test implemented
/// by the provided function.
/// Note: This method returns false for any condition put on an empty array.
/// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/some
#[wasm_bindgen(method)]
pub fn some(this: &Array, predicate: &mut FnMut(JsValue) -> bool) -> bool;

/// The sort() method sorts the elements of an array in place and returns
/// the array. The sort is not necessarily stable. The default sort
/// order is according to string Unicode code points.
Expand Down
31 changes: 31 additions & 0 deletions tests/all/js_globals/Array.rs
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,37 @@ fn sort() {
.test()
}

#[test]
fn some() {
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 has_elem(array: &js::Array, arg: JsValue) -> bool {
array.some(&mut |elem| arg == elem)
}

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

export function test() {
let elements = ["z", 1, "y", 2];

assert.deepStrictEqual(wasm.has_elem(elements, 2), true);
assert.deepStrictEqual(wasm.has_elem(elements, "y"), true);
assert.deepStrictEqual(wasm.has_elem(elements, "not an element"), false);
}
"#)
.test()
}

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