UNPKG

nhb-toolbox

Version:

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

43 lines (42 loc) 1.67 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.seasonPlugin = void 0; const seasons_1 = require("../seasons"); /** * Plugin to inject `season` method */ const seasonPlugin = (ChronosClass) => { ChronosClass.prototype.season = function (options) { const { preset = 'default' } = options ?? {}; const seasonSet = options?.seasons ?? seasons_1.SEASON_PRESETS[preset]; const dateStr = this.format('MM-DD'); for (const { name, boundary } of seasonSet) { if ('startDate' in boundary && 'endDate' in boundary) { const start = boundary.startDate; const end = boundary.endDate; if (start <= end) { if (dateStr >= start && dateStr <= end) return name; } else { // Handles wrap-around seasons like Dec–Feb if (dateStr >= start || dateStr <= end) return name; } } else if ('startMonth' in boundary && 'endMonth' in boundary) { const { startMonth, endMonth } = boundary; if (startMonth <= endMonth) { if (this.month >= startMonth && this.month <= endMonth) { return name; } } else { if (this.month >= startMonth || this.month <= endMonth) { return name; } } } } return 'Unknown'; }; }; exports.seasonPlugin = seasonPlugin;