From 07e61e11750efd2fae16d3f32afebd5a68cf9f49 Mon Sep 17 00:00:00 2001 From: Jannik Keye Date: Thu, 28 Jun 2018 21:54:10 +0200 Subject: [PATCH] feat(Map): add Map.has --- 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 e53ae6da..171c9632 100644 --- a/src/js.rs +++ b/src/js.rs @@ -324,6 +324,13 @@ extern { /// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map/get #[wasm_bindgen(method)] pub fn get(this: &Map, key: &JsValue) -> JsValue; + + /// The has() method returns a boolean indicating whether an element with + /// the specified key exists or not. + /// + /// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map/has + #[wasm_bindgen(method)] + pub fn has(this: &Map, key: &JsValue) -> bool; } // Math diff --git a/tests/all/js_globals/Map.rs b/tests/all/js_globals/Map.rs index 1279c482..5c31c4c2 100644 --- a/tests/all/js_globals/Map.rs +++ b/tests/all/js_globals/Map.rs @@ -95,4 +95,33 @@ fn get() { } "#) .test() +} + +#[test] +fn has() { + 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 has(this: &js::Map, key: &JsValue) -> bool { + this.has(key) + } + "#) + .file("test.ts", r#" + import * as assert from "assert"; + import * as wasm from "./out"; + + export function test() { + const map = new Map(); + map.set('foo', 'bar'); + assert.equal(wasm.has(map, 'foo'), true); + assert.equal(wasm.has(map, 'bar'), false); + } + "#) + .test() } \ No newline at end of file