methods/birth-date.js

  1. import formatDate from './format-date'
  2. /**
  3. * @method
  4. * @memberof ecomUtils
  5. * @name birthDate
  6. * @description Returns birth date formatted string from customer body object.
  7. * @param {object} customer - Customer body object or birth date object with day, month and year
  8. * @returns {string}
  9. *
  10. * @example
  11. * // costumer will be the parameter of this function and as result will return his birth date
  12. * // const customer = object (body customer)
  13. * // customer can be like:
  14. * const costumer = { main_email: 'joejonh@me.com', birth_date: { year: 1990, month: 10, day: 1 } }
  15. * ecomUtils.birthDate(costumer)
  16. * // => "10/1/1990"
  17. */
  18. const birthDate = customer => {
  19. if (typeof customer === 'object' && customer !== null) {
  20. let birth = customer.birth_date || customer
  21. if (birth) {
  22. let { day, month, year } = birth
  23. if (day && month && year) {
  24. // has complete customer birth date
  25. // mount Date object and return formatted date string
  26. return formatDate(new Date(year, month - 1, day))
  27. }
  28. }
  29. }
  30. // returns empty string by default
  31. return ''
  32. }
  33. export default birthDate