Skip to content

feat: initial support for Temporal equality #8007

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

Merged
merged 3 commits into from
May 23, 2025

Conversation

dirkluijk
Copy link
Contributor

@dirkluijk dirkluijk commented May 20, 2025

Description

Fixes #7991

Please don't delete this checklist! Before submitting the PR, please make sure you do the following:

  • It's really useful if your PR references an issue where it is discussed ahead of time. If the feature is substantial or introduces breaking changes without a discussion, PR might be closed.
  • Ideally, include a test that fails without this PR but passes with it.
  • Please, don't make changes to pnpm-lock.yaml unless you introduce a new test example.

Tests

  • Run the tests with pnpm test:ci.

Documentation

  • If you introduce new functionality, document it. You can run documentation with pnpm run docs command.

Changesets

  • Changes in changelog are generated from PR name. Please, make sure that it explains your changes in an understandable manner. Please, prefix changeset messages with feat:, fix:, perf:, docs:, or chore:.

Copy link

netlify bot commented May 20, 2025

Deploy Preview for vitest-dev ready!

Built without sensitive environment variables

Name Link
🔨 Latest commit 2e366b1
🔍 Latest deploy log https://app.netlify.com/projects/vitest-dev/deploys/682ee8f85842d600088b5124
😎 Deploy Preview https://deploy-preview-8007--vitest-dev.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify project configuration.

case '[object Temporal.PlainYearMonth]':
case '[object Temporal.PlainMonthDay]':
case '[object Temporal.Duration]':
return a.toString() === b.toString()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you know which is preferred whether toString equality and builtin equals method? https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Temporal/ZonedDateTime/equals

It looks like there are several old issues mentions calendar comparison (such as tc39/proposal-temporal#625), but the discussion seem quite technical 😅

Copy link
Contributor Author

@dirkluijk dirkluijk May 22, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

.equals() seems to compare including the calendar, so I think it's a good suggestion. Let's do it!

For Temporal.Duration this .equals() method doesn't exist, but I wasn't sure about that one to begin with. For Durations, there IS actually ambiguity. For example, are 60 seconds and 1 minute the same? If you compare the duration in time, yes; but if you literally compare all their components, no. Also, for example "1 day" is not necessarily the same as "24 hour" and depends on context.

My suggestion is to use .toString() for durations so it does a literal comparison of all the different components.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Indeed it looks like Duration is tricky. tc39/proposal-temporal#856 seems to explain the details.

As a builtin equality, I think choosing toString comparison makes sense since that's the most strict way to do it. Users can still add own custom equality tester to loosen it if desired.

@dirkluijk
Copy link
Contributor Author

@hi-ogawa I updated the PR:

  • added Temporal polyfill
  • adjusted equality check and use .equals()
  • for Duration I still compare the string representation to compare all its components.

Copy link
Contributor

@hi-ogawa hi-ogawa left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

@AriPerkkio AriPerkkio requested a review from sheremet-va May 23, 2025 08:50
@sheremet-va sheremet-va merged commit 52bd789 into vitest-dev:main May 23, 2025
12 of 13 checks passed
@dirkluijk dirkluijk deleted the temporal-support branch May 24, 2025 18:33
fabon-f added a commit to fabon-f/vremel that referenced this pull request Aug 4, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Strict equal not working with Temporal dates
3 participants