String - includes() support

This commit is contained in:
Satoshi Amemiya 2018-06-26 20:29:07 +09:00
parent 947dfbeae0
commit ae847861e7
2 changed files with 41 additions and 0 deletions

View File

@ -455,6 +455,12 @@ extern {
#[wasm_bindgen(method, js_class = "String", js_name = indexOf)] #[wasm_bindgen(method, js_class = "String", js_name = indexOf)]
pub fn index_of(this: &JsString, search_value: &JsString, from_index: i32) -> i32; pub fn index_of(this: &JsString, search_value: &JsString, from_index: i32) -> i32;
/// The includes() method determines whether one string may be found within another string, returning true or false as appropriate.
///
/// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/includes
#[wasm_bindgen(method, js_class = "String")]
pub fn includes(this: &JsString, search_string: &JsString, position: i32) -> bool;
/// The slice() method extracts a section of a string and returns it as a /// The slice() method extracts a section of a string and returns it as a
/// new string, without modifying the original string. /// new string, without modifying the original string.
/// ///

View File

@ -204,3 +204,38 @@ fn substr() {
"#) "#)
.test() .test()
} }
#[test]
fn includes() {
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 string_includes(this: &js::JsString, search_value: &js::JsString, position: i32) -> bool {
this.includes(search_value, position)
}
"#)
.file("test.ts", r#"
import * as assert from "assert";
import * as wasm from "./out";
export function test() {
let str = "Blue Whale";
// TODO: remove second parameter once we have optional parameters
assert.equal(wasm.string_includes(str, 'Blue', 0), true);
assert.equal(wasm.string_includes(str, 'Blute', 0), false);
assert.equal(wasm.string_includes(str, 'Whale', 0), true);
assert.equal(wasm.string_includes(str, 'Whale', 5), true);
assert.equal(wasm.string_includes(str, 'Whale', 7), false);
assert.equal(wasm.string_includes(str, '', 0), true);
assert.equal(wasm.string_includes(str, '', 16), true);
}
"#)
.test()
}