nhb-toolbox
Version:
A versatile collection of smart, efficient, and reusable utility functions and classes for everyday development needs.
28 lines (27 loc) • 877 B
JavaScript
import { DEFAULT_RANGES } from '../constants.js';
/** * Plugin to inject `getPartOfDay` method */
export const dayPartPlugin = (ChronosClass) => {
ChronosClass.prototype.getPartOfDay = function (config) {
const hour = this.hour;
const ranges = {
...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';
};
};