From 27d48ad267ad12bf8365caa40b940066d4426e86 Mon Sep 17 00:00:00 2001 From: Danielle Pham Date: Fri, 17 Aug 2018 20:46:44 -0400 Subject: [PATCH] Add bindings for String.from_code_point --- crates/js-sys/src/lib.rs | 34 ++++++++++++++++++++++++++++ crates/js-sys/tests/wasm/JsString.rs | 17 ++++++++++++++ 2 files changed, 51 insertions(+) diff --git a/crates/js-sys/src/lib.rs b/crates/js-sys/src/lib.rs index 67cff622c..855c4dc93 100644 --- a/crates/js-sys/src/lib.rs +++ b/crates/js-sys/src/lib.rs @@ -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; + + /// 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; + + /// 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; + + /// 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; + + /// 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; + + /// The `includes()` method determines whether one string may be found /// within another string, returning true or false as appropriate. /// diff --git a/crates/js-sys/tests/wasm/JsString.rs b/crates/js-sys/tests/wasm/JsString.rs index 66fc5771e..74f3eac8c 100644 --- a/crates/js-sys/tests/wasm/JsString.rs +++ b/crates/js-sys/tests/wasm/JsString.rs @@ -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 = 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");