diff --git a/src/js.rs b/src/js.rs index a08f20dd..aa2b456b 100644 --- a/src/js.rs +++ b/src/js.rs @@ -1889,6 +1889,14 @@ extern "C" { #[wasm_bindgen(method, js_class = "String", js_name = indexOf)] pub fn index_of(this: &JsString, search_value: &JsString, from_index: i32) -> i32; + /// The `lastIndexOf()` method returns the index within the calling String + /// object of the last occurrence of the specified value, searching + /// backwards from fromIndex. Returns -1 if the value is not found. + /// + /// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/lastIndexOf + #[wasm_bindgen(method, js_class = "String", js_name = lastIndexOf)] + pub fn last_index_of(this: &JsString, search_value: &JsString, from_index: i32) -> i32; + /// The `slice()` method extracts a section of a string and returns it as a /// new string, without modifying the original string. /// diff --git a/tests/all/js_globals/JsString.rs b/tests/all/js_globals/JsString.rs index 417884b1..792c6155 100755 --- a/tests/all/js_globals/JsString.rs +++ b/tests/all/js_globals/JsString.rs @@ -261,6 +261,45 @@ fn index_of() { .test() } +#[test] +fn last_index_of() { + project() + .file("src/lib.rs", r#" + #![feature(use_extern_macros, wasm_custom_section)] + + extern crate wasm_bindgen; + use wasm_bindgen::prelude::*; + use wasm_bindgen::js; + + #[wasm_bindgen] + pub fn string_last_index_of(this: &js::JsString, search_value: &js::JsString, from_index: i32) -> i32 { + this.last_index_of(search_value, from_index) + } + "#) + .file("test.js", r#" + import * as assert from "assert"; + import * as wasm from "./out"; + + export function test() { + let str = "canal"; + let len = str.length; + + // TODO: remove second parameter once we have optional parameters + assert.equal(wasm.string_last_index_of(str, 'a', len), 3); + assert.equal(wasm.string_last_index_of(str, 'a', 2), 1); + assert.equal(wasm.string_last_index_of(str, 'a', 0), -1); + // TODO: remove second parameter once we have optional parameters + assert.equal(wasm.string_last_index_of(str, 'x', len), -1); + assert.equal(wasm.string_last_index_of(str, 'c', -5), 0); + assert.equal(wasm.string_last_index_of(str, 'c', 0), 0); + // TODO: remove second parameter once we have optional parameters + assert.equal(wasm.string_last_index_of(str, '', len), 5); + assert.equal(wasm.string_last_index_of(str, '', 2), 2); + } + "#) + .test() +} + #[test] fn slice() { project()