mirror of
https://github.com/rustwasm/wasm-bindgen.git
synced 2024-11-28 23:14:12 +03:00
Merge pull request #307 from elpiel/string-substr
`String.prototype.substr()` support
This commit is contained in:
commit
76fcbf3c44
17
src/js.rs
17
src/js.rs
@ -341,6 +341,13 @@ extern {
|
||||
#[wasm_bindgen(js_name = JsString)]
|
||||
pub type 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;
|
||||
|
||||
/// The slice() method extracts a section of a string and returns it as a
|
||||
/// new string, without modifying the original string.
|
||||
///
|
||||
@ -348,12 +355,12 @@ extern {
|
||||
#[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.
|
||||
/// The substr() method returns the part of a string between
|
||||
/// the start index and a number of characters after it.
|
||||
///
|
||||
/// 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;
|
||||
/// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/substr
|
||||
#[wasm_bindgen(method, js_class = "String")]
|
||||
pub fn substr(this: &JsString, start: i32, length: i32) -> JsString;
|
||||
}
|
||||
|
||||
impl<'a> From<&'a str> for JsString {
|
||||
|
@ -59,3 +59,39 @@ fn slice() {
|
||||
"#)
|
||||
.test()
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn substr() {
|
||||
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 create_substr(this: &js::JsString, start: i32, length: i32) -> js::JsString {
|
||||
this.substr(start, length)
|
||||
}
|
||||
"#)
|
||||
.file("test.ts", r#"
|
||||
import * as assert from "assert";
|
||||
import * as wasm from "./out";
|
||||
|
||||
export function test() {
|
||||
let aString = "Mozilla";
|
||||
|
||||
assert.equal(wasm.create_substr(aString, 0, 1), "M");
|
||||
assert.equal(wasm.create_substr(aString, 1, 0), "");
|
||||
assert.equal(wasm.create_substr(aString, -1, 1), "a");
|
||||
assert.equal(wasm.create_substr(aString, 1, -1), "");
|
||||
// TODO: Uncomment and test these assertions, once we have support for optional parameters
|
||||
// assert.equal(wasm.create_substr(aString, -3), "lla");
|
||||
// assert.equal(wasm.create_substr(aString, 1), "ozilla");
|
||||
assert.equal(wasm.create_substr(aString, -20, 2), "Mo");
|
||||
assert.equal(wasm.create_substr(aString, 20, 2), "");
|
||||
}
|
||||
"#)
|
||||
.test()
|
||||
}
|
Loading…
Reference in New Issue
Block a user