nhb-toolbox
Version:
A versatile collection of smart, efficient, and reusable utility functions, classes and types for everyday development needs.
26 lines (25 loc) • 747 B
JavaScript
import { DATE_PART_RANGES } from '../constants.js';
export const dayPartPlugin = ($Chronos) => {
$Chronos.prototype.getPartOfDay = function (config) {
const hour = this.hour;
const ranges = {
...DATE_PART_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 {
if (hour >= from || hour <= to) {
return part;
}
}
}
return 'night';
};
};