Opening-Hours-Utils
A collection of opening hours-related utilities.
tl;dr
- Install by executing
npm install @wojtekmaj/opening-hours-utils
oryarn add @wojtekmaj/opening-hours-utils
. - Import by adding
import * as openingHoursUtils from '@wojtekmaj/opening-hours-utils'
. - Do stuff with it!
const openingHoursArray = getOpeningHoursArray('Mo-Fr 08:00-18:00;Sa 08:00-12:00');
User guide
Table of contents
getDailyOpeningHours()
getOpeningHours()
getNextOpenAt()
getNextOpenNow()
encodeOpeningHours()
isOpenAt()
isOpenNow()
getDailyOpeningHours()
Parses opening hours string and returns an array of objects with day
and hours
properties.
Sample usage
import { getDailyOpeningHours } from '@wojtekmaj/opening-hours-utils';
getDailyOpeningHours('Mo-Fr 08:00-17:30');
/**
* [
* { day: 'Mo', hours: [{ from: '08:00', to: '17:30' }] },
* { day: 'Tu', hours: [{ from: '08:00', to: '17:30' }] },
* { day: 'We', hours: [{ from: '08:00', to: '17:30' }] },
* { day: 'Th', hours: [{ from: '08:00', to: '17:30' }] },
* { day: 'Fr', hours: [{ from: '08:00', to: '17:30' }] },
* ]
*/
getOpeningHours()
Parses opening hours string and returns an array of objects with from
, to
, and hours
properties.
Sample usage
import { getOpeningHours } from '@wojtekmaj/opening-hours-utils';
getOpeningHours('Mo-Fr 08:00-17:30');
/**
* [
* {
* from: 'Mo',
* to: 'Fr',
* hours: [{ from: '08:00', to: '17:30' }],
* },
* ]
*/
getNextOpenAt()
Gets the next opening time for a given date. Returns null if for a given date place is open.
Sample usage
import { getNextOpenAt } from '@wojtekmaj/opening-hours-utils';
getNextOpenAt('Mo-Fr 08:00-17:30', saturdayNoon); // 'Mo 08:00'
getNextOpenNow()
Gets the next opening time. Returns null if place is currently open.
Sample usage
import { getNextOpenNow } from '@wojtekmaj/opening-hours-utils';
getNextOpenNow('Mo-Fr 08:00-17:30'); // 'Mo 08:00'
encodeOpeningHours()
Returns opening hours string given an array of objects with from
, to
, and hours
properties.
Sample usage
import { encodeOpeningHours } from '@wojtekmaj/opening-hours-utils';
encodeOpeningHours([
{
from: 'Mo',
to: 'Fr',
hours: [{ from: '08:00', to: '17:30' }],
},
]); // 'Mo-Fr 08:00-17:30'
isOpenAt()
Checks if place is open at a given date.
Sample usage
import { isOpenAt } from '@wojtekmaj/opening-hours-utils';
isOpenAt('Mo-Fr 08:00-17:30', mondayNoon); // true
isOpenNow()
Checks if place is open.
Sample usage
import { isOpenNow } from '@wojtekmaj/opening-hours-utils';
isOpenNow('Mo-Fr 08:00-17:30'); // true
License
The MIT License.
Author
Wojciech Maj [email protected] https://wojtekmaj.pl |