UNPKG

nhb-toolbox

Version:

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

32 lines (31 loc) 1.02 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.dayPartPlugin = void 0; const constants_1 = require("../constants"); /** * Plugin to inject `getPartOfDay` method */ const dayPartPlugin = (ChronosClass) => { ChronosClass.prototype.getPartOfDay = function (config) { const hour = this.hour; const ranges = { ...constants_1.DEFAULT_RANGES, ...config, }; for (const [part, [start, end]] of Object.entries(ranges)) { const from = Number(start); const to = Number(end); if (from <= to) { if (hour >= from && hour <= to) { return part; } } else { // Wraparound logic (e.g., 20 to 04 means 20–23 OR 00–04) if (hour >= from || hour <= to) { return part; } } } return 'night'; }; }; exports.dayPartPlugin = dayPartPlugin;