From 81d60c733a3e568f2769e31996216e30de7671ae Mon Sep 17 00:00:00 2001 From: Ryan Roemer Date: Fri, 13 Nov 2026 21:53:20 -0800 Subject: [PATCH 1/4] fix: compare Temporal values by toString instead of throwing (#139) --- .changeset/temporal-values.md | 5 ++ README.md | 2 +- index.js | 8 +- pack-snapshot.json | 2 +- test/fixtures/index.mjs | 156 +++++++++++++++++++++++++++++++++- 5 files changed, 168 insertions(+), 5 deletions(-) create mode 100644 .changeset/temporal-values.md diff --git a/.changeset/temporal-values.md b/.changeset/temporal-values.md new file mode 100644 index 0000000..bc026f5 --- /dev/null +++ b/.changeset/temporal-values.md @@ -0,0 +1,5 @@ +--- +"react-fast-compare": patch +--- + +Fix `isEqual` throwing when comparing two different `Temporal` values (e.g. `Temporal.PlainTime`), including ones nested in objects or arrays. They're now compared by their `toString()` (ISO string with calendar and time zone). diff --git a/README.md b/README.md index 9272b50..1ab50e8 100644 --- a/README.md +++ b/README.md @@ -25,7 +25,7 @@ $ npm install react-fast-compare - handles React-specific circular references, like elements - checks equality of Date and RegExp objects - should be as fast as [fast-deep-equal](https://github.com/epoberezkin/fast-deep-equal) via a single unified library, and with added guardrails for circular references. -- small: under 660 bytes minified+gzipped +- small: under 750 bytes minified+gzipped ## Usage diff --git a/index.js b/index.js index 7aee9f6..ebb163d 100644 --- a/index.js +++ b/index.js @@ -100,10 +100,16 @@ function equal(a, b) { // Apply guards for `Object.create(null)` handling. See: // - https://github.com/FormidableLabs/react-fast-compare/issues/64 // - https://github.com/epoberezkin/fast-deep-equal/issues/49 + // Temporal values throw from `valueOf()` by design, so skip it and let the + // `toString()` check below compare them (ISO string with calendar and time + // zone). `Symbol.toStringTag` detection works across realms and with + // polyfills, and only runs for objects that override `valueOf`. See: + // - https://github.com/FormidableLabs/react-fast-compare/issues/139 if ( a.valueOf !== Object.prototype.valueOf && typeof a.valueOf === 'function' && - typeof b.valueOf === 'function' + typeof b.valueOf === 'function' && + Object.prototype.toString.call(a).indexOf('[object Temporal.') !== 0 ) return a.valueOf() === b.valueOf(); if ( diff --git a/pack-snapshot.json b/pack-snapshot.json index 902029c..17f31b0 100644 --- a/pack-snapshot.json +++ b/pack-snapshot.json @@ -78,7 +78,7 @@ " return true;", " }", " if (a.constructor === RegExp) return a.source === b.source && a.flags === b.flags;", - " if (a.valueOf !== Object.prototype.valueOf && typeof a.valueOf === \"function\" && typeof b.valueOf === \"function\") return a.valueOf() === b.valueOf();", + " if (a.valueOf !== Object.prototype.valueOf && typeof a.valueOf === \"function\" && typeof b.valueOf === \"function\" && Object.prototype.toString.call(a).indexOf(\"[object Temporal.\") !== 0) return a.valueOf() === b.valueOf();", " if (a.toString !== Object.prototype.toString && typeof a.toString === \"function\" && typeof b.toString === \"function\") return a.toString() === b.toString();", " keys = Object.keys(a);", " length = keys.length;", diff --git a/test/fixtures/index.mjs b/test/fixtures/index.mjs index 2e53a94..402ab7a 100644 --- a/test/fixtures/index.mjs +++ b/test/fixtures/index.mjs @@ -106,6 +106,27 @@ const custom = [ value2: Object.assign(Object.create(null), { b: { c: true }, a: 1 }), equal: true, }, + // `valueOf` overrides still compare by `valueOf()` after the Temporal + // check (#139). `Date#toString()` drops milliseconds, so a `toString()` + // comparison would make these equal. + { + description: 'Dates 1 ms apart are not equal', + value1: new Date(0), + value2: new Date(1), + equal: false, + }, + { + description: 'equal boxed Numbers are equal', + value1: new Number(1), + value2: new Number(1), + equal: true, + }, + { + description: 'different boxed Strings are not equal', + value1: new String('a'), + value2: new String('b'), + equal: false, + }, ], }, ]; @@ -168,5 +189,136 @@ const dataViews = [ }, ]; -export { generic, es6, react, custom, dataViews }; -export const all = [].concat(generic, es6, react, custom, dataViews); +// Temporal (#139). Its `valueOf()` throws by design, so these throw in 3.2.2 +// and fast-deep-equal. Every engine we test has it built in; where it's +// missing, the cases are skipped instead of polyfilled. +const { Temporal } = globalThis; +const temporalCase = (description, value1, value2, equal) => + Temporal + ? { description, value1: value1(), value2: value2(), equal } + : { description, equal, skip: true }; + +const temporal = [ + { + description: 'Temporal', + tests: [ + temporalCase( + 'equal PlainDates are equal', + () => Temporal.PlainDate.from('2026-11-13'), + () => Temporal.PlainDate.from('2026-11-13'), + true + ), + temporalCase( + 'different PlainDates are not equal', + () => Temporal.PlainDate.from('2026-11-13'), + () => Temporal.PlainDate.from('2026-11-14'), + false + ), + temporalCase( + 'PlainDates with different calendars are not equal', + () => Temporal.PlainDate.from('2026-11-13'), + () => Temporal.PlainDate.from('2026-11-13[u-ca=hebrew]'), + false + ), + temporalCase( + 'equal PlainTimes are equal', + () => Temporal.PlainTime.from('07:00'), + () => Temporal.PlainTime.from('07:00'), + true + ), + temporalCase( + 'different PlainTimes are not equal', + () => Temporal.PlainTime.from('07:00'), + () => Temporal.PlainTime.from('08:00'), + false + ), + temporalCase( + 'equal PlainDateTimes are equal', + () => Temporal.PlainDateTime.from('2026-11-13T07:00'), + () => Temporal.PlainDateTime.from('2026-11-13T07:00'), + true + ), + temporalCase( + 'equal PlainYearMonths are equal', + () => Temporal.PlainYearMonth.from('2026-11'), + () => Temporal.PlainYearMonth.from('2026-11'), + true + ), + temporalCase( + 'equal PlainMonthDays are equal', + () => Temporal.PlainMonthDay.from('11-13'), + () => Temporal.PlainMonthDay.from('11-13'), + true + ), + temporalCase( + 'equal Instants are equal', + () => Temporal.Instant.fromEpochNanoseconds(1n), + () => Temporal.Instant.fromEpochNanoseconds(1n), + true + ), + temporalCase( + 'Instants 1 ns apart are not equal', + () => Temporal.Instant.fromEpochNanoseconds(1n), + () => Temporal.Instant.fromEpochNanoseconds(2n), + false + ), + temporalCase( + 'equal ZonedDateTimes are equal', + () => Temporal.ZonedDateTime.from('2026-11-13T07:00[Europe/Dublin]'), + () => Temporal.ZonedDateTime.from('2026-11-13T07:00[Europe/Dublin]'), + true + ), + temporalCase( + 'ZonedDateTimes at the same instant in different zones are not equal', + () => Temporal.ZonedDateTime.from('2026-11-13T07:00[UTC]'), + () => Temporal.ZonedDateTime.from('2026-11-13T07:00[Europe/London]'), + false + ), + temporalCase( + 'equal Durations are equal', + () => Temporal.Duration.from('PT1H30M'), + () => Temporal.Duration.from('PT1H30M'), + true + ), + temporalCase( + 'different Durations are not equal', + () => Temporal.Duration.from('PT1H30M'), + () => Temporal.Duration.from('PT90M'), + false + ), + temporalCase( + 'a PlainDate and a PlainDateTime are not equal', + () => Temporal.PlainDate.from('2026-11-13'), + () => Temporal.PlainDateTime.from('2026-11-13'), + false + ), + temporalCase( + 'a Temporal value and its string are not equal', + () => Temporal.PlainTime.from('07:00'), + () => '07:00:00', + false + ), + temporalCase( + 'objects with equal Temporal values are equal', + () => ({ time: Temporal.PlainTime.from('07:00') }), + () => ({ time: Temporal.PlainTime.from('07:00') }), + true + ), + temporalCase( + 'objects with different Temporal values are not equal', + () => ({ time: Temporal.PlainTime.from('07:00') }), + () => ({ time: Temporal.PlainTime.from('08:00') }), + false + ), + temporalCase( + 'arrays of equal Temporal values are equal', + () => [Temporal.PlainDate.from('2026-11-13')], + () => [Temporal.PlainDate.from('2026-11-13')], + true + ), + ], + }, +]; + +export { generic, es6, react, custom, dataViews, temporal }; +export const all = [].concat(generic, es6, react, custom, dataViews, temporal); From 801fc9110c804bb729ef0a7c8ab704fb7029e28a Mon Sep 17 00:00:00 2001 From: Ryan Roemer Date: Fri, 13 Nov 2026 22:49:55 -0800 Subject: [PATCH 2/4] fix: compare Temporal values with equals() instead of toString() Review follow-up for #160. The default toString() of ISO PlainYearMonth and PlainMonthDay omits the reference day/year, so two values that equals() calls different compared equal. - Temporal values now use equals(). Duration has none and falls through to the existing toString() check. - equals() also treats time zone aliases (Asia/Kolkata, Asia/Calcutta) as equal, and runs about 1.5-7x faster than toString(). - Fixtures: reference day/year and time zone alias cases. - pack-snapshot.json regenerated. Changeset text updated. Size: min+gz 729 -> 746 B (+17; +44 vs. 3.2.3). --- .changeset/temporal-values.md | 2 +- index.js | 16 +++++++++------- pack-snapshot.json | 5 ++++- test/fixtures/index.mjs | 20 ++++++++++++++++++++ 4 files changed, 34 insertions(+), 9 deletions(-) diff --git a/.changeset/temporal-values.md b/.changeset/temporal-values.md index bc026f5..5280f74 100644 --- a/.changeset/temporal-values.md +++ b/.changeset/temporal-values.md @@ -2,4 +2,4 @@ "react-fast-compare": patch --- -Fix `isEqual` throwing when comparing two different `Temporal` values (e.g. `Temporal.PlainTime`), including ones nested in objects or arrays. They're now compared by their `toString()` (ISO string with calendar and time zone). +Fix `isEqual` throwing when comparing two different `Temporal` values (e.g. `Temporal.PlainTime`), including ones nested in objects or arrays. They're now compared with their `equals()` method (`Temporal.Duration`, which has none, by `toString()`). diff --git a/index.js b/index.js index ebb163d..52b5068 100644 --- a/index.js +++ b/index.js @@ -100,18 +100,20 @@ function equal(a, b) { // Apply guards for `Object.create(null)` handling. See: // - https://github.com/FormidableLabs/react-fast-compare/issues/64 // - https://github.com/epoberezkin/fast-deep-equal/issues/49 - // Temporal values throw from `valueOf()` by design, so skip it and let the - // `toString()` check below compare them (ISO string with calendar and time - // zone). `Symbol.toStringTag` detection works across realms and with + // Temporal values throw from `valueOf()` by design, so compare them with + // `equals()`. `Duration` has none and falls through to the `toString()` + // check below. `Symbol.toStringTag` detection works across realms and with // polyfills, and only runs for objects that override `valueOf`. See: // - https://github.com/FormidableLabs/react-fast-compare/issues/139 if ( a.valueOf !== Object.prototype.valueOf && typeof a.valueOf === 'function' && - typeof b.valueOf === 'function' && - Object.prototype.toString.call(a).indexOf('[object Temporal.') !== 0 - ) - return a.valueOf() === b.valueOf(); + typeof b.valueOf === 'function' + ) { + if (Object.prototype.toString.call(a).indexOf('[object Temporal.') !== 0) + return a.valueOf() === b.valueOf(); + if (typeof a.equals === 'function') return a.equals(b); + } if ( a.toString !== Object.prototype.toString && typeof a.toString === 'function' && diff --git a/pack-snapshot.json b/pack-snapshot.json index 17f31b0..f28c010 100644 --- a/pack-snapshot.json +++ b/pack-snapshot.json @@ -78,7 +78,10 @@ " return true;", " }", " if (a.constructor === RegExp) return a.source === b.source && a.flags === b.flags;", - " if (a.valueOf !== Object.prototype.valueOf && typeof a.valueOf === \"function\" && typeof b.valueOf === \"function\" && Object.prototype.toString.call(a).indexOf(\"[object Temporal.\") !== 0) return a.valueOf() === b.valueOf();", + " if (a.valueOf !== Object.prototype.valueOf && typeof a.valueOf === \"function\" && typeof b.valueOf === \"function\") {", + " if (Object.prototype.toString.call(a).indexOf(\"[object Temporal.\") !== 0) return a.valueOf() === b.valueOf();", + " if (typeof a.equals === \"function\") return a.equals(b);", + " }", " if (a.toString !== Object.prototype.toString && typeof a.toString === \"function\" && typeof b.toString === \"function\") return a.toString() === b.toString();", " keys = Object.keys(a);", " length = keys.length;", diff --git a/test/fixtures/index.mjs b/test/fixtures/index.mjs index 402ab7a..9ada2d1 100644 --- a/test/fixtures/index.mjs +++ b/test/fixtures/index.mjs @@ -250,6 +250,20 @@ const temporal = [ () => Temporal.PlainMonthDay.from('11-13'), true ), + // The ISO reference day/year isn't in the default `toString()`, but + // `equals()` compares it. + temporalCase( + 'PlainYearMonths with different reference days are not equal', + () => new Temporal.PlainYearMonth(2026, 11, 'iso8601', 1), + () => new Temporal.PlainYearMonth(2026, 11, 'iso8601', 2), + false + ), + temporalCase( + 'PlainMonthDays with different reference years are not equal', + () => new Temporal.PlainMonthDay(11, 13, 'iso8601', 1972), + () => new Temporal.PlainMonthDay(11, 13, 'iso8601', 2000), + false + ), temporalCase( 'equal Instants are equal', () => Temporal.Instant.fromEpochNanoseconds(1n), @@ -274,6 +288,12 @@ const temporal = [ () => Temporal.ZonedDateTime.from('2026-11-13T07:00[Europe/London]'), false ), + temporalCase( + 'ZonedDateTimes in time zone aliases are equal', + () => Temporal.ZonedDateTime.from('2026-11-13T07:00[Asia/Kolkata]'), + () => Temporal.ZonedDateTime.from('2026-11-13T07:00[Asia/Calcutta]'), + true + ), temporalCase( 'equal Durations are equal', () => Temporal.Duration.from('PT1H30M'), From 6380499611e70a023f99b6a9ef85737368d89524 Mon Sep 17 00:00:00 2001 From: Ryan Roemer Date: Wed, 18 Nov 2026 13:48:58 -0800 Subject: [PATCH 3/4] fix: compare Temporal.Duration field by field Review follow-up for #160. Duration has no equals(), and its toString() balances sub-second units, so 1000 ms and 1 s (both PT1S) compared equal although their fields and toLocaleString() output differ. - Compare all 10 Duration fields instead of toString(). - Fixtures: 1000 ms vs 1 s, 1000 ns vs 1 us, equal sub-second durations. - pack-snapshot.json regenerated. Changeset text updated. - README: size claim updated to under 850 bytes. Size: min+gz 746 -> 809 B (+63; +107 vs. 3.2.3). --- .changeset/temporal-values.md | 2 +- README.md | 2 +- index.js | 22 +++++++++++++++++++--- pack-snapshot.json | 4 ++++ test/fixtures/index.mjs | 19 +++++++++++++++++++ 5 files changed, 44 insertions(+), 5 deletions(-) diff --git a/.changeset/temporal-values.md b/.changeset/temporal-values.md index 5280f74..cd12587 100644 --- a/.changeset/temporal-values.md +++ b/.changeset/temporal-values.md @@ -2,4 +2,4 @@ "react-fast-compare": patch --- -Fix `isEqual` throwing when comparing two different `Temporal` values (e.g. `Temporal.PlainTime`), including ones nested in objects or arrays. They're now compared with their `equals()` method (`Temporal.Duration`, which has none, by `toString()`). +Fix `isEqual` throwing when comparing two different `Temporal` values (e.g. `Temporal.PlainTime`), including ones nested in objects or arrays. They're now compared with their `equals()` method, and `Temporal.Duration`, which has none, field by field. diff --git a/README.md b/README.md index 1ab50e8..3939a67 100644 --- a/README.md +++ b/README.md @@ -25,7 +25,7 @@ $ npm install react-fast-compare - handles React-specific circular references, like elements - checks equality of Date and RegExp objects - should be as fast as [fast-deep-equal](https://github.com/epoberezkin/fast-deep-equal) via a single unified library, and with added guardrails for circular references. -- small: under 750 bytes minified+gzipped +- small: under 850 bytes minified+gzipped ## Usage diff --git a/index.js b/index.js index 52b5068..d0fde1f 100644 --- a/index.js +++ b/index.js @@ -5,6 +5,18 @@ var hasMap = typeof Map === 'function'; var hasSet = typeof Set === 'function'; var hasArrayBuffer = typeof ArrayBuffer === 'function' && !!ArrayBuffer.isView; var hasDataView = typeof DataView === 'function'; +var durationFields = [ + 'years', + 'months', + 'weeks', + 'days', + 'hours', + 'minutes', + 'seconds', + 'milliseconds', + 'microseconds', + 'nanoseconds', +]; // Note: We **don't** need `envHasBigInt64Array` in fde es6/index.js @@ -101,9 +113,10 @@ function equal(a, b) { // - https://github.com/FormidableLabs/react-fast-compare/issues/64 // - https://github.com/epoberezkin/fast-deep-equal/issues/49 // Temporal values throw from `valueOf()` by design, so compare them with - // `equals()`. `Duration` has none and falls through to the `toString()` - // check below. `Symbol.toStringTag` detection works across realms and with - // polyfills, and only runs for objects that override `valueOf`. See: + // `equals()`. `Duration` is the only type without one, and its `toString()` + // balances units (1000 ms and 1 s are both `PT1S`), so compare its fields. + // `Symbol.toStringTag` detection works across realms and with polyfills, + // and only runs for objects that override `valueOf`. See: // - https://github.com/FormidableLabs/react-fast-compare/issues/139 if ( a.valueOf !== Object.prototype.valueOf && @@ -113,6 +126,9 @@ function equal(a, b) { if (Object.prototype.toString.call(a).indexOf('[object Temporal.') !== 0) return a.valueOf() === b.valueOf(); if (typeof a.equals === 'function') return a.equals(b); + for (i = durationFields.length; i-- !== 0;) + if (a[durationFields[i]] !== b[durationFields[i]]) return false; + return true; } if ( a.toString !== Object.prototype.toString && diff --git a/pack-snapshot.json b/pack-snapshot.json index f28c010..53bfbcc 100644 --- a/pack-snapshot.json +++ b/pack-snapshot.json @@ -39,6 +39,8 @@ "", "var hasDataView = typeof DataView === \"function\";", "", + "var durationFields = [ \"years\", \"months\", \"weeks\", \"days\", \"hours\", \"minutes\", \"seconds\", \"milliseconds\", \"microseconds\", \"nanoseconds\" ];", + "", "function equal(a, b) {", " if (a === b) return true;", " if (a && b && typeof a == \"object\" && typeof b == \"object\") {", @@ -81,6 +83,8 @@ " if (a.valueOf !== Object.prototype.valueOf && typeof a.valueOf === \"function\" && typeof b.valueOf === \"function\") {", " if (Object.prototype.toString.call(a).indexOf(\"[object Temporal.\") !== 0) return a.valueOf() === b.valueOf();", " if (typeof a.equals === \"function\") return a.equals(b);", + " for (i = durationFields.length; i-- !== 0; ) if (a[durationFields[i]] !== b[durationFields[i]]) return false;", + " return true;", " }", " if (a.toString !== Object.prototype.toString && typeof a.toString === \"function\" && typeof b.toString === \"function\") return a.toString() === b.toString();", " keys = Object.keys(a);", diff --git a/test/fixtures/index.mjs b/test/fixtures/index.mjs index 9ada2d1..5325535 100644 --- a/test/fixtures/index.mjs +++ b/test/fixtures/index.mjs @@ -306,6 +306,25 @@ const temporal = [ () => Temporal.Duration.from('PT90M'), false ), + // `toString()` balances sub-second units, so both of these are `PT1S`. + temporalCase( + 'Durations of 1000 ms and 1 s are not equal', + () => Temporal.Duration.from({ milliseconds: 1000 }), + () => Temporal.Duration.from({ seconds: 1 }), + false + ), + temporalCase( + 'Durations of 1000 ns and 1 µs are not equal', + () => Temporal.Duration.from({ nanoseconds: 1000 }), + () => Temporal.Duration.from({ microseconds: 1 }), + false + ), + temporalCase( + 'equal Durations with sub-second fields are equal', + () => Temporal.Duration.from({ seconds: 1, milliseconds: 500 }), + () => Temporal.Duration.from({ seconds: 1, milliseconds: 500 }), + true + ), temporalCase( 'a PlainDate and a PlainDateTime are not equal', () => Temporal.PlainDate.from('2026-11-13'), From 588fbf3a93101ae4d9b4190474d11eb6e007b84d Mon Sep 17 00:00:00 2001 From: Ryan Roemer Date: Wed, 18 Nov 2026 19:48:30 -0800 Subject: [PATCH 4/4] fix: only compare Temporal.Duration tags by duration fields Review follow-up for #160. Any object tagged Temporal.* that overrides valueOf and has no equals() reached the duration field loop, where all 10 fields are undefined, so it always compared equal. - Run the field loop only for an exact Temporal.Duration tag. Other Temporal.* tags without equals() fall through to the toString() and key comparison. - Fixtures: a fake Temporal.Fake-tagged object, equal and unequal. - pack-snapshot.json regenerated. Size: min+gz 809 -> 830 B (+21; +128 vs. 3.2.3). --- index.js | 16 +++++++++++----- pack-snapshot.json | 10 +++++++--- test/fixtures/index.mjs | 27 +++++++++++++++++++++++++++ 3 files changed, 45 insertions(+), 8 deletions(-) diff --git a/index.js b/index.js index d0fde1f..8a21bf0 100644 --- a/index.js +++ b/index.js @@ -116,19 +116,25 @@ function equal(a, b) { // `equals()`. `Duration` is the only type without one, and its `toString()` // balances units (1000 ms and 1 s are both `PT1S`), so compare its fields. // `Symbol.toStringTag` detection works across realms and with polyfills, - // and only runs for objects that override `valueOf`. See: + // and only runs for objects that override `valueOf`. Any other `Temporal.*` + // tag without `equals()` falls through to the `toString()` check below. + // See: // - https://github.com/FormidableLabs/react-fast-compare/issues/139 + var tag; if ( a.valueOf !== Object.prototype.valueOf && typeof a.valueOf === 'function' && typeof b.valueOf === 'function' ) { - if (Object.prototype.toString.call(a).indexOf('[object Temporal.') !== 0) + tag = Object.prototype.toString.call(a); + if (tag.indexOf('[object Temporal.') !== 0) return a.valueOf() === b.valueOf(); if (typeof a.equals === 'function') return a.equals(b); - for (i = durationFields.length; i-- !== 0;) - if (a[durationFields[i]] !== b[durationFields[i]]) return false; - return true; + if (tag === '[object Temporal.Duration]') { + for (i = durationFields.length; i-- !== 0;) + if (a[durationFields[i]] !== b[durationFields[i]]) return false; + return true; + } } if ( a.toString !== Object.prototype.toString && diff --git a/pack-snapshot.json b/pack-snapshot.json index 53bfbcc..f95735f 100644 --- a/pack-snapshot.json +++ b/pack-snapshot.json @@ -80,11 +80,15 @@ " return true;", " }", " if (a.constructor === RegExp) return a.source === b.source && a.flags === b.flags;", + " var tag;", " if (a.valueOf !== Object.prototype.valueOf && typeof a.valueOf === \"function\" && typeof b.valueOf === \"function\") {", - " if (Object.prototype.toString.call(a).indexOf(\"[object Temporal.\") !== 0) return a.valueOf() === b.valueOf();", + " tag = Object.prototype.toString.call(a);", + " if (tag.indexOf(\"[object Temporal.\") !== 0) return a.valueOf() === b.valueOf();", " if (typeof a.equals === \"function\") return a.equals(b);", - " for (i = durationFields.length; i-- !== 0; ) if (a[durationFields[i]] !== b[durationFields[i]]) return false;", - " return true;", + " if (tag === \"[object Temporal.Duration]\") {", + " for (i = durationFields.length; i-- !== 0; ) if (a[durationFields[i]] !== b[durationFields[i]]) return false;", + " return true;", + " }", " }", " if (a.toString !== Object.prototype.toString && typeof a.toString === \"function\" && typeof b.toString === \"function\") return a.toString() === b.toString();", " keys = Object.keys(a);", diff --git a/test/fixtures/index.mjs b/test/fixtures/index.mjs index 5325535..82e0d20 100644 --- a/test/fixtures/index.mjs +++ b/test/fixtures/index.mjs @@ -62,6 +62,19 @@ const react = [ }, ]; +// Tagged like a Temporal type, overrides `valueOf`, has no `equals()`. +class FakeTemporal { + constructor(value) { + this.value = value; + } + get [Symbol.toStringTag]() { + return 'Temporal.Fake'; + } + valueOf() { + return this.value; + } +} + // Additional customized behavior. const custom = [ { @@ -127,6 +140,20 @@ const custom = [ value2: new String('b'), equal: false, }, + // Only a real `Temporal.Duration` tag gets the duration field compare + // (#139); other `Temporal.*` tags without `equals()` compare as objects. + { + description: 'objects with a non-Duration Temporal tag compare by keys', + value1: new FakeTemporal(1), + value2: new FakeTemporal(2), + equal: false, + }, + { + description: 'equal objects with a non-Duration Temporal tag are equal', + value1: new FakeTemporal(1), + value2: new FakeTemporal(1), + equal: true, + }, ], }, ];