Skip to content

Commit c26caf6

Browse files
data-pupalexcrichton
authored andcommitted
String - padEnd, padStart (#493)
1 parent 5f2f30d commit c26caf6

File tree

2 files changed

+128
-31
lines changed

2 files changed

+128
-31
lines changed

src/js.rs

+49-31
Original file line numberDiff line numberDiff line change
@@ -172,9 +172,9 @@ extern "C" {
172172
#[wasm_bindgen(method)]
173173
pub fn find(this: &Array, predicate: &mut FnMut(JsValue, u32, Array) -> bool) -> JsValue;
174174

175-
/// The findIndex() method returns the index of the first element in the array that
175+
/// The findIndex() method returns the index of the first element in the array that
176176
/// satisfies the provided testing function. Otherwise -1 is returned.
177-
///
177+
///
178178
/// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/findIndex
179179
#[wasm_bindgen(method, js_name = findIndex)]
180180
pub fn find_index(this: &Array, predicate: &mut FnMut(JsValue, u32, Array) -> bool) -> u32;
@@ -223,12 +223,12 @@ extern "C" {
223223
#[wasm_bindgen(method, getter, structural)]
224224
pub fn length(this: &Array) -> u32;
225225

226-
/// map calls a provided callback function once for each element in an array,
227-
/// in order, and constructs a new array from the results. callback is invoked
228-
/// only for indexes of the array which have assigned values, including undefined.
229-
/// It is not called for missing elements of the array (that is, indexes that have
226+
/// map calls a provided callback function once for each element in an array,
227+
/// in order, and constructs a new array from the results. callback is invoked
228+
/// only for indexes of the array which have assigned values, including undefined.
229+
/// It is not called for missing elements of the array (that is, indexes that have
230230
/// never been set, which have been deleted or which have never been assigned a value).
231-
///
231+
///
232232
/// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/map
233233
#[wasm_bindgen(method)]
234234
pub fn map(this: &Array, predicate: &mut FnMut(JsValue, u32, Array) -> JsValue) -> Array;
@@ -247,16 +247,16 @@ extern "C" {
247247
#[wasm_bindgen(method)]
248248
pub fn push(this: &Array, value: JsValue) -> u32;
249249

250-
/// The reduce() method applies a function against an accumulator and each element in
250+
/// The reduce() method applies a function against an accumulator and each element in
251251
/// the array (from left to right) to reduce it to a single value.
252-
///
252+
///
253253
/// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/Reduce
254254
#[wasm_bindgen(method)]
255255
pub fn reduce(this: &Array, predicate: &mut FnMut(JsValue, JsValue, u32, Array) -> JsValue, initial_value: JsValue) -> JsValue;
256256

257-
/// The reduceRight() method applies a function against an accumulator and each value
257+
/// The reduceRight() method applies a function against an accumulator and each value
258258
/// of the array (from right-to-left) to reduce it to a single value.
259-
///
259+
///
260260
/// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/ReduceRight
261261
#[wasm_bindgen(method, js_name = reduceRight)]
262262
pub fn reduce_right(this: &Array, predicate: &mut FnMut(JsValue, JsValue, u32, Array) -> JsValue, initial_value: JsValue) -> JsValue;
@@ -301,10 +301,10 @@ extern "C" {
301301
#[wasm_bindgen(method)]
302302
pub fn sort(this: &Array) -> Array;
303303

304-
/// The toLocaleString() method returns a string representing the elements of the array.
304+
/// The toLocaleString() method returns a string representing the elements of the array.
305305
/// The elements are converted to Strings using their toLocaleString methods and these
306306
/// Strings are separated by a locale-specific String (such as a comma “,”).
307-
///
307+
///
308308
/// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/toLocaleString
309309
#[wasm_bindgen(method, js_name = toLocaleString)]
310310
pub fn to_locale_string(this: &Array, locales: &JsValue, options: &JsValue) -> JsString;
@@ -342,7 +342,7 @@ extern "C" {
342342

343343
/// The `isView()` method returns true if arg is one of the `ArrayBuffer`
344344
/// views, such as typed array objects or a DataView; false otherwise.
345-
///
345+
///
346346
/// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/ArrayBuffer/isView
347347
#[wasm_bindgen(static_method_of = ArrayBuffer, js_name = isView)]
348348
pub fn is_view(value: JsValue) -> bool;
@@ -412,7 +412,7 @@ extern "C" {
412412
extern "C" {
413413
pub type DataView;
414414

415-
/// The `DataView` view provides a low-level interface for reading and
415+
/// The `DataView` view provides a low-level interface for reading and
416416
/// writing multiple number types in an `ArrayBuffer` irrespective of the
417417
/// platform's endianness.
418418
///
@@ -421,7 +421,7 @@ extern "C" {
421421
pub fn new(buffer: &ArrayBuffer, byteOffset: usize, byteLength: usize) -> DataView;
422422

423423
/// The ArrayBuffer referenced by this view. Fixed at construction time and thus read only.
424-
///
424+
///
425425
/// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/DataView/buffer
426426
#[wasm_bindgen(method, getter, structural)]
427427
pub fn buffer(this: &DataView) -> ArrayBuffer;
@@ -435,12 +435,12 @@ extern "C" {
435435

436436
/// The offset (in bytes) of this view from the start of its ArrayBuffer.
437437
/// Fixed at construction time and thus read only.
438-
///
438+
///
439439
/// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/DataView/byteOffset
440440
#[wasm_bindgen(method, getter, structural, js_name = byteOffset)]
441441
pub fn byte_offset(this: &DataView) -> usize;
442442

443-
/// The getInt8() method gets a signed 8-bit integer (byte) at the
443+
/// The getInt8() method gets a signed 8-bit integer (byte) at the
444444
/// specified byte offset from the start of the DataView.
445445
///
446446
/// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/DataView/getInt8
@@ -463,7 +463,7 @@ extern "C" {
463463

464464
/// The getUint16() an unsigned 16-bit integer (unsigned byte) at the specified
465465
/// byte offset from the start of the view.
466-
///
466+
///
467467
/// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/DataView/getUint16
468468
#[wasm_bindgen(method, js_name = getUint16)]
469469
pub fn get_uint16(this: &DataView, byte_offset: usize) -> u16;
@@ -477,21 +477,21 @@ extern "C" {
477477

478478
/// The getUint32() an unsigned 16-bit integer (unsigned byte) at the specified
479479
/// byte offset from the start of the view.
480-
///
480+
///
481481
/// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/DataView/getUint32
482482
#[wasm_bindgen(method, js_name = getUint32)]
483483
pub fn get_uint32(this: &DataView, byte_offset: usize) -> u32;
484484

485-
/// The getFloat32() method gets a signed 32-bit float (float) at the specified
485+
/// The getFloat32() method gets a signed 32-bit float (float) at the specified
486486
/// byte offset from the start of the DataView.
487-
///
487+
///
488488
/// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/DataView/getFloat32
489489
#[wasm_bindgen(method, js_name = getFloat32)]
490490
pub fn get_float32(this: &DataView, byte_offset: usize) -> f32;
491491

492-
/// The getFloat64() method gets a signed 32-bit float (float) at the specified
492+
/// The getFloat64() method gets a signed 32-bit float (float) at the specified
493493
/// byte offset from the start of the DataView.
494-
///
494+
///
495495
/// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/DataView/getFloat64
496496
#[wasm_bindgen(method, js_name = getFloat64)]
497497
pub fn get_float64(this: &DataView, byte_offset: usize) -> f64;
@@ -505,7 +505,7 @@ extern "C" {
505505

506506
/// The setUint8() method stores an unsigned 8-bit integer (byte) value at the
507507
/// specified byte offset from the start of the DataView.
508-
///
508+
///
509509
/// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/DataView/setUint8
510510
#[wasm_bindgen(method, js_name = setUint8)]
511511
pub fn set_uint8(this: &DataView, byte_offset: usize, value: u8);
@@ -519,7 +519,7 @@ extern "C" {
519519

520520
/// The setUint16() method stores an unsigned 16-bit integer (byte) value at the
521521
/// specified byte offset from the start of the DataView.
522-
///
522+
///
523523
/// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/DataView/setUint16
524524
#[wasm_bindgen(method, js_name = setUint16)]
525525
pub fn set_uint16(this: &DataView, byte_offset: usize, value: u16);
@@ -533,21 +533,21 @@ extern "C" {
533533

534534
/// The setUint32() method stores an unsigned 32-bit integer (byte) value at the
535535
/// specified byte offset from the start of the DataView.
536-
///
536+
///
537537
/// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/DataView/setUint32
538538
#[wasm_bindgen(method, js_name = setUint32)]
539539
pub fn set_uint32(this: &DataView, byte_offset: usize, value: u32);
540540

541-
/// The setFloat32() method stores a signed 32-bit float (float) value at the
541+
/// The setFloat32() method stores a signed 32-bit float (float) value at the
542542
/// specified byte offset from the start of the DataView.
543-
///
543+
///
544544
/// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/DataView/setFloat32
545545
#[wasm_bindgen(method, js_name = setFloat32)]
546546
pub fn set_float32(this: &DataView, byte_offset: usize, value: f32);
547547

548-
/// The setFloat64() method stores a signed 64-bit float (float) value at the
548+
/// The setFloat64() method stores a signed 64-bit float (float) value at the
549549
/// specified byte offset from the start of the DataView.
550-
///
550+
///
551551
/// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/DataView/setFloat64
552552
#[wasm_bindgen(method, js_name = setFloat64)]
553553
pub fn set_float64(this: &DataView, byte_offset: usize, value: f64);
@@ -1897,6 +1897,24 @@ extern "C" {
18971897
#[wasm_bindgen(method, js_class = "String", js_name = lastIndexOf)]
18981898
pub fn last_index_of(this: &JsString, search_value: &JsString, from_index: i32) -> i32;
18991899

1900+
/// The `padEnd()` method pads the current string with a given string
1901+
/// (repeated, if needed) so that the resulting string reaches a given
1902+
/// length. The padding is applied from the end (right) of the current
1903+
/// string.
1904+
///
1905+
/// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/padEnd
1906+
#[wasm_bindgen(method, js_class = "String", js_name = padEnd)]
1907+
pub fn pad_end(this: &JsString, target_length: u32, pad_string: &JsString) -> JsString;
1908+
1909+
/// The `padStart()` method pads the current string with another string
1910+
/// (repeated, if needed) so that the resulting string reaches the given
1911+
/// length. The padding is applied from the start (left) of the current
1912+
/// string.
1913+
///
1914+
/// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/padStart
1915+
#[wasm_bindgen(method, js_class = "String", js_name = padStart)]
1916+
pub fn pad_start(this: &JsString, target_length: u32, pad_string: &JsString) -> JsString;
1917+
19001918
/// The `slice()` method extracts a section of a string and returns it as a
19011919
/// new string, without modifying the original string.
19021920
///

tests/all/js_globals/JsString.rs

+79
Original file line numberDiff line numberDiff line change
@@ -300,6 +300,85 @@ fn last_index_of() {
300300
.test()
301301
}
302302

303+
#[test]
304+
fn pad_end() {
305+
project()
306+
.file("src/lib.rs", r#"
307+
#![feature(use_extern_macros, wasm_custom_section)]
308+
309+
extern crate wasm_bindgen;
310+
use wasm_bindgen::prelude::*;
311+
use wasm_bindgen::js;
312+
313+
#[wasm_bindgen]
314+
pub fn string_pad_end(
315+
this: &js::JsString,
316+
target_length: u32,
317+
pad_string: &js::JsString
318+
) -> js::JsString
319+
{
320+
this.pad_end(target_length, pad_string)
321+
}
322+
"#)
323+
.file("test.js", r#"
324+
import * as assert from "assert";
325+
import * as wasm from "./out";
326+
327+
export function test() {
328+
let str = "abc";
329+
330+
// TODO: remove second parameter once we have optional parameters
331+
assert.equal(wasm.string_pad_end(str, 10, " "), "abc ");
332+
// TODO: remove second parameter once we have optional parameters
333+
assert.equal(wasm.string_pad_end(str, 10, " "), "abc ");
334+
assert.equal(wasm.string_pad_end(str, 10, "foo"), "abcfoofoof");
335+
assert.equal(wasm.string_pad_end(str, 6, "123456"), "abc123");
336+
// TODO: remove second parameter once we have optional parameters
337+
assert.equal(wasm.string_pad_end(str, 1, " "), "abc");
338+
}
339+
"#)
340+
.test()
341+
}
342+
343+
#[test]
344+
fn pad_start() {
345+
project()
346+
.file("src/lib.rs", r#"
347+
#![feature(use_extern_macros, wasm_custom_section)]
348+
349+
extern crate wasm_bindgen;
350+
use wasm_bindgen::prelude::*;
351+
use wasm_bindgen::js;
352+
353+
#[wasm_bindgen]
354+
pub fn string_pad_start(
355+
this: &js::JsString,
356+
target_length: u32,
357+
pad_string: &js::JsString
358+
) -> js::JsString
359+
{
360+
this.pad_start(target_length, pad_string)
361+
}
362+
"#)
363+
.file("test.js", r#"
364+
import * as assert from "assert";
365+
import * as wasm from "./out";
366+
367+
export function test() {
368+
let str = "abc";
369+
370+
// TODO: remove second parameter once we have optional parameters
371+
assert.equal(wasm.string_pad_start(str, 10, " "), " abc");
372+
assert.equal(wasm.string_pad_start(str, 10, "foo"), "foofoofabc");
373+
assert.equal(wasm.string_pad_start(str, 6, "123465"), "123abc");
374+
assert.equal(wasm.string_pad_start(str, 8, "0"), "00000abc");
375+
// TODO: remove second parameter once we have optional parameters
376+
assert.equal(wasm.string_pad_start(str, 1, " "), "abc");
377+
}
378+
"#)
379+
.test()
380+
}
381+
303382
#[test]
304383
fn slice() {
305384
project()

0 commit comments

Comments
 (0)