month-class
Version:
Month class, manages information of the month from a given date
56 lines (52 loc) • 2.35 kB
JavaScript
/**
* @file Manages createWeekends module, used to create weekends property the
* class Month.
*/
// ━━ MODULE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
/**
* The `createWeekends()` function returns a number `array` that represent the
* month's days that are weekend. The value of the returned elements is the same
* `Date.prototype.getDate()`.
*
* The function requires an parameter `options`, must be an object with the
* properties `YYMMDD` `WEEKEND` and `SCE`.
*
* The `options.YYMMDD` value must be a number array with three elements, that
* represents a date (year, month and day). The value of the month is as
* `Date.prototype.getDate()`.
*
* The `options.WEEKEND` value must be a number array. The array values represent
* the days of the week that are weekend, the value of the elements is like
* `Date.prototype.getDay()`.
*
* The `options.SCE` value must be a number array with three elements. The array
* values represent the month's start day, month's current day and month's end
* day. The value of the elements is like `Date.prototype.getDate()`.
*
* @private
* @param {object} options - Function options.
* @param {Array.<number>} options.YYMMDD - A number array with three elements.
* @param {Array.<number>} options.WEEKEND - A number array.
* @param {Array.<number>} options.SCE - A number array with three elements.
* @returns {Array.<number>} A number array.
* @example const weekends = createWeekends({
* YYMMDD: [2020, 0, 1],
* WEEKEND: [6, 0],
* SCE: [1, 12, 31],
* }); // expected value [4, 5, 11, 12, 18, 19, 25, 26]
*
*/
const createWeekends = ({ YYMMDD, WEEKEND, SCE }) => {
const [YY, MM] = YYMMDD;
const total = SCE[2];
const temporary = new Date(YY, MM, 1);
const weekends = [];
for (let i = 1; i <= total; i += 1) {
temporary.setDate(i);
const weekday = temporary.getDay();
if (WEEKEND.includes(weekday)) weekends.push(i);
}
return weekends;
};
// ━━ EXPORT MODULE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
module.exports = createWeekends;