UNPKG

nhb-toolbox

Version:

A versatile collection of smart, efficient, and reusable utility functions and classes for everyday development needs.

54 lines (53 loc) 2.34 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.businessPlugin = void 0; const constants_1 = require("../constants"); /** * Plugin to inject `business` related methods */ const businessPlugin = (ChronosClass) => { ChronosClass.prototype.isWeekend = function (weekStartsOn = 0, weekendLength = 2) { const day = ChronosClass[constants_1.INTERNALS].internalDate(this).getDay(); const lastDayOfWeek = (weekStartsOn + 6) % 7; const secondLastDay = (weekStartsOn + 5) % 7; if (weekendLength === 1) { return day === lastDayOfWeek; } return day === lastDayOfWeek || day === secondLastDay; }; ChronosClass.prototype.isWorkday = function (weekStartsOn = 0, weekendLength = 2) { return !this.isWeekend(weekStartsOn, weekendLength); }; ChronosClass.prototype.isBusinessHour = function (options) { const { businessStartHour = 9, businessEndHour = 17, weekStartsOn = 0, weekendLength = 2, } = options ?? {}; if (this.isWeekend(weekStartsOn, weekendLength)) { return false; } const hour = ChronosClass[constants_1.INTERNALS].internalDate(this).getHours(); if (businessStartHour === businessEndHour) { return false; } if (businessStartHour < businessEndHour) { // Normal range, e.g. 9 → 17 return hour >= businessStartHour && hour < businessEndHour; } else { // Overnight shift, e.g. 22 → 6 return hour >= businessStartHour || hour < businessEndHour; } }; ChronosClass.prototype.toFiscalQuarter = function (startMonth = 7) { const month = ChronosClass[constants_1.INTERNALS].internalDate(this).getMonth() + 1; const adjusted = (month - startMonth + 12) % 12; return (Math.floor(adjusted / 3) + 1); }; ChronosClass.prototype.toAcademicYear = function () { const year = ChronosClass[constants_1.INTERNALS].internalDate(this).getFullYear(); const month = ChronosClass[constants_1.INTERNALS].internalDate(this).getMonth(); if (month >= 6) { return `${year}-${year + 1}`; } else { return `${year - 1}-${year}`; } }; }; exports.businessPlugin = businessPlugin;