tc39 / tc39/proposal-amount

Unit extensibility

Open
#25 3 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
HTML
Stars
58
Forks
12
Avg merge
2h 35m
Merged PRs (30d)
2

Description

I believe this is rightfully out of scope for the initial proposal here but I wanted to, at the very least, capture the idea while it is fresh in my head following the discussions this week. I absolutely appreciate and agree with the approach of the proposal relying on the CLDR unit specification for the definition of unit for amounts. However, I can also see valid use cases for allowing users to define units themselves. For this, a Unit type could be useful. This could be as simple as allowing a unit to be expressed as a Symbol:

const amount = Amount.from(12, { unit: Symbol('my-custom-unit') });

Obviously this would have Intl formatting challenges, however. An alternative approach could be to use a typed approach where a Unit may implement it's own toLocaleString(...) for Intl

const MyUnit = Unit.from({ toLocaleString(decimal, locale) { });

const amount = Amount.from(12, { unit: new MyUnit() });

There are, I'll admit, a number of flaws with both of these examples but again, the idea here is just to document the possibility.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reviewing the proposal's current definition of unit and the Amount.from API, then compare the Symbol and Unit examples with Intl formatting requirements. The issue is complete only when the project decides whether custom units are supported and documents a sufficiently precise API and formatting behavior.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript
Domain
api
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Active
Clarity
Needs clarification
Newbie friendliness
30/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.