From 68acbeab25f4d94ff35769e9d37e735dd2bc2d33 Mon Sep 17 00:00:00 2001 From: Matt Long Date: Wed, 20 Jun 2018 18:03:26 -0400 Subject: [PATCH] add binding for shift --- src/js.rs | 7 +++++++ tests/all/js_globals/Array.rs | 31 +++++++++++++++++++++++++++++++ 2 files changed, 38 insertions(+) diff --git a/src/js.rs b/src/js.rs index 246fa759d..ca1cbc3e7 100644 --- a/src/js.rs +++ b/src/js.rs @@ -158,4 +158,11 @@ extern { /// http://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/reverse #[wasm_bindgen(method)] pub fn reverse(this: &Array) -> Array; + + /// The shift() method removes the first element from an array and returns that removed element. + /// This method changes the length of the array. + /// + /// http://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/shift + #[wasm_bindgen(method)] + pub fn shift(this: &Array) -> JsValue; } \ No newline at end of file diff --git a/tests/all/js_globals/Array.rs b/tests/all/js_globals/Array.rs index 175267b43..2efa8150f 100644 --- a/tests/all/js_globals/Array.rs +++ b/tests/all/js_globals/Array.rs @@ -293,4 +293,35 @@ fn reverse() { } "#) .test() +} + +#[test] +fn shift() { + 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 shift_item(this: &js::Array) -> JsValue { + this.shift() + } + + "#) + .file("test.ts", r#" + import * as assert from "assert"; + import * as wasm from "./out"; + + export function test() { + let characters = [8, 5, 4, 3, 1, 2] + let shiftedItem = wasm.shift_item(characters); + + assert.equal(shiftedItem, 8); + assert.equal(characters.length, 5); + } + "#) + .test() } \ No newline at end of file