Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

js-sys: add Object.fromEntries #1456

Merged
merged 2 commits into from
Apr 26, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions crates/js-sys/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Object, JsValue>;

/// 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)
Expand Down
21 changes: 21 additions & 0 deletions crates/js-sys/tests/wasm/Object.rs
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,27 @@ 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());
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");
assert_eq!(Reflect::get(object.as_ref(), &"baz".into()).unwrap(), 42);

let not_iterable = Object::new();
let error = Object::from_entries(&not_iterable).unwrap_err();
assert!(error.is_instance_of::<TypeError>());
}

#[wasm_bindgen_test]
fn get_own_property_descriptor() {
let foo = foo_42();
Expand Down