Skip to content

Commit a32d255

Browse files
committed
js-sys: add Object.fromEntries
1 parent 7f4f9ce commit a32d255

File tree

2 files changed

+26
-0
lines changed

2 files changed

+26
-0
lines changed

crates/js-sys/src/lib.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2176,6 +2176,13 @@ extern "C" {
21762176
#[wasm_bindgen(static_method_of = Object)]
21772177
pub fn freeze(value: &Object) -> Object;
21782178

2179+
/// The Object.fromEntries() method transforms a list of key-value pairs
2180+
/// into an object.
2181+
///
2182+
/// [MDN documentation](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/fromEntries)
2183+
#[wasm_bindgen(static_method_of = Object, catch, js_name = fromEntries)]
2184+
pub fn from_entries(iterable: &JsValue) -> Result<Object, JsValue>;
2185+
21792186
/// The Object.getOwnPropertyDescriptor() method returns a
21802187
/// property descriptor for an own property (that is, one directly
21812188
/// present on an object and not in the object's prototype chain)

crates/js-sys/tests/wasm/Object.rs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,25 @@ fn entries() {
123123
});
124124
}
125125

126+
#[wasm_bindgen_test]
127+
fn from_entries() {
128+
let array = Array::new();
129+
let entry_one = Array::new();
130+
let entry_two = Array::new();
131+
entry_one.push(&"foo".into());
132+
entry_one.push(&"bar".into());
133+
entry_two.push(&"baz".into());
134+
entry_two.push(&42.into());
135+
let object = Object::from_entries(&array).unwrap();
136+
137+
assert_eq!(Reflect::get(object.as_ref(), &"foo".into()).unwrap(), "bar");
138+
assert_eq!(Reflect::get(object.as_ref(), &"baz".into()).unwrap(), 42);
139+
140+
let not_iterable = Object::new();
141+
let error = Object::from_entries(&not_iterable).unwrap_err();
142+
assert!(error.is_instance_of::<TypeError>());
143+
}
144+
126145
#[wasm_bindgen_test]
127146
fn get_own_property_descriptor() {
128147
let foo = foo_42();

0 commit comments

Comments
 (0)