mirror of
https://github.com/rustwasm/wasm-bindgen.git
synced 2024-12-25 19:11:45 +03:00
String - length, trim, trimStart & trimLeft, trimEnd & trimRight
This commit is contained in:
parent
16bc3eb7e7
commit
9f087241a1
43
src/js.rs
43
src/js.rs
@ -566,6 +566,13 @@ extern {
|
|||||||
#[wasm_bindgen(js_name = JsString)]
|
#[wasm_bindgen(js_name = JsString)]
|
||||||
pub type JsString;
|
pub type JsString;
|
||||||
|
|
||||||
|
/// The length property of a String object indicates the length of a string,
|
||||||
|
/// in UTF-16 code units.
|
||||||
|
///
|
||||||
|
/// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/length
|
||||||
|
#[wasm_bindgen(method, getter, structural)]
|
||||||
|
pub fn length(this: &JsString) -> u32;
|
||||||
|
|
||||||
/// The String object's charAt() method returns a new string consisting of the single
|
/// The String object's charAt() method returns a new string consisting of the single
|
||||||
/// UTF-16 code unit located at the specified offset into the string.
|
/// UTF-16 code unit located at the specified offset into the string.
|
||||||
///
|
///
|
||||||
@ -636,6 +643,42 @@ extern {
|
|||||||
/// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/substr
|
/// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/substr
|
||||||
#[wasm_bindgen(method, js_class = "String")]
|
#[wasm_bindgen(method, js_class = "String")]
|
||||||
pub fn substr(this: &JsString, start: i32, length: i32) -> JsString;
|
pub fn substr(this: &JsString, start: i32, length: i32) -> 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.).
|
||||||
|
///
|
||||||
|
/// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/trim
|
||||||
|
#[wasm_bindgen(method, js_class = "String")]
|
||||||
|
pub fn trim(this: &JsString) -> JsString;
|
||||||
|
|
||||||
|
/// The trimEnd() method removes whitespace from the end of a string.
|
||||||
|
/// trimRight() is an alias of this method.
|
||||||
|
///
|
||||||
|
/// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/trimEnd
|
||||||
|
#[wasm_bindgen(method, js_class = "String", js_name = trimEnd)]
|
||||||
|
pub fn trim_end(this: &JsString) -> JsString;
|
||||||
|
|
||||||
|
/// The trimEnd() method removes whitespace from the end of a string.
|
||||||
|
/// trimRight() is an alias of this method.
|
||||||
|
///
|
||||||
|
/// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/trimEnd
|
||||||
|
#[wasm_bindgen(method, js_class = "String", js_name = trimRight)]
|
||||||
|
pub fn trim_right(this: &JsString) -> JsString;
|
||||||
|
|
||||||
|
/// The trimStart() method removes whitespace from the beginning of a string.
|
||||||
|
/// trimLeft() is an alias of this method.
|
||||||
|
///
|
||||||
|
/// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/trimStart
|
||||||
|
#[wasm_bindgen(method, js_class = "String", js_name = trimStart)]
|
||||||
|
pub fn trim_start(this: &JsString) -> JsString;
|
||||||
|
|
||||||
|
/// The trimStart() method removes whitespace from the beginning of a string.
|
||||||
|
/// trimLeft() is an alias of this method.
|
||||||
|
///
|
||||||
|
/// 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;
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'a> From<&'a str> for JsString {
|
impl<'a> From<&'a str> for JsString {
|
||||||
|
@ -2,6 +2,37 @@
|
|||||||
|
|
||||||
use project;
|
use project;
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn length() {
|
||||||
|
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_length(this: &js::JsString) -> u32 {
|
||||||
|
this.length()
|
||||||
|
}
|
||||||
|
|
||||||
|
"#)
|
||||||
|
.file("test.ts", r#"
|
||||||
|
import * as assert from "assert";
|
||||||
|
import * as wasm from "./out";
|
||||||
|
|
||||||
|
export function test() {
|
||||||
|
let x = 'Mozilla';
|
||||||
|
assert.equal(wasm.string_length(x), 7);
|
||||||
|
|
||||||
|
let empty = '';
|
||||||
|
assert.equal(wasm.string_length(empty), 0);
|
||||||
|
}
|
||||||
|
"#)
|
||||||
|
.test()
|
||||||
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn char_at() {
|
fn char_at() {
|
||||||
project()
|
project()
|
||||||
@ -31,6 +62,35 @@ fn char_at() {
|
|||||||
.test()
|
.test()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn char_code_at() {
|
||||||
|
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_char_code_at(this: &js::JsString, index: u32) -> js::Number {
|
||||||
|
this.char_code_at(index)
|
||||||
|
}
|
||||||
|
"#)
|
||||||
|
.file("test.ts", r#"
|
||||||
|
import * as assert from "assert";
|
||||||
|
import * as wasm from "./out";
|
||||||
|
|
||||||
|
var anyString = 'Brave new world';
|
||||||
|
|
||||||
|
export function test() {
|
||||||
|
assert.equal(wasm.string_char_code_at(anyString, 0), 66);
|
||||||
|
assert.ok(isNaN(wasm.string_char_code_at(anyString, 999)));
|
||||||
|
}
|
||||||
|
"#)
|
||||||
|
.test()
|
||||||
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn code_point_at() {
|
fn code_point_at() {
|
||||||
project()
|
project()
|
||||||
@ -92,7 +152,7 @@ fn concat() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn char_code_at() {
|
fn includes() {
|
||||||
project()
|
project()
|
||||||
.file("src/lib.rs", r#"
|
.file("src/lib.rs", r#"
|
||||||
#![feature(proc_macro, wasm_custom_section)]
|
#![feature(proc_macro, wasm_custom_section)]
|
||||||
@ -102,19 +162,93 @@ fn char_code_at() {
|
|||||||
use wasm_bindgen::js;
|
use wasm_bindgen::js;
|
||||||
|
|
||||||
#[wasm_bindgen]
|
#[wasm_bindgen]
|
||||||
pub fn string_char_code_at(this: &js::JsString, index: u32) -> js::Number {
|
pub fn string_includes(this: &js::JsString, search_value: &js::JsString, position: i32) -> bool {
|
||||||
this.char_code_at(index)
|
this.includes(search_value, position)
|
||||||
}
|
}
|
||||||
"#)
|
"#)
|
||||||
.file("test.ts", r#"
|
.file("test.ts", r#"
|
||||||
import * as assert from "assert";
|
import * as assert from "assert";
|
||||||
import * as wasm from "./out";
|
import * as wasm from "./out";
|
||||||
|
|
||||||
var anyString = 'Brave new world';
|
export function test() {
|
||||||
|
let str = "Blue Whale";
|
||||||
|
|
||||||
|
// TODO: remove second parameter once we have optional parameters
|
||||||
|
assert.equal(wasm.string_includes(str, 'Blue', 0), true);
|
||||||
|
assert.equal(wasm.string_includes(str, 'Blute', 0), false);
|
||||||
|
assert.equal(wasm.string_includes(str, 'Whale', 0), true);
|
||||||
|
assert.equal(wasm.string_includes(str, 'Whale', 5), true);
|
||||||
|
assert.equal(wasm.string_includes(str, 'Whale', 7), false);
|
||||||
|
assert.equal(wasm.string_includes(str, '', 0), true);
|
||||||
|
assert.equal(wasm.string_includes(str, '', 16), true);
|
||||||
|
}
|
||||||
|
"#)
|
||||||
|
.test()
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn index_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_index_of(this: &js::JsString, search_value: &js::JsString, from_index: i32) -> i32 {
|
||||||
|
this.index_of(search_value, from_index)
|
||||||
|
}
|
||||||
|
"#)
|
||||||
|
.file("test.ts", r#"
|
||||||
|
import * as assert from "assert";
|
||||||
|
import * as wasm from "./out";
|
||||||
|
|
||||||
export function test() {
|
export function test() {
|
||||||
assert.equal(wasm.string_char_code_at(anyString, 0), 66);
|
let str = "Blue Whale";
|
||||||
assert.ok(isNaN(wasm.string_char_code_at(anyString, 999)));
|
|
||||||
|
// TODO: remove second parameter once we have optional parameters
|
||||||
|
assert.equal(wasm.string_index_of(str, 'Blue', 0), 0);
|
||||||
|
// TODO: remove second parameter once we have optional parameters
|
||||||
|
assert.equal(wasm.string_index_of(str, 'Blute', 0), -1);
|
||||||
|
assert.equal(wasm.string_index_of(str, 'Whale', 0), 5);
|
||||||
|
assert.equal(wasm.string_index_of(str, 'Whale', 5), 5);
|
||||||
|
assert.equal(wasm.string_index_of(str, 'Whale', 7), -1);
|
||||||
|
// TODO: remove second parameter once we have optional parameters
|
||||||
|
assert.equal(wasm.string_index_of(str, '', 0), 0);
|
||||||
|
assert.equal(wasm.string_index_of(str, '', 9), 9);
|
||||||
|
assert.equal(wasm.string_index_of(str, '', 10), 10);
|
||||||
|
assert.equal(wasm.string_index_of(str, '', 11), 10);
|
||||||
|
}
|
||||||
|
"#)
|
||||||
|
.test()
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn slice() {
|
||||||
|
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_slice(this: &js::JsString, start: u32, end: u32) -> js::JsString {
|
||||||
|
this.slice(start, end)
|
||||||
|
}
|
||||||
|
"#)
|
||||||
|
.file("test.ts", r#"
|
||||||
|
import * as assert from "assert";
|
||||||
|
import * as wasm from "./out";
|
||||||
|
|
||||||
|
export function test() {
|
||||||
|
let characters = "acxn18";
|
||||||
|
let subset = wasm.create_slice(characters, 1, 3);
|
||||||
|
|
||||||
|
assert.equal(subset, "cx");
|
||||||
}
|
}
|
||||||
"#)
|
"#)
|
||||||
.test()
|
.test()
|
||||||
@ -190,74 +324,6 @@ fn substring() {
|
|||||||
.test()
|
.test()
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn index_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_index_of(this: &js::JsString, search_value: &js::JsString, from_index: i32) -> i32 {
|
|
||||||
this.index_of(search_value, from_index)
|
|
||||||
}
|
|
||||||
"#)
|
|
||||||
.file("test.ts", r#"
|
|
||||||
import * as assert from "assert";
|
|
||||||
import * as wasm from "./out";
|
|
||||||
|
|
||||||
export function test() {
|
|
||||||
let str = "Blue Whale";
|
|
||||||
|
|
||||||
// TODO: remove second parameter once we have optional parameters
|
|
||||||
assert.equal(wasm.string_index_of(str, 'Blue', 0), 0);
|
|
||||||
// TODO: remove second parameter once we have optional parameters
|
|
||||||
assert.equal(wasm.string_index_of(str, 'Blute', 0), -1);
|
|
||||||
assert.equal(wasm.string_index_of(str, 'Whale', 0), 5);
|
|
||||||
assert.equal(wasm.string_index_of(str, 'Whale', 5), 5);
|
|
||||||
assert.equal(wasm.string_index_of(str, 'Whale', 7), -1);
|
|
||||||
// TODO: remove second parameter once we have optional parameters
|
|
||||||
assert.equal(wasm.string_index_of(str, '', 0), 0);
|
|
||||||
assert.equal(wasm.string_index_of(str, '', 9), 9);
|
|
||||||
assert.equal(wasm.string_index_of(str, '', 10), 10);
|
|
||||||
assert.equal(wasm.string_index_of(str, '', 11), 10);
|
|
||||||
}
|
|
||||||
"#)
|
|
||||||
.test()
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn slice() {
|
|
||||||
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_slice(this: &js::JsString, start: u32, end: u32) -> js::JsString {
|
|
||||||
this.slice(start, end)
|
|
||||||
}
|
|
||||||
"#)
|
|
||||||
.file("test.ts", r#"
|
|
||||||
import * as assert from "assert";
|
|
||||||
import * as wasm from "./out";
|
|
||||||
|
|
||||||
export function test() {
|
|
||||||
let characters = "acxn18";
|
|
||||||
let subset = wasm.create_slice(characters, 1, 3);
|
|
||||||
|
|
||||||
assert.equal(subset, "cx");
|
|
||||||
}
|
|
||||||
"#)
|
|
||||||
.test()
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn substr() {
|
fn substr() {
|
||||||
project()
|
project()
|
||||||
@ -295,7 +361,7 @@ fn substr() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn includes() {
|
fn trim() {
|
||||||
project()
|
project()
|
||||||
.file("src/lib.rs", r#"
|
.file("src/lib.rs", r#"
|
||||||
#![feature(proc_macro, wasm_custom_section)]
|
#![feature(proc_macro, wasm_custom_section)]
|
||||||
@ -305,8 +371,8 @@ fn includes() {
|
|||||||
use wasm_bindgen::js;
|
use wasm_bindgen::js;
|
||||||
|
|
||||||
#[wasm_bindgen]
|
#[wasm_bindgen]
|
||||||
pub fn string_includes(this: &js::JsString, search_value: &js::JsString, position: i32) -> bool {
|
pub fn string_trim(this: &js::JsString) -> js::JsString {
|
||||||
this.includes(search_value, position)
|
this.trim()
|
||||||
}
|
}
|
||||||
"#)
|
"#)
|
||||||
.file("test.ts", r#"
|
.file("test.ts", r#"
|
||||||
@ -314,16 +380,77 @@ fn includes() {
|
|||||||
import * as wasm from "./out";
|
import * as wasm from "./out";
|
||||||
|
|
||||||
export function test() {
|
export function test() {
|
||||||
let str = "Blue Whale";
|
assert.equal(wasm.string_trim(' foo '), 'foo');
|
||||||
|
// Another example of .trim() removing whitespace from just one side.
|
||||||
// TODO: remove second parameter once we have optional parameters
|
assert.equal(wasm.string_trim('foo '), 'foo');
|
||||||
assert.equal(wasm.string_includes(str, 'Blue', 0), true);
|
}
|
||||||
assert.equal(wasm.string_includes(str, 'Blute', 0), false);
|
"#)
|
||||||
assert.equal(wasm.string_includes(str, 'Whale', 0), true);
|
.test()
|
||||||
assert.equal(wasm.string_includes(str, 'Whale', 5), true);
|
}
|
||||||
assert.equal(wasm.string_includes(str, 'Whale', 7), false);
|
|
||||||
assert.equal(wasm.string_includes(str, '', 0), true);
|
#[test]
|
||||||
assert.equal(wasm.string_includes(str, '', 16), true);
|
fn trim_end_and_trim_right() {
|
||||||
|
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_trim_end(this: &js::JsString) -> js::JsString {
|
||||||
|
this.trim_end()
|
||||||
|
}
|
||||||
|
|
||||||
|
#[wasm_bindgen]
|
||||||
|
pub fn string_trim_right(this: &js::JsString) -> js::JsString {
|
||||||
|
this.trim_right()
|
||||||
|
}
|
||||||
|
"#)
|
||||||
|
.file("test.ts", r#"
|
||||||
|
import * as assert from "assert";
|
||||||
|
import * as wasm from "./out";
|
||||||
|
|
||||||
|
export function test() {
|
||||||
|
let greeting = ' Hello world! ';
|
||||||
|
let trimmed = ' Hello world!';
|
||||||
|
assert.equal(wasm.string_trim_end(greeting), trimmed);
|
||||||
|
assert.equal(wasm.string_trim_right(greeting), trimmed);
|
||||||
|
}
|
||||||
|
"#)
|
||||||
|
.test()
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn trim_start_and_trim_left() {
|
||||||
|
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_trim_start(this: &js::JsString) -> js::JsString {
|
||||||
|
this.trim_start()
|
||||||
|
}
|
||||||
|
|
||||||
|
#[wasm_bindgen]
|
||||||
|
pub fn string_trim_left(this: &js::JsString) -> js::JsString {
|
||||||
|
this.trim_left()
|
||||||
|
}
|
||||||
|
"#)
|
||||||
|
.file("test.ts", r#"
|
||||||
|
import * as assert from "assert";
|
||||||
|
import * as wasm from "./out";
|
||||||
|
|
||||||
|
export function test() {
|
||||||
|
let greeting = ' Hello world! ';
|
||||||
|
let trimmed = 'Hello world! ';
|
||||||
|
assert.equal(wasm.string_trim_start(greeting), trimmed);
|
||||||
|
assert.equal(wasm.string_trim_left(greeting), trimmed);
|
||||||
}
|
}
|
||||||
"#)
|
"#)
|
||||||
.test()
|
.test()
|
||||||
|
Loading…
Reference in New Issue
Block a user