UNPKG

dayjs-business-days2

Version:

Day.js plugin to add support for calculating dates only accounting for Business days

1 lines 10.9 kB
{"version":3,"sources":["../src/index.ts"],"sourcesContent":["import { Dayjs, PluginFunc } from 'dayjs';\n\nconst BusinessDaysPlugin: PluginFunc<plugin.BusinessDaysPluginOptions> = (options = {}, dayjsClass, dayjsFactory) => {\n const defaultWorkingWeekdays = [ 1, 2, 3, 4, 5 ];\n\n dayjsFactory.getWorkingWeekdays = function(): number[] {\n return options.workingWeekdays || defaultWorkingWeekdays;\n };\n\n dayjsFactory.setWorkingWeekdays = function(workingWeekdays: number[]): void {\n options.workingWeekdays = workingWeekdays;\n };\n\n dayjsFactory.getHolidays = function(): string[] {\n return options.holidays || [];\n };\n\n dayjsFactory.setHolidays = function(holidays: string[]): void {\n options.holidays = holidays;\n };\n\n dayjsFactory.getHolidayFormat = function(): string | undefined {\n return options.holidayFormat;\n };\n\n dayjsFactory.setHolidayFormat = function(holidayFormat: string): void {\n options.holidayFormat = holidayFormat;\n };\n\n dayjsFactory.getAdditionalWorkingDays = function(): string[] {\n return options.additionalWorkingDays || [];\n };\n\n dayjsFactory.setAdditionalWorkingDays = function(additionalWorkingDays: string[]): void {\n options.additionalWorkingDays = additionalWorkingDays;\n };\n\n dayjsFactory.getAdditionalWorkingDayFormat = function(): string | undefined {\n return options.additionalWorkingDayFormat;\n };\n\n dayjsFactory.setAdditionalWorkingDayFormat = function(additionalWorkingDayFormat: string): void {\n options.additionalWorkingDayFormat = additionalWorkingDayFormat;\n };\n\n dayjsClass.prototype.isHoliday = function(this: Dayjs): boolean {\n if (!options.holidays) {\n return false;\n }\n if (options.holidays.includes(this.format(options.holidayFormat))) {\n return true;\n }\n\n return false;\n };\n\n dayjsClass.prototype.isBusinessDay = function(this: Dayjs): boolean {\n const workingWeekdays = options.workingWeekdays || defaultWorkingWeekdays;\n\n if (this.isHoliday()) {\n return false;\n }\n if (this.isAdditionalWorkingDay()) {\n return true;\n }\n if (workingWeekdays.includes(this.day())) {\n return true;\n }\n\n return false;\n };\n\n dayjsClass.prototype.isAdditionalWorkingDay = function(this: Dayjs): boolean {\n if (!options.additionalWorkingDays) {\n return false;\n }\n if (options.additionalWorkingDays.includes(this.format(options.additionalWorkingDayFormat))) {\n return true;\n }\n\n return false;\n };\n\n dayjsClass.prototype.businessDaysAdd = function(this: Dayjs, days: number): Dayjs {\n const numericDirection = days < 0 ? -1 : 1;\n let currentDay = this.clone();\n let daysRemaining = Math.abs(days);\n\n while (daysRemaining > 0) {\n currentDay = currentDay.add(numericDirection, `d`);\n\n if (currentDay.isBusinessDay()) {\n daysRemaining -= 1;\n }\n }\n\n return currentDay;\n };\n\n dayjsClass.prototype.businessDaysSubtract = function(this: Dayjs, days: number): Dayjs {\n let currentDay = this.clone();\n\n currentDay = currentDay.businessDaysAdd(days * -1);\n\n return currentDay;\n };\n\n dayjsClass.prototype.businessDiff = function(this: Dayjs, date: Dayjs): number {\n const day1 = this.clone();\n const day2 = date.clone();\n\n const isPositiveDiff = day1 >= day2;\n let start = isPositiveDiff ? day2 : day1;\n const end = isPositiveDiff ? day1 : day2;\n\n let daysBetween = 0;\n\n if (start.isSame(end)) {\n return daysBetween;\n }\n\n while (start < end) {\n if (start.isBusinessDay()) {\n daysBetween += 1;\n }\n\n start = start.add(1, `d`);\n }\n\n return isPositiveDiff ? daysBetween : -daysBetween;\n };\n\n dayjsClass.prototype.nextBusinessDay = function(this: Dayjs): Dayjs {\n const searchLimit = 7;\n let currentDay = this.clone();\n\n let loopIndex = 1;\n while (loopIndex < searchLimit) {\n currentDay = currentDay.add(1, `day`);\n\n if (currentDay.isBusinessDay()) {\n break;\n }\n loopIndex += 1;\n }\n\n return currentDay;\n };\n\n dayjsClass.prototype.prevBusinessDay = function(this: Dayjs): Dayjs {\n const searchLimit = 7;\n let currentDay = this.clone();\n\n let loopIndex = 1;\n while (loopIndex < searchLimit) {\n currentDay = currentDay.subtract(1, `day`);\n\n if (currentDay.isBusinessDay()) {\n break;\n }\n loopIndex += 1;\n }\n\n return currentDay;\n };\n\n dayjsClass.prototype.businessDaysInMonth = function(this: Dayjs): Dayjs[] {\n if (!this.isValid()) {\n return [];\n }\n\n let currentDay = this.clone().startOf(`month`);\n const monthEnd = this.clone().endOf(`month`);\n const businessDays: Dayjs[] = [];\n let monthComplete = false;\n\n while (!monthComplete) {\n if (currentDay.isBusinessDay()) {\n businessDays.push(currentDay.clone());\n }\n\n currentDay = currentDay.add(1, `day`);\n\n if (currentDay.isAfter(monthEnd)) {\n monthComplete = true;\n }\n }\n\n return businessDays;\n };\n\n dayjsClass.prototype.lastBusinessDayOfMonth = function(this: Dayjs): Dayjs {\n const businessDays = this.businessDaysInMonth();\n const lastBusinessDay = businessDays[businessDays.length - 1];\n return lastBusinessDay;\n };\n\n dayjsClass.prototype.businessWeeksInMonth = function(this: Dayjs): Dayjs[][] {\n if (!this.isValid()) {\n return [];\n }\n\n let currentDay = this.clone().startOf(`month`);\n const monthEnd = this.clone().endOf(`month`);\n const businessWeeks: Dayjs[][] = [];\n let businessDays: Dayjs[] = [];\n let monthComplete = false;\n\n while (!monthComplete) {\n if (currentDay.isBusinessDay()) {\n businessDays.push(currentDay.clone());\n }\n\n if (currentDay.day() === 5 || currentDay.isSame(monthEnd, `day`)) {\n businessWeeks.push(businessDays);\n businessDays = [];\n }\n\n currentDay = currentDay.add(1, `day`);\n\n if (currentDay.isAfter(monthEnd)) {\n monthComplete = true;\n }\n }\n\n return businessWeeks;\n };\n};\n\nexport default BusinessDaysPlugin;\n"],"mappings":";;;;AAEA,IAAMA,qBAAmE,wBAACC,UAAU,CAAC,GAAGC,YAAYC,iBAAAA;AAClG,QAAMC,yBAAyB;IAAE;IAAG;IAAG;IAAG;IAAG;;AAE7CD,eAAaE,qBAAqB,WAAA;AAChC,WAAOJ,QAAQK,mBAAmBF;EACpC;AAEAD,eAAaI,qBAAqB,SAASD,iBAAyB;AAClEL,YAAQK,kBAAkBA;EAC5B;AAEAH,eAAaK,cAAc,WAAA;AACzB,WAAO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G,MAAO,KAAKI,WAAWe,OAAOS,UAAU,KAAK,GAAG;AAChEU,sBAAcN,KAAKF,YAAAA;AACnBA,uBAAe,CAAA;MACjB;AAEA1B,mBAAaA,WAAWK,IAAI,GAAG,KAAK;AAEpC,UAAIL,WAAW6B,QAAQL,QAAAA,GAAW;AAChCG,wBAAgB;MAClB;IACF;AAEA,WAAOO;EACT;AACF,GAjOyE;AAmOzE,IAAA,gBAAehE;","names":["BusinessDaysPlugin","options","dayjsClass","dayjsFactory","defaultWorkingWeekdays","getWorkingWeekdays","workingWeekdays","setWorkingWeekdays","getHolidays","holidays","setHolidays","getHolidayFormat","holidayFormat","setHolidayFormat","getAdditionalWorkingDays","additionalWorkingDays","setAdditionalWorkingDays","getAdditionalWorkingDayFormat","additionalWorkingDayFormat","setAdditionalWorkingDayFormat","prototype","isHoliday","includes","format","isBusinessDay","isAdditionalWorkingDay","day","businessDaysAdd","days","numericDirection","currentDay","clone","daysRemaining","Math","abs","add","businessDaysSubtract","businessDiff","date","day1","day2","isPositiveDiff","start","end","daysBetween","isSame","nextBusinessDay","searchLimit","loopIndex","prevBusinessDay","subtract","businessDaysInMonth","isValid","startOf","monthEnd","endOf","businessDays","monthComplete","push","isAfter","lastBusinessDayOfMonth","lastBusinessDay","length","businessWeeksInMonth","businessWeeks"]}