npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

slot-calculator

v2.2.1

Published

Calculate time slots for your users to choose from.

Downloads

54,410

Readme

Slot Calculator

GitHub Workflow Status npm

Calculate time slots for your users to choose from.

  • Intersect availabilities of multiple users/resources.
  • Full timezone support.
  • Blazing fast (usage of ES6 Sets, Maps and efficient data structures).
  • Works great with Luxon and V-Calendar.

Table of contents

Documentation

View the JS/TS reference. TypeScript language support in your IDE is sufficient to make use of this package's types and annotations.

Demo

Play with a live demo on CodeSandbox

Usage

Setup

npm install slot-calculator
import { getSlots } from "slot-calculator"

const { allSlots } = getSlots({
  from: "2022-01-01",
  to: "2022-01-02",
  duration: 60,
})

Returned values

  • allSlots: An array of all generated slots. Only use this as a starting point for manipulating the output.
  • availableSlots: An array of available slots. Only use this as a starting point for manipulating the output.
  • allDates: An array of dates, including unavailable ones, between the from and to configuration variables. Use this to allow users to specify their own datetime, but within your chosen bounds.
  • availableDates: An array of available dates. Use this to mark on a calendar which dates can be selected.
  • allSlotsByDay: Once a user has selected a date, use this object to easily find all the slots for that day.
  • availableSlotsByDay: Once a user has selected a date, use this object to easily find the available slots for that day.
  • timeTaken: Worried that the number crunching is slowing down your app? Monitor this variable to see the time taken by slot calculator.

Examples

For these examples to work, use this setup code:

import { DateTime, Settings } from "luxon";

Settings.defaultZone = "UTC";
const dateTimeRef = DateTime.utc(2022, 1, 1);

Basic usage

getSlots({
  from: dateTimeRef.toISO(),
  to: dateTimeRef.plus({ hour: 2 }).toISO(),
  duration: 60,
});

With availabilities and unavailabilities

getSlots({
  from: dateTimeRef.toISO(),
  to: dateTimeRef.plus({ hour: 3 }).toISO(),
  availability: [
    {
      from: dateTimeRef.plus({ hour: 1 }).toISO(),
      to: dateTimeRef.plus({ hour: 2 }).toISO(),
    },
  ],
  unavailability: [
    {
      from: dateTimeRef.plus({ hour: 2 }).toISO(),
      to: dateTimeRef.plus({ hour: 3 }).toISO(),
    },
  ],
  duration: 60,
});

With multiple users

getSlots({
  from: dateTimeRef.toISO(),
  to: dateTimeRef.plus({ hour: 2 }).toISO(),
  availability: [
    {
      day: "Saturday",
      from: "00:00",
      to: "01:00",
      metadata: {
        user: "Alice",
      },
    },
    {
      from: dateTimeRef.plus({ hour: 1 }).toISO(),
      to: dateTimeRef.plus({ hour: 2 }).toISO(),
      metadata: {
        user: "Bob",
      },
    },
  ],
  duration: 60,
});

With timezones

getSlots({
  from: dateTimeRef.toISO(),
  to: dateTimeRef.plus({ hour: 2 }).toISO(),
  outputTimezone: "Europe/Paris",
  availability: [
    {
      day: "Saturday",
      from: "01:00",
      to: "02:00",
      timezone: "Europe/Paris",
    },
  ],
  duration: 60,
});