From a32d2553fcbdc84f060003892f3f9858ef403804 Mon Sep 17 00:00:00 2001 From: Kevin Gibbons Date: Sat, 13 Apr 2019 21:08:07 -0700 Subject: [PATCH 1/2] js-sys: add Object.fromEntries --- crates/js-sys/src/lib.rs | 7 +++++++ crates/js-sys/tests/wasm/Object.rs | 19 +++++++++++++++++++ 2 files changed, 26 insertions(+) diff --git a/crates/js-sys/src/lib.rs b/crates/js-sys/src/lib.rs index f968873d..952da3ed 100644 --- a/crates/js-sys/src/lib.rs +++ b/crates/js-sys/src/lib.rs @@ -2176,6 +2176,13 @@ extern "C" { #[wasm_bindgen(static_method_of = Object)] pub fn freeze(value: &Object) -> Object; + /// The Object.fromEntries() method transforms a list of key-value pairs + /// into an object. + /// + /// [MDN documentation](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/fromEntries) + #[wasm_bindgen(static_method_of = Object, catch, js_name = fromEntries)] + pub fn from_entries(iterable: &JsValue) -> Result; + /// The Object.getOwnPropertyDescriptor() method returns a /// property descriptor for an own property (that is, one directly /// present on an object and not in the object's prototype chain) diff --git a/crates/js-sys/tests/wasm/Object.rs b/crates/js-sys/tests/wasm/Object.rs index cf49b7ac..0f7ed840 100644 --- a/crates/js-sys/tests/wasm/Object.rs +++ b/crates/js-sys/tests/wasm/Object.rs @@ -123,6 +123,25 @@ fn entries() { }); } +#[wasm_bindgen_test] +fn from_entries() { + let array = Array::new(); + let entry_one = Array::new(); + let entry_two = Array::new(); + entry_one.push(&"foo".into()); + entry_one.push(&"bar".into()); + entry_two.push(&"baz".into()); + entry_two.push(&42.into()); + let object = Object::from_entries(&array).unwrap(); + + assert_eq!(Reflect::get(object.as_ref(), &"foo".into()).unwrap(), "bar"); + assert_eq!(Reflect::get(object.as_ref(), &"baz".into()).unwrap(), 42); + + let not_iterable = Object::new(); + let error = Object::from_entries(¬_iterable).unwrap_err(); + assert!(error.is_instance_of::()); +} + #[wasm_bindgen_test] fn get_own_property_descriptor() { let foo = foo_42(); From 70480ad29d91c3e9aa4da4ffd2e46c1fdb786983 Mon Sep 17 00:00:00 2001 From: Kevin Gibbons Date: Thu, 25 Apr 2019 21:17:25 -0700 Subject: [PATCH 2/2] fix test --- crates/js-sys/tests/wasm/Object.rs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/crates/js-sys/tests/wasm/Object.rs b/crates/js-sys/tests/wasm/Object.rs index 0f7ed840..bc7c9b09 100644 --- a/crates/js-sys/tests/wasm/Object.rs +++ b/crates/js-sys/tests/wasm/Object.rs @@ -132,6 +132,8 @@ fn from_entries() { entry_one.push(&"bar".into()); entry_two.push(&"baz".into()); entry_two.push(&42.into()); + array.push(&entry_one); + array.push(&entry_two); let object = Object::from_entries(&array).unwrap(); assert_eq!(Reflect::get(object.as_ref(), &"foo".into()).unwrap(), "bar");