Merge pull request #323 from rail44/string-char_code_at

Support String.prototype.charCodeAt
This commit is contained in:
Nick Fitzgerald 2018-06-26 10:23:10 -07:00 committed by GitHub
commit e8c6c40f10
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 39 additions and 0 deletions

View File

@ -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.

View File

@ -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()