Skip to content

Commit 9193218

Browse files
Chris Kolodinalexcrichton
authored andcommitted
add bindings for array.prototype.some() (#341)
following the example set in #314
1 parent 2f465d3 commit 9193218

File tree

2 files changed

+38
-0
lines changed

2 files changed

+38
-0
lines changed

src/js.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,13 @@ extern {
205205
#[wasm_bindgen(method)]
206206
pub fn slice(this: &Array, start: u32, end: u32) -> Array;
207207

208+
/// The some() method tests whether at least one element in the array passes the test implemented
209+
/// by the provided function.
210+
/// Note: This method returns false for any condition put on an empty array.
211+
/// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/some
212+
#[wasm_bindgen(method)]
213+
pub fn some(this: &Array, predicate: &mut FnMut(JsValue) -> bool) -> bool;
214+
208215
/// The sort() method sorts the elements of an array in place and returns
209216
/// the array. The sort is not necessarily stable. The default sort
210217
/// order is according to string Unicode code points.

tests/all/js_globals/Array.rs

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,37 @@ fn sort() {
143143
.test()
144144
}
145145

146+
#[test]
147+
fn some() {
148+
project()
149+
.file("src/lib.rs", r#"
150+
#![feature(proc_macro, wasm_custom_section)]
151+
152+
extern crate wasm_bindgen;
153+
use wasm_bindgen::prelude::*;
154+
use wasm_bindgen::js;
155+
156+
#[wasm_bindgen]
157+
pub fn has_elem(array: &js::Array, arg: JsValue) -> bool {
158+
array.some(&mut |elem| arg == elem)
159+
}
160+
161+
"#)
162+
.file("test.ts", r#"
163+
import * as assert from "assert";
164+
import * as wasm from "./out";
165+
166+
export function test() {
167+
let elements = ["z", 1, "y", 2];
168+
169+
assert.deepStrictEqual(wasm.has_elem(elements, 2), true);
170+
assert.deepStrictEqual(wasm.has_elem(elements, "y"), true);
171+
assert.deepStrictEqual(wasm.has_elem(elements, "not an element"), false);
172+
}
173+
"#)
174+
.test()
175+
}
176+
146177
#[test]
147178
fn last_index_of() {
148179
project()

0 commit comments

Comments
 (0)