index.js 2.0 KB

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