tc39 / proposal-smart-unit-preferences

MIT License
21 stars 4 forks source link

Is arithmetic in scope for ECMA-402? #10

Open sffc opened 3 years ago

sffc commented 3 years ago

@zbraniecki and @littledan have pushed back on the idea of performing math on units as part of ECMA-402's black box. For example, this proposal necesitates the ability to provide input such as "2 meters" and have that converted to the correct unit or units, such as "600 cm" or "6 ft, 3 in".

I can see both perspectives here. On the one hand, a third-party math library would be better suited to performing this arithmetic. On the other, the current proposal covers the full-stack i18n use case, and exposing the individual pieces would amount to a much larger API surface that we would need to design.

devsnek commented 3 years ago

Obviously not proposing this exact api, but is this the sort of abstraction level that is being suggested? This math seems like the exact sort of thing intl should be responsible for.

new Intl.NumberFormat('en-US', {
  style: 'unit',
  unit: 'meter',
  usage: 'person-height',
  unitDisplay: 'long'
}).format(1.8, (from, to, value) => {
  switch (from) {
    case 'meter':
      switch (to) {
        case 'foot':
          return value * 3.281;
        case 'inch':
          return value * 39.37;
        // ...
        default:
          break;
      }
    case 'foot':
      // ...  
  }
  return 'unknown';
});