ben-allen / proposal-measure

MIT License
7 stars 0 forks source link

Representing Measures

Stage: Not presented yet to TC39

Champion: Ben Allen @ben-allen

Author: Ben Allen @ben-allen

Use cases and goals

There is a frequent need to keep numerical measurements together with their units of measurement. Moreover, there is a frequent need to convert measurements to different scales. We propose to create a new object for representing measurements and for converting measurements between scales.

This proposal was originally inspired by, and is to some extent a prerequisite for, the Smart Units proposal for localizable measurements. However, because the need to represent measurements and to convert between measurement scales transcends the context of localization, we have decided to split it out into its own proposal. Below find some of the use cases we'd like to address.

Description

We propose creating a new object, Measure, with the following properties. Note: ⚠️ all property/method names up for bikeshedding.

The object prototype would provide the following methods:

We propose using a limited subset of the units and conversion factors in CLDR's units.xml to determine what units and what conversions are supported. Since this proposal emerges from the context of internationalization/localization, we prefer that the units supported be the units useful for that context, with additional units only added sparingly.

Examples


let m = new Measure(1.8, "meter");
m.convert('foot', 2);
// { value: 5.91, unit: "foot" }
m.localeConvert("en-CA", "personHeight")
// {value: 5, minorValue: 11}
let m = new Measure (4000, "foot");
m.convert("kilometer");
// {value:  1.2192, unit: "kilometer"}
m.localeConvert('en-US', "road");
// {value: 0.76, unit: "mile"}