-
Notifications
You must be signed in to change notification settings - Fork 14
Add support for custom compound units #19
Comments
From the user perspective, there's only one thing: the API either supports or not compound unit. It would be weird that some compound units are chosen via I think custom compound units is just an internal detail that should be supported, but it shouldn't be exposed as a different API to the user. Let's be consistent and stick with either |
Interesting point. Let's consider meter per second squared. This is currently one of the first-class CLDR units. If we do the string approach, which would we want of the following?
If we do the API approach, how would you put multiple units in the numerator or denominator, such as in meter per second squared? Maybe something like this? new Intl.NumberFormat("es-mx", {
style: "unit",
unit: {
numerator: ["meter"],
denominator: ["second", "second"]
}
}); In the string approach, would we want to support putting multiple units on top, in the numerator, like we can do with the API approach? In either approach, how do we handle the case when locale data isn't capable of rendering the unit? For example, locale data is currently only smart enough to do one numerator and one denominator, except in the special case of units like meter-per-second-per-second, which have their own entry. We can't just check against a list of approved units any more; a proper check would be much more nuanced, possibly beyond the capability of the spec. We would maybe need to revisit #11. |
With respect to your question about multiple compound unit level (the nested-per), I would consider this a separate problem. Anyway, if this is supported, I would expect |
We already have to be a little bit smart to detect when a unit is "special" and has its own CLDR pattern. I don't think coalescing multiple units together would be a significant jump. That being said, I'm kind-of leaning toward keeping a strict whitelist of units supported. CLDR already covers most common compound units, and maybe we can add to the EcmaScript spec a handful of compound units important to web development, like megabytes per second. Then it's clear to developers and to users what's available and what's not. |
I'm a little worried about adding a new string-based syntax of our invention, even if it's relatively simple. We could avoid that by either always using this separated unit/perUnit form, or always using something from the whitelist. |
The decision from the ECMA 402 meeting was to expose the specific list of units anointed by CLDR, which may grow over time, and which includes certain popular compound units like meters per second. The comments included:
rxaviers expressed opposition:
Closing this ticket because there is no need to modify the proposal. |
Thanks for the minutes @sffc |
So on #17, if I understand Rafael's suggestion correctly, the spec actually will include a way to define arbitrary compound units as a string. This would invalidate most of my trepidations about introducing this feature into ECMAScript. I was concerned around the elegance of having a string unit identifier and the complexity of having to design an API for it. I'm reopening this ticket to continue discussion. |
I posted a couple comments over on the CLDR thread. One question we need to answer is how to handle the case when the implementation does not have the data to format a unit correctly. With the latest LDML revision, if accepted, the rule could be like this, where the "list below" refers to the full list of units that I pasted from CLDR into the ES spec:
|
I updated the spec with the new "core unit identifiers" syntax last week in 7606464. WRT correctness logic: we're still throwing a RangeError, but only if the unit nor the decomposed simple units exist in CLDR data. This way, we are getting full CLDR data coverage and only rejecting things for which we legitimately don't know how to format. |
CLDR already provides common compound units directly as top-level units, like
speed-meter-per-second
. However, it also has patterns that allow for custom combinations of units.This could be added as a field in the options enum:
That would give you fathoms per second.
ICU already supports this, and it would be trivial to add it to the spec.
The text was updated successfully, but these errors were encountered: