unsplash / intlc

Compile ICU messages into code. Supports TypeScript and JSX. No runtime.
MIT License
56 stars 3 forks source link

Support number "skeletons" #20

Open samhh opened 2 years ago

samhh commented 2 years ago

https://formatjs.io/docs/core-concepts/icu-syntax/#number-type

samhh commented 2 years ago

Some use cases in Unsplash web:

new Intl.NumberFormat(
  l,
  { style: 'percent' },
).format(n)

new Intl.NumberFormat(
  l,
  { style: 'unit', unit: 'megabyte' },
).format(n)

I think the ICU spec is broader than that which is supported by JavaScript, for example in terms of supported units.

The NumberFormat constructor throws in the presence of unknown units. The easiest approach is to only support the units in the above linked list. Alternatively we could support any unit and fall back to catching and returning a plainly formatted number, however I think feedback from the compiler for things like typos would be valuable.

samhh commented 2 years ago

I think feedback from the compiler for things like typos would be valuable.

Can we viably do this for things like currency codes?

Tempted to start that way and just expand the list whenever we or anyone else needs anything added. Codegen would be bloated quite a bit by any sort of try/catch solution.