Skip to content

Commit 76fcbf3

Browse files
authored
Merge pull request #307 from elpiel/string-substr
`String.prototype.substr()` support
2 parents 09cf02d + 8e8a02b commit 76fcbf3

File tree

2 files changed

+48
-5
lines changed

2 files changed

+48
-5
lines changed

src/js.rs

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -341,19 +341,26 @@ extern {
341341
#[wasm_bindgen(js_name = JsString)]
342342
pub type JsString;
343343

344+
/// The String object's charAt() method returns a new string consisting of the single
345+
/// UTF-16 code unit located at the specified offset into the string.
346+
///
347+
/// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/charAt
348+
#[wasm_bindgen(method, js_class = "String", js_name = charAt)]
349+
pub fn char_at(this: &JsString, index: u32) -> JsString;
350+
344351
/// The slice() method extracts a section of a string and returns it as a
345352
/// new string, without modifying the original string.
346353
///
347354
/// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/slice
348355
#[wasm_bindgen(method, js_class = "String")]
349356
pub fn slice(this: &JsString, start: u32, end: u32) -> JsString;
350357

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.
358+
/// The substr() method returns the part of a string between
359+
/// the start index and a number of characters after it.
353360
///
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;
361+
/// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/substr
362+
#[wasm_bindgen(method, js_class = "String")]
363+
pub fn substr(this: &JsString, start: i32, length: i32) -> JsString;
357364
}
358365

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

tests/all/js_globals/JsString.rs

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,3 +59,39 @@ fn slice() {
5959
"#)
6060
.test()
6161
}
62+
63+
#[test]
64+
fn substr() {
65+
project()
66+
.file("src/lib.rs", r#"
67+
#![feature(proc_macro, wasm_custom_section)]
68+
69+
extern crate wasm_bindgen;
70+
use wasm_bindgen::prelude::*;
71+
use wasm_bindgen::js;
72+
73+
#[wasm_bindgen]
74+
pub fn create_substr(this: &js::JsString, start: i32, length: i32) -> js::JsString {
75+
this.substr(start, length)
76+
}
77+
"#)
78+
.file("test.ts", r#"
79+
import * as assert from "assert";
80+
import * as wasm from "./out";
81+
82+
export function test() {
83+
let aString = "Mozilla";
84+
85+
assert.equal(wasm.create_substr(aString, 0, 1), "M");
86+
assert.equal(wasm.create_substr(aString, 1, 0), "");
87+
assert.equal(wasm.create_substr(aString, -1, 1), "a");
88+
assert.equal(wasm.create_substr(aString, 1, -1), "");
89+
// TODO: Uncomment and test these assertions, once we have support for optional parameters
90+
// assert.equal(wasm.create_substr(aString, -3), "lla");
91+
// assert.equal(wasm.create_substr(aString, 1), "ozilla");
92+
assert.equal(wasm.create_substr(aString, -20, 2), "Mo");
93+
assert.equal(wasm.create_substr(aString, 20, 2), "");
94+
}
95+
"#)
96+
.test()
97+
}

0 commit comments

Comments
 (0)