Skip to content

Commit d28d81f

Browse files
elpielfitzgen
authored andcommitted
Add basic support for String.prototype.charAt() (#306)
* String - charAt() implementation * String - charAt() - add js_class
1 parent 245f0f0 commit d28d81f

File tree

2 files changed

+36
-0
lines changed

2 files changed

+36
-0
lines changed

src/js.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -347,6 +347,13 @@ extern {
347347
/// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/slice
348348
#[wasm_bindgen(method, js_class = "String")]
349349
pub fn slice(this: &JsString, start: u32, end: u32) -> JsString;
350+
351+
/// The String object's charAt() method returns a new string consisting of the single
352+
/// UTF-16 code unit located at the specified offset into the string.
353+
///
354+
/// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/charAt
355+
#[wasm_bindgen(method, js_class = "String", js_name = charAt)]
356+
pub fn char_at(this: &JsString, index: u32) -> JsString;
350357
}
351358

352359
impl<'a> From<&'a str> for JsString {

tests/all/js_globals/JsString.rs

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,35 @@
22

33
use project;
44

5+
#[test]
6+
fn char_at() {
7+
project()
8+
.file("src/lib.rs", r#"
9+
#![feature(proc_macro, wasm_custom_section)]
10+
11+
extern crate wasm_bindgen;
12+
use wasm_bindgen::prelude::*;
13+
use wasm_bindgen::js;
14+
15+
#[wasm_bindgen]
16+
pub fn string_char_at(this: &js::JsString, index: u32) -> js::JsString {
17+
this.char_at(index)
18+
}
19+
"#)
20+
.file("test.ts", r#"
21+
import * as assert from "assert";
22+
import * as wasm from "./out";
23+
24+
var anyString = 'Brave new world';
25+
26+
export function test() {
27+
assert.equal(wasm.string_char_at(anyString, 0), "B");
28+
assert.equal(wasm.string_char_at(anyString, 999), "");
29+
}
30+
"#)
31+
.test()
32+
}
33+
534
#[test]
635
fn slice() {
736
project()

0 commit comments

Comments
 (0)