UNPKG

nhb-toolbox

Version:

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

39 lines (38 loc) 1.52 kB
import { SEASON_PRESETS } from '../seasons.js'; /** * Plugin to inject `season` method */ export const seasonPlugin = (ChronosClass) => { ChronosClass.prototype.season = function (options) { const { preset = 'default' } = options ?? {}; const seasonSet = options?.seasons ?? 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'; }; };