@ce1pers/date-helpers
Version:
Simple date time helpers.
122 lines (121 loc) • 4.29 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.computeCurrentMonthTotalDateCount = exports.computeMonthTotalDateCount = exports.obtainLastDay = exports.obtainLastDate = exports.obtainFirstDay = exports.obtainFirstDate = exports.convertDay = exports.parseTextFormat = void 0;
const constants_1 = require("../constants");
/**
* Parse day text format.
* @param {string} text
* @param {ConvertFormat} format
*/
const parseTextFormat = (text, format) => format === "uppercase"
? text.toUpperCase()
: format === "lowercase"
? text.toLowerCase()
: text;
exports.parseTextFormat = parseTextFormat;
/**
* Convert day index to string;
* @param {number} dayIndex Day value as index.
*/
const convertDay = (dayIndex, options = { convertFormat: "capitalize" }) => {
switch (dayIndex) {
case 0:
return (0, exports.parseTextFormat)(constants_1.DayConstants.Sunday, options?.convertFormat);
case 1:
return (0, exports.parseTextFormat)(constants_1.DayConstants.Monday, options?.convertFormat);
case 2:
return (0, exports.parseTextFormat)(constants_1.DayConstants.Tuesday, options?.convertFormat);
case 3:
return (0, exports.parseTextFormat)(constants_1.DayConstants.Wednesday, options?.convertFormat);
case 4:
return (0, exports.parseTextFormat)(constants_1.DayConstants.Thursday, options?.convertFormat);
case 5:
return (0, exports.parseTextFormat)(constants_1.DayConstants.Friday, options?.convertFormat);
case 6:
return (0, exports.parseTextFormat)(constants_1.DayConstants.Saturday, options?.convertFormat);
}
};
exports.convertDay = convertDay;
/**
* Obtain first date.
*/
const obtainFirstDate = (year, month) => {
const now = new Date();
now.setDate(1);
year && now.setFullYear(year);
month && now.setMonth(month - 1);
return now;
};
exports.obtainFirstDate = obtainFirstDate;
/**
* Obtain first day.
*/
const obtainFirstDay = (year, month, options) => {
const dayIndex = (0, exports.obtainFirstDate)(year, month).getDay();
const convertedDay = (0, exports.convertDay)(dayIndex, options);
return {
dayIndex,
convertedDay,
};
};
exports.obtainFirstDay = obtainFirstDay;
/**
* Obtain last date information.
* @param {number} year
* @param {number} month
*/
const obtainLastDate = (year, month) => {
const now = new Date();
year && now.setFullYear(year);
month ? now.setMonth(month) : now.setMonth(now.getMonth() + 1);
now.setDate(0);
return now;
};
exports.obtainLastDate = obtainLastDate;
/**
* Obtain last day information.
*/
const obtainLastDay = (year, month, options) => {
const now = (0, exports.obtainLastDate)(year, month);
console.log(now);
const dayIndex = now.getDay();
const convertedDay = (0, exports.convertDay)(dayIndex, options);
return {
dayIndex,
convertedDay,
};
};
exports.obtainLastDay = obtainLastDay;
/**
* Compute total calendar date count by month.
* @param {number} month
*/
const computeMonthTotalDateCount = (year, month) => {
const MAX_DATE_COUNT = 42;
const firstDay = (0, exports.obtainFirstDay)(year, month);
const lastDate = (0, exports.obtainLastDate)(year, month);
const previousMonthDateCount = firstDay.dayIndex;
const currentMonthDateCount = lastDate.getDate();
const nextMonthDateCount = MAX_DATE_COUNT - (previousMonthDateCount + currentMonthDateCount);
return {
currentMonthDateCount,
previousMonthDateCount,
nextMonthDateCount,
};
};
exports.computeMonthTotalDateCount = computeMonthTotalDateCount;
/**
* Compute current month total calendar date count.
*/
const computeCurrentMonthTotalDateCount = () => {
const firstDay = (0, exports.obtainFirstDay)();
const lastDay = (0, exports.obtainLastDay)();
const previousMonthDateCount = firstDay.dayIndex;
const nextMonthDateCount = 7 - (lastDay.dayIndex + 1);
return {
currentMonthDateCount: 35 - previousMonthDateCount - nextMonthDateCount,
previousMonthDateCount,
nextMonthDateCount,
};
};
exports.computeCurrentMonthTotalDateCount = computeCurrentMonthTotalDateCount;