Add bindings for String.prototype.replace

This commit is contained in:
Danielle Pham
2018-08-19 14:42:22 -04:00
parent 7b53b1c88e
commit 44877880bb
3 changed files with 37 additions and 1 deletions

View File

@ -1 +1,7 @@
exports.new_string_object = () => new String("hi");
exports.new_string_object = () => new String("hi");
exports.get_replacer_function = function() {
return function upperToHyphenLower(match, offset, string) {
return (offset > 0 ? '-' : '') + match.toLowerCase();
};
};

View File

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