diff --git a/src/js.rs b/src/js.rs index df26bd7b..2cdc6ce4 100644 --- a/src/js.rs +++ b/src/js.rs @@ -341,6 +341,21 @@ extern { #[wasm_bindgen(js_name = JsString)] pub type JsString; + /// The String object's charAt() method returns a new string consisting of the single + /// UTF-16 code unit located at the specified offset into the string. + /// + /// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/charAt + #[wasm_bindgen(method, js_class = "String", js_name = charAt)] + pub fn char_at(this: &JsString, index: u32) -> JsString; + + /// The indexOf() method returns the index within the calling String object of + /// the first occurrence of the specified value, starting the search at fromIndex. + /// Returns -1 if the value is not found. + /// + /// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/indexOf + #[wasm_bindgen(method, js_class = "String", js_name = indexOf)] + pub fn 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. /// @@ -348,12 +363,19 @@ extern { #[wasm_bindgen(method, js_class = "String")] pub fn slice(this: &JsString, start: u32, end: u32) -> JsString; - /// The String object's charAt() method returns a new string consisting of the single - /// UTF-16 code unit located at the specified offset into the string. + /// The startsWith() method determines whether a string begins with the characters + /// of a specified string, returning true or false as appropriate. /// - /// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/charAt - #[wasm_bindgen(method, js_class = "String", js_name = charAt)] - pub fn char_at(this: &JsString, index: u32) -> JsString; + /// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/startsWith + #[wasm_bindgen(method, js_class = "String", js_name = startsWith)] + pub fn starts_with(this: &JsString, search_string: &JsString, position: u32) -> bool; + + /// The substring() method returns the part of the string between the start and end indexes, + /// or to the end of the string. + /// + /// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/substring + #[wasm_bindgen(method, js_class = "String")] + pub fn substring(this: &JsString, index_start: u32, index_end: u32) -> JsString; } impl<'a> From<&'a str> for JsString { diff --git a/tests/all/js_globals/JsString.rs b/tests/all/js_globals/JsString.rs old mode 100755 new mode 100644 index 7d687252..cc2852d5 --- a/tests/all/js_globals/JsString.rs +++ b/tests/all/js_globals/JsString.rs @@ -59,3 +59,112 @@ fn slice() { "#) .test() } + +#[test] +fn starts_with() { + 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_starts_with(this: &js::JsString, search_string: &js::JsString, position: u32) -> bool { + this.starts_with(search_string, position) + } + "#) + .file("test.ts", r#" + import * as assert from "assert"; + import * as wasm from "./out"; + + export function test() { + let str = "To be, or not to be, that is the question."; + + // TODO: remove second parameter for both assertions once we have optional parameters + assert.ok(wasm.string_starts_with(str, 'To be', 0)); + assert.ok(!wasm.string_starts_with(str, 'not to be', 0)); + assert.ok(wasm.string_starts_with(str, 'not to be', 10)); + } + "#) + .test() +} + +#[test] +fn substring() { + 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_substring(this: &js::JsString, index_start: u32, index_end: u32) -> js::JsString { + this.substring(index_start, index_end) + } + "#) + .file("test.ts", r#" + import * as assert from "assert"; + import * as wasm from "./out"; + + export function test() { + let anyString = "Mozilla"; + + assert.equal(wasm.string_substring(anyString, 0, 1), 'M'); + assert.equal(wasm.string_substring(anyString, 1, 0), 'M'); + + assert.equal(wasm.string_substring(anyString, 0, 6), 'Mozill'); + + // TODO: Add test once we have optional parameters + // assert.equal(wasm.string_substring(anyString, 4), 'lla'); + assert.equal(wasm.string_substring(anyString, 4, 7), 'lla'); + assert.equal(wasm.string_substring(anyString, 7, 4), 'lla'); + + assert.equal(wasm.string_substring(anyString, 0, 7), 'Mozilla'); + assert.equal(wasm.string_substring(anyString, 0, 10), 'Mozilla'); + } + "#) + .test() +} + +#[test] +fn 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 string_index_of(this: &js::JsString, search_value: &js::JsString, from_index: i32) -> i32 { + this.index_of(search_value, from_index) + } + "#) + .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_index_of(str, 'Blue', 0), 0); + // TODO: remove second parameter once we have optional parameters + assert.equal(wasm.string_index_of(str, 'Blute', 0), -1); + assert.equal(wasm.string_index_of(str, 'Whale', 0), 5); + assert.equal(wasm.string_index_of(str, 'Whale', 5), 5); + assert.equal(wasm.string_index_of(str, 'Whale', 7), -1); + // TODO: remove second parameter once we have optional parameters + assert.equal(wasm.string_index_of(str, '', 0), 0); + assert.equal(wasm.string_index_of(str, '', 9), 9); + assert.equal(wasm.string_index_of(str, '', 10), 10); + assert.equal(wasm.string_index_of(str, '', 11), 10); + } + "#) + .test() +}