Add bindings for String.from_code_point

This commit is contained in:
Danielle Pham 2018-08-17 20:46:44 -04:00
parent 7a08da9205
commit 27d48ad267
2 changed files with 51 additions and 0 deletions

View File

@ -2973,6 +2973,40 @@ 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.
///
/// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/fromCodePoint
///
/// # Exceptions
///
/// A RangeError is thrown if an invalid Unicode code point is given
///
/// # Notes
///
/// There are a few bindings to `from_code_point` in `js-sys`: `from_code_point1`, `from_code_point2`, etc...
/// with different arities.
#[wasm_bindgen(catch, static_method_of = JsString, js_class = "String", js_name = fromCodePoint)]
pub fn from_code_point1(a: u32) -> Result<JsString, JsValue>;
/// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/fromCodePoint
#[wasm_bindgen(catch, static_method_of = JsString, js_class = "String", js_name = fromCodePoint)]
pub fn from_code_point2(a: u32, b: u32) -> Result<JsString, JsValue>;
/// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/fromCodePoint
#[wasm_bindgen(catch, static_method_of = JsString, js_class = "String", js_name = fromCodePoint)]
pub fn from_code_point3(a: u32, b: u32, c: u32) -> Result<JsString, JsValue>;
/// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/fromCodePoint
#[wasm_bindgen(catch, static_method_of = JsString, js_class = "String", js_name = fromCodePoint)]
pub fn from_code_point4(a: u32, b: u32, c: u32, d: u32) -> Result<JsString, JsValue>;
/// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/fromCodePoint
#[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.
///

View File

@ -85,6 +85,23 @@ fn from_char_code() {
assert_eq!(JsString::from_char_code4(codes[0], codes[1], codes[2], codes[3]), "½+¾=");
}
#[wasm_bindgen_test]
fn from_code_point() {
let s = "☃★♲你";
let codes : Vec<u32> = s.chars()
.map(|char| char as u32)
.collect();
assert_eq!(JsString::from_code_point1(codes[0]).unwrap(), "");
assert_eq!(JsString::from_code_point2(codes[0], codes[1]).unwrap(), "☃★");
assert_eq!(JsString::from_code_point3(codes[0], codes[1], codes[2]).unwrap(), "☃★♲");
assert_eq!(JsString::from_code_point4(codes[0], codes[1], codes[2], codes[3]).unwrap(), "☃★♲你");
assert!(!JsString::from_code_point1(0x10FFFF).is_err());
assert!(JsString::from_code_point1(0x110000).is_err());
assert!(JsString::from_code_point1(u32::max_value()).is_err());
}
#[wasm_bindgen_test]
fn includes() {
let str = JsString::from("Blue Whale");