From f7b511588b24aa0312a49ecbebe8d0f4be2f42d6 Mon Sep 17 00:00:00 2001 From: Michael Hoffmann Date: Wed, 19 Sep 2018 21:32:05 +0200 Subject: [PATCH] Add binding for Object.entries() --- crates/js-sys/src/lib.rs | 10 ++++++++++ crates/js-sys/tests/wasm/Object.rs | 13 +++++++++++++ 2 files changed, 23 insertions(+) diff --git a/crates/js-sys/src/lib.rs b/crates/js-sys/src/lib.rs index 08531aa4..b09f582d 100644 --- a/crates/js-sys/src/lib.rs +++ b/crates/js-sys/src/lib.rs @@ -2014,6 +2014,16 @@ extern "C" { #[wasm_bindgen(static_method_of = Object, js_name = defineProperties)] pub fn define_properties(obj: &Object, props: &Object) -> Object; + /// The Object.entries() method returns an array of a given + /// object's own enumerable property [key, value] pairs, in the + /// same order as that provided by a for...in loop (the difference + /// being that a for-in loop enumerates properties in the + /// prototype chain as well). + /// + /// [MDN documentation](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/entries) + #[wasm_bindgen(static_method_of = Object)] + pub fn entries(object: &Object) -> Array; + /// The `Object.freeze()` method freezes an object: that is, prevents new /// properties from being added to it; prevents existing properties from /// being removed; and prevents existing properties, or their enumerability, diff --git a/crates/js-sys/tests/wasm/Object.rs b/crates/js-sys/tests/wasm/Object.rs index ccd37a9a..52e6fa50 100644 --- a/crates/js-sys/tests/wasm/Object.rs +++ b/crates/js-sys/tests/wasm/Object.rs @@ -110,6 +110,19 @@ fn define_properties() { assert!(foo.has_own_property(&"car".into())); } +#[wasm_bindgen_test] +fn entries() { + let entries = Object::entries(&foo_42()); + assert_eq!(entries.length(), 1); + entries.for_each(&mut |x, _, _| { + assert!(x.is_object()); + let array: Array = x.into(); + assert_eq!(array.shift(), "foo"); + assert_eq!(array.shift(), 42); + assert_eq!(array.length(), 0); + }); +} + #[wasm_bindgen_test] fn get_own_property_descriptor() { let foo = foo_42();