index.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.default = intervalToDuration;
  6. var _index = _interopRequireDefault(require("../compareAsc/index.js"));
  7. var _index2 = _interopRequireDefault(require("../differenceInYears/index.js"));
  8. var _index3 = _interopRequireDefault(require("../differenceInMonths/index.js"));
  9. var _index4 = _interopRequireDefault(require("../differenceInDays/index.js"));
  10. var _index5 = _interopRequireDefault(require("../differenceInHours/index.js"));
  11. var _index6 = _interopRequireDefault(require("../differenceInMinutes/index.js"));
  12. var _index7 = _interopRequireDefault(require("../differenceInSeconds/index.js"));
  13. var _index8 = _interopRequireDefault(require("../isValid/index.js"));
  14. var _index9 = _interopRequireDefault(require("../_lib/requiredArgs/index.js"));
  15. var _index10 = _interopRequireDefault(require("../toDate/index.js"));
  16. var _index11 = _interopRequireDefault(require("../sub/index.js"));
  17. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  18. /**
  19. * @name intervalToDuration
  20. * @category Common Helpers
  21. * @summary Convert interval to duration
  22. *
  23. * @description
  24. * Convert a interval object to a duration object.
  25. *
  26. * @param {Interval} interval - the interval to convert to duration
  27. *
  28. * @returns {Duration} The duration Object
  29. * @throws {TypeError} Requires 2 arguments
  30. * @throws {RangeError} `start` must not be Invalid Date
  31. * @throws {RangeError} `end` must not be Invalid Date
  32. *
  33. * @example
  34. * // Get the duration between January 15, 1929 and April 4, 1968.
  35. * intervalToDuration({
  36. * start: new Date(1929, 0, 15, 12, 0, 0),
  37. * end: new Date(1968, 3, 4, 19, 5, 0)
  38. * })
  39. * // => { years: 39, months: 2, days: 20, hours: 7, minutes: 5, seconds: 0 }
  40. */
  41. function intervalToDuration(_ref) {
  42. var start = _ref.start,
  43. end = _ref.end;
  44. (0, _index9.default)(1, arguments);
  45. var dateLeft = (0, _index10.default)(start);
  46. var dateRight = (0, _index10.default)(end);
  47. if (!(0, _index8.default)(dateLeft)) {
  48. throw new RangeError('Start Date is invalid');
  49. }
  50. if (!(0, _index8.default)(dateRight)) {
  51. throw new RangeError('End Date is invalid');
  52. }
  53. var duration = {
  54. years: 0,
  55. months: 0,
  56. days: 0,
  57. hours: 0,
  58. minutes: 0,
  59. seconds: 0
  60. };
  61. var sign = (0, _index.default)(dateLeft, dateRight);
  62. duration.years = Math.abs((0, _index2.default)(dateLeft, dateRight));
  63. var remainingMonths = (0, _index11.default)(dateLeft, {
  64. years: sign * duration.years
  65. });
  66. duration.months = Math.abs((0, _index3.default)(remainingMonths, dateRight));
  67. var remainingDays = (0, _index11.default)(remainingMonths, {
  68. months: sign * duration.months
  69. });
  70. duration.days = Math.abs((0, _index4.default)(remainingDays, dateRight));
  71. var remainingHours = (0, _index11.default)(remainingDays, {
  72. days: sign * duration.days
  73. });
  74. duration.hours = Math.abs((0, _index5.default)(remainingHours, dateRight));
  75. var remainingMinutes = (0, _index11.default)(remainingHours, {
  76. hours: sign * duration.hours
  77. });
  78. duration.minutes = Math.abs((0, _index6.default)(remainingMinutes, dateRight));
  79. var remainingSeconds = (0, _index11.default)(remainingMinutes, {
  80. minutes: sign * duration.minutes
  81. });
  82. duration.seconds = Math.abs((0, _index7.default)(remainingSeconds, dateRight));
  83. return duration;
  84. }
  85. module.exports = exports.default;