SubscriptionFormatters.js 994 B

1234567891011121314151617181920212223242526272829303132333435363738
  1. const dateformat = require('dateformat')
  2. const { formatCurrency } = require('../../util/currency')
  3. /**
  4. * @param {number} priceInCents - price in the smallest currency unit (e.g. dollar cents, CLP units, ...)
  5. * @param {CurrencyCode?} currency - currency code (default to USD)
  6. * @param {string} [locale] - locale string
  7. * @returns {string} - formatted price
  8. */
  9. function formatPriceLocalized(priceInCents, currency = 'USD', locale) {
  10. const isNoCentsCurrency = ['CLP', 'JPY', 'KRW', 'VND'].includes(currency)
  11. const priceInCurrencyUnit = isNoCentsCurrency
  12. ? priceInCents
  13. : priceInCents / 100
  14. return formatCurrency(priceInCurrencyUnit, currency, locale)
  15. }
  16. function formatDateTime(date) {
  17. if (!date) {
  18. return null
  19. }
  20. return dateformat(date, 'mmmm dS, yyyy h:MM TT Z', true)
  21. }
  22. function formatDate(date) {
  23. if (!date) {
  24. return null
  25. }
  26. return dateformat(date, 'mmmm dS, yyyy', true)
  27. }
  28. module.exports = {
  29. formatPriceLocalized,
  30. formatDateTime,
  31. formatDate,
  32. }