From 8698084a432f7e4882ce4d27eebb150ff01aaf1d Mon Sep 17 00:00:00 2001 From: Danielle Pham Date: Sun, 19 Aug 2018 14:52:10 -0400 Subject: [PATCH] Add binding for String.prototype.search --- crates/js-sys/src/lib.rs | 7 +++++++ crates/js-sys/tests/wasm/JsString.rs | 15 +++++++++++++++ 2 files changed, 22 insertions(+) diff --git a/crates/js-sys/src/lib.rs b/crates/js-sys/src/lib.rs index 6cdc1fdd..6c491346 100644 --- a/crates/js-sys/src/lib.rs +++ b/crates/js-sys/src/lib.rs @@ -3089,6 +3089,13 @@ extern "C" { #[wasm_bindgen(method, js_class = "String", js_name = replace)] pub fn replace_function(this: &JsString, pattern: &RegExp, replacement: &Function) -> JsString; + /// The search() method executes a search for a match between + /// a regular expression and this String object. + /// + /// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/search + #[wasm_bindgen(method, js_class = "String")] + pub fn search(this: &JsString, pattern: &RegExp) -> 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/crates/js-sys/tests/wasm/JsString.rs b/crates/js-sys/tests/wasm/JsString.rs index 47a3a8fe..898ee767 100644 --- a/crates/js-sys/tests/wasm/JsString.rs +++ b/crates/js-sys/tests/wasm/JsString.rs @@ -300,6 +300,21 @@ fn replace() { assert_eq!(result, "border-top"); } +#[wasm_bindgen_test] +fn search() { + let js = JsString::from("The quick brown fox jumped over the lazy dog. If the dog reacted, was it really lazy?"); + let re = RegExp::new("[^\\w\\s]", "g"); + + assert_eq!(js.search(&re), 44); + + let js = JsString::from("hey JudE"); + let re1 = RegExp::new("[A-Z]", "g"); + let re2 = RegExp::new("[.]", "g"); + + assert_eq!(js.search(&re1), 4); + assert_eq!(js.search(&re2), -1); +} + #[wasm_bindgen_test] fn slice() { let characters = JsString::from("acxn18");