index.js 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. import requiredArgs from "../_lib/requiredArgs/index.js";
  2. /**
  3. * @name formatISODuration
  4. * @category Common Helpers
  5. * @summary Format a duration object according as ISO 8601 duration string
  6. *
  7. * @description
  8. * Format a duration object according to the ISO 8601 duration standard (https://www.digi.com/resources/documentation/digidocs/90001437-13/reference/r_iso_8601_duration_format.htm)
  9. *
  10. * @param {Duration} duration - the duration to format
  11. *
  12. * @returns {String} The ISO 8601 duration string
  13. * @throws {TypeError} Requires 1 argument
  14. * @throws {Error} Argument must be an object
  15. *
  16. * @example
  17. * // Format the given duration as ISO 8601 string
  18. * formatISODuration({
  19. * years: 39,
  20. * months: 2,
  21. * days: 20,
  22. * hours: 7,
  23. * minutes: 5,
  24. * seconds: 0
  25. * })
  26. * //=> 'P39Y2M20DT0H0M0S'
  27. */
  28. export default function formatISODuration(duration) {
  29. requiredArgs(1, arguments);
  30. if (typeof duration !== 'object') throw new Error('Duration must be an object');
  31. var _duration$years = duration.years,
  32. years = _duration$years === void 0 ? 0 : _duration$years,
  33. _duration$months = duration.months,
  34. months = _duration$months === void 0 ? 0 : _duration$months,
  35. _duration$days = duration.days,
  36. days = _duration$days === void 0 ? 0 : _duration$days,
  37. _duration$hours = duration.hours,
  38. hours = _duration$hours === void 0 ? 0 : _duration$hours,
  39. _duration$minutes = duration.minutes,
  40. minutes = _duration$minutes === void 0 ? 0 : _duration$minutes,
  41. _duration$seconds = duration.seconds,
  42. seconds = _duration$seconds === void 0 ? 0 : _duration$seconds;
  43. return "P".concat(years, "Y").concat(months, "M").concat(days, "DT").concat(hours, "H").concat(minutes, "M").concat(seconds, "S");
  44. }