From 6f90bd677b4665418dd8dfd4f61197b496473cb7 Mon Sep 17 00:00:00 2001 From: Jannik Keye Date: Thu, 28 Jun 2018 21:55:55 +0200 Subject: [PATCH] feat(Map): add Map.set --- src/js.rs | 7 +++++++ tests/all/js_globals/Map.rs | 29 +++++++++++++++++++++++++++++ 2 files changed, 36 insertions(+) diff --git a/src/js.rs b/src/js.rs index 1e5d5f97..fae6379d 100644 --- a/src/js.rs +++ b/src/js.rs @@ -338,6 +338,13 @@ extern { /// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map #[wasm_bindgen(constructor)] pub fn new() -> Map; + + /// The set() method adds or updates an element with a specified key + /// and value to a Map object. + /// + /// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map/set + #[wasm_bindgen(method)] + pub fn set(this: &Map, key: &JsValue, value: &JsValue) -> Map; } // Math diff --git a/tests/all/js_globals/Map.rs b/tests/all/js_globals/Map.rs index 2bbdddd9..049b0df6 100644 --- a/tests/all/js_globals/Map.rs +++ b/tests/all/js_globals/Map.rs @@ -152,4 +152,33 @@ fn new() { } "#) .test() +} + +#[test] +fn set() { + 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 set(this: &js::Map, key: &JsValue, value: &JsValue) -> js::Map { + this.set(key, value) + } + "#) + .file("test.ts", r#" + import * as assert from "assert"; + import * as wasm from "./out"; + + export function test() { + const map = new Map(); + const newMap = wasm.set(map, 'foo', 'bar'); + assert.equal(map.has('foo'), true); + assert.equal(newMap.has('foo'), true); + } + "#) + .test() } \ No newline at end of file