From 667733e9290ce81e56bcdfe0fbbd8e3ac29993d4 Mon Sep 17 00:00:00 2001 From: Matt Long Date: Wed, 20 Jun 2018 17:36:35 -0400 Subject: [PATCH] add binding for lastIndexOf --- src/js.rs | 7 +++++++ tests/all/js_globals/Array.rs | 38 +++++++++++++++++++++++++++++++++++ 2 files changed, 45 insertions(+) diff --git a/src/js.rs b/src/js.rs index 0d39a508..7fd0737c 100644 --- a/src/js.rs +++ b/src/js.rs @@ -105,4 +105,11 @@ extern { /// 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; + + /// The lastIndexOf() method returns the last index at which a given element can be found in the array, or -1 if it is not present. + /// The array is searched backwards, starting at fromIndex. + /// + /// http://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/lastIndexOf + #[wasm_bindgen(method, js_name = lastIndexOf)] + pub fn last_index_of(this: &Array, value: JsValue, from_index: i32) -> i32; } \ No newline at end of file diff --git a/tests/all/js_globals/Array.rs b/tests/all/js_globals/Array.rs index f4699253..98354206 100644 --- a/tests/all/js_globals/Array.rs +++ b/tests/all/js_globals/Array.rs @@ -39,3 +39,41 @@ fn index_of() { "#) .test() } + +#[test] +fn last_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_last_index_of(this: &js::Array, value: JsValue, from_index: i32) -> i32 { + this.last_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", "x", "c", "x", "n"]; + let index = wasm.get_last_index_of(characters, "x", 5); + let notFoundIndex = wasm.get_last_index_of(characters, "z", 5); + + assert.equal(index, 3); + assert.equal(notFoundIndex, -1); + + let withFromIndex = wasm.get_last_index_of(characters, "x", 2); + let withFromIndexNotFound = wasm.get_last_index_of(characters, "x", 0); + + assert.equal(withFromIndex, 1); + assert.equal(withFromIndexNotFound, -1); + } + "#) + .test() +} \ No newline at end of file