mirror of
https://github.com/rustwasm/wasm-bindgen.git
synced 2024-12-25 11:02:11 +03:00
String - toString and valueOf
This commit is contained in:
parent
9f087241a1
commit
16517fadcb
12
src/js.rs
12
src/js.rs
@ -644,6 +644,12 @@ extern {
|
||||
#[wasm_bindgen(method, js_class = "String")]
|
||||
pub fn substr(this: &JsString, start: i32, length: i32) -> JsString;
|
||||
|
||||
/// The toString() method returns a string representing the specified object.
|
||||
///
|
||||
/// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/toString
|
||||
#[wasm_bindgen(method, js_class = "String", js_name = toString)]
|
||||
pub fn to_string(this: &JsString) -> JsString;
|
||||
|
||||
/// The trim() method removes whitespace from both ends of a string.
|
||||
/// Whitespace in this context is all the whitespace characters
|
||||
/// (space, tab, no-break space, etc.) and all the line terminator characters (LF, CR, etc.).
|
||||
@ -679,6 +685,12 @@ extern {
|
||||
/// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/trimStart
|
||||
#[wasm_bindgen(method, js_class = "String", js_name = trimLeft)]
|
||||
pub fn trim_left(this: &JsString) -> JsString;
|
||||
|
||||
/// The valueOf() method returns the primitive value of a String object.
|
||||
///
|
||||
/// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/valueOf
|
||||
#[wasm_bindgen(method, js_class = "String", js_name = valueOf)]
|
||||
pub fn value_of(this: &JsString) -> JsString;
|
||||
}
|
||||
|
||||
impl<'a> From<&'a str> for JsString {
|
||||
|
@ -360,6 +360,33 @@ fn substr() {
|
||||
.test()
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn to_string() {
|
||||
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_to_string(this: &js::JsString) -> js::JsString {
|
||||
this.to_string()
|
||||
}
|
||||
"#)
|
||||
.file("test.ts", r#"
|
||||
import * as assert from "assert";
|
||||
import * as wasm from "./out";
|
||||
|
||||
export function test() {
|
||||
let greeting = 'Hello world!';
|
||||
assert.equal(wasm.string_to_string(greeting), 'Hello world!');
|
||||
}
|
||||
"#)
|
||||
.test()
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn trim() {
|
||||
project()
|
||||
@ -455,3 +482,30 @@ fn trim_start_and_trim_left() {
|
||||
"#)
|
||||
.test()
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn value_of() {
|
||||
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_value_of(this: &js::JsString) -> js::JsString {
|
||||
this.value_of()
|
||||
}
|
||||
"#)
|
||||
.file("test.ts", r#"
|
||||
import * as assert from "assert";
|
||||
import * as wasm from "./out";
|
||||
|
||||
export function test() {
|
||||
let greeting = new String('Hello world!');
|
||||
assert.equal(wasm.string_value_of(greeting), 'Hello world!');
|
||||
}
|
||||
"#)
|
||||
.test()
|
||||
}
|
Loading…
Reference in New Issue
Block a user