globalizejs / globalize

A JavaScript library for internationalization and localization that leverages the official Unicode CLDR JSON data
https://globalizejs.com
MIT License
4.8k stars 603 forks source link

Date format to parts #678

Closed rxaviers closed 7 years ago

rxaviers commented 7 years ago

Context

Formatter is designed to provide an opaque string as an output that is not meant to be manipulated by the consumer, which makes impossible for UX/design using rich formatting, e.g., "May <strong>05</strong> 2014", "09:49 <i>pm</i>", and "<strong>Monday</strong>, 09/12/2015".

Goal

Expose format to parts similarly to Ecma-402 Intl.DateTimeFormat.prototype.formatToParts().

References

rxaviers commented 7 years ago

Some API ideas

  1. Add option.parts to dateFormatter (and formatDate) that takes true or false (default). Downside: return different types depending on the options...
  2. Add new function pairs: dateToPartsFormatter and formatDateToParts. Downside: additional work for static compiler.
  3. Add a function on the returned formatter, e.g., dateFormatter() -> fn.toParts(). Downside: very hacky.
rxaviers commented 7 years ago

For the record, @jzaefferer and I have discussed these options and we leaned towards option 2. It seems the one the fits the best for globalize in a way that keeps API simple. Suggestions and ideas are welcome...

kborchers commented 7 years ago

I am by no means an expert here but reviewing the information you have provided, I would also lean toward option option 2 if that helps at all.

rxaviers commented 7 years ago

Thank you @kborchers, it definitely helps.