这是indexloc提供的服务,不要输入任何密码
Skip to content

Unit extensibility #25

@jasnell

Description

@jasnell

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions