diff --git a/crates/js-sys/src/lib.rs b/crates/js-sys/src/lib.rs index aab68ce9..9aaad5d8 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 ecdaa6ad..ca416cc9 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");