You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

46 lines
1.9 KiB

  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 to ISO 8601 Duration standards (https://www.digi.com/resources/documentation/digidocs/90001437-13/reference/r_iso_8601_duration_format.htm)
  12. *
  13. * @param {Duration} duration
  14. *
  15. * @returns {String} The ISO 8601 Duration string
  16. * @throws {TypeError} Requires 1 argument
  17. * @throws {Error} Argument must be an object
  18. *
  19. * @example
  20. * // Get the ISO 8601 Duration between January 15, 1929 and April 4, 1968.
  21. * const result = formatISODuration({ years: 39, months: 2, days: 20, hours: 7, minutes: 5, seconds: 0 })
  22. * // => 'P39Y2M20DT0H0M0S'
  23. */
  24. function formatISODuration(duration) {
  25. (0, _index.default)(1, arguments);
  26. if (typeof duration !== 'object') throw new Error('Duration must be an object');
  27. var _duration$years = duration.years,
  28. years = _duration$years === void 0 ? 0 : _duration$years,
  29. _duration$months = duration.months,
  30. months = _duration$months === void 0 ? 0 : _duration$months,
  31. _duration$days = duration.days,
  32. days = _duration$days === void 0 ? 0 : _duration$days,
  33. _duration$hours = duration.hours,
  34. hours = _duration$hours === void 0 ? 0 : _duration$hours,
  35. _duration$minutes = duration.minutes,
  36. minutes = _duration$minutes === void 0 ? 0 : _duration$minutes,
  37. _duration$seconds = duration.seconds,
  38. seconds = _duration$seconds === void 0 ? 0 : _duration$seconds;
  39. return "P".concat(years, "Y").concat(months, "M").concat(days, "DT").concat(hours, "H").concat(minutes, "M").concat(seconds, "S");
  40. }
  41. module.exports = exports.default;