@empellio/business-hours
Version:
A lightweight and accurate TypeScript library for handling business opening hours. Supports multiple daily time slots, holidays, exceptions, overnight openings, and timezone-aware calculations.
94 lines (88 loc) • 2.43 kB
text/typescript
type WeekdayKey = 'mon' | 'tue' | 'wed' | 'thu' | 'fri' | 'sat' | 'sun';
type HHMM = `${number}${number}:${number}${number}`;
type SlotInput = {
open: HHMM;
close: HHMM;
};
type DaySpec = 'closed' | SlotInput[];
type Holiday = {
date: string;
closed?: boolean;
slots?: SlotInput[];
note?: string;
};
type Exception = {
date: string;
closed?: boolean;
slots?: SlotInput[];
note?: string;
};
type Config = {
timezone: string;
week: Record<WeekdayKey, DaySpec>;
holidays?: Holiday[];
exceptions?: Exception[];
locale?: string;
firstDayOfWeek?: WeekdayKey;
strictValidation?: boolean;
};
type DateLike = Date | string;
type OpenCloseWindow = {
start: Date;
end: Date;
};
type CloseAt = {
at: Date;
};
type BusinessHours = {
isOpenNow(): boolean;
isOpenAt(date: DateLike): boolean;
nextOpen(from?: DateLike): OpenCloseWindow | null;
nextClose(from?: DateLike): CloseAt | null;
currentSlot(at?: DateLike): {
open: Date;
close: Date;
} | null;
timeUntilClose(at?: DateLike): {
ms: number;
minutes: number;
} | null;
timeUntilOpen(at?: DateLike): {
ms: number;
minutes: number;
} | null;
todaysSlots(at?: DateLike): Array<{
open: Date;
close: Date;
}>;
slotsOn(date: DateLike): Array<{
open: Date;
close: Date;
}>;
weeklySummary(options?: {
join?: boolean;
}): string[] | string;
listUpcomingSlots(daysAhead?: number): Array<{
date: string;
slots: Array<{
open: Date;
close: Date;
}>;
}>;
withOverrides(overrides: Partial<Config>): BusinessHours;
toJSON(): Config;
};
declare function createBusinessHours(config: Config): BusinessHours;
declare function formatSlot(slot: {
open: Date;
close: Date;
}, options?: {
locale?: string;
timezone?: string;
}): string;
declare function formatDayName(weekday: WeekdayKey, options?: {
locale?: string;
}): string;
declare function toJSON(config: Config): Config;
declare function fromJSON(configLike: Config): BusinessHours;
export { type BusinessHours, type CloseAt, type Config, type DateLike, type DaySpec, type Exception, type HHMM, type Holiday, type OpenCloseWindow, type SlotInput, type WeekdayKey, createBusinessHours, formatDayName, formatSlot, fromJSON, toJSON };