Skip to content

Commit de0ba29

Browse files
belfzalexcrichton
authored andcommitted
implements bindings for Object.is (#537)
* implements bindings for Object.is * adds counterpart test cases for non-equal values
1 parent 82c2dfa commit de0ba29

File tree

2 files changed

+26
-0
lines changed

2 files changed

+26
-0
lines changed

crates/js-sys/src/lib.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1712,6 +1712,12 @@ extern "C" {
17121712
#[wasm_bindgen(method, js_name = hasOwnProperty)]
17131713
pub fn has_own_property(this: &Object, property: &JsValue) -> bool;
17141714

1715+
/// The Object.is() method determines whether two values are the same value.
1716+
///
1717+
/// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/is
1718+
#[wasm_bindgen(static_method_of = Object)]
1719+
pub fn is(value_1: &JsValue, value_2: &JsValue) -> bool;
1720+
17151721
/// The `Object.isExtensible()` method determines if an object is extensible
17161722
/// (whether it can have new properties added to it).
17171723
///

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

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
use wasm_bindgen::prelude::*;
22
use wasm_bindgen_test::*;
3+
use std::f64::NAN;
34
use js_sys::*;
45

56
#[wasm_bindgen]
@@ -48,6 +49,25 @@ fn to_string() {
4849
assert_eq!(foo_42().to_string(), "[object Object]");
4950
}
5051

52+
#[wasm_bindgen_test]
53+
fn is() {
54+
let object = JsValue::from(Object::new());
55+
assert!(Object::is(&object, &object));
56+
assert!(Object::is(&JsValue::undefined(), &JsValue::undefined()));
57+
assert!(Object::is(&JsValue::null(), &JsValue::null()));
58+
assert!(Object::is(&JsValue::TRUE, &JsValue::TRUE));
59+
assert!(Object::is(&JsValue::FALSE, &JsValue::FALSE));
60+
assert!(Object::is(&"foo".into(), &"foo".into()));
61+
assert!(Object::is(&JsValue::from(42), &JsValue::from(42)));
62+
assert!(Object::is(&JsValue::from(NAN), &JsValue::from(NAN)));
63+
64+
let another_object = JsValue::from(Object::new());
65+
assert!(!Object::is(&object, &another_object));
66+
assert!(!Object::is(&JsValue::TRUE, &JsValue::FALSE));
67+
assert!(!Object::is(&"foo".into(), &"bar".into()));
68+
assert!(!Object::is(&JsValue::from(23), &JsValue::from(42)));
69+
}
70+
5171
#[wasm_bindgen_test]
5272
fn is_extensible() {
5373
let object = Object::new();

0 commit comments

Comments
 (0)