Skip to content

Commit 4444492

Browse files
committed
Merge branch 'master' into string-support
2 parents 36e79d2 + 76fcbf3 commit 4444492

File tree

2 files changed

+72
-29
lines changed

2 files changed

+72
-29
lines changed

src/js.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -376,6 +376,13 @@ extern {
376376
/// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/substring
377377
#[wasm_bindgen(method, js_class = "String")]
378378
pub fn substring(this: &JsString, index_start: u32, index_end: u32) -> JsString;
379+
380+
/// The substr() method returns the part of a string between
381+
/// the start index and a number of characters after it.
382+
///
383+
/// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/substr
384+
#[wasm_bindgen(method, js_class = "String")]
385+
pub fn substr(this: &JsString, start: i32, length: i32) -> JsString;
379386
}
380387

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

tests/all/js_globals/JsString.rs

Lines changed: 65 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -31,35 +31,6 @@ fn char_at() {
3131
.test()
3232
}
3333

34-
#[test]
35-
fn slice() {
36-
project()
37-
.file("src/lib.rs", r#"
38-
#![feature(proc_macro, wasm_custom_section)]
39-
40-
extern crate wasm_bindgen;
41-
use wasm_bindgen::prelude::*;
42-
use wasm_bindgen::js;
43-
44-
#[wasm_bindgen]
45-
pub fn create_slice(this: &js::JsString, start: u32, end: u32) -> js::JsString {
46-
this.slice(start, end)
47-
}
48-
"#)
49-
.file("test.ts", r#"
50-
import * as assert from "assert";
51-
import * as wasm from "./out";
52-
53-
export function test() {
54-
let characters = "acxn18";
55-
let subset = wasm.create_slice(characters, 1, 3);
56-
57-
assert.equal(subset, "cx");
58-
}
59-
"#)
60-
.test()
61-
}
62-
6334
#[test]
6435
fn starts_with() {
6536
project()
@@ -168,3 +139,68 @@ fn index_of() {
168139
"#)
169140
.test()
170141
}
142+
143+
#[test]
144+
fn slice() {
145+
project()
146+
.file("src/lib.rs", r#"
147+
#![feature(proc_macro, wasm_custom_section)]
148+
149+
extern crate wasm_bindgen;
150+
use wasm_bindgen::prelude::*;
151+
use wasm_bindgen::js;
152+
153+
#[wasm_bindgen]
154+
pub fn create_slice(this: &js::JsString, start: u32, end: u32) -> js::JsString {
155+
this.slice(start, end)
156+
}
157+
"#)
158+
.file("test.ts", r#"
159+
import * as assert from "assert";
160+
import * as wasm from "./out";
161+
162+
export function test() {
163+
let characters = "acxn18";
164+
let subset = wasm.create_slice(characters, 1, 3);
165+
166+
assert.equal(subset, "cx");
167+
}
168+
"#)
169+
.test()
170+
}
171+
172+
#[test]
173+
fn substr() {
174+
project()
175+
.file("src/lib.rs", r#"
176+
#![feature(proc_macro, wasm_custom_section)]
177+
178+
extern crate wasm_bindgen;
179+
use wasm_bindgen::prelude::*;
180+
use wasm_bindgen::js;
181+
182+
#[wasm_bindgen]
183+
pub fn create_substr(this: &js::JsString, start: i32, length: i32) -> js::JsString {
184+
this.substr(start, length)
185+
}
186+
"#)
187+
.file("test.ts", r#"
188+
import * as assert from "assert";
189+
import * as wasm from "./out";
190+
191+
export function test() {
192+
let aString = "Mozilla";
193+
194+
assert.equal(wasm.create_substr(aString, 0, 1), "M");
195+
assert.equal(wasm.create_substr(aString, 1, 0), "");
196+
assert.equal(wasm.create_substr(aString, -1, 1), "a");
197+
assert.equal(wasm.create_substr(aString, 1, -1), "");
198+
// TODO: Uncomment and test these assertions, once we have support for optional parameters
199+
// assert.equal(wasm.create_substr(aString, -3), "lla");
200+
// assert.equal(wasm.create_substr(aString, 1), "ozilla");
201+
assert.equal(wasm.create_substr(aString, -20, 2), "Mo");
202+
assert.equal(wasm.create_substr(aString, 20, 2), "");
203+
}
204+
"#)
205+
.test()
206+
}

0 commit comments

Comments
 (0)