From ae847861e7a0f9fa1e8403d77b6a0aab964f3c9f Mon Sep 17 00:00:00 2001 From: Satoshi Amemiya Date: Tue, 26 Jun 2018 20:29:07 +0900 Subject: [PATCH] String - includes() support --- src/js.rs | 6 ++++++ tests/all/js_globals/JsString.rs | 35 ++++++++++++++++++++++++++++++++ 2 files changed, 41 insertions(+) diff --git a/src/js.rs b/src/js.rs index cef43b16..0bdba97a 100644 --- a/src/js.rs +++ b/src/js.rs @@ -455,6 +455,12 @@ extern { #[wasm_bindgen(method, js_class = "String", js_name = indexOf)] 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 /// new string, without modifying the original string. /// diff --git a/tests/all/js_globals/JsString.rs b/tests/all/js_globals/JsString.rs index 9bfe0ba4..be63c137 100644 --- a/tests/all/js_globals/JsString.rs +++ b/tests/all/js_globals/JsString.rs @@ -204,3 +204,38 @@ fn substr() { "#) .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() +}