From 3d72a90481e662de514f373de3aae08bb2f05e62 Mon Sep 17 00:00:00 2001 From: Satoshi Amemiya Date: Wed, 27 Jun 2018 00:48:09 +0900 Subject: [PATCH] Support String.prototype.concat --- src/js.rs | 7 +++++++ tests/all/js_globals/JsString.rs | 32 ++++++++++++++++++++++++++++++++ 2 files changed, 39 insertions(+) diff --git a/src/js.rs b/src/js.rs index cef43b16..65dabb18 100644 --- a/src/js.rs +++ b/src/js.rs @@ -447,6 +447,13 @@ extern { #[wasm_bindgen(method, js_class = "String", js_name = charAt)] pub fn char_at(this: &JsString, index: u32) -> JsString; + + /// 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/concat + #[wasm_bindgen(method, js_class = "String")] + pub fn concat(this: &JsString, string_2: &JsString) -> JsString; + /// 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. diff --git a/tests/all/js_globals/JsString.rs b/tests/all/js_globals/JsString.rs index 9bfe0ba4..ea302e54 100644 --- a/tests/all/js_globals/JsString.rs +++ b/tests/all/js_globals/JsString.rs @@ -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 starts_with() { project()