Fix FluentDateTime and FluentNumber primitive conversions by julienw · Pull Request #641 · projectfluent/fluent.js · GitHub
Skip to content

Fix FluentDateTime and FluentNumber primitive conversions#641

Merged
eemeli merged 5 commits into
projectfluent:mainfrom
julienw:fix-datetime-valueof
Apr 2, 2025
Merged

Fix FluentDateTime and FluentNumber primitive conversions#641
eemeli merged 5 commits into
projectfluent:mainfrom
julienw:fix-datetime-valueof

Conversation

@julienw

@julienw julienw commented Apr 2, 2025

Copy link
Copy Markdown
Contributor

This fixes a problem when using a custom function taking a FluentDateTime as a parameter, like we're doing in the profiler code.

We're relying on being able to convert a FluentDateTime value to a number:
https://github.com/firefox-devtools/profiler/blob/f699b44fcf763bdfb6ebf36c32d668e4d296f5d6/src/utils/l10n-ftl-functions.js#L87-L91

export const SHORTDATE: FluentFunction = (args, _named) => {
  const date = args[0];
  const nowTimestamp = Date.now();


  const timeDifference = nowTimestamp - +date;

used in:
https://github.com/firefox-devtools/profiler/blob/9c8fb5561537894aee2fe8a265d2c127e83ed0e4/locales/en-US/app.ftl#L679-L683

# The function SHORTDATE is specific to the profiler. It changes the rendering
# depending on the proximity of the date from the current date.
# Variables:
#   $date (Date) - The date to display in a shorter way
NumberFormat--short-date = { SHORTDATE($date) }

The good thing is that the code was already present as a toNumber function. It's not clear to me why the original code didn't use valueOf, but doing it in this patch fixes the problem for me.

I actuallty tested locally with our profiler code, that it fixes the issue.

Hope this solution works for you :-)

@eemeli eemeli changed the title Override valueOf in FluentDateTime so that it can be converted to a n… Fix FluentDateTime and FluentNumber primitive conversions Apr 2, 2025
@eemeli

eemeli commented Apr 2, 2025

Copy link
Copy Markdown
Member

@eemeli eemeli requested review from flodolo and mathjazz April 2, 2025 10:34
@eemeli eemeli merged commit 0fc166e into projectfluent:main Apr 2, 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.

3 participants