utils.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. var __defProp = Object.defineProperty;
  2. var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
  3. var __getOwnPropNames = Object.getOwnPropertyNames;
  4. var __hasOwnProp = Object.prototype.hasOwnProperty;
  5. var __export = (target, all) => {
  6. for (var name2 in all)
  7. __defProp(target, name2, { get: all[name2], enumerable: true });
  8. };
  9. var __copyProps = (to, from, except, desc) => {
  10. if (from && typeof from === "object" || typeof from === "function") {
  11. for (let key of __getOwnPropNames(from))
  12. if (!__hasOwnProp.call(to, key) && key !== except)
  13. __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
  14. }
  15. return to;
  16. };
  17. var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
  18. var stdin_exports = {};
  19. __export(stdin_exports, {
  20. bem: () => bem,
  21. calcDateNum: () => calcDateNum,
  22. cloneDate: () => cloneDate,
  23. cloneDates: () => cloneDates,
  24. compareDay: () => compareDay,
  25. compareMonth: () => compareMonth,
  26. formatMonthTitle: () => formatMonthTitle,
  27. getDayByOffset: () => getDayByOffset,
  28. getMonthByOffset: () => getMonthByOffset,
  29. getNextDay: () => getNextDay,
  30. getNextMonth: () => getNextMonth,
  31. getNextYear: () => getNextYear,
  32. getPrevDay: () => getPrevDay,
  33. getPrevMonth: () => getPrevMonth,
  34. getPrevYear: () => getPrevYear,
  35. getToday: () => getToday,
  36. getYearByOffset: () => getYearByOffset,
  37. name: () => name,
  38. t: () => t
  39. });
  40. module.exports = __toCommonJS(stdin_exports);
  41. var import_utils = require("../utils");
  42. const [name, bem, t] = (0, import_utils.createNamespace)("calendar");
  43. const formatMonthTitle = (date) => t("monthTitle", date.getFullYear(), date.getMonth() + 1);
  44. function compareMonth(date1, date2) {
  45. const year1 = date1.getFullYear();
  46. const year2 = date2.getFullYear();
  47. if (year1 === year2) {
  48. const month1 = date1.getMonth();
  49. const month2 = date2.getMonth();
  50. return month1 === month2 ? 0 : month1 > month2 ? 1 : -1;
  51. }
  52. return year1 > year2 ? 1 : -1;
  53. }
  54. function compareDay(day1, day2) {
  55. const compareMonthResult = compareMonth(day1, day2);
  56. if (compareMonthResult === 0) {
  57. const date1 = day1.getDate();
  58. const date2 = day2.getDate();
  59. return date1 === date2 ? 0 : date1 > date2 ? 1 : -1;
  60. }
  61. return compareMonthResult;
  62. }
  63. const cloneDate = (date) => new Date(date);
  64. const cloneDates = (dates) => Array.isArray(dates) ? dates.map(cloneDate) : cloneDate(dates);
  65. function getDayByOffset(date, offset) {
  66. const cloned = cloneDate(date);
  67. cloned.setDate(cloned.getDate() + offset);
  68. return cloned;
  69. }
  70. function getMonthByOffset(date, offset) {
  71. const cloned = cloneDate(date);
  72. cloned.setMonth(cloned.getMonth() + offset);
  73. if (cloned.getDate() !== date.getDate()) {
  74. cloned.setDate(0);
  75. }
  76. return cloned;
  77. }
  78. function getYearByOffset(date, offset) {
  79. const cloned = cloneDate(date);
  80. cloned.setFullYear(cloned.getFullYear() + offset);
  81. if (cloned.getDate() !== date.getDate()) {
  82. cloned.setDate(0);
  83. }
  84. return cloned;
  85. }
  86. const getPrevDay = (date) => getDayByOffset(date, -1);
  87. const getNextDay = (date) => getDayByOffset(date, 1);
  88. const getPrevMonth = (date) => getMonthByOffset(date, -1);
  89. const getNextMonth = (date) => getMonthByOffset(date, 1);
  90. const getPrevYear = (date) => getYearByOffset(date, -1);
  91. const getNextYear = (date) => getYearByOffset(date, 1);
  92. const getToday = () => {
  93. const today = /* @__PURE__ */ new Date();
  94. today.setHours(0, 0, 0, 0);
  95. return today;
  96. };
  97. function calcDateNum(date) {
  98. const day1 = date[0].getTime();
  99. const day2 = date[1].getTime();
  100. return (day2 - day1) / (1e3 * 60 * 60 * 24) + 1;
  101. }