From 7da0d96b1effd4302aab03460999e7973162f6fb Mon Sep 17 00:00:00 2001 From: jmagaram Date: Mon, 13 Mar 2023 20:57:04 -0700 Subject: [PATCH 01/10] Object.is documentation --- src/Core__Object.res | 35 +++- test/ObjectTests.mjs | 437 +++++++++++++++++++++++++++++++++++++++++++ test/ObjectTests.res | 56 ++++++ test/TestSuite.mjs | 19 +- test/TestSuite.res | 1 + 5 files changed, 544 insertions(+), 4 deletions(-) create mode 100644 test/ObjectTests.mjs create mode 100644 test/ObjectTests.res diff --git a/src/Core__Object.res b/src/Core__Object.res index e2e14c50..bd72a01c 100644 --- a/src/Core__Object.res +++ b/src/Core__Object.res @@ -1,6 +1,39 @@ @obj external empty: unit => {..} = "" -@val external is: ('a, 'b) => bool = "Object.is" +/** +`Object.is` determines if two objects are identical in all contexts. Objects, arrays, records, and other non-primitives are only identical if they reference the **exact** same object in memory. Primitives like ints, floats, bools, and strings are identical if they have the same value. + +**Note:** In most scenarios use `==` and `===`. If the data type you want to compare by value has an `equals` function, use it. + +The `==` operator is different in ReScript than Javascript. Non-primitives like arrays and records are considered equal if they have the same contents. + +In ReScript, the `===` operator performs a strict equality check, like Javascript, and is similar to (but not idential to) `Object.is`. See the references. + +## Examples +```rescript +Object.is(25, 13) // false +Object.is("abc", "abc") // true +Object.is(undefined, undefined) // true +Object.is(undefined, null) // false +Object.is(-0.0, 0.0) // false +Object.is(list{1, 2}, list{1, 2}) // false + +let fruit = {"name": "Apple" } +Object.is(fruit, fruit) // true +Object.is(fruit, {"name": "Apple" }) // false +fruit == {"name": "Apple" } // true +fruit === {"name": "Apple" } // false + +Object.is([1, 2, 3], [1, 2, 3]) // false +[1, 2, 3] == [1, 2, 3] // true +[1, 2, 3] === [1, 2, 3] // false +``` +## Specifications +- [ECMAScript Language Specification](https://tc39.es/ecma262/multipage/fundamental-objects.html#sec-object.is) +- [`Object.is on Mozilla`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/is) +*/ +@val +external is: ('a, 'b) => bool = "Object.is" @val external create: {..} => {..} = "Object.create" @val external createWithProperties: ({..}, {..}) => {..} = "Object.create" diff --git a/test/ObjectTests.mjs b/test/ObjectTests.mjs new file mode 100644 index 00000000..0d1cdd8a --- /dev/null +++ b/test/ObjectTests.mjs @@ -0,0 +1,437 @@ +// Generated by ReScript, PLEASE EDIT WITH CARE + +import * as Test from "./Test.mjs"; +import * as Caml_obj from "rescript/lib/es6/caml_obj.js"; + +var eq = Caml_obj.equal; + +Test.run([ + [ + "ObjectTests.res", + 7, + 20, + 41 + ], + "is: different types" + ], Object.is("abc", false), eq, false); + +Test.run([ + [ + "ObjectTests.res", + 9, + 20, + 30 + ], + "is: ints" + ], Object.is(25, 25), eq, true); + +Test.run([ + [ + "ObjectTests.res", + 11, + 20, + 33 + ], + "is: strings" + ], Object.is("abc", "abc"), eq, true); + +Test.run([ + [ + "ObjectTests.res", + 12, + 20, + 33 + ], + "is: strings" + ], Object.is("abc", "ABC"), eq, false); + +Test.run([ + [ + "ObjectTests.res", + 14, + 20, + 44 + ], + "is: null and undefined" + ], Object.is(null, undefined), eq, false); + +Test.run([ + [ + "ObjectTests.res", + 15, + 20, + 44 + ], + "is: null and undefined" + ], Object.is(undefined, undefined), eq, true); + +Test.run([ + [ + "ObjectTests.res", + 16, + 20, + 44 + ], + "is: null and undefined" + ], Object.is(null, null), eq, true); + +Test.run([ + [ + "ObjectTests.res", + 17, + 20, + 44 + ], + "is: null and undefined" + ], Object.is(null, undefined), eq, false); + +Test.run([ + [ + "ObjectTests.res", + 18, + 20, + 44 + ], + "is: undefined and None" + ], Object.is(undefined, undefined), eq, true); + +var nums = [ + 1, + 2, + 3 +]; + +Test.run([ + [ + "ObjectTests.res", + 21, + 20, + 32 + ], + "is: arrays" + ], Object.is([ + 1, + 2, + 3 + ], [ + 1, + 2, + 3 + ]), eq, false); + +Test.run([ + [ + "ObjectTests.res", + 22, + 20, + 32 + ], + "is: arrays" + ], Object.is(nums, nums), eq, true); + +Test.run([ + [ + "ObjectTests.res", + 23, + 20, + 32 + ], + "is: arrays" + ], Caml_obj.equal([ + 1, + 2, + 3 + ], [ + 1, + 2, + 3 + ]), eq, true); + +Test.run([ + [ + "ObjectTests.res", + 24, + 20, + 32 + ], + "is: arrays" + ], [ + 1, + 2, + 3 + ] === [ + 1, + 2, + 3 + ], eq, false); + +Test.run([ + [ + "ObjectTests.res", + 26, + 20, + 30 + ], + "is: list" + ], Object.is({ + hd: 1, + tl: { + hd: 2, + tl: { + hd: 3, + tl: /* [] */0 + } + } + }, { + hd: 1, + tl: { + hd: 2, + tl: { + hd: 3, + tl: /* [] */0 + } + } + }), eq, false); + +Test.run([ + [ + "ObjectTests.res", + 27, + 20, + 30 + ], + "is: list" + ], Caml_obj.equal({ + hd: 1, + tl: { + hd: 2, + tl: { + hd: 3, + tl: /* [] */0 + } + } + }, { + hd: 1, + tl: { + hd: 2, + tl: { + hd: 3, + tl: /* [] */0 + } + } + }), eq, true); + +Test.run([ + [ + "ObjectTests.res", + 28, + 20, + 30 + ], + "is: list" + ], ({ + hd: 1, + tl: { + hd: 2, + tl: { + hd: 3, + tl: /* [] */0 + } + } + }) === ({ + hd: 1, + tl: { + hd: 2, + tl: { + hd: 3, + tl: /* [] */0 + } + } + }), eq, false); + +var d = new Date(2000, 1); + +Test.run([ + [ + "ObjectTests.res", + 32, + 13, + 23 + ], + "is: date" + ], Object.is(new Date(2000, 1), new Date(2000, 1)), eq, false); + +Test.run([ + [ + "ObjectTests.res", + 37, + 20, + 30 + ], + "is: date" + ], Object.is(d, d), eq, true); + +var x = { + a: 1 +}; + +Test.run([ + [ + "ObjectTests.res", + 40, + 20, + 33 + ], + "is: objects" + ], Object.is(x, x), eq, true); + +Test.run([ + [ + "ObjectTests.res", + 41, + 20, + 33 + ], + "is: objects" + ], Object.is({ + a: 1 + }, { + a: 1 + }), eq, false); + +Test.run([ + [ + "ObjectTests.res", + 42, + 20, + 33 + ], + "is: objects" + ], Object.is({}, {}), eq, false); + +Test.run([ + [ + "ObjectTests.res", + 43, + 20, + 45 + ], + "is: === and == operator" + ], x === x, eq, true); + +Test.run([ + [ + "ObjectTests.res", + 44, + 20, + 45 + ], + "is: === and == operator" + ], Caml_obj.equal(x, x), eq, true); + +Test.run([ + [ + "ObjectTests.res", + 45, + 20, + 45 + ], + "is: === and == operator" + ], Caml_obj.equal({ + a: 1 + }, { + a: 1 + }), eq, true); + +Test.run([ + [ + "ObjectTests.res", + 47, + 20, + 31 + ], + "is: zeros" + ], Object.is(0, 0), eq, true); + +Test.run([ + [ + "ObjectTests.res", + 48, + 20, + 31 + ], + "is: zeros" + ], Object.is(-0.0, -0.0), eq, true); + +Test.run([ + [ + "ObjectTests.res", + 49, + 20, + 31 + ], + "is: zeros" + ], Object.is(0.0, -0.0), eq, false); + +function mkBig(s) { + return BigInt(s); +} + +Test.run([ + [ + "ObjectTests.res", + 52, + 20, + 32 + ], + "is: bigint" + ], Object.is(BigInt("123456789"), BigInt("123456789")), eq, true); + +Test.run([ + [ + "ObjectTests.res", + 53, + 20, + 32 + ], + "is: bigint" + ], Object.is(BigInt("123489"), BigInt("123456789")), eq, false); + +Test.run([ + [ + "ObjectTests.res", + 54, + 20, + 32 + ], + "is: bigint" + ], Object.is(BigInt("000000000"), BigInt("0")), eq, true); + +Test.run([ + [ + "ObjectTests.res", + 55, + 20, + 32 + ], + "is: bigint" + ], Caml_obj.equal(BigInt("123"), BigInt("123")), eq, true); + +Test.run([ + [ + "ObjectTests.res", + 56, + 20, + 32 + ], + "is: bigint" + ], BigInt("123") === BigInt("123"), eq, true); + +export { + eq , + nums , + d , + x , + mkBig , +} +/* Not a pure module */ diff --git a/test/ObjectTests.res b/test/ObjectTests.res new file mode 100644 index 00000000..ad6551ac --- /dev/null +++ b/test/ObjectTests.res @@ -0,0 +1,56 @@ +open RescriptCore + +let eq = (a, b) => a == b + +// ===== is ===== + +Test.run(__POS_OF__("is: different types"), Object.is("abc", false), eq, false) + +Test.run(__POS_OF__("is: ints"), Object.is(25, 25), eq, true) + +Test.run(__POS_OF__("is: strings"), Object.is("abc", "abc"), eq, true) +Test.run(__POS_OF__("is: strings"), Object.is("abc", "ABC"), eq, false) + +Test.run(__POS_OF__("is: null and undefined"), Object.is(null, undefined), eq, false) +Test.run(__POS_OF__("is: null and undefined"), Object.is(undefined, undefined), eq, true) +Test.run(__POS_OF__("is: null and undefined"), Object.is(null, null), eq, true) +Test.run(__POS_OF__("is: null and undefined"), Object.is(null, None), eq, false) +Test.run(__POS_OF__("is: undefined and None"), Object.is(undefined, None), eq, true) // hmmm... + +let nums = [1, 2, 3] +Test.run(__POS_OF__("is: arrays"), Object.is([1, 2, 3], [1, 2, 3]), eq, false) +Test.run(__POS_OF__("is: arrays"), Object.is(nums, nums), eq, true) +Test.run(__POS_OF__("is: arrays"), [1, 2, 3] == [1, 2, 3], eq, true) +Test.run(__POS_OF__("is: arrays"), [1, 2, 3] === [1, 2, 3], eq, false) + +Test.run(__POS_OF__("is: list"), Object.is(list{1, 2, 3}, list{1, 2, 3}), eq, false) +Test.run(__POS_OF__("is: list"), list{1, 2, 3} == list{1, 2, 3}, eq, true) +Test.run(__POS_OF__("is: list"), list{1, 2, 3} === list{1, 2, 3}, eq, false) + +let d = Date.makeWithYM(~year=2000, ~month=1) +Test.run( + __POS_OF__("is: date"), + Object.is(Date.makeWithYM(~year=2000, ~month=1), Date.makeWithYM(~year=2000, ~month=1)), + eq, + false, +) +Test.run(__POS_OF__("is: date"), Object.is(d, d), eq, true) + +let x = {"a": 1} +Test.run(__POS_OF__("is: objects"), Object.is(x, x), eq, true) +Test.run(__POS_OF__("is: objects"), Object.is({"a": 1}, {"a": 1}), eq, false) +Test.run(__POS_OF__("is: objects"), Object.is(Object.empty(), Object.empty()), eq, false) // hmm... +Test.run(__POS_OF__("is: === and == operator"), x === x, eq, true) +Test.run(__POS_OF__("is: === and == operator"), x == x, eq, true) +Test.run(__POS_OF__("is: === and == operator"), {"a": 1} == {"a": 1}, eq, true) + +Test.run(__POS_OF__("is: zeros"), Object.is(-0, -0), eq, true) +Test.run(__POS_OF__("is: zeros"), Object.is(-0.0, -0.0), eq, true) +Test.run(__POS_OF__("is: zeros"), Object.is(0.0, -0.0), eq, false) + +let mkBig = s => BigInt.fromString(s) +Test.run(__POS_OF__("is: bigint"), Object.is(mkBig("123456789"), mkBig("123456789")), eq, true) +Test.run(__POS_OF__("is: bigint"), Object.is(mkBig("123489"), mkBig("123456789")), eq, false) +Test.run(__POS_OF__("is: bigint"), Object.is(mkBig("000000000"), mkBig("0")), eq, true) +Test.run(__POS_OF__("is: bigint"), mkBig("123") == mkBig("123"), eq, true) +Test.run(__POS_OF__("is: bigint"), mkBig("123") === mkBig("123"), eq, true) diff --git a/test/TestSuite.mjs b/test/TestSuite.mjs index c968bd8f..f5084781 100644 --- a/test/TestSuite.mjs +++ b/test/TestSuite.mjs @@ -4,6 +4,7 @@ import * as IntTests from "./IntTests.mjs"; import * as TestTests from "./TestTests.mjs"; import * as ArrayTests from "./ArrayTests.mjs"; import * as ErrorTests from "./ErrorTests.mjs"; +import * as ObjectTests from "./ObjectTests.mjs"; import * as PromiseTest from "./PromiseTest.mjs"; var bign = TestTests.bign; @@ -26,10 +27,18 @@ var Concurrently = PromiseTest.Concurrently; var panicTest = ErrorTests.panicTest; -var eq = IntTests.eq; - var $$catch = IntTests.$$catch; +var eq = ObjectTests.eq; + +var nums = ObjectTests.nums; + +var d = ObjectTests.d; + +var x = ObjectTests.x; + +var mkBig = ObjectTests.mkBig; + export { bign , TestError , @@ -41,7 +50,11 @@ export { Catching , Concurrently , panicTest , - eq , $$catch , + eq , + nums , + d , + x , + mkBig , } /* IntTests Not a pure module */ diff --git a/test/TestSuite.res b/test/TestSuite.res index 6277bf57..ca2a63ca 100644 --- a/test/TestSuite.res +++ b/test/TestSuite.res @@ -3,3 +3,4 @@ include PromiseTest include ErrorTests include ArrayTests include IntTests +include ObjectTests From da6197a150f14b0fb89e49d319e4eca6f8828609 Mon Sep 17 00:00:00 2001 From: jmagaram Date: Mon, 13 Mar 2023 22:00:07 -0700 Subject: [PATCH 02/10] reference to rescript docs, +0/-0, small tweaks --- src/Core__Object.res | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/src/Core__Object.res b/src/Core__Object.res index bd72a01c..f968ffaf 100644 --- a/src/Core__Object.res +++ b/src/Core__Object.res @@ -1,13 +1,14 @@ @obj external empty: unit => {..} = "" /** -`Object.is` determines if two objects are identical in all contexts. Objects, arrays, records, and other non-primitives are only identical if they reference the **exact** same object in memory. Primitives like ints, floats, bools, and strings are identical if they have the same value. +`Object.is` determines if two objects are identical in all contexts. Objects, arrays, records, and other non-primitives are only identical if they reference the **exact** same object in memory. Primitives like ints, floats, and strings are identical if they have the same value. `+0` and `-0` are distinct. NaN is +equal to itself. -**Note:** In most scenarios use `==` and `===`. If the data type you want to compare by value has an `equals` function, use it. +**Note:** In most scenarios use `==` and `===`. If the type you want to compare by value has an `equals` function, use it. -The `==` operator is different in ReScript than Javascript. Non-primitives like arrays and records are considered equal if they have the same contents. +The `==` operator [is different in ReScript than Javascript](https://rescript-lang.org/docs/manual/latest/overview#boolean). Arrays, records and other non-primitives are equal if they have the same contents (deep equality). -In ReScript, the `===` operator performs a strict equality check, like Javascript, and is similar to (but not idential to) `Object.is`. See the references. +In ReScript, the `===` operator performs a strict equality check, like Javascript, and is similar but not identical to `Object.is`; see [Mozilla](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/is). ## Examples ```rescript @@ -31,6 +32,7 @@ Object.is([1, 2, 3], [1, 2, 3]) // false ## Specifications - [ECMAScript Language Specification](https://tc39.es/ecma262/multipage/fundamental-objects.html#sec-object.is) - [`Object.is on Mozilla`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/is) +- [Equality operators in ReScript](https://rescript-lang.org/docs/manual/latest/overview#boolean) */ @val external is: ('a, 'b) => bool = "Object.is" From 8f1d753f78fac3146dbbc009f4a7ac40ff593b66 Mon Sep 17 00:00:00 2001 From: jmagaram Date: Mon, 13 Mar 2023 22:03:22 -0700 Subject: [PATCH 03/10] unnecessary paragraph mark --- src/Core__Object.res | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/Core__Object.res b/src/Core__Object.res index f968ffaf..4a790343 100644 --- a/src/Core__Object.res +++ b/src/Core__Object.res @@ -1,8 +1,7 @@ @obj external empty: unit => {..} = "" /** -`Object.is` determines if two objects are identical in all contexts. Objects, arrays, records, and other non-primitives are only identical if they reference the **exact** same object in memory. Primitives like ints, floats, and strings are identical if they have the same value. `+0` and `-0` are distinct. NaN is -equal to itself. +`Object.is` determines if two objects are identical in all contexts. Objects, arrays, records, and other non-primitives are only identical if they reference the **exact** same object in memory. Primitives like ints, floats, and strings are identical if they have the same value. `+0` and `-0` are distinct. NaN is equal to itself. **Note:** In most scenarios use `==` and `===`. If the type you want to compare by value has an `equals` function, use it. From 5db75f0dd221d0ac0efc040e5ec2594d1981362e Mon Sep 17 00:00:00 2001 From: jmagaram Date: Tue, 14 Mar 2023 10:33:31 -0700 Subject: [PATCH 04/10] cleaned up a bit --- src/Core__Object.res | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/src/Core__Object.res b/src/Core__Object.res index 4a790343..357e62a5 100644 --- a/src/Core__Object.res +++ b/src/Core__Object.res @@ -1,7 +1,7 @@ @obj external empty: unit => {..} = "" /** -`Object.is` determines if two objects are identical in all contexts. Objects, arrays, records, and other non-primitives are only identical if they reference the **exact** same object in memory. Primitives like ints, floats, and strings are identical if they have the same value. `+0` and `-0` are distinct. NaN is equal to itself. +`is` determines if two objects are identical in all contexts. Objects, arrays, records, and other non-primitives are only identical if they reference the **exact** same object in memory. Primitives like ints, floats, and strings are identical if they have the same value. `+0` and `-0` are distinct. NaN is equal to itself. **Note:** In most scenarios use `==` and `===`. If the type you want to compare by value has an `equals` function, use it. @@ -10,6 +10,7 @@ The `==` operator [is different in ReScript than Javascript](https://rescript-la In ReScript, the `===` operator performs a strict equality check, like Javascript, and is similar but not identical to `Object.is`; see [Mozilla](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/is). ## Examples + ```rescript Object.is(25, 13) // false Object.is("abc", "abc") // true @@ -18,17 +19,18 @@ Object.is(undefined, null) // false Object.is(-0.0, 0.0) // false Object.is(list{1, 2}, list{1, 2}) // false +Object.is([1, 2, 3], [1, 2, 3]) // false +[1, 2, 3] == [1, 2, 3] // true +[1, 2, 3] === [1, 2, 3] // false + let fruit = {"name": "Apple" } Object.is(fruit, fruit) // true Object.is(fruit, {"name": "Apple" }) // false fruit == {"name": "Apple" } // true fruit === {"name": "Apple" } // false - -Object.is([1, 2, 3], [1, 2, 3]) // false -[1, 2, 3] == [1, 2, 3] // true -[1, 2, 3] === [1, 2, 3] // false ``` ## Specifications + - [ECMAScript Language Specification](https://tc39.es/ecma262/multipage/fundamental-objects.html#sec-object.is) - [`Object.is on Mozilla`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/is) - [Equality operators in ReScript](https://rescript-lang.org/docs/manual/latest/overview#boolean) From 2741d73371690e70d16a5f3c94eb3efebd402e8e Mon Sep 17 00:00:00 2001 From: jmagaram Date: Wed, 15 Mar 2023 12:27:37 -0700 Subject: [PATCH 05/10] change recommendation text --- src/Core__Object.res | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Core__Object.res b/src/Core__Object.res index 357e62a5..89c8f69f 100644 --- a/src/Core__Object.res +++ b/src/Core__Object.res @@ -3,7 +3,7 @@ /** `is` determines if two objects are identical in all contexts. Objects, arrays, records, and other non-primitives are only identical if they reference the **exact** same object in memory. Primitives like ints, floats, and strings are identical if they have the same value. `+0` and `-0` are distinct. NaN is equal to itself. -**Note:** In most scenarios use `==` and `===`. If the type you want to compare by value has an `equals` function, use it. +**Note:** In most scenarios use `==` or `===` or the custom `equals` function (if provided) for the type. The `==` operator [is different in ReScript than Javascript](https://rescript-lang.org/docs/manual/latest/overview#boolean). Arrays, records and other non-primitives are equal if they have the same contents (deep equality). From 8f6db7ce1996b92cb187fec6ba3d61e2fa41459e Mon Sep 17 00:00:00 2001 From: jmagaram Date: Thu, 16 Mar 2023 22:45:34 -0700 Subject: [PATCH 06/10] MDN not Mozilla --- src/Core__Object.res | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Core__Object.res b/src/Core__Object.res index 89c8f69f..811840cb 100644 --- a/src/Core__Object.res +++ b/src/Core__Object.res @@ -32,7 +32,7 @@ fruit === {"name": "Apple" } // false ## Specifications - [ECMAScript Language Specification](https://tc39.es/ecma262/multipage/fundamental-objects.html#sec-object.is) -- [`Object.is on Mozilla`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/is) +- [`Object.is on MDN`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/is) - [Equality operators in ReScript](https://rescript-lang.org/docs/manual/latest/overview#boolean) */ @val From 2d0fb2c3728fbdfa458d947c6684a4183d5f3cc2 Mon Sep 17 00:00:00 2001 From: jmagaram Date: Tue, 21 Mar 2023 11:11:54 -0700 Subject: [PATCH 07/10] remove Specifications section --- src/Core__Object.res | 11 +++-------- 1 file changed, 3 insertions(+), 8 deletions(-) diff --git a/src/Core__Object.res b/src/Core__Object.res index 811840cb..38cb4ac9 100644 --- a/src/Core__Object.res +++ b/src/Core__Object.res @@ -1,13 +1,13 @@ @obj external empty: unit => {..} = "" /** -`is` determines if two objects are identical in all contexts. Objects, arrays, records, and other non-primitives are only identical if they reference the **exact** same object in memory. Primitives like ints, floats, and strings are identical if they have the same value. `+0` and `-0` are distinct. NaN is equal to itself. +`is` determines if two objects are identical in all contexts. Objects, arrays, records, and other non-primitives are only identical if they reference the **exact** same object in memory. Primitives like ints, floats, and strings are identical if they have the same value. `+0` and `-0` are distinct. NaN is equal to itself. See [Object.is on MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/is) -**Note:** In most scenarios use `==` or `===` or the custom `equals` function (if provided) for the type. +In most scenarios use `==` or `===` or the custom `equals` function (if provided) for the type. The `==` operator [is different in ReScript than Javascript](https://rescript-lang.org/docs/manual/latest/overview#boolean). Arrays, records and other non-primitives are equal if they have the same contents (deep equality). -In ReScript, the `===` operator performs a strict equality check, like Javascript, and is similar but not identical to `Object.is`; see [Mozilla](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/is). +In ReScript, the `===` operator performs a strict equality check, like Javascript, and is similar but not identical to `is` ## Examples @@ -29,11 +29,6 @@ Object.is(fruit, {"name": "Apple" }) // false fruit == {"name": "Apple" } // true fruit === {"name": "Apple" } // false ``` -## Specifications - -- [ECMAScript Language Specification](https://tc39.es/ecma262/multipage/fundamental-objects.html#sec-object.is) -- [`Object.is on MDN`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/is) -- [Equality operators in ReScript](https://rescript-lang.org/docs/manual/latest/overview#boolean) */ @val external is: ('a, 'b) => bool = "Object.is" From 3bca6782387854bf61e2ac690216a47105803e9f Mon Sep 17 00:00:00 2001 From: jmagaram Date: Tue, 21 Mar 2023 12:45:04 -0700 Subject: [PATCH 08/10] remove some of the docs based on Glenn's feedback --- src/Core__Object.res | 4 ---- 1 file changed, 4 deletions(-) diff --git a/src/Core__Object.res b/src/Core__Object.res index 38cb4ac9..02326974 100644 --- a/src/Core__Object.res +++ b/src/Core__Object.res @@ -5,10 +5,6 @@ In most scenarios use `==` or `===` or the custom `equals` function (if provided) for the type. -The `==` operator [is different in ReScript than Javascript](https://rescript-lang.org/docs/manual/latest/overview#boolean). Arrays, records and other non-primitives are equal if they have the same contents (deep equality). - -In ReScript, the `===` operator performs a strict equality check, like Javascript, and is similar but not identical to `is` - ## Examples ```rescript From 5dec67f77cf4ddd371920b985f6827e81b702a06 Mon Sep 17 00:00:00 2001 From: jmagaram Date: Tue, 21 Mar 2023 13:46:21 -0700 Subject: [PATCH 09/10] stricted comparison types glennsl feedback --- src/Core__Object.res | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Core__Object.res b/src/Core__Object.res index 02326974..a30a3acd 100644 --- a/src/Core__Object.res +++ b/src/Core__Object.res @@ -27,7 +27,7 @@ fruit === {"name": "Apple" } // false ``` */ @val -external is: ('a, 'b) => bool = "Object.is" +external is: ('a, 'a) => bool = "Object.is" @val external create: {..} => {..} = "Object.create" @val external createWithProperties: ({..}, {..}) => {..} = "Object.create" From 08db9a8c8c1c1f11bf8b1558bd7285c49e3c7e67 Mon Sep 17 00:00:00 2001 From: jmagaram Date: Tue, 21 Mar 2023 13:50:52 -0700 Subject: [PATCH 10/10] stricter types on object.is, must be same test updates --- test/ObjectTests.mjs | 30 ------------------------------ test/ObjectTests.res | 6 +++--- 2 files changed, 3 insertions(+), 33 deletions(-) diff --git a/test/ObjectTests.mjs b/test/ObjectTests.mjs index 0d1cdd8a..d9145984 100644 --- a/test/ObjectTests.mjs +++ b/test/ObjectTests.mjs @@ -5,16 +5,6 @@ import * as Caml_obj from "rescript/lib/es6/caml_obj.js"; var eq = Caml_obj.equal; -Test.run([ - [ - "ObjectTests.res", - 7, - 20, - 41 - ], - "is: different types" - ], Object.is("abc", false), eq, false); - Test.run([ [ "ObjectTests.res", @@ -75,26 +65,6 @@ Test.run([ "is: null and undefined" ], Object.is(null, null), eq, true); -Test.run([ - [ - "ObjectTests.res", - 17, - 20, - 44 - ], - "is: null and undefined" - ], Object.is(null, undefined), eq, false); - -Test.run([ - [ - "ObjectTests.res", - 18, - 20, - 44 - ], - "is: undefined and None" - ], Object.is(undefined, undefined), eq, true); - var nums = [ 1, 2, diff --git a/test/ObjectTests.res b/test/ObjectTests.res index ad6551ac..5c1a4b8d 100644 --- a/test/ObjectTests.res +++ b/test/ObjectTests.res @@ -4,7 +4,7 @@ let eq = (a, b) => a == b // ===== is ===== -Test.run(__POS_OF__("is: different types"), Object.is("abc", false), eq, false) +// Test.run(__POS_OF__("is: different types"), Object.is("abc", false), eq, false) Test.run(__POS_OF__("is: ints"), Object.is(25, 25), eq, true) @@ -14,8 +14,8 @@ Test.run(__POS_OF__("is: strings"), Object.is("abc", "ABC"), eq, false) Test.run(__POS_OF__("is: null and undefined"), Object.is(null, undefined), eq, false) Test.run(__POS_OF__("is: null and undefined"), Object.is(undefined, undefined), eq, true) Test.run(__POS_OF__("is: null and undefined"), Object.is(null, null), eq, true) -Test.run(__POS_OF__("is: null and undefined"), Object.is(null, None), eq, false) -Test.run(__POS_OF__("is: undefined and None"), Object.is(undefined, None), eq, true) // hmmm... +// Test.run(__POS_OF__("is: null and undefined"), Object.is(null, None), eq, false) +// Test.run(__POS_OF__("is: undefined and None"), Object.is(undefined, None), eq, true) let nums = [1, 2, 3] Test.run(__POS_OF__("is: arrays"), Object.is([1, 2, 3], [1, 2, 3]), eq, false)