Add binding for String.prototype.match

This commit is contained in:
Danielle Pham 2018-08-18 21:55:59 -04:00
parent 12a6aaa1bf
commit 7b53b1c88e
2 changed files with 31 additions and 2 deletions

View File

@ -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<JsString, JsValue>;
/// 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<Object>;
/// 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).
///

View File

@ -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}");