Skip to content

Commit 36e79d2

Browse files
committed
String - startsWith/substring/indexOf
1 parent 09cf02d commit 36e79d2

File tree

2 files changed

+136
-5
lines changed

2 files changed

+136
-5
lines changed

src/js.rs

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -341,19 +341,41 @@ 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+
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+
344359
/// The slice() method extracts a section of a string and returns it as a
345360
/// new string, without modifying the original string.
346361
///
347362
/// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/slice
348363
#[wasm_bindgen(method, js_class = "String")]
349364
pub fn slice(this: &JsString, start: u32, end: u32) -> JsString;
350365

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.
366+
/// The startsWith() method determines whether a string begins with the characters
367+
/// of a specified string, returning true or false as appropriate.
353368
///
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;
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;
357379
}
358380

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

tests/all/js_globals/JsString.rs

100755100644
Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,3 +59,112 @@ fn slice() {
5959
"#)
6060
.test()
6161
}
62+
63+
#[test]
64+
fn starts_with() {
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 string_starts_with(this: &js::JsString, search_string: &js::JsString, position: u32) -> bool {
75+
this.starts_with(search_string, position)
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 str = "To be, or not to be, that is the question.";
84+
85+
// TODO: remove second parameter for both assertions once we have optional parameters
86+
assert.ok(wasm.string_starts_with(str, 'To be', 0));
87+
assert.ok(!wasm.string_starts_with(str, 'not to be', 0));
88+
assert.ok(wasm.string_starts_with(str, 'not to be', 10));
89+
}
90+
"#)
91+
.test()
92+
}
93+
94+
#[test]
95+
fn substring() {
96+
project()
97+
.file("src/lib.rs", r#"
98+
#![feature(proc_macro, wasm_custom_section)]
99+
100+
extern crate wasm_bindgen;
101+
use wasm_bindgen::prelude::*;
102+
use wasm_bindgen::js;
103+
104+
#[wasm_bindgen]
105+
pub fn string_substring(this: &js::JsString, index_start: u32, index_end: u32) -> js::JsString {
106+
this.substring(index_start, index_end)
107+
}
108+
"#)
109+
.file("test.ts", r#"
110+
import * as assert from "assert";
111+
import * as wasm from "./out";
112+
113+
export function test() {
114+
let anyString = "Mozilla";
115+
116+
assert.equal(wasm.string_substring(anyString, 0, 1), 'M');
117+
assert.equal(wasm.string_substring(anyString, 1, 0), 'M');
118+
119+
assert.equal(wasm.string_substring(anyString, 0, 6), 'Mozill');
120+
121+
// TODO: Add test once we have optional parameters
122+
// assert.equal(wasm.string_substring(anyString, 4), 'lla');
123+
assert.equal(wasm.string_substring(anyString, 4, 7), 'lla');
124+
assert.equal(wasm.string_substring(anyString, 7, 4), 'lla');
125+
126+
assert.equal(wasm.string_substring(anyString, 0, 7), 'Mozilla');
127+
assert.equal(wasm.string_substring(anyString, 0, 10), 'Mozilla');
128+
}
129+
"#)
130+
.test()
131+
}
132+
133+
#[test]
134+
fn index_of() {
135+
project()
136+
.file("src/lib.rs", r#"
137+
#![feature(proc_macro, wasm_custom_section)]
138+
139+
extern crate wasm_bindgen;
140+
use wasm_bindgen::prelude::*;
141+
use wasm_bindgen::js;
142+
143+
#[wasm_bindgen]
144+
pub fn string_index_of(this: &js::JsString, search_value: &js::JsString, from_index: i32) -> i32 {
145+
this.index_of(search_value, from_index)
146+
}
147+
"#)
148+
.file("test.ts", r#"
149+
import * as assert from "assert";
150+
import * as wasm from "./out";
151+
152+
export function test() {
153+
let str = "Blue Whale";
154+
155+
// TODO: remove second parameter once we have optional parameters
156+
assert.equal(wasm.string_index_of(str, 'Blue', 0), 0);
157+
// TODO: remove second parameter once we have optional parameters
158+
assert.equal(wasm.string_index_of(str, 'Blute', 0), -1);
159+
assert.equal(wasm.string_index_of(str, 'Whale', 0), 5);
160+
assert.equal(wasm.string_index_of(str, 'Whale', 5), 5);
161+
assert.equal(wasm.string_index_of(str, 'Whale', 7), -1);
162+
// TODO: remove second parameter once we have optional parameters
163+
assert.equal(wasm.string_index_of(str, '', 0), 0);
164+
assert.equal(wasm.string_index_of(str, '', 9), 9);
165+
assert.equal(wasm.string_index_of(str, '', 10), 10);
166+
assert.equal(wasm.string_index_of(str, '', 11), 10);
167+
}
168+
"#)
169+
.test()
170+
}

0 commit comments

Comments
 (0)