diff --git a/src/js.rs b/src/js.rs index bebe3cb9..df26bd7b 100644 --- a/src/js.rs +++ b/src/js.rs @@ -347,6 +347,13 @@ extern { /// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/slice #[wasm_bindgen(method, js_class = "String")] pub fn slice(this: &JsString, start: u32, end: u32) -> JsString; + + /// The String object's charAt() method returns a new string consisting of the single + /// UTF-16 code unit located at the specified offset into the string. + /// + /// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/charAt + #[wasm_bindgen(method, js_class = "String", js_name = charAt)] + pub fn char_at(this: &JsString, index: u32) -> JsString; } impl<'a> From<&'a str> for JsString { diff --git a/tests/all/js_globals/JsString.rs b/tests/all/js_globals/JsString.rs index 112f2d2b..7d687252 100755 --- a/tests/all/js_globals/JsString.rs +++ b/tests/all/js_globals/JsString.rs @@ -2,6 +2,35 @@ use project; +#[test] +fn char_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_at(this: &js::JsString, index: u32) -> js::JsString { + this.char_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_at(anyString, 0), "B"); + assert.equal(wasm.string_char_at(anyString, 999), ""); + } + "#) + .test() +} + #[test] fn slice() { project()