From 7b53b1c88ec828f52c2ed613716aac6e113d585c Mon Sep 17 00:00:00 2001 From: Danielle Pham Date: Sat, 18 Aug 2018 21:55:59 -0400 Subject: [PATCH 1/4] 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}"); From 44877880bbf2ebc5062529becdb488c9c58db42b Mon Sep 17 00:00:00 2001 From: Danielle Pham Date: Sun, 19 Aug 2018 14:42:22 -0400 Subject: [PATCH 2/4] Add bindings for String.prototype.replace --- crates/js-sys/src/lib.rs | 14 ++++++++++++++ crates/js-sys/tests/wasm/JsString.js | 8 +++++++- crates/js-sys/tests/wasm/JsString.rs | 16 ++++++++++++++++ 3 files changed, 37 insertions(+), 1 deletion(-) diff --git a/crates/js-sys/src/lib.rs b/crates/js-sys/src/lib.rs index 75d52cec..6cdc1fdd 100644 --- a/crates/js-sys/src/lib.rs +++ b/crates/js-sys/src/lib.rs @@ -3075,6 +3075,20 @@ extern "C" { #[wasm_bindgen(method, js_class = "String")] pub fn repeat(this: &JsString, count: i32) -> JsString; + /// The replace() method returns a new string with some or all matches of a pattern + /// replaced by a replacement. The pattern can be a string or a RegExp, and + /// the replacement can be a string or a function to be called for each match. + /// + /// Note: The original string will remain unchanged. + /// + /// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/replace + #[wasm_bindgen(method, js_class = "String")] + pub fn replace(this: &JsString, pattern: &RegExp, replacement: &str) -> JsString; + + /// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/replace + #[wasm_bindgen(method, js_class = "String", js_name = replace)] + pub fn replace_function(this: &JsString, pattern: &RegExp, replacement: &Function) -> JsString; + /// 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.js b/crates/js-sys/tests/wasm/JsString.js index 04a64a9a..fbaec7ae 100644 --- a/crates/js-sys/tests/wasm/JsString.js +++ b/crates/js-sys/tests/wasm/JsString.js @@ -1 +1,7 @@ -exports.new_string_object = () => new String("hi"); \ No newline at end of file +exports.new_string_object = () => new String("hi"); + +exports.get_replacer_function = function() { + return function upperToHyphenLower(match, offset, string) { + return (offset > 0 ? '-' : '') + match.toLowerCase(); + }; +}; diff --git a/crates/js-sys/tests/wasm/JsString.rs b/crates/js-sys/tests/wasm/JsString.rs index a22fd653..47a3a8fe 100644 --- a/crates/js-sys/tests/wasm/JsString.rs +++ b/crates/js-sys/tests/wasm/JsString.rs @@ -7,6 +7,7 @@ use js_sys::*; #[wasm_bindgen(module = "tests/wasm/JsString.js")] extern { fn new_string_object() -> JsValue; + fn get_replacer_function() -> Function; } #[wasm_bindgen_test] @@ -284,6 +285,21 @@ fn repeat() { assert_eq!(JsString::from("test").repeat(3), "testtesttest"); } +#[wasm_bindgen_test] +fn replace() { + 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("dog", "g"); + let result = js.replace(&re, "ferret"); + + assert_eq!(result, "The quick brown fox jumped over the lazy ferret. If the ferret reacted, was it really lazy?"); + + let js = JsString::from("borderTop"); + let re = RegExp::new("[A-Z]", "g"); + let result = js.replace_function(&re, &get_replacer_function()); + + assert_eq!(result, "border-top"); +} + #[wasm_bindgen_test] fn slice() { let characters = JsString::from("acxn18"); From 8698084a432f7e4882ce4d27eebb150ff01aaf1d Mon Sep 17 00:00:00 2001 From: Danielle Pham Date: Sun, 19 Aug 2018 14:52:10 -0400 Subject: [PATCH 3/4] 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"); From 4f294224f079cfae9d1388edbc07f1f62601f815 Mon Sep 17 00:00:00 2001 From: Danielle Pham Date: Sun, 19 Aug 2018 15:09:45 -0400 Subject: [PATCH 4/4] Add bindings for String.prototype.split --- crates/js-sys/src/lib.rs | 11 ++++++++++ crates/js-sys/tests/wasm/JsString.rs | 33 ++++++++++++++++++++++++++++ 2 files changed, 44 insertions(+) diff --git a/crates/js-sys/src/lib.rs b/crates/js-sys/src/lib.rs index 6c491346..6f36d8f8 100644 --- a/crates/js-sys/src/lib.rs +++ b/crates/js-sys/src/lib.rs @@ -3103,6 +3103,17 @@ extern "C" { #[wasm_bindgen(method, js_class = "String")] pub fn slice(this: &JsString, start: u32, end: u32) -> JsString; + /// The split() method splits a String object into an array of strings by separating the string + /// into substrings, using a specified separator string to determine where to make each split. + /// + /// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/split + #[wasm_bindgen(method, js_class = "String")] + pub fn split(this: &JsString, separator: &str) -> Array; + + /// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/split + #[wasm_bindgen(method, js_class = "String", js_name = split)] + pub fn split_limit(this: &JsString, separator: &str, limit: u32) -> Array; + /// The `startsWith()` method determines whether a string begins with the /// characters of a specified string, returning true or false as /// appropriate. diff --git a/crates/js-sys/tests/wasm/JsString.rs b/crates/js-sys/tests/wasm/JsString.rs index 898ee767..5458e3e7 100644 --- a/crates/js-sys/tests/wasm/JsString.rs +++ b/crates/js-sys/tests/wasm/JsString.rs @@ -321,6 +321,39 @@ fn slice() { assert_eq!(characters.slice(1, 3), "cx"); } +#[wasm_bindgen_test] +fn split() { + let js = JsString::from("Oh brave new world"); + let result = js.split(" "); + + let mut v = Vec::with_capacity(result.length() as usize); + result.for_each(&mut |x, _, _| v.push(x)); + + assert_eq!(v[0], "Oh"); + assert_eq!(v[1], "brave"); + assert_eq!(v[2], "new"); + assert_eq!(v[3], "world"); + + let js = JsString::from("Oct,Nov,Dec"); + let result = js.split(","); + + let mut v = Vec::with_capacity(result.length() as usize); + result.for_each(&mut |x, _, _| v.push(x)); + + assert_eq!(v[0], "Oct"); + assert_eq!(v[1], "Nov"); + assert_eq!(v[2], "Dec"); + + let result = js.split_limit(",", 2); + + let mut v = Vec::with_capacity(result.length() as usize); + result.for_each(&mut |x, _, _| v.push(x)); + + assert_eq!(result.length(), 2); + assert_eq!(v[0], "Oct"); + assert_eq!(v[1], "Nov"); +} + #[wasm_bindgen_test] fn starts_with() { let js = JsString::from("To be, or not to be, that is the question.");