Skip to content
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

Add Duration.divide #2494

Merged
merged 3 commits into from
Apr 11, 2024
Merged

Conversation

thewilkybarkid
Copy link
Contributor

@thewilkybarkid thewilkybarkid commented Apr 9, 2024

Type

  • Refactor
  • Feature
  • Bug Fix
  • Optimization
  • Documentation Update

Description

Adds divide to the Duration module (it already has times).

Unlike Number.divide and BigInt.divide, this doesn't have safe/unsafe variants.

Related

  • Related Issue #
  • Closes #

Sorry, something went wrong.

Copy link

changeset-bot bot commented Apr 9, 2024

🦋 Changeset detected

Latest commit: 5fc0797

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 16 packages
Name Type
effect Patch
@effect/cli Patch
@effect/experimental Patch
@effect/opentelemetry Patch
@effect/platform-browser Patch
@effect/platform-bun Patch
@effect/platform-node-shared Patch
@effect/platform-node Patch
@effect/platform Patch
@effect/printer-ansi Patch
@effect/printer Patch
@effect/rpc-http Patch
@effect/rpc Patch
@effect/schema Patch
@effect/typeclass Patch
@effect/vitest Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@thewilkybarkid
Copy link
Contributor Author

Looking to replace https://github.com/PREreview/coar-notify/blob/958faa16e9bf2fa4aee536c1d66d7ba4d5c36c89/src/BullMq.ts#L135-L136.

Could be achieved with

Duration.times(value, 0.5)

but a division API would be more straightforward.

@jessekelly881
Copy link
Contributor

jessekelly881 commented Apr 9, 2024

A Duration.map might be useful as well.

Duration.map("30 seconds", {
  onMillis: (val: number) => val / 2,
  onNanos: (val: bigint) => val / 2n
})

@tim-smart
Copy link
Contributor

Hmm I think we might need a safe variant, and call this unsafeDivide.

@thewilkybarkid
Copy link
Contributor Author

@tim-smart I've had a stab at that. I've followed the Number/BigInt modules in making it different between what returns a None and what throws.

} = dual(
2,
(self: DurationInput, by: number): Option.Option<Duration> =>
match(self, {
Copy link
Contributor

Choose a reason for hiding this comment

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

You can simplify this by wrapping unsafeDivide with a try catch.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The results are a bit different (#2494 (comment)).

@tim-smart tim-smart merged commit e3e0924 into Effect-TS:main Apr 11, 2024
12 checks passed
@github-actions github-actions bot mentioned this pull request Apr 10, 2024
@thewilkybarkid thewilkybarkid deleted the duration-divide branch April 12, 2024 05:46
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Archived in project
Development

Successfully merging this pull request may close these issues.

None yet

3 participants