diff --git a/src/js.rs b/src/js.rs index 3171daca..e81de9b6 100644 --- a/src/js.rs +++ b/src/js.rs @@ -474,6 +474,16 @@ extern { #[wasm_bindgen(method, js_class = "String", js_name = charAt)] pub fn char_at(this: &JsString, index: u32) -> JsString; + /// The charCodeAt() method returns an integer between 0 and 65535 representing the UTF-16 code unit at + /// the given index (the UTF-16 code unit matches the Unicode code point for code points representable in + /// a single UTF-16 code unit, but might also be the first code unit of a surrogate pair for + /// code points not representable in a single UTF-16 code unit, e.g. Unicode code points > 0x10000). + /// If you want the entire code point value, use codePointAt(). + /// + /// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/charCodeAt + #[wasm_bindgen(method, js_class = "String", js_name = charCodeAt)] + pub fn char_code_at(this: &JsString, index: u32) -> Number; + /// The indexOf() method returns the index within the calling String object of /// the first occurrence of the specified value, starting the search at fromIndex. /// Returns -1 if the value is not found. diff --git a/tests/all/js_globals/JsString.rs b/tests/all/js_globals/JsString.rs index be63c137..c154895d 100644 --- a/tests/all/js_globals/JsString.rs +++ b/tests/all/js_globals/JsString.rs @@ -31,6 +31,35 @@ fn char_at() { .test() } +#[test] +fn char_code_at() { + project() + .file("src/lib.rs", r#" + #![feature(proc_macro, wasm_custom_section)] + + extern crate wasm_bindgen; + use wasm_bindgen::prelude::*; + use wasm_bindgen::js; + + #[wasm_bindgen] + pub fn string_char_code_at(this: &js::JsString, index: u32) -> js::Number { + this.char_code_at(index) + } + "#) + .file("test.ts", r#" + import * as assert from "assert"; + import * as wasm from "./out"; + + var anyString = 'Brave new world'; + + export function test() { + assert.equal(wasm.string_char_code_at(anyString, 0), 66); + assert.ok(isNaN(wasm.string_char_code_at(anyString, 999))); + } + "#) + .test() +} + #[test] fn starts_with() { project()