add binding for includes

This commit is contained in:
Matt Long 2018-06-20 18:27:10 -04:00
parent eb6c2a239c
commit a8cd428850
2 changed files with 43 additions and 0 deletions

View File

@ -177,4 +177,11 @@ extern {
/// http://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/toString
#[wasm_bindgen(method, js_name = toString)]
pub fn to_string(this: &Array) -> String;
/// The includes() method determines whether an array includes a certain element, returning true or false as appropriate.
///
/// http://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/includes
#[wasm_bindgen(method)]
pub fn includes(this: &Array, value: JsValue, from_index: i32) -> bool;
}

View File

@ -386,3 +386,39 @@ fn to_string() {
"#)
.test()
}
#[test]
fn includes() {
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_includes(this: &js::Array, value: JsValue, from_index: i32) -> bool {
this.includes(value, from_index)
}
"#)
.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]
let isTwoIncluded = wasm.array_includes(characters, 2, 0);
let isNineIncluded = wasm.array_includes(characters, 9, 0);
assert.ok(isTwoIncluded);
assert.ok(!isNineIncluded);
let isThreeIncluded = wasm.array_includes(characters, 3, 4);
assert.ok(!isThreeIncluded);
}
"#)
.test()
}