From b81dc462205411eb0f365aeccefab836f45d720c Mon Sep 17 00:00:00 2001 From: Lachezar Lechev Date: Sun, 24 Jun 2018 23:03:39 +0200 Subject: [PATCH 1/2] String - substr() support --- src/js.rs | 8 +++++++ tests/all/js_globals/JsString.rs | 36 ++++++++++++++++++++++++++++++++ 2 files changed, 44 insertions(+) diff --git a/src/js.rs b/src/js.rs index c5f0d51e..bf8b175f 100644 --- a/src/js.rs +++ b/src/js.rs @@ -340,6 +340,14 @@ extern { /// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/slice #[wasm_bindgen(method, js_class = "String")] pub fn slice(this: &JsString, start: u32, end: u32) -> JsString; + + /// 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/substr + /// TODO: Add `NaN` and `undefined` support + #[wasm_bindgen(method, js_class = "String")] + pub fn substr(this: &JsString, start: i32, length: i32) -> JsString; } impl<'a> From<&'a str> for JsString { diff --git a/tests/all/js_globals/JsString.rs b/tests/all/js_globals/JsString.rs index 112f2d2b..45c78a73 100755 --- a/tests/all/js_globals/JsString.rs +++ b/tests/all/js_globals/JsString.rs @@ -30,3 +30,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() +} \ No newline at end of file From 8e8a02bf73070c56974be4de8c5addb2dcd7f78c Mon Sep 17 00:00:00 2001 From: Lachezar Lechev Date: Mon, 25 Jun 2018 21:32:48 +0200 Subject: [PATCH 2/2] js.rs - remove todo --- src/js.rs | 1 - 1 file changed, 1 deletion(-) diff --git a/src/js.rs b/src/js.rs index 093382f4..7e718d41 100644 --- a/src/js.rs +++ b/src/js.rs @@ -359,7 +359,6 @@ extern { /// the start index and a number of characters after it. /// /// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/substr - /// TODO: Add `NaN` and `undefined` support #[wasm_bindgen(method, js_class = "String")] pub fn substr(this: &JsString, start: i32, length: i32) -> JsString; }