Merge pull request #325 from rail44/string-concat

Support String.prototype.concat
This commit is contained in:
Nick Fitzgerald 2018-06-26 10:34:50 -07:00 committed by GitHub
commit 09969db5cf
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 45 additions and 7 deletions

View File

@ -583,13 +583,11 @@ extern {
#[wasm_bindgen(method, js_class = "String", js_name = charCodeAt)]
pub fn char_code_at(this: &JsString, index: u32) -> Number;
/// The indexOf() method returns the index within the calling String object of
/// the first occurrence of the specified value, starting the search at fromIndex.
/// Returns -1 if the value is not found.
/// The concat() method concatenates the string arguments to the calling string and returns a new string.
///
/// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/indexOf
#[wasm_bindgen(method, js_class = "String", js_name = indexOf)]
pub fn index_of(this: &JsString, search_value: &JsString, from_index: i32) -> i32;
/// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/concat
#[wasm_bindgen(method, js_class = "String")]
pub fn concat(this: &JsString, string_2: &JsString) -> JsString;
/// The includes() method determines whether one string may be found within another string, returning true or false as appropriate.
///
@ -597,6 +595,14 @@ extern {
#[wasm_bindgen(method, js_class = "String")]
pub fn includes(this: &JsString, search_string: &JsString, position: i32) -> bool;
/// The indexOf() method returns the index within the calling String object of
/// the first occurrence of the specified value, starting the search at fromIndex.
/// Returns -1 if the value is not found.
///
/// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/indexOf
#[wasm_bindgen(method, js_class = "String", js_name = indexOf)]
pub fn index_of(this: &JsString, search_value: &JsString, from_index: i32) -> i32;
/// The slice() method extracts a section of a string and returns it as a
/// new string, without modifying the original string.
///

View File

@ -31,6 +31,38 @@ fn char_at() {
.test()
}
#[test]
fn concat() {
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 string_concat(this: &js::JsString, string_2: &js::JsString) -> js::JsString {
this.concat(string_2)
}
"#)
.file("test.ts", r#"
import * as assert from "assert";
import * as wasm from "./out";
export function test() {
// TODO: Implement ability to receive multiple optional arguments
assert.equal(wasm.string_concat('Hello ', 'World'), 'Hello World');
assert.equal(wasm.string_concat('foo', {}), 'foo[object Object]');
assert.equal(wasm.string_concat('foo', []), 'foo');
assert.equal(wasm.string_concat('foo', null), 'foonull');
assert.equal(wasm.string_concat('foo', true), 'footrue');
assert.equal(wasm.string_concat('foo', 1234), 'foo1234');
}
"#)
.test()
}
#[test]
fn char_code_at() {
project()
@ -59,7 +91,7 @@ fn char_code_at() {
"#)
.test()
}
#[test]
fn starts_with() {
project()