index.js 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. "use strict";
  2. var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault").default;
  3. Object.defineProperty(exports, "__esModule", {
  4. value: true
  5. });
  6. exports.default = formatISODuration;
  7. var _typeof2 = _interopRequireDefault(require("@babel/runtime/helpers/typeof"));
  8. var _index = _interopRequireDefault(require("../_lib/requiredArgs/index.js"));
  9. /**
  10. * @name formatISODuration
  11. * @category Common Helpers
  12. * @summary Format a duration object according as ISO 8601 duration string
  13. *
  14. * @description
  15. * 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)
  16. *
  17. * @param {Duration} duration - the duration to format
  18. *
  19. * @returns {String} The ISO 8601 duration string
  20. * @throws {TypeError} Requires 1 argument
  21. * @throws {Error} Argument must be an object
  22. *
  23. * @example
  24. * // Format the given duration as ISO 8601 string
  25. * const result = formatISODuration({
  26. * years: 39,
  27. * months: 2,
  28. * days: 20,
  29. * hours: 7,
  30. * minutes: 5,
  31. * seconds: 0
  32. * })
  33. * //=> 'P39Y2M20DT0H0M0S'
  34. */
  35. function formatISODuration(duration) {
  36. (0, _index.default)(1, arguments);
  37. if ((0, _typeof2.default)(duration) !== 'object') throw new Error('Duration must be an object');
  38. var _duration$years = duration.years,
  39. years = _duration$years === void 0 ? 0 : _duration$years,
  40. _duration$months = duration.months,
  41. months = _duration$months === void 0 ? 0 : _duration$months,
  42. _duration$days = duration.days,
  43. days = _duration$days === void 0 ? 0 : _duration$days,
  44. _duration$hours = duration.hours,
  45. hours = _duration$hours === void 0 ? 0 : _duration$hours,
  46. _duration$minutes = duration.minutes,
  47. minutes = _duration$minutes === void 0 ? 0 : _duration$minutes,
  48. _duration$seconds = duration.seconds,
  49. seconds = _duration$seconds === void 0 ? 0 : _duration$seconds;
  50. return "P".concat(years, "Y").concat(months, "M").concat(days, "DT").concat(hours, "H").concat(minutes, "M").concat(seconds, "S");
  51. }
  52. module.exports = exports.default;