UNPKG

this-month

Version:

Utility library to get details about an specific month.

172 lines (153 loc) 4.94 kB
'use strict'; Object.defineProperty(exports, "__esModule", { value: true }); var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }(); var _utils = require('./utils'); function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } var ThisMonth = function () { function ThisMonth(args) { _classCallCheck(this, ThisMonth); var year = args.year, month = args.month, daysOff = args.daysOff, holidays = args.holidays; this.year = year; this.month = month; this.daysOff = (0, _utils.getDaysOff)(daysOff); this.holidays = (0, _utils.getHolidays)(holidays); } _createClass(ThisMonth, [{ key: 'weeklyDaysOff', value: function weeklyDaysOff() { return this.daysOff.join(','); } }, { key: 'numberOfWeeks', value: function numberOfWeeks() { var firstOfMonth = new Date(this.year, this.month - 1, 1); var lastOfMonth = new Date(this.year, this.month, 0); var used = firstOfMonth.getDay() + lastOfMonth.getDate(); var weeks = Math.ceil(used / 7); // 7 days return weeks; } }, { key: 'numberOfNaturalDays', value: function numberOfNaturalDays() { return new Date(this.year, this.month, 0).getDate(); } }, { key: 'numberOfDaysOff', value: function numberOfDaysOff() { var count = 0; for (var i = 0; i < this.daysOff.length; i += 1) { count += (0, _utils.numberOfSpecificDay)({ month: this.month, year: this.year, day: this.daysOff[i] }); } return count; } }, { key: 'numberOfHolidays', value: function numberOfHolidays() { var excludeWhenDayOff = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : false; var count = 0; for (var i = 0; i < this.holidays.length; i += 1) { var holiday = this.holidays[i]; var hDate = holiday.split('-'); var hYear = parseInt(hDate[0], 10); var hMonth = parseInt(hDate[1], 10); if (hYear === this.year && hMonth === this.month) { var dayNumber = new Date(holiday).getUTCDay(); var isDayOff = false; if (excludeWhenDayOff) { for (var p = 0; p < this.daysOff.length; p += 1) { if (dayNumber === this.daysOff[p]) { isDayOff = true; } } } if (!isDayOff) { count += 1; } } } return count; } }, { key: 'numberOfBusinessDays', value: function numberOfBusinessDays() { var naturalDays = this.numberOfNaturalDays(); var totalDaysOff = this.numberOfDaysOff(); var totalHolidaysExcludingDaysOff = this.numberOfHolidays(true); return naturalDays - totalDaysOff - totalHolidaysExcludingDaysOff; } }, { key: 'numberOfSundays', value: function numberOfSundays() { return (0, _utils.numberOfSpecificDay)({ month: this.month, year: this.year, day: 0 }); } }, { key: 'numberOfMondays', value: function numberOfMondays() { return (0, _utils.numberOfSpecificDay)({ month: this.month, year: this.year, day: 1 }); } }, { key: 'numberOfTuesdays', value: function numberOfTuesdays() { return (0, _utils.numberOfSpecificDay)({ month: this.month, year: this.year, day: 2 }); } }, { key: 'numberOfWednesdays', value: function numberOfWednesdays() { return (0, _utils.numberOfSpecificDay)({ month: this.month, year: this.year, day: 3 }); } }, { key: 'numberOfThursdays', value: function numberOfThursdays() { return (0, _utils.numberOfSpecificDay)({ month: this.month, year: this.year, day: 4 }); } }, { key: 'numberOfFridays', value: function numberOfFridays() { return (0, _utils.numberOfSpecificDay)({ month: this.month, year: this.year, day: 5 }); } }, { key: 'numberOfSaturdays', value: function numberOfSaturdays() { return (0, _utils.numberOfSpecificDay)({ month: this.month, year: this.year, day: 6 }); } }]); return ThisMonth; }(); exports.default = ThisMonth;