Skip to content

Extract time functions#1365

Merged
aeisenberg merged 3 commits intomainfrom
aeisenberg/time
May 30, 2022
Merged

Extract time functions#1365
aeisenberg merged 3 commits intomainfrom
aeisenberg/time

Conversation

@aeisenberg
Copy link
Contributor

Create the time.ts module as a place to put fime functions.
Move two time functions there and create tests for them.

The humanizeUnit function now uses ECMAscript apis. This ensures
that pluralization happens appropriately.

Also, fix a small bug in the results view to enure repository
is correctly pluralized.

Checklist

  • [n/a] CHANGELOG.md has been updated to incorporate all user visible changes made by this pull request.
  • [n/a] Issues have been created for any UI or other user-facing changes made by this pull request.
  • [Maintainers only] If this pull request makes user-facing changes that require documentation changes, open a corresponding docs pull request in the github/codeql repo and add the ready-for-doc-review label there.

Create the `time.ts` module as a place to put fime functions.
Move two time functions there and create tests for them.

The `humanizeUnit` function now uses ECMAscript apis. This ensures
that pluralization happens appropriately.

Also, fix a small bug in the results view to enure `repository`
is correctly pluralized.
@aeisenberg aeisenberg requested review from a team as code owners May 26, 2022 22:47
Copy link
Contributor

@angelapwen angelapwen left a comment

Choose a reason for hiding this comment

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

✅ assuming the line I commented on makes sense 😸

Comment on lines 26 to 28
{humanizeDuration(lastUpdated)}
{humanizeDuration(lastUpdated && -lastUpdated)}
Copy link
Contributor

Choose a reason for hiding this comment

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

I'm not sure I understand this line of code — could you explain it?

Copy link
Contributor

Choose a reason for hiding this comment

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

I was about to ask the same question! What does it mean to && these two numbers? 😅

Copy link
Contributor Author

Choose a reason for hiding this comment

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

lastUpdated may be undefined. If so, do not want to negate it (this would convert it to NaN, which is BAD).

If lastUpdated is 0, then negating is a noop. So we don't need to negate.

Would it be easier to understand if I did this?


lastUpdated === undefined ? undefined : -lastUpdated

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Suggested change
{humanizeDuration(lastUpdated)}
{humanizeDuration(lastUpdated && -lastUpdated)}
{humanizeDuration(lastUpdated === undefined ? undefined : -lastUpdated)}

Copy link
Contributor

Choose a reason for hiding this comment

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

Hm, yes, I find that easier to understand. What happens if lastUpdated is an integer other than 0 though? Do we expect it to be a positive or negative integer (and why do we need to negate it at all if the absolute value is taken in humanizeDuration()?)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good questions. If lastUpdated is something other than 0, then it is negated (meaning, a time in the past).

Absolute value is used to determine which unit to use. A positive integer indicates "XXX units from now". A negative integer indicates "XXX units ago".

Maybe it would be clearer if we ensured that lastUpdated was always negative and I can put a comment there.

Copy link
Contributor

Choose a reason for hiding this comment

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

oh yes! I see how it works with the new change now. I think that would make it clearer, yes!

numeric: 'auto',
});

// All these are approximate, specifically months and years
Copy link
Contributor

Choose a reason for hiding this comment

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

Nit

Suggested change
// All these are approximate, specifically months and years
// Months and years are approximate

return '';
}

if (Math.abs(diffInMs) < HOUR_IN_MILLIS) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Should we be covering seconds as well?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It didn't seem to make sense for our purposes.

});
}

export function humanizeUnit(diffInMs?: number): string {
Copy link
Contributor

Choose a reason for hiding this comment

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

I have a very tired brain, but I can't easily understand the difference between humanizeDuration and humanizeUnit - can we think of different names?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I'll add comments. I can't think of better names.

Rename, add comments, and extract some local variables.
@aeisenberg
Copy link
Contributor Author

All good now?

Copy link
Contributor

@charisk charisk left a comment

Choose a reason for hiding this comment

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

Sorry for the delay, thought this was approved!

@aeisenberg aeisenberg merged commit 8ff21d6 into main May 30, 2022
@aeisenberg aeisenberg deleted the aeisenberg/time branch May 30, 2022 14:54
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.

4 participants