mirror of
https://github.com/rustwasm/wasm-bindgen.git
synced 2024-12-25 11:02:11 +03:00
add binding for indexOf
This commit is contained in:
parent
289f2a88cf
commit
4a96ba3c72
12
src/js.rs
12
src/js.rs
@ -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;
|
||||||
|
}
|
41
tests/all/js_globals/Array.rs
Normal file
41
tests/all/js_globals/Array.rs
Normal 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()
|
||||||
|
}
|
@ -3,6 +3,7 @@
|
|||||||
use super::project;
|
use super::project;
|
||||||
|
|
||||||
mod Object;
|
mod Object;
|
||||||
|
mod Array;
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
#[cfg(feature = "std")]
|
#[cfg(feature = "std")]
|
||||||
|
Loading…
Reference in New Issue
Block a user