feat(js-sys) Implement String.replace(&str, …).

This commit is contained in:
Ivan Enderlin 2018-08-20 11:01:56 +02:00
parent 6d49c76bc4
commit 1e27b588e2
No known key found for this signature in database
GPG Key ID: 461F31FCE31F4AAD
2 changed files with 21 additions and 4 deletions

View File

@ -3082,11 +3082,18 @@ extern "C" {
/// ///
/// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/replace /// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/replace
#[wasm_bindgen(method, js_class = "String")] #[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 /// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/replace
#[wasm_bindgen(method, js_class = "String", js_name = 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 /// The search() method executes a search for a match between
/// a regular expression and this String object. /// a regular expression and this String object.

View File

@ -287,15 +287,25 @@ fn repeat() {
#[wasm_bindgen_test] #[wasm_bindgen_test]
fn replace() { 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 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 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?"); 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 js = JsString::from("borderTop");
let re = RegExp::new("[A-Z]", "g"); 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"); assert_eq!(result, "border-top");
} }