Skip to content

Commit 02328cf

Browse files
authored
Merge pull request #310 from elpiel/string-support
`String.prototype.startsWith`, `String.prototype.substring` and `String.prototype.indexOf`
2 parents 76fcbf3 + 4444492 commit 02328cf

File tree

2 files changed

+132
-1
lines changed

2 files changed

+132
-1
lines changed

src/js.rs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -348,13 +348,35 @@ extern {
348348
#[wasm_bindgen(method, js_class = "String", js_name = charAt)]
349349
pub fn char_at(this: &JsString, index: u32) -> JsString;
350350

351+
/// The indexOf() method returns the index within the calling String object of
352+
/// the first occurrence of the specified value, starting the search at fromIndex.
353+
/// Returns -1 if the value is not found.
354+
///
355+
/// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/indexOf
356+
#[wasm_bindgen(method, js_class = "String", js_name = indexOf)]
357+
pub fn index_of(this: &JsString, search_value: &JsString, from_index: i32) -> i32;
358+
351359
/// The slice() method extracts a section of a string and returns it as a
352360
/// new string, without modifying the original string.
353361
///
354362
/// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/slice
355363
#[wasm_bindgen(method, js_class = "String")]
356364
pub fn slice(this: &JsString, start: u32, end: u32) -> JsString;
357365

366+
/// The startsWith() method determines whether a string begins with the characters
367+
/// of a specified string, returning true or false as appropriate.
368+
///
369+
/// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/startsWith
370+
#[wasm_bindgen(method, js_class = "String", js_name = startsWith)]
371+
pub fn starts_with(this: &JsString, search_string: &JsString, position: u32) -> bool;
372+
373+
/// The substring() method returns the part of the string between the start and end indexes,
374+
/// or to the end of the string.
375+
///
376+
/// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/substring
377+
#[wasm_bindgen(method, js_class = "String")]
378+
pub fn substring(this: &JsString, index_start: u32, index_end: u32) -> JsString;
379+
358380
/// The substr() method returns the part of a string between
359381
/// the start index and a number of characters after it.
360382
///

tests/all/js_globals/JsString.rs

100755100644
Lines changed: 110 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,115 @@ fn char_at() {
3131
.test()
3232
}
3333

34+
#[test]
35+
fn starts_with() {
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 string_starts_with(this: &js::JsString, search_string: &js::JsString, position: u32) -> bool {
46+
this.starts_with(search_string, position)
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 str = "To be, or not to be, that is the question.";
55+
56+
// TODO: remove second parameter for both assertions once we have optional parameters
57+
assert.ok(wasm.string_starts_with(str, 'To be', 0));
58+
assert.ok(!wasm.string_starts_with(str, 'not to be', 0));
59+
assert.ok(wasm.string_starts_with(str, 'not to be', 10));
60+
}
61+
"#)
62+
.test()
63+
}
64+
65+
#[test]
66+
fn substring() {
67+
project()
68+
.file("src/lib.rs", r#"
69+
#![feature(proc_macro, wasm_custom_section)]
70+
71+
extern crate wasm_bindgen;
72+
use wasm_bindgen::prelude::*;
73+
use wasm_bindgen::js;
74+
75+
#[wasm_bindgen]
76+
pub fn string_substring(this: &js::JsString, index_start: u32, index_end: u32) -> js::JsString {
77+
this.substring(index_start, index_end)
78+
}
79+
"#)
80+
.file("test.ts", r#"
81+
import * as assert from "assert";
82+
import * as wasm from "./out";
83+
84+
export function test() {
85+
let anyString = "Mozilla";
86+
87+
assert.equal(wasm.string_substring(anyString, 0, 1), 'M');
88+
assert.equal(wasm.string_substring(anyString, 1, 0), 'M');
89+
90+
assert.equal(wasm.string_substring(anyString, 0, 6), 'Mozill');
91+
92+
// TODO: Add test once we have optional parameters
93+
// assert.equal(wasm.string_substring(anyString, 4), 'lla');
94+
assert.equal(wasm.string_substring(anyString, 4, 7), 'lla');
95+
assert.equal(wasm.string_substring(anyString, 7, 4), 'lla');
96+
97+
assert.equal(wasm.string_substring(anyString, 0, 7), 'Mozilla');
98+
assert.equal(wasm.string_substring(anyString, 0, 10), 'Mozilla');
99+
}
100+
"#)
101+
.test()
102+
}
103+
104+
#[test]
105+
fn index_of() {
106+
project()
107+
.file("src/lib.rs", r#"
108+
#![feature(proc_macro, wasm_custom_section)]
109+
110+
extern crate wasm_bindgen;
111+
use wasm_bindgen::prelude::*;
112+
use wasm_bindgen::js;
113+
114+
#[wasm_bindgen]
115+
pub fn string_index_of(this: &js::JsString, search_value: &js::JsString, from_index: i32) -> i32 {
116+
this.index_of(search_value, from_index)
117+
}
118+
"#)
119+
.file("test.ts", r#"
120+
import * as assert from "assert";
121+
import * as wasm from "./out";
122+
123+
export function test() {
124+
let str = "Blue Whale";
125+
126+
// TODO: remove second parameter once we have optional parameters
127+
assert.equal(wasm.string_index_of(str, 'Blue', 0), 0);
128+
// TODO: remove second parameter once we have optional parameters
129+
assert.equal(wasm.string_index_of(str, 'Blute', 0), -1);
130+
assert.equal(wasm.string_index_of(str, 'Whale', 0), 5);
131+
assert.equal(wasm.string_index_of(str, 'Whale', 5), 5);
132+
assert.equal(wasm.string_index_of(str, 'Whale', 7), -1);
133+
// TODO: remove second parameter once we have optional parameters
134+
assert.equal(wasm.string_index_of(str, '', 0), 0);
135+
assert.equal(wasm.string_index_of(str, '', 9), 9);
136+
assert.equal(wasm.string_index_of(str, '', 10), 10);
137+
assert.equal(wasm.string_index_of(str, '', 11), 10);
138+
}
139+
"#)
140+
.test()
141+
}
142+
34143
#[test]
35144
fn slice() {
36145
project()
@@ -94,4 +203,4 @@ fn substr() {
94203
}
95204
"#)
96205
.test()
97-
}
206+
}

0 commit comments

Comments
 (0)