UNPKG

moment-business-days-sk

Version:

MomentJS plugin to use business days with slovak holidays

98 lines (97 loc) 3.56 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; const moment_business_days_1 = __importDefault(require("moment-business-days")); const MIN_YEAR = 1000; const MIN_YEAR_CACHED = 1750; const MAX_YEAR_CACHED = 2250; const MAX_YEAR = 3000; moment_business_days_1.default.locale('sk'); moment_business_days_1.default.updateLocale('sk', { holidays: [ '01/01', '05/07', '29/08', '01/09', '17/11', '06/01', '01/05', '08/05', '15/09', '01/11', '24/12', '25/12', '26/12', ], holidayFormat: 'DD/MM', holiday: function (someMoment) { const year = someMoment.year(); const month = someMoment.month() + 1; const date = someMoment.date(); const day = someMoment.day(); if (year === 2018 && month === 10 && 30 === date) { return true; } if ((month == 3 || month == 4) && (day == 5 || day == 1)) { const someEaster = someMoment.easter(year); if (day == 5) { const easterFriday = moment_business_days_1.default(someEaster).subtract(2, 'd'); if (someMoment.isSame(easterFriday, 'day')) { return true; } } if (day == 1) { const easterMonday = moment_business_days_1.default(someEaster).add(1, 'd'); if (someMoment.isSame(easterMonday, 'day')) { return true; } } } return false; }, }); const easterCache = new Map(); function computeEaster(Y) { const C = Math.floor(Y / 100); const N = Y - 19 * Math.floor(Y / 19); const K = Math.floor((C - 17) / 25); let I = C - Math.floor(C / 4) - Math.floor((C - K) / 3) + 19 * N + 15; I = I - 30 * Math.floor(I / 30); I = I - Math.floor(I / 28) * (1 - Math.floor(I / 28) * Math.floor(29 / (I + 1)) * Math.floor((21 - N) / 11)); let J = Y + Math.floor(Y / 4) + I + 2 - C + Math.floor(C / 4); J = J - 7 * Math.floor(J / 7); const L = I - J; const M = 3 + Math.floor((L + 40) / 44); const D = L + 28 - 31 * Math.floor(M / 4); return { year: Y, month: M - 1, day: D }; } function computeEasterAlt(year) { const a = ((year / 100) | 0) * 1483 - ((year / 400) | 0) * 2225 + 2613; const b = ((((year % 19) * 3510 + ((a / 25) | 0) * 319) / 330) | 0) % 29; const c = 148 - b - (((((year * 5) / 4) | 0) + a - b) % 7); return { year: year, month: ((c / 31) | 0) - 1, day: (c % 31) + 1 }; } moment_business_days_1.default.fn.easter = function (year) { if (!year) year = this.year(); if (year < MIN_YEAR || year > MAX_YEAR) { return undefined; } let aEaster = undefined; if (year >= MIN_YEAR_CACHED && year <= MAX_YEAR_CACHED) { aEaster = easterCache.get(year); } if (aEaster === undefined) { aEaster = moment_business_days_1.default(computeEaster(year)); if (year >= MIN_YEAR_CACHED && year <= MAX_YEAR_CACHED) { easterCache.set(year, aEaster); } } return moment_business_days_1.default(aEaster); }; moment_business_days_1.default.fn.addHoliday = function (dateDDMM) { const locale = this.localeData(); locale._holidays.push(dateDDMM); }; module.exports = moment_business_days_1.default;