SubscriptionFormatters.js 870 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. const dateformat = require('dateformat')
  2. const currencySymbols = {
  3. EUR: '€',
  4. USD: '$',
  5. GBP: '£',
  6. SEK: 'kr',
  7. CAD: '$',
  8. NOK: 'kr',
  9. DKK: 'kr',
  10. AUD: '$',
  11. NZD: '$',
  12. CHF: 'Fr',
  13. SGD: '$',
  14. INR: '₹',
  15. }
  16. module.exports = {
  17. formatPrice(priceInCents, currency) {
  18. if (!currency) {
  19. currency = 'USD'
  20. }
  21. let string = String(Math.round(priceInCents))
  22. if (string.length === 2) {
  23. string = `0${string}`
  24. }
  25. if (string.length === 1) {
  26. string = `00${string}`
  27. }
  28. if (string.length === 0) {
  29. string = '000'
  30. }
  31. const cents = string.slice(-2)
  32. const dollars = string.slice(0, -2)
  33. const symbol = currencySymbols[currency]
  34. return `${symbol}${dollars}.${cents}`
  35. },
  36. formatDate(date) {
  37. if (!date) {
  38. return null
  39. }
  40. return dateformat(date, 'mmmm dS, yyyy h:MM TT Z', true)
  41. },
  42. }