UNPKG

calendar-ts

Version:
75 lines (74 loc) 2.49 kB
var __defProp = Object.defineProperty; var __getOwnPropDesc = Object.getOwnPropertyDescriptor; var __getOwnPropNames = Object.getOwnPropertyNames; var __hasOwnProp = Object.prototype.hasOwnProperty; var __export = (target, all) => { for (var name in all) __defProp(target, name, { get: all[name], enumerable: true }); }; var __copyProps = (to, from, except, desc) => { if (from && typeof from === "object" || typeof from === "function") { for (let key of __getOwnPropNames(from)) if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); } return to; }; var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); // src/index.ts var src_exports = {}; __export(src_exports, { default: () => src_default }); module.exports = __toCommonJS(src_exports); var Calendar = class { constructor(firstWeekDay = 0) { this.firstWeekDay = firstWeekDay; } weekStartDate(date) { const starDate = new Date(date.getTime()); while (starDate.getDay() !== this.firstWeekDay) { starDate.setDate(starDate.getDate() - 1); } return starDate; } monthDates(year, month) { if (year < 1970) { console.warn("year must be a number >= 1970"); } if (month < 0 || month > 11) { console.error("month must be a number (Jan is 0)"); } let week = []; const weeks = []; let date = this.weekStartDate(new Date(year, month, 1)); do { for (let i = 0; i < 7; i++) { week.push(date); date = new Date(date.getTime()); date.setDate(date.getDate() + 1); } weeks.push(week); week = []; } while (date.getMonth() <= month && date.getFullYear() === year); return weeks; } monthDays(year, month) { return this.monthDates(year, month).map((row) => row.map((col) => col.getMonth() === month ? col.getDate() : 0)); } monthText(year, month) { const getDayOrBlank = (date) => { let s = date.getMonth() === month ? date.getDate().toString() : " "; while (s.length < 2) { s = ` ${s}`; } return s; }; const weeks = this.monthDates(year, month).map((row) => row.map((col) => getDayOrBlank(col))); return weeks.join("\n"); } }; var src_default = Calendar; // Annotate the CommonJS export names for ESM import in node: 0 && (module.exports = {}); //# sourceMappingURL=index.cjs.map