add binding for indexOf

This commit is contained in:
Matt Long 2018-06-20 17:16:57 -04:00
parent 289f2a88cf
commit 4a96ba3c72
3 changed files with 54 additions and 0 deletions

View File

@ -94,3 +94,15 @@ extern {
}
// Array
#[wasm_bindgen]
extern {
pub type Array;
/// The indexOf() method returns the first index at which a given element can be found in the array, or -1 if it is not present.
///
/// http://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/indexOf
#[wasm_bindgen(method, js_name = indexOf)]
pub fn index_of(this: &Array, value: JsValue, from_index: i32) -> i32;
}

View File

@ -0,0 +1,41 @@
#![allow(non_snake_case)]
use project;
#[test]
fn index_of() {
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 get_index_of(this: &js::Array, value: JsValue, from_index: i32) -> i32 {
this.index_of(value, from_index)
}
"#)
.file("test.ts", r#"
import * as assert from "assert";
import * as wasm from "./out";
export function test() {
let characters = ["a", "c", "x", "n"];
let index = wasm.get_index_of(characters, "x", 0);
let notFoundIndex = wasm.get_index_of(characters, "z", 0);
assert.equal(index, 2);
assert.equal(notFoundIndex, -1);
let withFromIndex = wasm.get_index_of(characters, "x", -3);
let withFromIndexNotFound = wasm.get_index_of(characters, "a", -2);
assert.equal(withFromIndex, 2);
assert.equal(withFromIndexNotFound, -1);
}
"#)
.test()
}

View File

@ -3,6 +3,7 @@
use super::project;
mod Object;
mod Array;
#[test]
#[cfg(feature = "std")]