From 1e27b588e2ef52abec9a3e0763d2b84326986fa7 Mon Sep 17 00:00:00 2001 From: Ivan Enderlin Date: Mon, 20 Aug 2018 11:01:56 +0200 Subject: [PATCH] =?UTF-8?q?feat(js-sys)=20Implement=20`String.replace(&str?= =?UTF-8?q?,=20=E2=80=A6)`.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- crates/js-sys/src/lib.rs | 11 +++++++++-- crates/js-sys/tests/wasm/JsString.rs | 14 ++++++++++++-- 2 files changed, 21 insertions(+), 4 deletions(-) diff --git a/crates/js-sys/src/lib.rs b/crates/js-sys/src/lib.rs index 48e7cd30..37bd4a55 100644 --- a/crates/js-sys/src/lib.rs +++ b/crates/js-sys/src/lib.rs @@ -3082,11 +3082,18 @@ extern "C" { /// /// 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; + pub fn replace(this: &JsString, pattern: &str, 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; + pub fn replace_with_function(this: &JsString, pattern: &str, replacement: &Function) -> JsString; + + #[wasm_bindgen(method, js_class = "String", js_name = replace)] + pub fn replace_by_pattern(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_by_pattern_with_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. diff --git a/crates/js-sys/tests/wasm/JsString.rs b/crates/js-sys/tests/wasm/JsString.rs index 5458e3e7..30ab4771 100644 --- a/crates/js-sys/tests/wasm/JsString.rs +++ b/crates/js-sys/tests/wasm/JsString.rs @@ -287,15 +287,25 @@ fn repeat() { #[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 result = js.replace("dog", "ferret"); + + assert_eq!(result, "The quick brown fox jumped over the lazy ferret. If the dog reacted, was it really lazy?"); + + let js = JsString::from("borderTop"); + let result = js.replace_with_function("T", &get_replacer_function()); + + assert_eq!(result, "border-top"); + 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"); + let result = js.replace_by_pattern(&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()); + let result = js.replace_by_pattern_with_function(&re, &get_replacer_function()); assert_eq!(result, "border-top"); }