String - lastIndexOf (#490)

This commit is contained in:
data-pup
2018-07-17 13:12:36 -04:00
committed by Alex Crichton
parent 9d27b44a4a
commit 5f2f30dba1
2 changed files with 47 additions and 0 deletions

View File

@ -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.
///

View File

@ -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()