-
Notifications
You must be signed in to change notification settings - Fork 106
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Unit formatting API #32
Comments
We should take input from http://www.unicode.org/reports/tr35/tr35-general.html#Unit_Elements |
Relevant CLDR entry example: http://www.unicode.org/cldr/charts/27/summary/pl.html#5556 |
It should be required to be explicitly passed in. You do NOT want I would require the formatter to be explicit, or, have some object type where the object specifies the type. Perhaps a subclassed Unit conversion, unit discovery, etc should be based on top of a service that just formats explicit units. Shades of the |
An alternative API var formatter = Intl.UnitFormat(locales, unit[, options]);
formatter.format(value);
// Example
var formatter = Intl.UnitFormat("en", "second");
formatter(2);
// > "2 seconds"
var formatterShort = Intl.UnitFormat("en", "second", {
form: "short"
});
formatterShort(2);
// > "2 sec"
var speedFormatter = Intl.UnitFormat("en", "kilometer-per-hour", {
form: "short"
});
speedFormatter(100);
// > "100 kph"
var speedFormatterPt = Intl.UnitFormat("pt", "kilometer-per-hour", {
form: "short"
});
speedFormatterPt(100);
// > "100 km/h" |
@rxaviers I like this format better, but I think it should be |
@rxaviers - I like your proposal better than mine. Thanks! One question - did you mean for |
I like what I'm seeing here, but I want to bikeshed on the api a little bit more until we feel confortable with it, here is an alternative proposal where we will have:
var formatter = Intl.UnitFormat(locales[, options]);
formatter.format(value);
// Example
new Intl.UnitFormat("en", {
unit: "second"
}).format(2);
// > "2 seconds"
new Intl.UnitFormat("en", {
style: "short",
unit: "second"
}).format(2);
// > "2 sec"
new Intl.UnitFormat("en", {
style: "short",
unit: "kilometer-per-hour"
}).format(100);
// > "100 kph"
new Intl.UnitFormat("pt", {
style: "short",
unit: "kilometer-per-hour"
}).format(100);
// > "100 km/h" |
@zbraniecki whoops, I forgot the @caridy, we're definitely making improvements, thanks. @srl295, sure, we should brainstorm about the unit values. The reasons I like the shorter form (e.g.,
1: On CLDR docs (6.1 per Unit patterns), it says |
@rxaviers in #35 (comment) suggests that things like durations (hh:mm:ss -> 00:23:15) should be part of UnitFormat, let's discuss how to alter the API proposal to achieve that. For this kind of formatting to work we need to either choose a max-min units, or some reference unit. For Time, I feel like maxUnit/minUnit would be better, because those can be then used for UIs like video/music players (00:23 in the song, or -01:34 left) or stopwatch/timer (00:00:23.15). So, my proposal is:
Open questions:
Alternatively, maybe we shouldn't merge DurationFormat with UnitFormat? Maybe it is different enough that it will just not fit? Ideas? Opinions? |
I don't like min/max definitions. Instead, I think we can use a more familiar schema, which is the use of |
I understand why you don't like it. I'm also not super happy with it, but I can't find any other way to squeeze all of the forms in one API. My current thinking goes in the direction of the distinction between formatting that tries to build the best 'written' representation of data ('20 seconds', '5 minutes', '10 hours'), and more UI-like scientific representation ( I'm thinking a lot about this vs. number format. I feel like it's pretty unfortunate that I still think that So, in a perfect world, I imagine the former group go into
and then if we establish some rule that, for example for
The benefit of that is that it would work for type The problem is that we don't have how to accommodate compound patterns, and of course, that that train has already left. The other part of the equation is what I so far am calling For that, users want to define the upper and lower limit (hour-second, minute-millisecond, feet-inches), we don't need long/short/narrow forms, we don't need compound units, we don't need guessing, we need to fit the data into a pattern. I feel like the goal should be to squeeze both into one API, but I struggle to find common option list for both models. For height, we could want to show Regarding time, my list of displays I can imagine would be needed are:
Any ideas how to accommodate all those forms in one API? Maybe we just need to functions on the formatter? Like Ugh, it's complex :) |
So, it seems that ICU is using |
I ended up opening #47 to discuss the duration formatting. Without it, I feel like One item from CLDR that I see missing from the API is unit sequence - http://www.unicode.org/reports/tr35/tr35-general.html#Unit_Sequences Is there room for that in this API? |
Asking because CLDR examples - Once we get the API's working, we could construct the ICU example by running two components via |
True but with a few additional rules. About currency, the symbol position appears after the number in some locales, but before in others, which doesn't happen with unit in general (always after); the fraction digits also change depending of the chosen currency (e.g., JPY uses no fraction digits, others use 5 decimal points instead of 2). About percent, the number value gets adjusted on formatting, i.e., 0.1 becomes 10%. |
Good point. Perhaps, keeping UnitFormat simple (single units and compound only) and then using UnitFormat + ListFormat for the "5° 30′" and "3 ft 2 in" ones would be the best approach indeed. I've noticed from your link that CLDR 6.2 Unit Sequences section points to the listPattern data. So, that seems the way to go. For clarity, I understood that if we take that approach, that's what we are going to get:
|
I'm implementing
To the last point, @rxaviers - I don't think that We can either add another style to |
Please, could you clarify? What about using the
I'm referring to CLDR 6.2 Unit Sequences section and the listPattern data, which should be used to glue the unit sequences, right? Did I understand you well? Using their own example: |
Do you want to allow for arbitrary units on both sides?
That's cool! Didn't see that before. So, we should just add |
Yeap, for arbitrary units on both sides. Although some might not make any sense, it's more generic and up to the user to pick. It also goes in line with CLDR 6.1 per Unit patterns, including the precomputed forms.
Yeap, possibly :) |
Landed As you can see the Let's see how it'll handle in real life environment. |
Heads up, this has been advanced to Stage 0 as of today. |
We plan to request stage 2 at the next TC39 meeting |
This proposal has not received an approval to move to stage 2 yet. The TC39 committee asked us to
@maggiepint may want to look at https://github.com/zbraniecki/proposal-intl-unit-format My current plan is to focus on ListFormat first, then RelativeTimeFormat, but in the meantime I'd be happy to see more conversation about the scope and shape of UnitFormat proposal. |
Has there been any discussion on 'compound' unit format? '3 meters and 45 cenitmers' or '3 m 45 cm' As with 'Relative Date', UnitFormat can also benefit from that. |
@jungshik IIRC, we have discussed briefly in some of the F2F meetings in the pass. Do you think that we need to open a new issue to track just that? |
@jungshik Do you think we should include compound units in the first version, or as a follow-on? |
See my follow-up in #215. I included an option for compound units. |
Is support for IEC 80000-13 binary prefixes planned? It would be a great pity to perpetuate the ambiguity about the meaning of k, M, G, etc. |
We use the units provided by CLDR. Myself and others have long requested that the binary prefixes be added for digital units, but CLDR has held the position that the goal of CLDR units is to reflect common use, not necessarily correct use. As long as the "old" prefixes are in common use and the "new" prefixes are not, there will continue to be pushback for adding them to CLDR. Here is one discussion on the topic: https://unicode.org/cldr/trac/ticket/7214
There is a new ticket however where these units appear to be back on the table. I am putting a reference to this thread on that ticket: |
On Linux binary prefixes are used quite commonly. Don't make Google the ultimate oracle. Besides, producers of mass memory usually (understandably, as it gives higher numbers) use the SI prefixes correctly, e.g. a 100 MB hard disk can store 100000000 bytes. |
This has been folded into |
* remove console.log call and bump to 1.24.1 * update package-lock.json * fix Interval.hasSame behaviour for empty intervals (#712) Fixes #709 * Mark the package as side-effects-free (#713) Addresses #710 * Add DATE_MED_WITH_WEEKDAY preset (#716) * All tests assume America/NewYork timezone (#718) * Add missing values in conversion matrices (#720) * Remove normalization in ShiftTo (#721) * RelativeTime test fails on last day of the month (#719) * Update wording around date equality test (#725) * Update wording around date equality test As discussed in #108 * Fix for negative values in plus/minus. Fixes #645 and #669 (#722) Requires the `fullConversionMatrices` and `noNormalizationInShiftTo` branches to be merged first. * Fixed typos in docs where 'month' was used in place of 'weekday' (#732) * feat: add support for large durations in seconds (#737) * Bump lodash from 4.17.14 to 4.17.19 (#739) Bumps [lodash](https://github.com/lodash/lodash) from 4.17.14 to 4.17.19. - [Release notes](https://github.com/lodash/lodash/releases) - [Commits](lodash/lodash@4.17.14...4.17.19) Signed-off-by: dependabot[bot] <support@github.com> Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> * Interval creation supports Settings.throwOnInvalid. Fixes #728 (#761) * formatOffset handles signed offsets. Fixes #665 (#756) * Update package-lock.json after npm install (#751) Co-authored-by: Gilles Debunne <gilles.debunne@gmail.com> * Add tests around DateTime.toISO() (#752) * Round minutes in timezone offset display. Fixes #724 (#755) * Support arbitrary precision in ISO milliseconds. Fixes #757 (#758) * Support arbitrary precision in ISO milliseconds. Fixes #757 * Limit regex to 30 digits to prevent DOS * fromFormat handles non breakable spaces. Fixes #714 (#762) * Update Why page, remove outdated Relative references. Fixes #679. (#765) * bump to 1.25.0 * Introduce DateTime.now() (#766) * Bump codecov from 3.6.5 to 3.7.1 (#744) Bumps [codecov](https://github.com/codecov/codecov-node) from 3.6.5 to 3.7.1. - [Release notes](https://github.com/codecov/codecov-node/releases) - [Commits](codecov/codecov-node@v3.6.5...v3.7.1) Signed-off-by: dependabot[bot] <support@github.com> Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> * Update endOf() documentation with the same units as startOf() (#779) * UnitFormat was folded into Unified NumberFormat (#785) As per tc39/ecma402#32 (comment) Co-authored-by: Isaac Cambron <isaac@isaaccambron.com> Co-authored-by: downace <downace@users.noreply.github.com> Co-authored-by: Ryota Kameoka <kameoka.ryota@gmail.com> Co-authored-by: saltire <saltire@users.noreply.github.com> Co-authored-by: Jon Knowles <jknowles@digitalprimates.net> Co-authored-by: Jörg Bayreuther <jb@visualjerk.de> Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> Co-authored-by: Gilles Debunne <63719587+gdebunne@users.noreply.github.com> Co-authored-by: Anthon Holmqvist <anthonkendel@users.noreply.github.com> Co-authored-by: Bart Enkelaar <benkelaar@gmail.com>
Unit formatting is similar to number formatting, but it involves one more units.
The goal is to produce strings like "5 MB/s", "10 KB", "35 °C" etc.
I believe the most relevant for the Web audience areas would be:
and maybe:
I would suggest that we limit ourselves first to just short version of units and use compound patterns to produce related units (like speed out of length and duration units).
In the future we may leave a window open to add 'variant' option for 'short'/'long' etc.
Proposed API:
Open questions
The text was updated successfully, but these errors were encountered: