From 7b53b1c88ec828f52c2ed613716aac6e113d585c Mon Sep 17 00:00:00 2001 From: Danielle Pham Date: Sat, 18 Aug 2018 21:55:59 -0400 Subject: [PATCH] Add binding for String.prototype.match --- crates/js-sys/src/lib.rs | 8 ++++++-- crates/js-sys/tests/wasm/JsString.rs | 25 +++++++++++++++++++++++++ 2 files changed, 31 insertions(+), 2 deletions(-) diff --git a/crates/js-sys/src/lib.rs b/crates/js-sys/src/lib.rs index e429d413..75d52cec 100644 --- a/crates/js-sys/src/lib.rs +++ b/crates/js-sys/src/lib.rs @@ -2974,7 +2974,6 @@ extern "C" { #[wasm_bindgen(static_method_of = JsString, js_class = "String", js_name = fromCharCode)] pub fn from_char_code5(a: u32, b: u32, c: u32, d: u32, e: u32) -> JsString; - /// The static String.fromCodePoint() method returns a string created by /// using the specified sequence of code points. /// @@ -3007,7 +3006,6 @@ extern "C" { #[wasm_bindgen(catch, static_method_of = JsString, js_class = "String", js_name = fromCodePoint)] pub fn from_code_point5(a: u32, b: u32, c: u32, d: u32, e: u32) -> Result; - /// The `includes()` method determines whether one string may be found /// within another string, returning true or false as appropriate. /// @@ -3039,6 +3037,12 @@ extern "C" { #[wasm_bindgen(method, js_class = "String", js_name = localeCompare)] pub fn locale_compare(this: &JsString, compare_string: &str, locales: &Array, options: &Object) -> i32; + /// The match() method retrieves the matches when matching a string against a regular expression. + /// + /// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/match + #[wasm_bindgen(method, js_class = "String", js_name = match)] + pub fn match_(this: &JsString, pattern: &RegExp) -> Option; + /// The normalize() method returns the Unicode Normalization Form /// of a given string (if the value isn't a string, it will be converted to one first). /// diff --git a/crates/js-sys/tests/wasm/JsString.rs b/crates/js-sys/tests/wasm/JsString.rs index ca416cc9..a22fd653 100644 --- a/crates/js-sys/tests/wasm/JsString.rs +++ b/crates/js-sys/tests/wasm/JsString.rs @@ -216,6 +216,31 @@ fn locale_compare() { assert!(js_ten.locale_compare(two, &locales, &options) > 0); } +#[wasm_bindgen_test] +fn match_() { + let s = "The quick brown fox jumped over the lazy dog. It barked."; + let re = RegExp::new("[A-Z]", "g"); + let result = JsString::from(s).match_(&re); + let obj = result.unwrap(); + + assert_eq!(Reflect::get(obj.as_ref(), &"0".into()), "T"); + assert_eq!(Reflect::get(obj.as_ref(), &"1".into()), "I"); + + let result = JsString::from("foo").match_(&re); + assert!(result.is_none()); + + let s = "For more information, see Chapter 3.4.5.1"; + let re = RegExp::new("see (chapter \\d+(\\.\\d)*)", "i"); + let result = JsString::from(s).match_(&re); + let obj = result.unwrap(); + + assert_eq!(Reflect::get(obj.as_ref(), &"0".into()), "see Chapter 3.4.5.1"); + assert_eq!(Reflect::get(obj.as_ref(), &"1".into()), "Chapter 3.4.5.1"); + assert_eq!(Reflect::get(obj.as_ref(), &"2".into()), ".1"); + assert_eq!(Reflect::get(obj.as_ref(), &"index".into()), 22); + assert_eq!(Reflect::get(obj.as_ref(), &"input".into()), s); +} + #[wasm_bindgen_test] fn normalize() { let js = JsString::from("\u{1E9B}\u{0323}");