mirror of
https://github.com/fluencelabs/wasm-bindgen
synced 2025-03-18 19:20:51 +00:00
Merge pull request #732 from quelledanielle/js_string_bindings
Add remaining String.prototype bindings
This commit is contained in:
commit
f8605108b7
@ -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).
|
||||
///
|
||||
@ -3071,6 +3075,27 @@ 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 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.
|
||||
///
|
||||
@ -3078,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.
|
||||
|
@ -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();
|
||||
};
|
||||
};
|
||||
|
@ -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]
|
||||
@ -216,6 +217,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}");
|
||||
@ -259,12 +285,75 @@ 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 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");
|
||||
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.");
|
||||
|
Loading…
x
Reference in New Issue
Block a user